prepares event propagation
[src/app-framework-binder.git] / plugins / samples / ClientCtx.c
1 /*
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <json.h>
21
22 #include "afb-plugin.h"
23
24 typedef struct {
25   /* 
26    * In case your plugin is implemented on multiple files or used share routines
27    * with other plugins, it might not be possible to use global static variable.
28    * In this case you can attach a static handle to your plugin. This handle
29    * is passed within each API call under request->handle
30    * 
31    */  
32   void *anythingYouWant;
33
34   
35 } MyPluginHandleT;
36
37 typedef struct {
38   /* 
39    * client context is attached a session but private to a each plugin.
40    * Context is passed to each API under request->context
41    * 
42    * Note:
43    *  -client context is free when a session is closed. Developer should not
44    *   forget that even if context is private to each plugin, session is unique
45    *   to a client. When session close, every plugin are notified to free there
46    *   private context.
47    *  -by default standard "free" function from libc is used to free context.
48    *   Developer may define it own under plugin->freeCB. This call received
49    *   FreeCtxCb(void *ClientCtx, void*PluginHandle, char*SessionUUID) if
50    *   FreeCtxCb=(void*)-1 then context wont be free by session manager.
51    *  -when an API use AFB_SESSION_RESET this close the session and each plugin
52    *   will be notified to free ressources.
53    */
54     
55   int  count;
56   char *abcd;
57   
58 } MyClientContextT;
59
60
61 // Plugin handle should not be in stack (malloc or static)
62 static MyPluginHandleT global_handle;
63     
64 // This function is call at session open time. Any client trying to 
65 // call it with an already open session will be denied.
66 // Ex: http://localhost:1234/api/context/create?token=123456789
67 static void myCreate (struct afb_req request)
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     afb_req_success_f(request, NULL, "SUCCESS: create client context for plugin [%s]", handle->anythingYouWant);
78 }
79
80 // This function can only be called with a valid token. Token should be renew before
81 // session timeout a standard renew api is avaliable at /api/token/renew this API
82 // can be called automatically with <token-renew> HTML5 widget.
83 // ex: http://localhost:1234/api/context/action?token=xxxxxx-xxxxxx-xxxxx-xxxxx-xxxxxx
84 static void myAction (struct afb_req request)
85 {
86     MyPluginHandleT  *handle = (MyPluginHandleT*) &global_handle;
87     MyClientContextT *ctx = (MyClientContextT*) *request.context;
88     
89     // store something in our plugin private client context
90     ctx->count++;
91     afb_req_success_f(request, NULL, "SUCCESS: plugin [%s] Check=[%d]\n", handle->anythingYouWant, ctx->count);
92 }
93
94 // After execution of this function, client session will be close and if they
95 // created a context [request->context != NULL] every plugins will be notified
96 // that they should free context resources.
97 // ex: http://localhost:1234/api/context/close?token=xxxxxx-xxxxxx-xxxxx-xxxxx-xxxxxx
98 static void myClose (struct afb_req request)
99 {
100     MyPluginHandleT  *handle = (MyPluginHandleT*) &global_handle;
101     MyClientContextT *ctx = (MyClientContextT*) *request.context;
102     
103     // store something in our plugin private client context
104     ctx->count++;
105     afb_req_success_f(request, NULL, "SUCCESS: plugin [%s] Close=[%d]\n", handle->anythingYouWant, ctx->count);
106 }
107
108 static void freeCtxCB (MyClientContextT *ctx) {
109     MyPluginHandleT  *handle = (MyPluginHandleT*) &global_handle;
110     fprintf (stderr, "FreeCtxCB Plugin=[%s]  count=[%d]", (char*)handle->anythingYouWant, ctx->count);
111     free (ctx);
112     
113     // Note: handle should be free it is a static resource attached to plugin and not to session
114 }
115
116 // NOTE: this sample does not use session to keep test a basic as possible
117 //       in real application most APIs should be protected with AFB_SESSION_CHECK
118 static const struct AFB_restapi pluginApis[]= {
119   {"create", AFB_SESSION_CREATE, myCreate  , "Create a new session"},
120   {"action", AFB_SESSION_CHECK , myAction  , "Use Session Context"},
121   {"close" , AFB_SESSION_CLOSE , myClose   , "Free Context"},
122   {NULL}
123 };
124
125 static const struct AFB_plugin plugin_desc = {
126         .type = AFB_PLUGIN_JSON,
127         .info = "Sample of Client Context Usage",
128         .prefix = "context",
129         .apis = pluginApis,
130         .freeCtxCB = (void*)freeCtxCB
131 };
132
133 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)
134 {
135         global_handle.anythingYouWant = "anythingYouWant";
136         return &plugin_desc;
137 }
138