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