1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

api: wait up to 1 sec for all outstanding API requests to be completed.

This commit is contained in:
Steffen Vogel 2017-08-10 13:35:08 +02:00
parent 34b384c23d
commit 3db1374404
3 changed files with 19 additions and 5 deletions

View file

@ -56,6 +56,8 @@ int api_ws_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void *
if (ret)
return -1;
list_push(&w->api->sessions, s);
lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), s->peer.name, sizeof(s->peer.name), s->peer.ip, sizeof(s->peer.ip));
debug(LOG_API, "New API session initiated: version=%d, mode=websocket, remote=%s (%s)", s->version, s->peer.name, s->peer.ip);
@ -65,6 +67,8 @@ int api_ws_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void *
ret = api_session_destroy(s);
if (ret)
return -1;
list_remove(&w->api->sessions, s);
debug(LOG_API, "Closed API session");
@ -120,6 +124,8 @@ int api_http_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void
if (ret)
return -1;
list_push(&w->api->sessions, s);
lws_get_peer_addresses(wsi, lws_get_socket_fd(wsi), s->peer.name, sizeof(s->peer.name), s->peer.ip, sizeof(s->peer.ip));
debug(LOG_API, "New API session initiated: version=%d, mode=http, remote=%s (%s)", s->version, s->peer.name, s->peer.ip);
@ -147,6 +153,9 @@ int api_http_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void
ret = api_session_destroy(s);
if (ret)
return -1;
list_remove(&w->api->sessions, s);
break;
case LWS_CALLBACK_HTTP_BODY:
@ -170,7 +179,7 @@ int api_http_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void
web_buffer_write(&s->response.body, wsi);
if (s->completed && s->response.body.len == 0)
return -1;
return -1; /* Close connection */
break;
default:
@ -213,6 +222,11 @@ int api_start(struct api *a)
int api_stop(struct api *a)
{
info("Stopping API sub-system");
for (int i = 0; i < 10 && list_length(&a->sessions) > 0; i++) {
info("Wait for API requests to complete");
usleep(100 * 1e-3);
}
list_destroy(&a->sessions, (dtor_cb_t) api_session_destroy, false);

View file

@ -431,11 +431,11 @@ int super_node_stop(struct super_node *sn)
}
}
#ifdef WITH_WEB
web_stop(&sn->web);
#endif
#ifdef WITH_API
api_stop(&sn->api);
#endif
#ifdef WITH_WEB
web_stop(&sn->web);
#endif
log_stop(&sn->log);

View file

@ -236,7 +236,7 @@ int web_start(struct web *w)
int web_stop(struct web *w)
{
if (w->state == STATE_STARTED)
if (w->state != STATE_STARTED)
return 0;
info("Stopping Web sub-system");