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