Add C++ interface
[src/app-framework-binder.git] / bindings / tutorial / tuto-3.cpp
1 #include <string.h>
2 #include <json-c/json.h>
3
4 #include <afb/afb-binding>
5
6 afb::event event_login, event_logout;
7
8 void login(afb_req r)
9 {
10         json_object *args, *user, *passwd;
11         char *usr;
12         afb::req req(r);
13
14         args = req.json();
15         if (!json_object_object_get_ex(args, "user", &user)
16          || !json_object_object_get_ex(args, "password", &passwd)) {
17                 AFB_REQ_ERROR(req, "login, bad request: %s", json_object_get_string(args));
18                 req.fail("bad-request");
19         } else if (afb_req_context_get(req)) {
20                 AFB_REQ_ERROR(req, "login, bad state, logout first");
21                 req.fail("bad-state");
22         } else if (strcmp(json_object_get_string(passwd), "please")) {
23                 AFB_REQ_ERROR(req, "login, unauthorized: %s", json_object_get_string(args));
24                 req.fail("unauthorized");
25         } else {
26                 usr = strdup(json_object_get_string(user));
27                 AFB_REQ_NOTICE(req, "login user: %s", usr);
28                 req.session_set_LOA(1);
29                 req.context_set(usr, free);
30                 req.success();
31                 event_login.push(json_object_new_string(usr));
32         }
33 }
34
35 void action(afb_req r)
36 {
37         json_object *args, *val;
38         char *usr;
39         afb::req req(r);
40
41         args = req.json();
42         usr = (char*)req.context_get();
43         AFB_REQ_NOTICE(req, "action for user %s: %s", usr, json_object_get_string(args));
44         if (json_object_object_get_ex(args, "subscribe", &val)) {
45                 if (json_object_get_boolean(val)) {
46                         AFB_REQ_NOTICE(req, "user %s subscribes to events", usr);
47                         req.subscribe(event_login);
48                         req.subscribe(event_logout);
49                 } else {
50                         AFB_REQ_NOTICE(req, "user %s unsubscribes to events", usr);
51                         req.unsubscribe(event_login);
52                         req.unsubscribe(event_logout);
53                 }
54         }
55         req.success(json_object_get(args));
56 }
57
58 void logout(afb_req r)
59 {
60         char *usr;
61         afb::req req(r);
62
63         usr = (char*)req.context_get();
64         AFB_REQ_NOTICE(req, "login user %s out", usr);
65         event_logout.push(json_object_new_string(usr));
66         req.session_set_LOA(0);
67         req.context_clear();
68         req.success();
69 }
70
71 int init()
72 {
73         AFB_NOTICE("init");
74         event_login = afb_daemon_make_event("login");
75         event_logout = afb_daemon_make_event("logout");
76         if (afb_event_is_valid(event_login) && afb_event_is_valid(event_logout))
77                 return 0;
78         AFB_ERROR("Can't create events");
79         return -1;
80 }
81
82 const afb_verb_v2 verbs[] = {
83         afb::verb("login", login, "log in the system"),
84         afb::verb("action", action, "perform an action", AFB_SESSION_LOA_1),
85         afb::verb("logout", logout, "log out the system", AFB_SESSION_LOA_1),
86         afb::verbend()
87 };
88
89 const afb_binding_v2 afbBindingV2 = afb::binding("tuto-3", verbs, "third tutorial: C++", init);
90