From 1ea6bd0f466a10d29f12801aa35fb6d2b30443a1 Mon Sep 17 00:00:00 2001 From: José Bollo Date: Wed, 27 Jul 2016 15:51:01 +0200 Subject: Expose use of the event loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/CMakeLists.txt | 2 +- src/afb-client-demo.c | 16 +++++++++++++--- src/afb-ws-client.c | 17 ++--------------- src/afb-ws-client.h | 8 +++----- src/afb-ws-json1.c | 3 ++- src/afb-ws.c | 6 ++++-- src/afb-ws.h | 3 ++- src/afb-wsj1.c | 4 ++-- src/afb-wsj1.h | 5 ++++- src/export-afbwsc.map | 1 - 10 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 915da89d..af4f10c1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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}) diff --git a/src/afb-client-demo.c b/src/afb-client-demo.c index 1ead5f74..e8ee2f11 100644 --- a/src/afb-client-demo.c +++ b/src/afb-client-demo.c @@ -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; } diff --git a/src/afb-ws-client.c b/src/afb-ws-client.c index de44a398..0faa60f4 100644 --- a/src/afb-ws-client.c +++ b/src/afb-ws-client.c @@ -29,7 +29,6 @@ #include #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(); -} - diff --git a/src/afb-ws-client.h b/src/afb-ws-client.h index cac4c782..7dd825fe 100644 --- a/src/afb-ws-client.h +++ b/src/afb-ws-client.h @@ -19,15 +19,13 @@ 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); diff --git a/src/afb-ws-json1.c b/src/afb-ws-json1.c index 9d295e78..8f558ab6 100644 --- a/src/afb-ws-json1.c +++ b/src/afb-ws-json1.c @@ -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; diff --git a/src/afb-ws.c b/src/afb-ws.c index 34796818..53fb60ae 100644 --- a/src/afb-ws.c +++ b/src/afb-ws.c @@ -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; diff --git a/src/afb-ws.h b/src/afb-ws.h index 49deaacd..cf01b051 100644 --- a/src/afb-ws.h +++ b/src/afb-ws.h @@ -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); diff --git a/src/afb-wsj1.c b/src/afb-wsj1.c index 068a3326..253bb5ea 100644 --- a/src/afb-wsj1.c +++ b/src/afb-wsj1.c @@ -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; diff --git a/src/afb-wsj1.h b/src/afb-wsj1.h index 49b77828..1f433483 100644 --- a/src/afb-wsj1.h +++ b/src/afb-wsj1.h @@ -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' diff --git a/src/export-afbwsc.map b/src/export-afbwsc.map index ac7dea58..c775a16f 100644 --- a/src/export-afbwsc.map +++ b/src/export-afbwsc.map @@ -1,7 +1,6 @@ { global: afb_ws_client_connect_wsj1; - afb_ws_client_get_event_loop; afb_wsj1_*; afb_common_*; local: -- cgit 1.2.3-korg