From 3d2e90469b6c74ed28040800d40a7aa22a9e912c Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 13 Apr 2024 09:13:38 +0000 Subject: [PATCH] reformat git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@17 f982e544-4a7d-3444-ad1a-fde59a2a69f1 --- DevForge/Makefile | 4 ++-- DevForge/config.c | 46 +++++++++++++++++++++++++++++++++++++++++++- DevForge/devforge.h | 1 + DevForge/df_config.h | 6 ++++-- DevForge/main.c | 34 +++++++++++++++++++++++++------- Module/http.c | 17 ++++++++++++++++ 6 files changed, 96 insertions(+), 12 deletions(-) diff --git a/DevForge/Makefile b/DevForge/Makefile index 6f3b442..9ce892e 100644 --- a/DevForge/Makefile +++ b/DevForge/Makefile @@ -1,8 +1,8 @@ # $Id$ -EXTRA_CFLAGS = +EXTRA_CFLAGS = -pthread EXTRA_LDFLAGS = -EXTRA_LIBS = +EXTRA_LIBS = -pthread .PHONY: all clean diff --git a/DevForge/config.c b/DevForge/config.c index 5db210c..a4559ff 100644 --- a/DevForge/config.c +++ b/DevForge/config.c @@ -37,6 +37,7 @@ #include #include +#include #include #include #include @@ -53,11 +54,39 @@ void devforge_add_mod(void* lib) { } loaded_mods[i] = malloc(sizeof(**loaded_mods)); loaded_mods[i]->lib = lib; + loaded_mods[i]->joinable = false; loaded_mods[i + 1] = NULL; free(old_mods); } -struct devforge_config config = {.devforge_log = devforge_log}; +char** params = NULL; + +void devforge_add_param(const char* key, const char* value) { + char** old_params = params; + int i; + for(i = 0; old_params[i] != NULL; i++) + ; + params = malloc(sizeof(*params) * (i + 3)); + for(i = 0; old_params[i] != NULL; i++) { + params[i] = old_params[i]; + } + params[i] = devforge_strdup(key); + params[i + 1] = devforge_strdup(value); + params[i + 2] = NULL; + free(old_params); +} + +char* devforge_get_param(const char* param) { + int i; + for(i = 0; params[i] != NULL; i += 2) { + if(strcmp(params[i], param) == 0) { + return params[i + 1]; + } + } + return NULL; +} + +struct devforge_config config = {.devforge_log = devforge_log, .devforge_get_param = devforge_get_param}; int devforge_load_config(const char* path) { if(server_root == NULL) { @@ -69,6 +98,11 @@ int devforge_load_config(const char* path) { loaded_mods[0] = NULL; } + if(params == NULL) { + params = malloc(sizeof(*params)); + params[0] = NULL; + } + char* str = devforge_strcat3("Loading the config `", path, "`"); devforge_log(DF_LOG, str); free(str); @@ -94,6 +128,15 @@ int devforge_load_config(const char* path) { if(strcmp(key, "ServerRoot") == 0) { free(server_root); server_root = devforge_strdup(value); + } else if(strcmp(key, "Set") == 0) { + for(; value[i] != 0 && !(value[i] == ' ' || value[i] == '\t'); i++) + ; + if(value[i] != 0) { + for(; value[i] != 0 && (value[i] == ' ' || value[i] == '\t'); i++) value[i] = 0; + if(value[i] != 0) { + devforge_add_param(value, value + i); + } + } } else if(strcmp(key, "LoadModule") == 0) { char* path; if(value[0] == '/') { @@ -119,6 +162,7 @@ int devforge_load_config(const char* path) { } free(path); } + break; } } } diff --git a/DevForge/devforge.h b/DevForge/devforge.h index d1b692a..329d21b 100644 --- a/DevForge/devforge.h +++ b/DevForge/devforge.h @@ -39,6 +39,7 @@ struct devforge_config { void (*devforge_log)(const char*, const char*); + char* (*devforge_get_param)(const char* param); }; #endif diff --git a/DevForge/df_config.h b/DevForge/df_config.h index 43b2263..2e11ff5 100644 --- a/DevForge/df_config.h +++ b/DevForge/df_config.h @@ -31,15 +31,17 @@ #ifndef __DEVFORGE_DF_CONFIG_H__ #define __DEVFORGE_DF_CONFIG_H__ +#include +#include #include -#include int devforge_load_config(const char* path); int devforge_create_config(const char* path); struct devforge_mod { void* lib; - pid_t pid; + pthread_t thread; + bool joinable; }; #ifdef DF_CONFIG_SRC diff --git a/DevForge/main.c b/DevForge/main.c index 34b9852..40c3546 100644 --- a/DevForge/main.c +++ b/DevForge/main.c @@ -33,10 +33,13 @@ #include "df_config.h" #include "df_log.h" +#include #include #include +#include #include -#include + +extern struct devforge_config config; int main(int argc, char** argv) { int i; @@ -73,19 +76,36 @@ int main(int argc, char** argv) { if(ret != 0) return ret; } devforge_log(DF_INFO, "Hello World, initialization done"); - if(loaded_mods != NULL){ + if(loaded_mods != NULL) { devforge_log(DF_INFO, "Starting server"); int i; bool loaded_srv = false; - for(i = 0; loaded_mods[i] != NULL; i++){ + for(i = 0; loaded_mods[i] != NULL; i++) { const char* type = (const char*)dlsym(loaded_mods[i]->lib, "mod_type"); - if(type != NULL){ - if(strcmp(type, DF_MOD_SRV) == 0){ - loaded_srv = true; + if(type != NULL) { + if(strcmp(type, DF_MOD_SRV) == 0) { + void* (*mod_server_thread)(void*) = (void* (*)(void*))dlsym(loaded_mods[i]->lib, "mod_server_thread"); + if(mod_server_thread != NULL) { + pthread_create(&loaded_mods[i]->thread, NULL, mod_server_thread, &config); + loaded_mods[i]->joinable = true; + loaded_srv = true; + } + } + } + } + + if(loaded_srv) { + int ret = 0; + for(i = 0; loaded_mods[i] != NULL; i++) { + if(loaded_mods[i]->joinable) { + int* status; + pthread_join(loaded_mods[i]->thread, (void*)&status); + if(*status != 0) ret = *status; + free(status); } } + return ret; } - if(loaded_srv) return 0; } devforge_log(DF_ERROR, "Server module was not loaded!"); } diff --git a/Module/http.c b/Module/http.c index 210b402..9a5228f 100644 --- a/Module/http.c +++ b/Module/http.c @@ -30,6 +30,10 @@ #include "../DevForge/devforge.h" #include "../DevForge/df_log.h" +#include "../DevForge/df_util.h" + +#include +#include const char mod_type[] = DF_MOD_SRV; @@ -38,5 +42,18 @@ struct devforge_config* config; int mod_init(struct devforge_config* _config) { config = _config; config->devforge_log(DF_INFO, "HTTP Module init"); + char* port = config->devforge_get_param("HTTPPort"); + if(port == NULL) port = "1024"; + char* log = devforge_strcat("HTTP Server will listen on port ", port); + config->devforge_log(DF_INFO, log); + free(log); return 0; } + +void* mod_server_thread(void* ptr) { + int* ret = malloc(sizeof(*ret)); + *ret = 0; + struct devforge_config* sconfig = (struct devforge_config*)ptr; + sconfig->devforge_log(DF_INFO, "HTTP server preparing"); + return (void*)ret; +} -- 2.43.0