# $Id$
-EXTRA_CFLAGS =
+EXTRA_CFLAGS = -pthread
EXTRA_LDFLAGS =
-EXTRA_LIBS =
+EXTRA_LIBS = -pthread
.PHONY: all clean
#include <dlfcn.h>
#include <errno.h>
+#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
}
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) {
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);
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] == '/') {
}
free(path);
}
+ break;
}
}
}
struct devforge_config {
void (*devforge_log)(const char*, const char*);
+ char* (*devforge_get_param)(const char* param);
};
#endif
#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
#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;
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!");
}
#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;
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;
+}