X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=bindings%2Fsamples%2FDemoContext.c;h=a2fec7502f541448ebf4798241470926f1059211;hb=6120bbbd2cbd1f06904f887957d92ac740631c55;hp=ef70375958bcc9f0e2a4fe45753687ae9eb2cc33;hpb=7059e59cddc1c81321639875636e88895bc14309;p=src%2Fapp-framework-binder.git diff --git a/bindings/samples/DemoContext.c b/bindings/samples/DemoContext.c index ef703759..a2fec750 100644 --- a/bindings/samples/DemoContext.c +++ b/bindings/samples/DemoContext.c @@ -1,5 +1,5 @@ /* - * 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"); @@ -19,13 +19,14 @@ #include #include -#include +#define AFB_BINDING_VERSION 1 +#include 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 @@ -38,13 +39,13 @@ typedef struct { * -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) @@ -53,7 +54,7 @@ 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); @@ -66,7 +67,11 @@ static void myCreate (struct afb_req request) 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); @@ -79,7 +84,11 @@ static void myAction (struct afb_req request) 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); @@ -121,7 +130,7 @@ static void clientCheckLOA(struct afb_req request) // 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"}, @@ -144,8 +153,8 @@ static const struct AFB_verb_desc_v1 verbs[]= { {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", @@ -153,7 +162,7 @@ static const struct AFB_plugin plugin_desc = { } }; -const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf) +const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf) { return &plugin_desc; }