b59400e1e170c8cbfb44f8f63b224ccba24638bc
[src/app-framework-binder.git] / plugins / samples / ClientCtx.c
1 /*
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "local-def.h"
21
22 typedef struct {
23   /* 
24    * In case your plugin is implemented on multiple files or used share routines
25    * with other plugins, it might not be possible to use global static variable.
26    * In this case you can attach a static handle to your plugin. This handle
27    * is passed within each API call under request->handle
28    * 
29    */  
30   void *anythingYouWant;
31
32   
33 } MyPluginHandleT;
34
35 typedef struct {
36   /* 
37    * client context is attached a session but private to a each plugin.
38    * Context is passed to each API under request->context
39    * 
40    * Note:
41    *  -client context is free when a session is closed. Developer should not
42    *   forget that even if context is private to each plugin, session is unique
43    *   to a client. When session close, every plugin are notified to free there
44    *   private context.
45    *  -by default standard "free" function from libc is used to free context.
46    *   Developer may define it own under plugin->freeCB. This call received
47    *   FreeCtxCb(void *ClientCtx, void*PluginHandle, char*SessionUUID) if
48    *   FreeCtxCb=(void*)-1 then context wont be free by session manager.
49    *  -when an API use AFB_SESSION_RESET this close the session and each plugin
50    *   will be notified to free ressources.
51    */
52     
53   int  count;
54   char *abcd;
55   
56 } MyClientContextT;
57
58
59 // Plugin handle should not be in stack (malloc or static)
60 STATIC MyPluginHandleT global_handle;
61     
62 // This function is call at session open time. Any client trying to 
63 // call it with an already open session will be denied.
64 // Ex: http://localhost:1234/api/context/create?token=123456789
65 STATIC json_object* myCreate (AFB_request *request) {
66     json_object *jresp;
67     
68     MyClientContextT *ctx= malloc (sizeof (MyClientContextT));
69     MyPluginHandleT  *handle = (MyPluginHandleT*) &global_handle;
70
71     // store something in our plugin private client context
72     ctx->count = 0;
73     ctx->abcd  = "SomeThingUseful";        
74
75     request->context = ctx;
76     jresp =  jsonNewMessage(AFB_SUCCESS, "SUCCESS: create client context for plugin [%s]", handle->anythingYouWant);
77            
78     return jresp;
79 }
80
81 // This function can only be called with a valid token. Token should be renew before
82 // session timeout a standard renew api is avaliable at /api/token/renew this API
83 // can be called automatically with <token-renew> HTML5 widget.
84 // ex: http://localhost:1234/api/context/action?token=xxxxxx-xxxxxx-xxxxx-xxxxx-xxxxxx
85 STATIC json_object* myAction (AFB_request *request) {
86     json_object* jresp;
87     MyPluginHandleT  *handle = (MyPluginHandleT*) &global_handle;
88     MyClientContextT *ctx= (MyClientContextT*) request->context;
89     
90     // store something in our plugin private client context
91     ctx->count++;
92     jresp =  jsonNewMessage(AFB_SUCCESS, "SUCCESS: plugin [%s] Check=[%d]\n", handle->anythingYouWant, ctx->count);
93          
94     return jresp;
95 }
96
97 // After execution of this function, client session will be close and if they
98 // created a context [request->context != NULL] every plugins will be notified
99 // that they should free context resources.
100 // ex: http://localhost:1234/api/context/close?token=xxxxxx-xxxxxx-xxxxx-xxxxx-xxxxxx
101 STATIC json_object* myClose (AFB_request *request) {
102     json_object* jresp;
103     MyPluginHandleT  *handle = (MyPluginHandleT*) &global_handle;
104     MyClientContextT *ctx= (MyClientContextT*) request->context;
105     
106     // store something in our plugin private client context
107     ctx->count++;
108     jresp =  jsonNewMessage(AFB_SUCCESS, "SUCCESS: plugin [%s] Close=[%d]\n", handle->anythingYouWant, ctx->count);
109     
110     // Note Context resource should be free in FreeCtxCB and not here in case session timeout.
111     return jresp;
112 }
113
114 STATIC void freeCtxCB (MyClientContextT *ctx) {
115     MyPluginHandleT  *handle = (MyPluginHandleT*) &global_handle;
116     fprintf (stderr, "FreeCtxCB Plugin=[%s]  count=[%d]", (char*)handle->anythingYouWant, ctx->count);
117     free (ctx);
118     
119     // Note: handle should be free it is a static resource attached to plugin and not to session
120 }
121
122 // NOTE: this sample does not use session to keep test a basic as possible
123 //       in real application most APIs should be protected with AFB_SESSION_CHECK
124 STATIC  AFB_restapi pluginApis[]= {
125   {"create", AFB_SESSION_CREATE, (AFB_apiCB)myCreate  , "Create a new session"},
126   {"action", AFB_SESSION_CHECK , (AFB_apiCB)myAction   , "Use Session Context"},
127   {"close" , AFB_SESSION_CLOSE , (AFB_apiCB)myClose   , "Free Context"},
128   {NULL}
129 };
130
131 PUBLIC AFB_plugin *pluginRegister () {
132     
133     AFB_plugin *plugin = malloc (sizeof (AFB_plugin));
134     plugin->type     = AFB_PLUGIN_JSON;
135     plugin->info     = "Sample of Client Context Usage";
136     plugin->prefix   = "context";
137     plugin->apis     = pluginApis;
138     plugin->freeCtxCB= (AFB_freeCtxCB) freeCtxCB;
139     
140     // feed plugin handle before returning from registration
141     global_handle.anythingYouWant = "My Plugin Handle";
142     
143     return (plugin);
144 };