Expose use of the event loop
authorJosé Bollo <jose.bollo@iot.bzh>
Wed, 27 Jul 2016 13:51:01 +0000 (15:51 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Wed, 27 Jul 2016 15:40:58 +0000 (17:40 +0200)
The use of the event loop where previously hidden
in internal deep places of the websocket modules.

This commits enforce the client of the library to
explicitely tell what event loop must be used.
This has 3 effects:
 - you know that the systemd event loop is used
 - you tell the event loop to use (no confusion)
 - you don't depend on afb-common.c

Change-Id: Id13d8a96f981183c299cde414d9bb0cd77fe3daa
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/CMakeLists.txt
src/afb-client-demo.c
src/afb-ws-client.c
src/afb-ws-client.h
src/afb-ws-json1.c
src/afb-ws.c
src/afb-ws.h
src/afb-wsj1.c
src/afb-wsj1.h
src/export-afbwsc.map

index 915da89..af4f10c 100644 (file)
@@ -75,7 +75,7 @@ INSTALL(TARGETS afb-daemon
 ###########################################
 # build and install libafbwsc
 ###########################################
-ADD_LIBRARY(afbwsc SHARED afb-ws.c afb-ws-client.c afb-wsj1.c websock.c afb-common.c)
+ADD_LIBRARY(afbwsc SHARED afb-ws.c afb-ws-client.c afb-wsj1.c websock.c)
 SET_TARGET_PROPERTIES(afbwsc PROPERTIES
        VERSION ${LIBAFBWSC_VERSION}
        SOVERSION ${LIBAFBWSC_SOVERSION})
index 1ead5f7..e8ee2f1 100644 (file)
@@ -65,6 +65,9 @@ static void usage(int status, char *arg0)
 /* entry function */
 int main(int ac, char **av, char **env)
 {
+       int rc;
+       sd_event *loop;
+
        /* check the argument count */
        if (ac != 2 && ac != 4 && ac != 5)
                usage(1, av[0]);
@@ -73,8 +76,15 @@ int main(int ac, char **av, char **env)
        if (!strcmp(av[1], "-h") || !strcmp(av[1], "--help"))
                usage(0, av[0]);
 
+       /* get the default event loop */
+       rc = sd_event_default(&loop);
+       if (rc < 0) {
+               fprintf(stderr, "connection to default event loop failed: %s\n", strerror(-rc));
+               return 1;
+       }
+
        /* connect the websocket wsj1 to the uri given by the first argument */
-       wsj1 = afb_ws_client_connect_wsj1(av[1], &itf, NULL);
+       wsj1 = afb_ws_client_connect_wsj1(loop, av[1], &itf, NULL);
        if (wsj1 == NULL) {
                fprintf(stderr, "connection to %s failed: %m\n", av[1]);
                return 1;
@@ -84,7 +94,7 @@ int main(int ac, char **av, char **env)
        if (ac == 2) {
                /* get requests from stdin */
                fcntl(0, F_SETFL, O_NONBLOCK);
-               sd_event_add_io(afb_ws_client_get_event_loop(), &evsrc, 0, EPOLLIN, io_event_callback, NULL);
+               sd_event_add_io(loop, &evsrc, 0, EPOLLIN, io_event_callback, NULL);
        } else {
                /* the request is defined by the arguments */
                exonrep = 1;
@@ -93,7 +103,7 @@ int main(int ac, char **av, char **env)
 
        /* loop until end */
        for(;;)
-               sd_event_run(afb_ws_client_get_event_loop(), 30000000);
+               sd_event_run(loop, 30000000);
        return 0;
 }
 
index de44a39..0faa60f 100644 (file)
@@ -29,7 +29,6 @@
 #include <fcntl.h>
 
 #include "afb-wsj1.h"
-#include "afb-common.h"
 
 /**************** WebSocket handshake ****************************/
 
@@ -315,7 +314,6 @@ invalid:
        errno = EINVAL;
 error:
        return -1;
-
 }
 
 
@@ -323,7 +321,7 @@ error:
 
 static const char *proto_json1[2] = { "x-afb-ws-json1",        NULL };
 
-struct afb_wsj1 *afb_ws_client_connect_wsj1(const char *uri, struct afb_wsj1_itf *itf, void *closure)
+struct afb_wsj1 *afb_ws_client_connect_wsj1(struct sd_event *eloop, const char *uri, struct afb_wsj1_itf *itf, void *closure)
 {
        int rc, fd;
        char *host, *service, xhost[32];
@@ -364,7 +362,7 @@ struct afb_wsj1 *afb_ws_client_connect_wsj1(const char *uri, struct afb_wsj1_itf
                        if (rc == 0) {
                                rc = negociate(fd, proto_json1, path, xhost);
                                if (rc == 0) {
-                                       result = afb_wsj1_create(fd, itf, closure);
+                                       result = afb_wsj1_create(eloop, fd, itf, closure);
                                        if (result != NULL) {
                                                fcntl(fd, F_SETFL, O_NONBLOCK);
                                                break;
@@ -407,15 +405,4 @@ static char *makequery(const char *path, const char *uuid, const char *token)
 }
 #endif
 
-/*
- *
- * Returns the internal event loop coming from afb-common
- *
- * Returns the handle to the event loop
- */
-struct sd_event *afb_ws_client_get_event_loop()
-{
-       return afb_common_get_event_loop();
-}
-
 
index cac4c78..7dd825f 100644 (file)
 
 struct afb_wsj1;
 struct afb_wsj1_itf;
+struct sd_event;
 
 /*
  * Makes the WebSocket handshake at the 'uri' and if successful
  * instanciate a wsj1 websocket for this connection using 'itf' and 'closure'.
  * (see afb_wsj1_create).
+ * The systemd event loop 'eloop' is used to handle the websocket.
  * Returns NULL in case of failure with errno set appriately.
  */
-extern struct afb_wsj1 *afb_ws_client_connect_wsj1(const char *uri, struct afb_wsj1_itf *itf, void *closure);
-
-struct sd_event;
-extern struct sd_event *afb_ws_client_get_event_loop();
-
+extern struct afb_wsj1 *afb_ws_client_connect_wsj1(struct sd_event *eloop, const char *uri, struct afb_wsj1_itf *itf, void *closure);
index 9d295e7..8f558ab 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "afb-wsj1.h"
 #include "afb-ws-json1.h"
+#include "afb-common.h"
 #include "afb-msg-json.h"
 #include "session.h"
 #include "afb-apis.h"
@@ -143,7 +144,7 @@ struct afb_ws_json1 *afb_ws_json1_create(int fd, struct afb_context *context, vo
        if (result->session == NULL)
                goto error2;
 
-       result->wsj1 = afb_wsj1_create(fd, &wsj1_itf, result);
+       result->wsj1 = afb_wsj1_create(afb_common_get_event_loop(), fd, &wsj1_itf, result);
        if (result->wsj1 == NULL)
                goto error3;
 
index 3479681..53fb60a 100644 (file)
@@ -135,10 +135,12 @@ static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, voi
  * Creates the afb_ws structure for the file descritor
  * 'fd' and the callbacks described by the interface 'itf'
  * and its 'closure'.
+ * When the creation is a success, the systemd event loop 'eloop' is
+ * used for handling event for 'fd'.
  *
  * Returns the handle for the afb_ws created or NULL on error.
  */
-struct afb_ws *afb_ws_create(int fd, const struct afb_ws_itf *itf, void *closure)
+struct afb_ws *afb_ws_create(struct sd_event *eloop, int fd, const struct afb_ws_itf *itf, void *closure)
 {
        int rc;
        struct afb_ws *result;
@@ -164,7 +166,7 @@ struct afb_ws *afb_ws_create(int fd, const struct afb_ws_itf *itf, void *closure
                goto error2;
 
        /* creates the evsrc */
-       rc = sd_event_add_io(afb_common_get_event_loop(), &result->evsrc, result->fd, EPOLLIN, io_event_callback, result);
+       rc = sd_event_add_io(eloop, &result->evsrc, result->fd, EPOLLIN, io_event_callback, result);
        if (rc < 0) {
                errno = -rc;
                goto error3;
index 49deaac..cf01b05 100644 (file)
@@ -18,6 +18,7 @@
 #pragma once
 
 struct afb_ws;
+struct sd_event;
 
 struct afb_ws_itf
 {
@@ -28,7 +29,7 @@ struct afb_ws_itf
        void (*on_hangup) (void *); /* optional, it is safe too call afb_ws_destroy within the callback */
 };
 
-extern struct afb_ws *afb_ws_create(int fd, const struct afb_ws_itf *itf, void *closure);
+extern struct afb_ws *afb_ws_create(struct sd_event *eloop, int fd, const struct afb_ws_itf *itf, void *closure);
 extern void afb_ws_destroy(struct afb_ws *ws);
 extern void afb_ws_hangup(struct afb_ws *ws);
 extern int afb_ws_close(struct afb_ws *ws, uint16_t code, const char *reason);
index 068a332..253bb5e 100644 (file)
@@ -79,7 +79,7 @@ struct afb_wsj1
        struct wsj1_call *calls;
 };
 
-struct afb_wsj1 *afb_wsj1_create(int fd, struct afb_wsj1_itf *itf, void *closure)
+struct afb_wsj1 *afb_wsj1_create(struct sd_event *eloop, int fd, struct afb_wsj1_itf *itf, void *closure)
 {
        struct afb_wsj1 *result;
 
@@ -97,7 +97,7 @@ struct afb_wsj1 *afb_wsj1_create(int fd, struct afb_wsj1_itf *itf, void *closure
        if (result->tokener == NULL)
                goto error2;
 
-       result->ws = afb_ws_create(fd, &wsj1_itf, result);
+       result->ws = afb_ws_create(eloop, fd, &wsj1_itf, result);
        if (result->ws == NULL)
                goto error3;
 
index 49b7782..1f43348 100644 (file)
@@ -21,6 +21,7 @@ struct afb_wsj1;
 struct afb_wsj1_msg;
 
 struct json_object;
+struct sd_event;
 
 /*
  * Interface for callback functions.
@@ -49,9 +50,11 @@ struct afb_wsj1_itf {
 /*
  * Creates the afb_wsj1 socket connected to the file descriptor 'fd'
  * and having the callback interface defined by 'itf' for the 'closure'.
+ * When the creation is a success, the systemd event loop 'eloop' is
+ * used for handling event for 'fd'.
  * Returns the created wsj1 websocket or NULL in case of error.
  */
-extern struct afb_wsj1 *afb_wsj1_create(int fd, struct afb_wsj1_itf *itf, void *closure);
+extern struct afb_wsj1 *afb_wsj1_create(struct sd_event *eloop, int fd, struct afb_wsj1_itf *itf, void *closure);
 
 /*
  * Increases by one the count of reference to 'wsj1'
index ac7dea5..c775a16 100644 (file)
@@ -1,7 +1,6 @@
 {
 global:
        afb_ws_client_connect_wsj1;
-       afb_ws_client_get_event_loop;
        afb_wsj1_*;
        afb_common_*;
 local: