add: status for mqtt
fix: refactor connection handlers
This commit is contained in:
parent
2bc11ee829
commit
6c6e5023da
19 changed files with 534 additions and 183 deletions
src
40
src/main.c
40
src/main.c
|
@ -11,6 +11,7 @@
|
|||
#include <handlers.h>
|
||||
#include <enums.h>
|
||||
#include <helpers.h>
|
||||
#include <status.h>
|
||||
#include <confini.h>
|
||||
#include <models/controller.h>
|
||||
|
||||
|
@ -26,6 +27,7 @@ terminate(int signum)
|
|||
sqlite3_close(global_database);
|
||||
|
||||
router_free();
|
||||
status_free();
|
||||
|
||||
exit(signum);
|
||||
}
|
||||
|
@ -49,6 +51,11 @@ main(int argc, const char** argv)
|
|||
/******************** LOAD CONFIG ********************/
|
||||
|
||||
global_config.file = "core.ini";
|
||||
global_config.log_level = LOG_LEVEL_INFO;
|
||||
global_config.discovery_port = 4421;
|
||||
global_config.mqtt_port = 1885;
|
||||
global_config.server_port = 5000;
|
||||
|
||||
strcpy(global_config.user, "");
|
||||
strcpy(global_config.group, "");
|
||||
|
||||
|
@ -56,7 +63,6 @@ main(int argc, const char** argv)
|
|||
strcpy(global_config.not_found_file_type, "text/html");
|
||||
strcpy(global_config.not_found_content, "404 - NOT FOUND");
|
||||
strcpy(global_config.not_found_content_type, "text/plain");
|
||||
global_config.log_level = LOG_LEVEL_INFO;
|
||||
|
||||
helper_parse_cli(argc, argv, &global_config);
|
||||
|
||||
|
@ -82,18 +88,30 @@ main(int argc, const char** argv)
|
|||
|
||||
/******************** SETUP CONNECTION ********************/
|
||||
|
||||
struct mg_connection *c;
|
||||
struct mg_mqtt_broker brk;
|
||||
|
||||
mg_mgr_init(&mgr, NULL);
|
||||
c = mg_bind(&mgr, global_config.server_port, handler_connection);
|
||||
|
||||
if(c == NULL)
|
||||
char address[100];
|
||||
sprintf(address, "tcp://0.0.0.0:%u", global_config.server_port);
|
||||
struct mg_connection *c_http = mg_bind(&mgr, address, handler_http);
|
||||
if(c_http == NULL)
|
||||
{
|
||||
LOG_FATAL("failed to bind to port %s\n", global_config.server_port);
|
||||
LOG_FATAL("failed to bind http server to port %u\n", global_config.server_port);
|
||||
exit(1);
|
||||
}
|
||||
mg_set_protocol_http_websocket(c_http);
|
||||
|
||||
mg_set_protocol_http_websocket(c);
|
||||
sprintf(address, "tcp://0.0.0.0:%u", global_config.mqtt_port);
|
||||
struct mg_connection *c_mqtt = mg_bind(&mgr, address, handler_mqtt);
|
||||
if(c_mqtt == NULL)
|
||||
{
|
||||
LOG_FATAL("failed to bind mqtt server to port %u\n", global_config.mqtt_port);
|
||||
exit(1);
|
||||
}
|
||||
mg_mqtt_broker_init(&brk, NULL);
|
||||
c_mqtt->priv_2 = &brk;
|
||||
mg_set_protocol_mqtt(c_mqtt);
|
||||
|
||||
helper_drop_privileges();
|
||||
|
||||
|
@ -116,16 +134,24 @@ main(int argc, const char** argv)
|
|||
sqlite3_exec(global_database, "PRAGMA foreign_keys = ON", 0, 0, 0);
|
||||
|
||||
|
||||
/******************** INIT ROUTER ********************/
|
||||
/******************** INIT COMPONENTS ********************/
|
||||
|
||||
router_init();
|
||||
status_init();
|
||||
|
||||
|
||||
/******************** START MAIN LOOP ********************/
|
||||
|
||||
time_t timer = time(NULL);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
mg_mgr_poll(&mgr, 1000);
|
||||
if(time(NULL) - timer >= 10)
|
||||
{
|
||||
status_broadcast(&mgr);
|
||||
timer = time(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
terminate(0);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue