add: debug logging and status cache

This commit is contained in:
Tobias Reisinger 2020-08-01 16:26:34 +02:00
parent 398019afe8
commit 9e5718db43
3 changed files with 59 additions and 15 deletions

View file

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7) cmake_minimum_required (VERSION 3.7)
project(core project(core
VERSION 0.2.4 VERSION 0.2.5
LANGUAGES C) LANGUAGES C)
add_executable(core src/main.c) add_executable(core src/main.c)

View file

@ -90,6 +90,7 @@ handle_http_request(struct mg_connection *nc, struct http_message *hm)
/* Normalize path - resolve "." and ".." (in-place). */ /* Normalize path - resolve "." and ".." (in-place). */
if (!mg_normalize_uri_path(&hm->uri, &hm->uri)) { if (!mg_normalize_uri_path(&hm->uri, &hm->uri)) {
mg_http_send_error(nc, 400, global_config.http_server_opts.extra_headers); mg_http_send_error(nc, 400, global_config.http_server_opts.extra_headers);
LOGGER_DEBUG("failed to normalize uri %.*s\n", hm->uri.len, hm->uri.p);
return; return;
} }
char *request_file_org = malloc(sizeof(char) * hm->uri.len); char *request_file_org = malloc(sizeof(char) * hm->uri.len);
@ -112,10 +113,12 @@ handle_http_request(struct mg_connection *nc, struct http_message *hm)
{ {
response.status_code = 0; response.status_code = 0;
mg_serve_http(nc, hm, global_config.http_server_opts); mg_serve_http(nc, hm, global_config.http_server_opts);
LOGGER_DEBUG("serving %.*s\n", hm->uri.len, hm->uri.p);
return; return;
} }
else else
{ {
LOGGER_DEBUG("serving 'not found'\n");
endpoint = router_get_not_found_endpoint(); endpoint = router_get_not_found_endpoint();
} }
} }
@ -142,10 +145,13 @@ handle_http_request(struct mg_connection *nc, struct http_message *hm)
} }
else else
{ {
LOGGER_DEBUG("calling endpoint function %p\n", endpoint->func);
endpoint->func(nc, hm, endpoint->args, &response); endpoint->func(nc, hm, endpoint->args, &response);
LOGGER_DEBUG("sending response to %p\n", nc);
send_response(nc, &response); send_response(nc, &response);
} }
LOGGER_DEBUG("freeing endpoint args\n");
for(int i = 0; i < endpoint->args_count; ++i) for(int i = 0; i < endpoint->args_count; ++i)
{ {
if(endpoint->args[i].type == ENDPOINT_ARG_TYPE_STR) if(endpoint->args[i].type == ENDPOINT_ARG_TYPE_STR)
@ -169,6 +175,7 @@ handler_http(struct mg_connection *nc, int ev, void *p)
} }
if(ev == MG_EV_HTTP_REQUEST) if(ev == MG_EV_HTTP_REQUEST)
{ {
LOGGER_DEBUG("new http request (%p)\n", nc);
struct http_message *hm = (struct http_message*)p; struct http_message *hm = (struct http_message*)p;
handle_http_request(nc, hm); handle_http_request(nc, hm);
} }

View file

@ -2,16 +2,21 @@
#include <logger.h> #include <logger.h>
relay_t **global_relay_status_list; relay_t **global_relay_status_list;
char *relay_status_list_json_str;
size_t relay_status_list_json_str_len;
void void
status_init() status_init()
{ {
global_relay_status_list = relay_get_all(); global_relay_status_list = relay_get_all();
relay_status_list_json_str = NULL;
relay_status_list_json_str_len = 0;
} }
void void
status_reload_entry(int relay_id) status_reload_entry(int relay_id)
{ {
LOGGER_DEBUG("reloading relay status\n");
relay_t **relays = global_relay_status_list; relay_t **relays = global_relay_status_list;
for(int i = 0; relays[i] != NULL; ++i) for(int i = 0; relays[i] != NULL; ++i)
{ {
@ -25,12 +30,15 @@ status_reload_entry(int relay_id)
relay_free(relays[i]); relay_free(relays[i]);
relays[i] = updated_relay; relays[i] = updated_relay;
relays[i]->is_on = is_on_backup; relays[i]->is_on = is_on_backup;
relay_status_list_json_str = NULL;
relay_status_list_json_str_len = 0;
} }
} }
void void
status_update_entry(int relay_id, int is_on) status_update_entry(int relay_id, int is_on)
{ {
LOGGER_DEBUG("updating relay status ([%d] = %d)\n", relay_id, is_on);
relay_t **relays = global_relay_status_list; relay_t **relays = global_relay_status_list;
for(int i = 0; relays[i] != NULL; ++i) for(int i = 0; relays[i] != NULL; ++i)
{ {
@ -39,32 +47,62 @@ status_update_entry(int relay_id, int is_on)
continue; continue;
} }
relays[i]->is_on = is_on; relays[i]->is_on = is_on;
relay_status_list_json_str = NULL;
relay_status_list_json_str_len = 0;
} }
} }
static int is_websocket(const struct mg_connection *nc) static int
is_websocket(const struct mg_connection *nc)
{ {
return nc->flags & MG_F_IS_WEBSOCKET; return nc->flags & MG_F_IS_WEBSOCKET;
} }
static void
validate_json_str_cache()
{
if(!relay_status_list_json_str)
{
relay_t **relays = global_relay_status_list;
cJSON *json = relay_list_to_json(relays);
char *json_str = cJSON_Print(json);
size_t json_str_len = strlen(json_str);
LOGGER_DEBUG("writing new json string to cache\n");
relay_status_list_json_str = json_str;
relay_status_list_json_str_len = json_str_len;
cJSON_Delete(json);
}
}
void void
status_broadcast(struct mg_mgr *mgr) status_broadcast(struct mg_mgr *mgr)
{ {
struct mg_connection *c; struct mg_connection *c;
relay_t **relays = global_relay_status_list; validate_json_str_cache();
char *json_str = relay_status_list_json_str;
size_t json_str_len = relay_status_list_json_str_len;
cJSON *json = relay_list_to_json(relays); int c_count = 0;
char *json_str = cJSON_Print(json);
size_t json_str_len = strlen(json_str);
for (c = mg_next(mgr, NULL); c != NULL; c = mg_next(mgr, c)) { for (c = mg_next(mgr, NULL); c != NULL; c = mg_next(mgr, c)) {
if(is_websocket(c)) if(is_websocket(c))
{ {
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, json_str, json_str_len); mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, json_str, json_str_len);
++c_count;
} }
} }
free(json_str);
cJSON_Delete(json); if(c_count)
{
LOGGER_DEBUG("finished status broadcast to %d client(s)\n", c_count);
}
} }
void void
@ -75,20 +113,19 @@ status_send(struct mg_connection *c)
return; return;
} }
relay_t **relays = global_relay_status_list; validate_json_str_cache();
cJSON *json = relay_list_to_json(relays); char *json_str = relay_status_list_json_str;
char *json_str = cJSON_Print(json); size_t json_str_len = relay_status_list_json_str_len;
size_t json_str_len = strlen(json_str);
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, json_str, json_str_len); mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, json_str, json_str_len);
free(json_str); LOGGER_DEBUG("finished status send\n");
cJSON_Delete(json);
} }
void void
status_free() status_free()
{ {
relay_free_list(global_relay_status_list); relay_free_list(global_relay_status_list);
free(relay_status_list_json_str);
} }