prepares event propagation
authorJosé Bollo <jose.bollo@iot.bzh>
Mon, 18 Apr 2016 15:08:26 +0000 (17:08 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Tue, 19 Apr 2016 09:50:40 +0000 (11:50 +0200)
Change-Id: Ib824c6dea1837cc1cbb70a2c82363c9b0a0517d4
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
include/afb-evmgr-itf.h [moved from include/afb-poll-itf.h with 65% similarity]
include/afb-plugin.h
include/afb-pollmgr-itf.h [new file with mode: 0644]
plugins/afm-main-plugin/afm-main-plugin.c
plugins/samples/ClientCtx.c
plugins/samples/HelloWorld.c
plugins/samples/SamplePost.c
plugins/session/token-api.c
src/afb-api-so.c
src/afb-apis.c

similarity index 65%
rename from include/afb-poll-itf.h
rename to include/afb-evmgr-itf.h
index b70eb99..2268ec9 100644 (file)
 
 #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);
+}
+
index 2c9935e..c7252d6 100644 (file)
@@ -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 (file)
index 0000000..c3a62c1
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2016 IoT.bzh
+ * Author: José Bollo <jose.bollo@iot.bzh>
+ *
+ * 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);
+}
+
+
index 2d350df..d7ef198 100644 (file)
 #define _GNU_SOURCE         /* See feature_test_macros(7) */
 #include <stdio.h>
 #include <string.h>
+#include <assert.h>
 #include <json.h>
 
 #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;
 }
 
index 2d5ebfe..24cb4f3 100644 (file)
@@ -20,7 +20,6 @@
 #include <json.h>
 
 #include "afb-plugin.h"
-#include "afb-req-itf.h"
 
 typedef struct {
   /* 
index d02fd11..bf809cc 100644 (file)
@@ -20,7 +20,6 @@
 #include <json.h>
 
 #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)
index 6def3b9..f484d13 100644 (file)
@@ -21,7 +21,6 @@
 #include <json.h>
 
 #include "afb-plugin.h"
-#include "afb-req-itf.h"
 
 
 // Sample Generic Ping Debug API
index 7633b95..91f9cd8 100644 (file)
@@ -20,7 +20,6 @@
 #include <json.h>
 
 #include "afb-plugin.h"
-#include "afb-req-itf.h"
 
 // Dummy sample of Client Application Context
 typedef struct {
index 093a8a9..60af72e 100644 (file)
@@ -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"
 #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)
        };
 }
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
index 1cc0648..0af3b42 100644 (file)
 
 #define _GNU_SOURCE
 
+#include <stdlib.h>
 #include <stdio.h>
-#include <assert.h>
 #include <string.h>
-#include <dirent.h>
-#include <dlfcn.h>
-#include <unistd.h>
-#include <limits.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <signal.h>
-#include <time.h>
-#include <sys/syscall.h>
-#include <setjmp.h>
-
-#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;