Fix rpm packaging following review 20926
[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 req)
10 {
11         json_object *args, *user, *passwd;
12         char *usr;
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(1, nullptr, free, usr);
30                 req.success();
31                 event_login.push(json_object_new_string(usr));
32         }
33 }
34
35 void action(afb::req req)
36 {
37         json_object *args, *val;
38         char *usr;
39
40         args = req.json();
41 //      usr = (char*)req.context_get();
42 usr = nullptr;
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 req)
59 {
60         char *usr;
61
62 //      usr = (char*)req.context_get();
63 usr = nullptr;
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 #if AFB_BINDING_VERSION >= 3
73         afb_api_t api
74 #endif
75 )
76 {
77         AFB_NOTICE("init");
78         event_login = afb_daemon_make_event("login");
79         event_logout = afb_daemon_make_event("logout");
80         if (afb_event_is_valid(event_login) && afb_event_is_valid(event_logout))
81                 return 0;
82         AFB_ERROR("Can't create events");
83         return -1;
84 }
85
86 const afb_verb_t verbs[] = {
87         afb::verb("login", login, "log in the system"),
88         afb::verb("action", action, "perform an action", AFB_SESSION_LOA_1),
89         afb::verb("logout", logout, "log out the system", AFB_SESSION_LOA_1),
90         afb::verbend()
91 };
92
93 const afb_binding_t afbBindingExport = afb::binding("tuto-3", verbs, "third tutorial: C++", init);
94
95