]> Nishi Git Mirror - mandshurica.git/commitdiff
reformat
authornishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Sat, 13 Apr 2024 09:13:38 +0000 (09:13 +0000)
committernishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Sat, 13 Apr 2024 09:13:38 +0000 (09:13 +0000)
git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@17 f982e544-4a7d-3444-ad1a-fde59a2a69f1

DevForge/Makefile
DevForge/config.c
DevForge/devforge.h
DevForge/df_config.h
DevForge/main.c
Module/http.c

index 6f3b44256e725c649c3e5c0b15a6d07c0fdc77c2..9ce892e7383adc228eff8a15c586b6253eabd83a 100644 (file)
@@ -1,8 +1,8 @@
 # $Id$
 
-EXTRA_CFLAGS =
+EXTRA_CFLAGS = -pthread
 EXTRA_LDFLAGS =
-EXTRA_LIBS =
+EXTRA_LIBS = -pthread
 
 .PHONY: all clean
 
index 5db210c7948c000324e8b018b4fa77217fb29b7c..a4559ff8749932689fd56f2e20715357ccbd4600 100644 (file)
@@ -37,6 +37,7 @@
 
 #include <dlfcn.h>
 #include <errno.h>
+#include <pthread.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -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;
                                                }
                                        }
                                }
index d1b692a58a9361d40cb5656ef97116b6fceda80f..329d21be28a9c673d0625a43add06cf5f48043f1 100644 (file)
@@ -39,6 +39,7 @@
 
 struct devforge_config {
        void (*devforge_log)(const char*, const char*);
+       char* (*devforge_get_param)(const char* param);
 };
 
 #endif
index 43b22635fa7991f309f107579b68a061481d8ad0..2e11ff5498b7dba3da84bc83657712c89a78ddd2 100644 (file)
 #ifndef __DEVFORGE_DF_CONFIG_H__
 #define __DEVFORGE_DF_CONFIG_H__
 
+#include <pthread.h>
+#include <stdbool.h>
 #include <stddef.h>
-#include <sys/types.h>
 
 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
index 34b9852437e3c52c3c11737babf8ef95af3ab61d..40c3546f4d644a276698fd518f49ccfe81f3d25f 100644 (file)
 #include "df_config.h"
 #include "df_log.h"
 
+#include <dlfcn.h>
 #include <stdbool.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
-#include <dlfcn.h>
+
+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!");
 }
index 210b402ad1dbb29bf91956d0d7d0aadf422c33f7..9a5228fc1b4221b32b0e4ba337c1fdb392063d5a 100644 (file)
 
 #include "../DevForge/devforge.h"
 #include "../DevForge/df_log.h"
+#include "../DevForge/df_util.h"
+
+#include <stddef.h>
+#include <stdlib.h>
 
 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;
+}