From 469e619ac45402471ea7d8c792ffc06e79346ef2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Mon, 18 Apr 2016 17:08:26 +0200 Subject: [PATCH] prepares event propagation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: Ib824c6dea1837cc1cbb70a2c82363c9b0a0517d4 Signed-off-by: José Bollo --- include/{afb-poll-itf.h => afb-evmgr-itf.h} | 22 ++++++---- include/afb-plugin.h | 28 ++++++++++-- include/afb-pollmgr-itf.h | 61 ++++++++++++++++++++++++++ plugins/afm-main-plugin/afm-main-plugin.c | 37 +++++++++++----- plugins/samples/ClientCtx.c | 1 - plugins/samples/HelloWorld.c | 1 - plugins/samples/SamplePost.c | 1 - plugins/session/token-api.c | 1 - src/afb-api-so.c | 66 ++++++++++++++--------------- src/afb-apis.c | 20 ++------- 10 files changed, 163 insertions(+), 75 deletions(-) rename include/{afb-poll-itf.h => afb-evmgr-itf.h} (65%) create mode 100644 include/afb-pollmgr-itf.h diff --git a/include/afb-poll-itf.h b/include/afb-evmgr-itf.h similarity index 65% rename from include/afb-poll-itf.h rename to include/afb-evmgr-itf.h index b70eb993..2268ec90 100644 --- a/include/afb-poll-itf.h +++ b/include/afb-evmgr-itf.h @@ -17,13 +17,19 @@ #pragma once -struct afb_pollitf -{ - int (*wait)(int timeout, void *pollclosure); - void *(*open)(int fd, void *closure, void *pollclosure); - int (*on_readable)(void *hndl, void (*cb)(void *closure)); - int (*on_writable)(void *hndl, void (*cb)(void *closure)); - void (*on_hangup)(void *hndl, void (*cb)(void *closure)); - void (*close)(void *hndl); +struct json_object; + +struct afb_evmgr_itf { + void (*push)(void *evmgr, const char *name, struct json_object *object); +}; + +struct afb_evmgr { + const struct afb_evmgr_itf *itf; + void *closure; }; +static inline void afb_evmgr_push(struct afb_evmgr mgr, const char *name, struct json_object *object) +{ + return mgr.itf->push(mgr.closure, name, object); +} + diff --git a/include/afb-plugin.h b/include/afb-plugin.h index 2c9935e6..c7252d64 100644 --- a/include/afb-plugin.h +++ b/include/afb-plugin.h @@ -17,7 +17,9 @@ #pragma once -struct afb_req; +#include "afb-req-itf.h" +#include "afb-pollmgr-itf.h" +#include "afb-evmgr-itf.h" /* Plugin Type */ enum AFB_pluginE @@ -63,13 +65,33 @@ enum AFB_Mode { AFB_MODE_GLOBAL }; +struct afb_daemon_itf { + struct afb_evmgr (*get_evmgr)(void *closure); + struct afb_pollmgr (*get_pollmgr)(void *closure); +}; + +struct afb_daemon { + const struct afb_daemon_itf *itf; + void *closure; +}; + struct AFB_interface { int verbosity; enum AFB_Mode mode; - const struct afb_pollitf *pollitf; - void *pollclosure; + struct afb_daemon daemon; }; extern const struct AFB_plugin *pluginRegister (const struct AFB_interface *interface); +static inline struct afb_evmgr afb_daemon_get_evmgr(struct afb_daemon daemon) +{ + return daemon.itf->get_evmgr(daemon.closure); +} + +static inline struct afb_pollmgr afb_daemon_get_pollmgr(struct afb_daemon daemon) +{ + return daemon.itf->get_pollmgr(daemon.closure); +} + + diff --git a/include/afb-pollmgr-itf.h b/include/afb-pollmgr-itf.h new file mode 100644 index 00000000..c3a62c17 --- /dev/null +++ b/include/afb-pollmgr-itf.h @@ -0,0 +1,61 @@ +/* + * Copyright 2016 IoT.bzh + * Author: José Bollo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +struct afb_pollmgr_itf +{ + int (*wait)(int timeout, void *pollclosure); + void *(*open)(int fd, void *closure, void *pollclosure); + int (*on_readable)(void *hndl, void (*cb)(void *closure)); + int (*on_writable)(void *hndl, void (*cb)(void *closure)); + void (*on_hangup)(void *hndl, void (*cb)(void *closure)); + void (*close)(void *hndl); +}; + +struct afb_pollmgr +{ + const struct afb_pollmgr_itf *itf; + void *closure; +}; + +static inline int afb_pollmgr_wait(struct afb_pollmgr pollmgr, int timeout) +{ + return pollmgr.itf->wait(timeout, pollmgr.closure); +} + +static inline void *afb_pollmgr_open(struct afb_pollmgr pollmgr, int fd, void *closure) +{ + return pollmgr.itf->open(fd, closure, pollmgr.closure); +} + +static inline int afb_pollmgr_on_readable(struct afb_pollmgr pollmgr, void *hndl, void (*cb)(void *closure)) +{ + return pollmgr.itf->on_readable(hndl, cb); +} + +static inline int afb_pollmgr_on_writable(struct afb_pollmgr pollmgr, void *hndl, void (*cb)(void *closure)) +{ + return pollmgr.itf->on_writable(hndl, cb); +} + +static inline void afb_pollmgr_on_hangup(struct afb_pollmgr pollmgr, void *hndl, void (*cb)(void *closure)) +{ + pollmgr.itf->on_hangup(hndl, cb); +} + + diff --git a/plugins/afm-main-plugin/afm-main-plugin.c b/plugins/afm-main-plugin/afm-main-plugin.c index 2d350df2..d7ef198d 100644 --- a/plugins/afm-main-plugin/afm-main-plugin.c +++ b/plugins/afm-main-plugin/afm-main-plugin.c @@ -18,17 +18,17 @@ #define _GNU_SOURCE /* See feature_test_macros(7) */ #include #include +#include #include #include "afb-plugin.h" -#include "afb-req-itf.h" -#include "afb-poll-itf.h" #include "utils-sbus.h" #include "utils-jbus.h" static const char _auto_[] = "auto"; static const char _continue_[] = "continue"; +static const char _changed_[] = "changed"; static const char _detail_[] = "detail"; static const char _id_[] = "id"; static const char _install_[] = "install"; @@ -46,9 +46,15 @@ static const char _uninstall_[] = "uninstall"; static const char _uri_[] = "uri"; static const struct AFB_interface *interface; +static struct afb_evmgr evmgr; static struct jbus *jbus; +static void application_list_changed(const char *data, void *closure) +{ + afb_evmgr_push(evmgr, "application-list-changed", NULL); +} + static struct json_object *embed(const char *tag, struct json_object *obj) { struct json_object *result; @@ -285,19 +291,24 @@ static struct sbus_itf sbusitf; const struct AFB_plugin *pluginRegister(const struct AFB_interface *itf) { + int rc; + struct afb_pollmgr pollmgr; struct sbus *sbus; /* records the interface */ assert (interface == NULL); + interface = itf; + evmgr = afb_daemon_get_evmgr(itf->daemon); /* creates the sbus for session */ - sbusitf.wait = itf->pollitf->wait; - sbusitf.open = itf->pollitf->open; - sbusitf.on_readable = itf->pollitf->on_readable; - sbusitf.on_writable = itf->pollitf->on_writable; - sbusitf.on_hangup = itf->pollitf->on_hangup; - sbusitf.close = itf->pollitf->close; - sbus = sbus_session(&sbusitf, itf->pollclosure); + pollmgr = afb_daemon_get_pollmgr(itf->daemon); + sbusitf.wait = pollmgr.itf->wait; + sbusitf.open = pollmgr.itf->open; + sbusitf.on_readable = pollmgr.itf->on_readable; + sbusitf.on_writable = pollmgr.itf->on_writable; + sbusitf.on_hangup = pollmgr.itf->on_hangup; + sbusitf.close = pollmgr.itf->close; + sbus = sbus_session(&sbusitf, pollmgr.closure); if (sbus == NULL) { fprintf(stderr, "ERROR: %s:%d: can't connect to DBUS session\n", __FILE__, __LINE__); return NULL; @@ -310,7 +321,13 @@ const struct AFB_plugin *pluginRegister(const struct AFB_interface *itf) return NULL; } - + /* records the signal handler */ + rc = jbus_on_signal_s(jbus, _changed_, application_list_changed, NULL); + if (rc < 0) { + jbus_unref(jbus); + return NULL; + } + return &plug_desc; } diff --git a/plugins/samples/ClientCtx.c b/plugins/samples/ClientCtx.c index 2d5ebfe1..24cb4f30 100644 --- a/plugins/samples/ClientCtx.c +++ b/plugins/samples/ClientCtx.c @@ -20,7 +20,6 @@ #include #include "afb-plugin.h" -#include "afb-req-itf.h" typedef struct { /* diff --git a/plugins/samples/HelloWorld.c b/plugins/samples/HelloWorld.c index d02fd117..bf809cc3 100644 --- a/plugins/samples/HelloWorld.c +++ b/plugins/samples/HelloWorld.c @@ -20,7 +20,6 @@ #include #include "afb-plugin.h" -#include "afb-req-itf.h" // Sample Generic Ping Debug API static void ping(struct afb_req request, json_object *jresp, const char *tag) diff --git a/plugins/samples/SamplePost.c b/plugins/samples/SamplePost.c index 6def3b9c..f484d131 100644 --- a/plugins/samples/SamplePost.c +++ b/plugins/samples/SamplePost.c @@ -21,7 +21,6 @@ #include #include "afb-plugin.h" -#include "afb-req-itf.h" // Sample Generic Ping Debug API diff --git a/plugins/session/token-api.c b/plugins/session/token-api.c index 7633b950..91f9cd8f 100644 --- a/plugins/session/token-api.c +++ b/plugins/session/token-api.c @@ -20,7 +20,6 @@ #include #include "afb-plugin.h" -#include "afb-req-itf.h" // Dummy sample of Client Application Context typedef struct { diff --git a/src/afb-api-so.c b/src/afb-api-so.c index 093a8a93..60af72ef 100644 --- a/src/afb-api-so.c +++ b/src/afb-api-so.c @@ -33,7 +33,8 @@ #include "afb-plugin.h" #include "afb-req-itf.h" -#include "afb-poll-itf.h" +#include "afb-pollmgr-itf.h" +#include "afb-evmgr-itf.h" #include "session.h" #include "afb-apis.h" @@ -41,17 +42,19 @@ #include "verbose.h" #include "utils-upoll.h" +extern __thread sigjmp_buf *error_handler; + struct api_so_desc { - struct AFB_plugin *plugin; /* descriptor */ - void *handle; /* context of dlopen */ - struct AFB_interface interface; + struct AFB_plugin *plugin; /* descriptor */ + void *handle; /* context of dlopen */ + struct AFB_interface interface; /* interface */ }; static int api_timeout = 15; static const char plugin_register_function[] = "pluginRegister"; -static const struct afb_pollitf upollitf = { +static const struct afb_pollmgr_itf pollmgr_itf = { .wait = (void*)upoll_wait, .open = (void*)upoll_open, .on_readable = (void*)upoll_on_readable, @@ -60,6 +63,29 @@ static const struct afb_pollitf upollitf = { .close = (void*)upoll_close }; +static void afb_api_so_evmgr_push(struct api_so_desc *desc, const char *name, struct json_object *object) +{ +} + +static const struct afb_evmgr_itf evmgr_itf = { + .push = (void*)afb_api_so_evmgr_push +}; + +static struct afb_evmgr afb_api_so_get_evmgr(struct api_so_desc *desc) +{ + return (struct afb_evmgr){ .itf = &evmgr_itf, .closure = desc }; +} + +static struct afb_pollmgr afb_api_so_get_pollmgr(struct api_so_desc *desc) +{ + return (struct afb_pollmgr){ .itf = &pollmgr_itf, .closure = NULL }; +} + +static const struct afb_daemon_itf daemon_itf = { + .get_evmgr = (void*)afb_api_so_get_evmgr, + .get_pollmgr = (void*)afb_api_so_get_pollmgr +}; + static void free_context(struct api_so_desc *desc, void *context) { void (*cb)(void*); @@ -71,9 +97,6 @@ static void free_context(struct api_so_desc *desc, void *context) free(context); } - -// Check of apiurl is declare in this plugin and call it -extern __thread sigjmp_buf *error_handler; static void trapping_call(struct afb_req req, void(*cb)(struct afb_req)) { volatile int signum, timerset; @@ -82,7 +105,6 @@ static void trapping_call(struct afb_req req, void(*cb)(struct afb_req)) struct sigevent sevp; struct itimerspec its; - // save context before calling the API timerset = 0; older = error_handler; signum = setjmp(jmpbuf); @@ -155,8 +177,6 @@ static void call(struct api_so_desc *desc, struct afb_req req, const char *verb, afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, desc->plugin->prefix); } - - int afb_api_so_add_plugin(const char *path) { struct api_so_desc *desc; @@ -187,8 +207,8 @@ int afb_api_so_add_plugin(const char *path) /* init the interface */ desc->interface.verbosity = 0; desc->interface.mode = AFB_MODE_LOCAL; - desc->interface.pollitf = &upollitf; - desc->interface.pollclosure = NULL; + desc->interface.daemon.itf = &daemon_itf; + desc->interface.daemon.closure = desc; /* init the plugin */ desc->plugin = pluginRegisterFct(&desc->interface); @@ -327,23 +347,3 @@ int afb_api_so_add_pathset(const char *pathset) }; } - - - - - - - - - - - - - - - - - - - - diff --git a/src/afb-apis.c b/src/afb-apis.c index 1cc0648a..0af3b42c 100644 --- a/src/afb-apis.c +++ b/src/afb-apis.c @@ -18,28 +18,14 @@ #define _GNU_SOURCE +#include #include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "afb-plugin.h" -#include "afb-req-itf.h" -#include "afb-poll-itf.h" #include "session.h" -#include "afb-apis.h" #include "verbose.h" -#include "utils-upoll.h" +#include "afb-apis.h" +#include "afb-req-itf.h" struct api_desc { struct afb_api api; -- 2.16.6