From f49c760e97b3cbd582de3a6458eb100643ca0c80 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Sat, 15 Aug 2020 16:06:39 +0200 Subject: [PATCH] add: faster broadcast on status change --- src/main.c | 9 +--- src/status.c | 120 +++++++++++++++++++++++++++++---------------------- 2 files changed, 70 insertions(+), 59 deletions(-) diff --git a/src/main.c b/src/main.c index 525c710..cc60f25 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include @@ -142,16 +141,10 @@ main(int argc, const char** argv) /******************** START MAIN LOOP ********************/ - time_t timer = time(NULL); - for (;;) { mg_mgr_poll(&mgr, 200); - if(time(NULL) - timer >= 10) - { - status_broadcast(&mgr); - timer = time(NULL); - } + status_broadcast(&mgr); } terminate(0); diff --git a/src/status.c b/src/status.c index ec171b7..c3995d1 100644 --- a/src/status.c +++ b/src/status.c @@ -1,3 +1,5 @@ +#include + #include #include @@ -8,56 +10,7 @@ relay_t **global_relay_status_list; char *relay_status_list_json_str; size_t relay_status_list_json_str_len; -void -status_init() -{ - global_relay_status_list = relay_get_all(); - relay_status_list_json_str = NULL; - relay_status_list_json_str_len = 0; -} - -void -status_reload_entry(int relay_id) -{ - LOGGER_DEBUG("reloading relay status\n"); - relay_t **relays = global_relay_status_list; - for(int i = 0; relays[i] != NULL; ++i) - { - if(relays[i]->id != relay_id) - { - continue; - } - - int is_on_backup = relays[i]->is_on; - relay_t *updated_relay = relay_get_by_id(relay_id); - relay_free(relays[i]); - relays[i] = updated_relay; - relays[i]->is_on = is_on_backup; - relay_status_list_json_str = NULL; - relay_status_list_json_str_len = 0; - - cache_invalidate_relay(relay_id); - } -} - -void -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; - for(int i = 0; relays[i] != NULL; ++i) - { - if(relays[i]->id != relay_id) - { - continue; - } - relays[i]->is_on = is_on; - relay_status_list_json_str = NULL; - relay_status_list_json_str_len = 0; - - cache_invalidate_relay(relay_id); - } -} +time_t timer; static int is_websocket(const struct mg_connection *nc) @@ -65,6 +18,14 @@ is_websocket(const struct mg_connection *nc) return nc->flags & MG_F_IS_WEBSOCKET; } +static void +invalidate_json_str_cache() +{ + relay_status_list_json_str = NULL; + relay_status_list_json_str_len = 0; + timer = 0; +} + static void validate_json_str_cache() { @@ -83,13 +44,70 @@ validate_json_str_cache() cJSON_Delete(json); } - } +void +status_init() +{ + global_relay_status_list = relay_get_all(); + + invalidate_json_str_cache(); + timer = time(NULL); +} + +void +status_reload_entry(int relay_id) +{ + LOGGER_DEBUG("reloading relay status\n"); + relay_t **relays = global_relay_status_list; + for(int i = 0; relays[i] != NULL; ++i) + { + if(relays[i]->id != relay_id) + { + continue; + } + + int is_on_backup = relays[i]->is_on; + relay_t *updated_relay = relay_get_by_id(relay_id); + relay_free(relays[i]); + relays[i] = updated_relay; + relays[i]->is_on = is_on_backup; + + invalidate_json_str_cache(); + + cache_invalidate_relay(relay_id); + } +} + +void +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; + for(int i = 0; relays[i] != NULL; ++i) + { + if(relays[i]->id != relay_id) + { + continue; + } + relays[i]->is_on = is_on; + + invalidate_json_str_cache(); + + cache_invalidate_relay(relay_id); + } +} + void status_broadcast(struct mg_mgr *mgr) { + if(timer && (timer - time(NULL) < 10)) + { + return; + } + timer = time(NULL); + struct mg_connection *c; validate_json_str_cache();