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