summaryrefslogtreecommitdiff
path: root/src/coordinator.c
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2014-09-14 09:24:10 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2014-09-14 09:24:10 +0200
commit12b41ea402aef5b932eb0ba2c123ecd94b48f44f (patch)
tree7397930967105dd55cb10f05e61dcc1cb7a8fe04 /src/coordinator.c
parent5257e805a9499229af234180a9d68104a563cf80 (diff)
downloadbiruda-12b41ea402aef5b932eb0ba2c123ecd94b48f44f.tar.gz
biruda-12b41ea402aef5b932eb0ba2c123ecd94b48f44f.tar.bz2
got a primitive discovery test running
Diffstat (limited to 'src/coordinator.c')
-rw-r--r--src/coordinator.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/coordinator.c b/src/coordinator.c
index 3b3d87c..ebdfdfc 100644
--- a/src/coordinator.c
+++ b/src/coordinator.c
@@ -1 +1,98 @@
#include "coordinator.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 coordinator_thread;
+static int coordinator_sock;
+static int coordinator_terminate = 0;
+
+static void *coordinator_func( void *thread_data )
+{
+ char *control = (char *)thread_data;
+
+ coordinator_sock = nn_socket( AF_SP, NN_RESPONDENT );
+
+ (void)nn_connect( coordinator_sock, control );
+
+ printf( "coordinator connected to %s\n", control );
+
+ sleep( 1 );
+
+ while( !coordinator_terminate ) {
+ printf( "coordinator idle: %d\n", coordinator_terminate );
+
+ sleep( 1 );
+
+ char *answer = NULL;
+ int bytes = nn_recv( coordinator_sock, &answer, NN_MSG, 0 );
+ if( bytes == ETIMEDOUT ) continue;
+ if( bytes >= 0 ) {
+ printf( "coordinator received: %s\n", answer );
+ nn_freemsg( answer );
+
+ json_object *obj = json_object_new_object( );
+ json_object *op = json_object_new_string( "register" );
+ json_object_object_add( obj, "op", op );
+ json_object *role = json_object_new_string( "coordinator" );
+ json_object_object_add( obj, "role", role );
+ const char *msg = json_object_to_json_string( obj );
+ int msg_size = strlen( msg ) + 1;
+ printf( "coordinator send: %s\n", msg );
+ bytes = nn_send( coordinator_sock, msg, msg_size, 0 );
+ if( bytes != msg_size ) {
+ fprintf( stderr, "ERROR: truncated message!" );
+ }
+ json_object_put( obj );
+
+ }
+ }
+
+ (void)nn_shutdown( coordinator_sock, 0 );
+
+ return NULL;
+}
+
+int coordinator_init( const char *control )
+{
+ pthread_attr_t attr;
+ int res;
+
+ coordinator_terminate = 0;
+
+ res = pthread_attr_init( &attr );
+ if( res != 0 ) {
+ return 1;
+ }
+
+ res = pthread_create( &coordinator_thread, &attr, coordinator_func, (void *)control );
+ if( res != 0 ) {
+ return 1;
+ }
+
+ return 0;
+}
+
+int coordinator_free( )
+{
+ void *result;
+ int res;
+
+ coordinator_terminate = 1;
+
+ res = pthread_join( coordinator_thread, &result );
+ if( res != 0 ) {
+ return 1;
+ }
+
+ return 0;
+
+}