diff --git a/src/main.c b/src/main.c index 31ba36c..d9506bf 100644 --- a/src/main.c +++ b/src/main.c @@ -17,16 +17,6 @@ static void signal_handler(int signo) { } } -// Only used to construct basic variables of a service before initialization -struct PreService { - char* id; - char* name; - char* icon; - char* text; - char* endpoints; - uint32_t backoff; -}; - static int config_handler(void* user, const char* section, const char* name, const char* value) { if (strcmp(section, "") == 0) { if (strcmp(name, "service_name") == 0) { @@ -55,13 +45,13 @@ static int config_handler(void* user, const char* section, const char* name, con } } else if (strncmp(CONFIG_SERVICE_PREFIX, section, strlen(CONFIG_SERVICE_PREFIX)) == 0) { const char* id = section+strlen(CONFIG_SERVICE_PREFIX); - Vector* tmp_services = (Vector*)user; - struct PreService* current_service = NULL; + Vector* service_vector = (Vector*)user; + struct Service* current_service = NULL; - Iterator it = vector_begin(tmp_services); - Iterator end = vector_end(tmp_services); + Iterator it = vector_begin(service_vector); + Iterator end = vector_end(service_vector); for (; !iterator_equals(&it, &end); iterator_increment(&it)) { - struct PreService* current = *(struct PreService**)iterator_get(&it); + struct Service* current = *(struct Service**)iterator_get(&it); if (strcmp(id, current->id) == 0) { current_service = current; break; @@ -72,7 +62,7 @@ static int config_handler(void* user, const char* section, const char* name, con current_service->id = malloc(strlen(id)+1); strcpy(current_service->id, id); current_service->backoff = 0; - vector_push_back(tmp_services, ¤t_service); + vector_push_back(service_vector, ¤t_service); } if (strcmp(name, "name") == 0) { current_service->name = malloc(strlen(value)+1); @@ -84,8 +74,15 @@ static int config_handler(void* user, const char* section, const char* name, con current_service->icon = malloc(strlen(value)+1); strcpy(current_service->icon, value); } else if (strcmp(name, "address") == 0) { - current_service->endpoints = malloc(strlen(value)+1); - strcpy(current_service->endpoints, value); + char* endpoints = malloc(strlen(value)); + strncpy(endpoints, value, strlen(value)); + char* token = strtok(endpoints, ","); + vector_setup(¤t_service->endpoints, 255, sizeof(struct Endpoint*)); + while (token != NULL) { + add_endpoint(current_service, token); + fflush(stdout); + token = strtok(NULL, ","); + } } else if (strcmp(name, "backoff") == 0) { current_service->backoff = atoi(value); } else { @@ -139,46 +136,25 @@ int main(int argc, char** argv) { } // Load configuration file - Vector tmp_services; - vector_setup(&tmp_services, 255, sizeof(struct PreService*)); + vector_setup(&services, 255, sizeof(struct Service*)); printf("Loading configuration file: %s\n", config_path); - int err = ini_parse(config_path, config_handler, &tmp_services); + int err = ini_parse(config_path, config_handler, &services); if (err < 0) { fprintf(stderr, "Failed to load configuration file: %i\n", err); return 1; } - // initialize Services from temp vector - vector_setup(&services, 255, sizeof(struct Service*)); - Iterator it = vector_begin(&tmp_services); - Iterator end = vector_end(&tmp_services); + Iterator it = vector_begin(&services); + Iterator end = vector_end(&services); for (; !iterator_equals(&it, &end); iterator_increment(&it)) { - struct PreService* preservice = *(struct PreService**)iterator_get(&it); - printf("Initializing service \"%s\"\n", preservice->name); + struct Service* service = *(struct Service**)iterator_get(&it); + printf("Configuring service \"%s\"\n", service->name); fflush(stdout); - struct Service* service = malloc(8192); - service->id = preservice->id; - service->name = preservice->name; - service->icon = preservice->icon; - service->text = preservice->text; service->last_state = DOWN; - service->backoff = preservice->backoff == 0 ? global_backoff : preservice->backoff; - - char* endpoints = malloc(strlen(preservice->endpoints)+1); - strcpy(endpoints, preservice->endpoints); - char* token = strtok(endpoints, ","); - vector_setup(&service->endpoints, 255, sizeof(struct Endpoint*)); - while (token != NULL) { - add_endpoint(service, token); - fflush(stdout); - token = strtok(NULL, ","); - } - vector_push_back(&services, &service); + service->backoff = service->backoff == 0 ? global_backoff : service->backoff; load_service(&service); } vector_shrink_to_fit(&services); - vector_clear(&tmp_services); - vector_destroy(&tmp_services); struct MHD_Daemon *daemon = MHD_start_daemon(MHD_USE_AUTO | MHD_USE_DUAL_STACK | MHD_USE_INTERNAL_POLLING_THREAD, listen_port, NULL, NULL, &http_response, NULL, MHD_OPTION_END); if (daemon == NULL) {