summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2014-09-13 21:39:21 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2014-09-13 21:39:21 +0200
commit5257e805a9499229af234180a9d68104a563cf80 (patch)
tree5cccc5d546e5ec17f618ccc558f2c473a0a0a5a4
parent6627513d260e807f99fd02cc6571601d794437de (diff)
downloadbiruda-5257e805a9499229af234180a9d68104a563cf80.tar.gz
biruda-5257e805a9499229af234180a9d68104a563cf80.tar.bz2
add json, nanomsg in master
-rw-r--r--README4
-rw-r--r--src/GNUmakefile9
-rw-r--r--src/biruda.c24
-rw-r--r--src/coordinator.c1
-rw-r--r--src/coordinator.h4
-rw-r--r--src/master.c89
-rw-r--r--src/master.h7
-rw-r--r--src/worker.c1
-rw-r--r--src/worker.h4
9 files changed, 140 insertions, 3 deletions
diff --git a/README b/README
index 9c5aee8..b9266c4 100644
--- a/README
+++ b/README
@@ -25,6 +25,10 @@ Requirements
http://nanomsg.org/
* libmicrohttpd: for an embedded web interface
http://www.gnu.org/software/libmicrohttpd/
+* pthreads-win32: for pthread wrapper on Windows
+ http://sourceware.org/pthreads-win32/
+* json-c: JSON for message payload
+ https://github.com/json-c/json-c/wiki
Other projects
--------------
diff --git a/src/GNUmakefile b/src/GNUmakefile
index cd74947..d3d1fb8 100644
--- a/src/GNUmakefile
+++ b/src/GNUmakefile
@@ -4,16 +4,19 @@ all: biruda
CFLAGS = -g -std=c99 -Wall -pedantic
LDFLAGS =
-LIBS = -lconfuse
+LIBS = -lconfuse -lpthread -lnanomsg -ljson-c
%.o : %.c
$(CC) $(CFLAGS) -c -o $@ $<
-biruda: biruda.o biruda_cmdline.o
+biruda: biruda.o biruda_cmdline.o master.o coordinator.o worker.o
$(CC) -o $@ $(LDFLAGS) $^ $(LIBS)
biruda_cmdline.o: biruda_cmdline.c
-biruda.o: biruda.c biruda_cmdline.h
+biruda.o: biruda.c biruda_cmdline.h master.h coordinator.h worker.h
+master.o: master.c master.h
+coordinator.o: coordinator.c coordinator.h
+worker.o: worker.c worker.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 6c23e6c..6e8a932 100644
--- a/src/biruda.c
+++ b/src/biruda.c
@@ -2,10 +2,15 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
+#include <unistd.h>
#include "biruda_cmdline.h"
#include "confuse.h"
+#include "master.h"
+#include "worker.h"
+#include "coordinator.h"
+
static const char *DEFAULT_CONFIG_FILE = "/etc/biruda/biruda.conf";
static const unsigned int DEFAULT_WEBSERVER_PORT = 8080;
static const char *DEFAULT_WEBSERVER_HOST = "localhost";
@@ -118,6 +123,14 @@ static void print_config( struct gengetopt_args_info *args_info, cfg_t *cfg )
puts( "" );
}
+static int create_master( cfg_t *cfg )
+{
+ cfg_t *master_cfg = cfg_getnsec( cfg, "master", 0 );
+ char *control = cfg_getstr( master_cfg, "control" );
+
+ return master_init( control );
+}
+
int main( int argc, char *argv[] )
{
struct gengetopt_args_info args_info;
@@ -146,6 +159,17 @@ int main( int argc, char *argv[] )
cmdline_parser_free( &args_info );
return( ( test_config( &args_info ) == 0 ) ? EXIT_SUCCESS : EXIT_FAILURE );
}
+
+ if( create_master( cfg ) != 0 ) {
+ fprintf( stderr, "FATAL: Unable to create master thread!\n" );
+ cfg_free( cfg );
+ cmdline_parser_free( &args_info );
+ exit( EXIT_FAILURE );
+ }
+
+ sleep( 4 );
+
+ master_free( );
cfg_free( cfg );
cmdline_parser_free( &args_info );
diff --git a/src/coordinator.c b/src/coordinator.c
new file mode 100644
index 0000000..3b3d87c
--- /dev/null
+++ b/src/coordinator.c
@@ -0,0 +1 @@
+#include "coordinator.h"
diff --git a/src/coordinator.h b/src/coordinator.h
new file mode 100644
index 0000000..a52fa6d
--- /dev/null
+++ b/src/coordinator.h
@@ -0,0 +1,4 @@
+#ifndef _BIRUDA_COORDINGATOR_HEADER_INCLUDED
+#define _BIRUDA_COORDINGATOR_HEADER_INCLUDED
+
+#endif
diff --git a/src/master.c b/src/master.c
new file mode 100644
index 0000000..3deba9e
--- /dev/null
+++ b/src/master.c
@@ -0,0 +1,89 @@
+#include "master.h"
+
+#include <pthread.h>
+
+#include "nanomsg/nn.h"
+#include "nanomsg/survey.h"
+
+#include "json-c/json.h"
+
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+
+static pthread_t master_thread;
+static int master_sock;
+static int master_terminate = 0;
+
+static void *master_func( void *thread_data )
+{
+ char *control = (char *)thread_data;
+
+ master_sock = nn_socket( AF_SP, NN_SURVEYOR );
+
+ (void)nn_bind( master_sock, control );
+
+ while( !master_terminate ) {
+ json_object *obj = json_object_new_object( );
+ json_object *op = json_object_new_string( "discover" );
+ json_object_object_add( obj, "op", op );
+ const char *msg = json_object_to_json_string( obj );
+ int msg_size = strlen( msg ) + 1;
+ printf( "master send: %s\n", msg );
+ int bytes = nn_send( master_sock, msg, msg_size, 0 );
+ if( bytes != msg_size ) {
+ fprintf( stderr, "ERROR: truncated message!" );
+ }
+ json_object_put( obj );
+
+ sleep( 1 );
+
+ char *answer = NULL;
+ bytes = nn_recv( master_sock, &answer, NN_MSG, 0 );
+ if( bytes == ETIMEDOUT ) continue;
+ if( bytes >= 0 ) {
+ printf( "master recevied: %s\n", answer );
+ nn_freemsg( answer );
+ }
+ }
+
+ (void)nn_shutdown( master_sock, 0 );
+
+ return NULL;
+}
+
+int master_init( const char *control )
+{
+ pthread_attr_t attr;
+ int res;
+
+ master_terminate = 0;
+
+ res = pthread_attr_init( &attr );
+ if( res != 0 ) {
+ return 1;
+ }
+
+ res = pthread_create( &master_thread, &attr, master_func, (void *)control );
+ if( res != 0 ) {
+ return 1;
+ }
+
+ return 0;
+}
+
+int master_free( )
+{
+ void *result;
+ int res;
+
+ master_terminate = 1;
+
+ res = pthread_join( master_thread, &result );
+ if( res != 0 ) {
+ return 1;
+ }
+
+ return 0;
+
+}
diff --git a/src/master.h b/src/master.h
new file mode 100644
index 0000000..f40fc0c
--- /dev/null
+++ b/src/master.h
@@ -0,0 +1,7 @@
+#ifndef _BIRUDA_MASTER_HEADER_INCLUDED
+#define _BIRUDA_MASTER_HEADER_INCLUDED
+
+int master_init( const char *control );
+int master_free( );
+
+#endif
diff --git a/src/worker.c b/src/worker.c
new file mode 100644
index 0000000..7560393
--- /dev/null
+++ b/src/worker.c
@@ -0,0 +1 @@
+#include "worker.h"
diff --git a/src/worker.h b/src/worker.h
new file mode 100644
index 0000000..799ef9c
--- /dev/null
+++ b/src/worker.h
@@ -0,0 +1,4 @@
+#ifndef _BIRUDA_WORKER_HEADER_INCLUDED
+#define _BIRUDA_WORKER_HEADER_INCLUDED
+
+#endif