2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Fulup Ar Foll"
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "afb-plugin.h"
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
32 void *anythingYouWant;
39 * client context is attached a session but private to a each plugin.
40 * Context is passed to each API under request->context
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
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.
61 // Plugin handle should not be in stack (malloc or static)
62 static MyPluginHandleT global_handle;
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)
69 MyClientContextT *ctx = malloc (sizeof (MyClientContextT));
70 MyPluginHandleT *handle = (MyPluginHandleT*) &global_handle;
72 // store something in our plugin private client context
74 ctx->abcd = "SomeThingUseful";
76 *request.context = ctx;
77 afb_req_success_f(request, NULL, "SUCCESS: create client context for plugin [%s]", handle->anythingYouWant);
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)
86 MyPluginHandleT *handle = (MyPluginHandleT*) &global_handle;
87 MyClientContextT *ctx = (MyClientContextT*) *request.context;
89 // store something in our plugin private client context
91 afb_req_success_f(request, NULL, "SUCCESS: plugin [%s] Check=[%d]\n", handle->anythingYouWant, ctx->count);
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)
100 MyPluginHandleT *handle = (MyPluginHandleT*) &global_handle;
101 MyClientContextT *ctx = (MyClientContextT*) *request.context;
103 // store something in our plugin private client context
105 afb_req_success_f(request, NULL, "SUCCESS: plugin [%s] Close=[%d]\n", handle->anythingYouWant, ctx->count);
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);
113 // Note: handle should be free it is a static resource attached to plugin and not to session
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"},
125 static const struct AFB_plugin plugin_desc = {
126 .type = AFB_PLUGIN_JSON,
127 .info = "Sample of Client Context Usage",
130 .freeCtxCB = (void*)freeCtxCB
133 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)
135 global_handle.anythingYouWant = "anythingYouWant";