diff --git a/include/villas/web.h b/include/villas/web.h index 072186946..605964ffd 100644 --- a/include/villas/web.h +++ b/include/villas/web.h @@ -7,6 +7,9 @@ #pragma once +#include +#include + #include "common.h" /* Forward declarations */ @@ -24,6 +27,8 @@ struct web { const char *htdocs; /**< The root directory for files served via HTTP. */ const char *ssl_cert; /**< Path to the SSL certitifcate for HTTPS / WSS. */ const char *ssl_private_key; /**< Path to the SSL private key for HTTPS / WSS. */ + + pthread_t thread; }; /** Initialize the web interface. @@ -39,7 +44,4 @@ int web_start(struct web *w); int web_stop(struct web *w); /** Parse HTTPd and WebSocket related options */ -int web_parse(struct web *w, config_setting_t *lcs); - -/** libwebsockets service routine. Call periodically */ -int web_service(struct web *w); \ No newline at end of file +int web_parse(struct web *w, config_setting_t *lcs); \ No newline at end of file diff --git a/lib/web.c b/lib/web.c index c65821f97..5860b53dd 100644 --- a/lib/web.c +++ b/lib/web.c @@ -122,6 +122,18 @@ static void logger(int level, const char *msg) { } } +static void * worker(void *ctx) +{ + struct web *w = ctx; + + assert(w->state == STATE_STARTED); + + for (;;) + lws_service(w->context, 100); + + return NULL; +} + int web_init(struct web *w, struct api *a) { lws_set_log_level((1 << LLL_COUNT) - 1, logger); @@ -154,6 +166,8 @@ int web_parse(struct web *w, config_setting_t *cfg) int web_start(struct web *w) { + int ret; + /* Start server */ struct lws_context_creation_info ctx_info = { .options = LWS_SERVER_OPTION_EXPLICIT_VHOSTS | LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT, @@ -185,10 +199,14 @@ int web_start(struct web *w) if (w->vhost == NULL) error("WebSocket: failed to initialize server"); } + + ret = pthread_create(&w->thread, NULL, worker, w); + if (ret) + error("Failed to start Web worker"); w->state = STATE_STARTED; - return 0; + return ret; } int web_stop(struct web *w) @@ -198,6 +216,11 @@ int web_stop(struct web *w) if (w->state == STATE_STARTED) lws_cancel_service(w->context); + /** @todo Wait for all connections to be closed */ + + pthread_cancel(w->thread); + pthread_join(w->thread, NULL); + w->state = STATE_STOPPED; return 0; @@ -215,10 +238,3 @@ int web_destroy(struct web *w) return 0; } - -int web_service(struct web *w) -{ - assert(w->state == STATE_STARTED); - - return lws_service(w->context, 10); -} diff --git a/src/node.c b/src/node.c index acd4feb7a..2a88df697 100644 --- a/src/node.c +++ b/src/node.c @@ -123,8 +123,6 @@ int main(int argc, char *argv[]) last = time_now(); } - - web_service(&sn.web); /** @todo Maybe we should move this to another thread */ } return 0; diff --git a/src/pipe.c b/src/pipe.c index 049067749..82db53fd8 100644 --- a/src/pipe.c +++ b/src/pipe.c @@ -247,12 +247,8 @@ int main(int argc, char *argv[]) pthread_create(&recvv.thread, NULL, recv_loop, NULL); pthread_create(&sendd.thread, NULL, send_loop, NULL); - for (;;) { - if (node->_vt->start == websocket_start) - web_service(&sn.web); - else - sleep(1); - } + for (;;) + sleep(1); return 0; }