/*
- * Copyright (C) 2015, 2016 "IoT.bzh"
+ * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
* Author "Fulup Ar Foll"
*
* Licensed under the Apache License, Version 2.0 (the "License");
#include <stdio.h>
#include <json-c/json.h>
-#include <afb/afb-plugin.h>
+#define AFB_BINDING_VERSION 1
+#include <afb/afb-binding.h>
typedef struct {
- /*
+ /*
* client context is attached a session but private to a each plugin.
* Context is passed to each API under request->context
- *
+ *
* Note:
* -client context is free when a session is closed. Developer should not
* forget that even if context is private to each plugin, session is unique
* -when an API use AFB_SESSION_RESET this close the session and each plugin
* will be notified to free ressources.
*/
-
+
int count;
char *abcd;
-
+
} MyClientContextT;
-// This function is call at session open time. Any client trying to
+// This function is call at session open time. Any client trying to
// call it with an already open session will be denied.
// Ex: http://localhost:1234/api/context/create?token=123456789
static void myCreate (struct afb_req request)
// store something in our plugin private client context
ctx->count = 0;
- ctx->abcd = "SomeThingUseful";
+ ctx->abcd = "SomeThingUseful";
afb_req_context_set(request, ctx, free);
afb_req_success_f(request, NULL, "SUCCESS: create client context for plugin [%s]", ctx->abcd);
static void myAction (struct afb_req request)
{
MyClientContextT *ctx = (MyClientContextT*) afb_req_context_get(request);
-
+
+ if (!ctx) {
+ afb_req_fail(request, "invalid-state", "Can't perform action");
+ return;
+ }
// store something in our plugin private client context
ctx->count++;
afb_req_success_f(request, NULL, "SUCCESS: plugin [%s] Check=[%d]\n", ctx->abcd, ctx->count);
static void myClose (struct afb_req request)
{
MyClientContextT *ctx = (MyClientContextT*) afb_req_context_get(request);
-
+
+ if (!ctx) {
+ afb_req_success(request, NULL, NULL);
+ return;
+ }
// store something in our plugin private client context
ctx->count++;
afb_req_success_f(request, NULL, "SUCCESS: plugin [%s] Close=[%d]\n", ctx->abcd, ctx->count);
// NOTE: this sample does not use session to keep test a basic as possible
// in real application most APIs should be protected with AFB_SESSION_CHECK
-static const struct AFB_verb_desc_v1 verbs[]= {
+static const struct afb_verb_desc_v1 verbs[]= {
{"create", AFB_SESSION_CREATE, myCreate , "Create a new session"},
{"action", AFB_SESSION_CHECK , myAction , "Use Session Context"},
{"close" , AFB_SESSION_CLOSE , myClose , "Free Context"},
{NULL}
};
-static const struct AFB_plugin plugin_desc = {
- .type = AFB_PLUGIN_VERSION_1,
+static const struct afb_binding plugin_desc = {
+ .type = AFB_BINDING_VERSION_1,
.v1 = {
.info = "Sample of Client Context Usage",
.prefix = "context",
}
};
-const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf)
+const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)
{
return &plugin_desc;
}