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: add api_session_name in order to improve debug output

This commit is contained in:
Steffen Vogel 2017-08-27 17:59:59 +02:00
parent 7dd8407960
commit 98ea4d92b7
3 changed files with 53 additions and 14 deletions

View file

@ -45,9 +45,9 @@ struct api_session {
enum state state;
enum api_version version;
enum api_mode mode;
int runs;
bool completed; /**< Did we receive the complete body yet? */
struct {
struct buffer buffer;
@ -56,6 +56,8 @@ struct api_session {
struct lws *wsi;
struct api *api;
char *_name;
};
int api_session_init(struct api_session *s, enum api_mode m);
@ -63,3 +65,5 @@ int api_session_init(struct api_session *s, enum api_mode m);
int api_session_destroy(struct api_session *s);
int api_session_run_command(struct api_session *s, json_t *req, json_t **resp);
char * api_session_name(struct api_session *s);

View file

@ -63,7 +63,7 @@ int api_ws_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void *
s->wsi = wsi;
s->api = w->api;
debug(LOG_API, "New API session initiated: version=%d, mode=websocket", s->version);
debug(LOG_API, "Initiated API session: %s", api_session_name(s));
break;
case LWS_CALLBACK_CLOSED:
@ -71,7 +71,7 @@ int api_ws_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void *
if (ret)
return -1;
debug(LOG_API, "Closed API session");
debug(LOG_API, "Closing API session: %s", api_session_name(s));
break;
@ -88,11 +88,11 @@ int api_ws_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void *
pushed = queue_push(&s->request.queue, req);
if (pushed != 1)
warn("Queue overun in Api session");
warn("Queue overun in API session");
pushed = queue_signalled_push(&w->api->pending, s);
if (pushed != 1)
warn("Queue overrun in Api");
warn("Queue overrun in API");
}
break;
@ -145,7 +145,7 @@ int api_http_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void
s->wsi = wsi;
s->api = w->api;
debug(LOG_API, "New API session initiated: version=%d, mode=http", s->version);
debug(LOG_API, "Initiated API session: %s", api_session_name(s));
break;
@ -172,11 +172,11 @@ int api_http_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void
pushed = queue_push(&s->request.queue, req);
if (pushed != 1)
warn("Queue overrun for Api session");
warn("Queue overrun for API session: %s", api_session_name(s));
pushed = queue_signalled_push(&w->api->pending, s);
if (pushed != 1)
warn("Queue overrun for Api");
warn("Queue overrun for API");
break;
@ -198,6 +198,8 @@ int api_http_protocol_cb(struct lws *wsi, enum lws_callback_reasons reason, void
lws_write(wsi, (unsigned char *) headers, strlen(headers), LWS_WRITE_HTTP_HEADERS);
lws_write(wsi, (unsigned char *) s->response.buffer.buf, s->response.buffer.len, LWS_WRITE_HTTP);
debug(LOG_API, "Closing API session: %s", api_session_name(s));
return -1; /* Close connection */
}
@ -239,7 +241,7 @@ int api_start(struct api *a)
ret = pthread_create(&a->thread, NULL, worker, a);
if (ret)
error("Failed to start Api worker thread");
error("Failed to start API worker thread");
a->state = STATE_STARTED;
@ -261,11 +263,11 @@ int api_stop(struct api *a)
ret = pthread_cancel(a->thread);
if (ret)
serror("Failed to cancel Api worker thread");
serror("Failed to cancel API worker thread");
ret = pthread_join(a->thread, NULL);
if (ret)
serror("Failed to join Api worker thread");
serror("Failed to join API worker thread");
a->state = STATE_STOPPED;
@ -282,10 +284,13 @@ static void * worker(void *ctx)
json_t *req, *resp;
for (;;) {
info("waiting for aPI jobs");
pulled = queue_signalled_pull(&a->pending, (void **) &s);
if (pulled != 1)
continue;
info("processing api request");
queue_pull(&s->request.queue, (void **) &req);
api_session_run_command(s, req, &resp);

View file

@ -30,7 +30,8 @@ int api_session_init(struct api_session *s, enum api_mode m)
{
int ret;
s->completed = false;
s->runs = 0;
s->mode = m;
ret = buffer_init(&s->request.buffer, 0);
if (ret)
@ -47,6 +48,8 @@ int api_session_init(struct api_session *s, enum api_mode m)
ret = queue_init(&s->response.queue, 128, &memtype_heap);
if (ret)
return ret;
s->_name = NULL;
return 0;
}
@ -73,6 +76,9 @@ int api_session_destroy(struct api_session *s)
ret = queue_destroy(&s->response.queue);
if (ret)
return ret;
if (s->_name)
free(s->_name);
s->state = STATE_DESTROYED;
@ -111,7 +117,7 @@ int api_session_run_command(struct api_session *s, json_t *json_in, json_t **jso
goto out;
}
debug(LOG_API, "Running API request: %s", p->name);
debug(LOG_API, "Running API request: action=%s, id=%s", action, id);
ret = p->api.cb(&p->api, json_args, &json_resp, s);
if (ret)
@ -126,7 +132,31 @@ int api_session_run_command(struct api_session *s, json_t *json_in, json_t **jso
"id", id,
"response", json_resp);
out: debug(LOG_API, "API request completed with code: %d", ret);
out: debug(LOG_API, "Completed API request: action=%s, id=%s, code=%d", action, id, ret);
s->runs++;
return 0;
}
char * api_session_name(struct api_session *s)
{
if (!s->_name) {
char *mode;
switch (s->mode) {
case API_MODE_WS: mode = "ws"; break;
case API_MODE_HTTP: mode = "http"; break;
default: mode = "unknown"; break;
}
char name[128];
char ip[128];
lws_get_peer_addresses(s->wsi, lws_get_socket_fd(s->wsi), name, sizeof(name), ip, sizeof(ip));
s->_name = strcatf(&s->_name, "version=%d, mode=%s, runs=%d, remote.name=%s, remote.ip=%s", s->version, mode, s->runs, name, ip);
}
return s->_name;
}