2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Fulup Ar Foll"
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.
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.
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/>.
20 #include "local-def.h"
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
30 void *anythingYouWant;
37 * client context is attached a session but private to a each plugin.
38 * Context is passed to each API under request->context
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
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.
60 // This function is call at session open time. Any client trying to
61 // call it with an already open session will be denied.
62 // Ex: http://localhost:1234/api/context/create?token=123456789
63 STATIC json_object* myCreate (AFB_request *request) {
66 MyClientContextT *ctx= malloc (sizeof (MyClientContextT));
67 MyPluginHandleT *handle = (MyPluginHandleT*) request->handle;
69 // store something in our plugin private client context
71 ctx->abcd = "SomeThingUseful";
73 request->context = ctx;
74 jresp = jsonNewMessage(AFB_SUCCESS, "SUCCESS: create client context for plugin [%s]", handle->anythingYouWant);
79 // This function can only be called with a valid token. Token should be renew before
80 // session timeout a standard renew api is avaliable at /api/token/renew this API
81 // can be called automatically with <token-renew> HTML5 widget.
82 // ex: http://localhost:1234/api/context/action?token=xxxxxx-xxxxxx-xxxxx-xxxxx-xxxxxx
83 STATIC json_object* myAction (AFB_request *request) {
85 MyPluginHandleT *handle = (MyPluginHandleT*) request->handle;
86 MyClientContextT *ctx= (MyClientContextT*) request->context;
88 // store something in our plugin private client context
90 jresp = jsonNewMessage(AFB_SUCCESS, "SUCCESS: plugin [%s] Check=[%d]\n", handle->anythingYouWant, ctx->count);
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 json_object* myClose (AFB_request *request) {
101 MyPluginHandleT *handle = (MyPluginHandleT*) request->handle;
102 MyClientContextT *ctx= (MyClientContextT*) request->context;
104 // store something in our plugin private client context
106 jresp = jsonNewMessage(AFB_SUCCESS, "SUCCESS: plugin [%s] Close=[%d]\n", handle->anythingYouWant, ctx->count);
108 // Note Context resource should be free in FreeCtxCB and not here in case session timeout.
112 STATIC void freeCtxCB (MyClientContextT *ctx, MyPluginHandleT *handle, char *uuid) {
113 fprintf (stderr, "FreeCtxCB uuid=[%s] Plugin=[%s] count=[%d]", uuid, (char*)handle->anythingYouWant, ctx->count);
116 // Note: handle should be free it is a static resource attached to plugin and not to session
119 // NOTE: this sample does not use session to keep test a basic as possible
120 // in real application most APIs should be protected with AFB_SESSION_CHECK
121 STATIC AFB_restapi pluginApis[]= {
122 {"create", AFB_SESSION_CREATE, (AFB_apiCB)myCreate , "Create a new session"},
123 {"action", AFB_SESSION_CHECK , (AFB_apiCB)myAction , "Use Session Context"},
124 {"close" , AFB_SESSION_CLOSE , (AFB_apiCB)myClose , "Free Context"},
128 PUBLIC AFB_plugin *pluginRegister () {
130 // Plugin handle should not be in stack (malloc or static)
131 STATIC MyPluginHandleT handle;
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->handle = &handle;
139 plugin->freeCtxCB= (AFB_freeCtxCB) freeCtxCB;
141 // feed plugin handle before returning from registration
142 handle.anythingYouWant = "My Plugin Handle";