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