diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2014-11-13 19:31:03 +0100 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2014-11-13 19:31:03 +0100 |
commit | 7cffcc99678cb62c11f33bf6ff6979229754cbc7 (patch) | |
tree | 44fd9c53628283dde155e550a865b3bd216fb13e | |
parent | 7d239d4d733a00eb0d44b8a29d67c2ec312a1604 (diff) | |
download | biruda-7cffcc99678cb62c11f33bf6ff6979229754cbc7.tar.gz biruda-7cffcc99678cb62c11f33bf6ff6979229754cbc7.tar.bz2 |
added a small built-in webserver
-rw-r--r-- | TODOS | 3 | ||||
-rw-r--r-- | src/GNUmakefile | 7 | ||||
-rw-r--r-- | src/biruda.c | 41 | ||||
-rw-r--r-- | src/biruda.conf | 2 | ||||
-rw-r--r-- | src/master.c | 3 | ||||
-rw-r--r-- | src/webserver.c | 62 | ||||
-rw-r--r-- | src/webserver.h | 8 |
7 files changed, 113 insertions, 13 deletions
@@ -10,4 +10,7 @@ https://lkml.org/lkml/2012/9/5/419 http://www.codeguru.com/cpp/misc/misc/system/article.php/c8973/Determine-Windows-Version-and-Edition.htm http://xmodulo.com/alternatives-skype-linux.html +-which embedded web server +microhttpd (GNU) or swill + diff --git a/src/GNUmakefile b/src/GNUmakefile index 0704fba..f6db48e 100644 --- a/src/GNUmakefile +++ b/src/GNUmakefile @@ -4,20 +4,21 @@ all: biruda CFLAGS = -g -std=c99 -Wall -pedantic -D_XOPEN_SOURCE=600 LDFLAGS = -LIBS = -lconfuse -lpthread -lnanomsg -ljson-c +LIBS = -lconfuse -lpthread -lnanomsg -ljson-c -lmicrohttpd %.o : %.c $(CC) $(CFLAGS) -c -o $@ $< -biruda: biruda.o biruda_cmdline.o master.o coordinator.o worker.o system.o +biruda: biruda.o biruda_cmdline.o master.o coordinator.o worker.o system.o webserver.o $(CC) -o $@ $(LDFLAGS) $^ $(LIBS) biruda_cmdline.o: biruda_cmdline.c -biruda.o: biruda.c biruda_cmdline.h master.h coordinator.h worker.h port.h system.h +biruda.o: biruda.c biruda_cmdline.h master.h coordinator.h worker.h port.h system.h webserver.h master.o: master.c master.h port.h coordinator.o: coordinator.c coordinator.h port.h system.h worker.o: worker.c worker.h port.h system.o: system.c system.h +webserver.o: webserver.c webserver.h biruda_cmdline.c: biruda.ggo gengetopt -F biruda_cmdline --unamed-opts --conf-parser --include-getopt -i $< diff --git a/src/biruda.c b/src/biruda.c index 57e2995..ae6d614 100644 --- a/src/biruda.c +++ b/src/biruda.c @@ -19,6 +19,7 @@ #include "master.h" #include "worker.h" #include "coordinator.h" +#include "webserver.h" #include "port.h" @@ -76,7 +77,7 @@ static int read_config( const char *filename, cfg_t **cfg ) CFG_SEC( "master", opts_master, CFGF_MULTI ), CFG_SEC( "coordinator", opts_coordinator, CFGF_MULTI ), CFG_SEC( "worker", opts_worker, CFGF_MULTI | CFGF_TITLE ), - CFG_SEC( "webserver", opts_webserver, 0 ), + CFG_SEC( "webserver", opts_webserver, CFGF_MULTI ), CFG_END( ) }; @@ -129,12 +130,14 @@ static void print_config( struct gengetopt_args_info *args_info, cfg_t *cfg ) } cfg_t *webserver_cfg = cfg_getnsec( cfg, "webserver", 0 ); - - puts( "Webserver:" ); - printf( " Host: %s\n", cfg_getstr( webserver_cfg, "host" ) ); - printf( " Port: %ld\n", cfg_getint( webserver_cfg, "port" ) ); - printf( " Number of threads: %ld\n", cfg_getint( webserver_cfg, "threads" ) ); - puts( "" ); + unsigned int has_webserver = cfg_size( cfg, "webserver" ); + if( has_webserver > 0 ) { + printf( "Webserver:\n" ); + printf( " Host: %s\n", cfg_getstr( webserver_cfg, "host" ) ); + printf( " Port: %ld\n", cfg_getint( webserver_cfg, "port" ) ); + printf( " Number of threads: %ld\n", cfg_getint( webserver_cfg, "threads" ) ); + puts( "" ); + } } static int create_master( cfg_t *cfg ) @@ -153,6 +156,14 @@ static int create_coordinator( cfg_t *cfg ) return coordinator_init( control ); } +static int create_webserver( cfg_t *cfg ) +{ + cfg_t *webserver_cfg = cfg_getnsec( cfg, "webserver", 0 ); + unsigned int port = cfg_getint( webserver_cfg, "port" ); + + return webserver_init( port ); +} + static volatile int got_terminate = 0; #ifdef _WIN32 @@ -225,6 +236,16 @@ int main( int argc, char *argv[] ) exit( EXIT_FAILURE ); } } + + unsigned int has_webserver = cfg_size( cfg, "webserver" ); + if( has_webserver ) { + if( create_webserver( cfg ) != 0 ) { + fprintf( stderr, "FATAL: Unable to create webserver thread!\n" ); + cfg_free( cfg ); + cmdline_parser_free( &args_info ); + exit( EXIT_FAILURE ); + } + } if( args_info.foreground_given ) { #ifdef _WIN32 @@ -245,6 +266,9 @@ int main( int argc, char *argv[] ) } } + if( has_webserver ) { + webserver_terminate( ); + } if( has_coordinator ) { coordinator_terminate( ); } @@ -258,6 +282,9 @@ int main( int argc, char *argv[] ) if( has_master ) { master_free( ); } + if( has_webserver ) { + webserver_free( ); + } cfg_free( cfg ); cmdline_parser_free( &args_info ); diff --git a/src/biruda.conf b/src/biruda.conf index 6f4f408..51da929 100644 --- a/src/biruda.conf +++ b/src/biruda.conf @@ -4,7 +4,7 @@ master { # control = "ipc:///tmp/biruda.ipc" # control = "inproc://control" - control = "tcp://localhost:5555" + control = "tcp://*:5555" } coordinator diff --git a/src/master.c b/src/master.c index 0f3ac7a..02037e5 100644 --- a/src/master.c +++ b/src/master.c @@ -128,6 +128,5 @@ int master_free( ) return 1; } - return 0; - + return 0; } diff --git a/src/webserver.c b/src/webserver.c new file mode 100644 index 0000000..c53dd93 --- /dev/null +++ b/src/webserver.c @@ -0,0 +1,62 @@ +#include "webserver.h" + +#include <microhttpd.h> + +#include <string.h> +#include <stdio.h> + +static struct MHD_Daemon *daemon; + +static int handle_request( void *cls, struct MHD_Connection *connection, + const char *url, const char *method, const char *version, + const char *upload_data, size_t *upload_data_size, void **ptr ) +{ + struct MHD_Response *response; + int ret; + + if( strcmp( method, "GET" ) != 0 ) { + return MHD_NO; + } + + if( strcmp( version, "HTTP/1.1" ) != 0 ) { + return MHD_NO; + } + + fprintf( stderr, "http request: %s\n", url ); + + char *biruda_msg = "Welcome to biruda!"; + response = MHD_create_response_from_buffer( strlen( biruda_msg ), + (void *)biruda_msg, MHD_RESPMEM_MUST_COPY ); + ret = MHD_queue_response( connection, MHD_HTTP_OK, response ); + MHD_destroy_response( response ); + + return ret; +} + +int webserver_init( unsigned int port ) +{ + daemon = MHD_start_daemon( + MHD_USE_SELECT_INTERNALLY, port, + NULL, + NULL, + &handle_request, + MHD_OPTION_END ); + if( daemon == 0 ) { + return 1; + } + + puts( "http daemon started" ); + + return 0; +} + +void webserver_terminate( ) +{ + MHD_stop_daemon( daemon ); + puts( "http daemon stopped" ); +} + +int webserver_free( ) +{ + return 0; +} diff --git a/src/webserver.h b/src/webserver.h new file mode 100644 index 0000000..16d94df --- /dev/null +++ b/src/webserver.h @@ -0,0 +1,8 @@ +#ifndef _BIRUDA_WEBSERVER_HEADER_INCLUDED +#define _BIRUDA_WEBSERVER_HEADER_INCLUDED + +int webserver_init( unsigned int port ); +void webserver_terminate( ); +int webserver_free( ); + +#endif |