From 194652df9ac875547e3ff2bf745620f70de88aa5 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Fri, 19 Sep 2014 13:28:09 +0200 Subject: recognize 'op' as 'discover' in coordinator --- src/coordinator.c | 77 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/src/coordinator.c b/src/coordinator.c index fb2d0fa..4189660 100644 --- a/src/coordinator.c +++ b/src/coordinator.c @@ -45,42 +45,51 @@ static void *coordinator_func( void *thread_data ) int bytes = nn_recv( coordinator_sock, &answer, NN_MSG, 0 ); if( coordinator_must_terminate ) 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 ); - char hostname[100]; - gethostname( hostname, sizeof( hostname ) ); - json_object *host = json_object_new_string( hostname ); - json_object_object_add( obj, "host", host ); - unsigned int nofCpus = system_available_cpus( ); - json_object *cpus = json_object_new_int( nofCpus ); - json_object_object_add( obj, "cpus", cpus ); - char cpe_name[100]; - system_cpe_name( cpe_name, sizeof( cpe_name ) ); - json_object *os = json_object_new_string( cpe_name ); - json_object_object_add( obj, "os", os ); - 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 < 0 ) { - if( errno == ETERM ) { - coordinator_must_terminate = 1; - continue; - } else { - fprintf( stderr, "ERROR: nn_send returned %d: %s (%d)\n", - bytes, nn_strerror( errno ), errno ); + json_object *recv_obj = json_tokener_parse( answer ); + const char *recv_obj_str = json_object_to_json_string( recv_obj ); + printf( "coordinator received: %s\n", recv_obj_str ); + json_object *op_obj; + json_object_object_get_ex( recv_obj, "op", &op_obj ); + const char *op = json_object_get_string( op_obj ); + + if( strcmp( op, "discover" ) == 0 ) { + 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 ); + char hostname[100]; + gethostname( hostname, sizeof( hostname ) ); + json_object *host = json_object_new_string( hostname ); + json_object_object_add( obj, "host", host ); + unsigned int nofCpus = system_available_cpus( ); + json_object *cpus = json_object_new_int( nofCpus ); + json_object_object_add( obj, "cpus", cpus ); + char cpe_name[100]; + system_cpe_name( cpe_name, sizeof( cpe_name ) ); + json_object *os = json_object_new_string( cpe_name ); + json_object_object_add( obj, "os", os ); + 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 < 0 ) { + if( errno == ETERM ) { + coordinator_must_terminate = 1; + continue; + } else { + fprintf( stderr, "ERROR: nn_send returned %d: %s (%d)\n", + bytes, nn_strerror( errno ), errno ); + } } + if( bytes != msg_size ) { + fprintf( stderr, "ERROR: truncated message!" ); + } + json_object_put( obj ); } - if( bytes != msg_size ) { - fprintf( stderr, "ERROR: truncated message!" ); - } - json_object_put( obj ); + + json_object_put( recv_obj ); + nn_freemsg( answer ); } if( bytes < 0 ) { if( errno == EAGAIN || errno == EINTR ) { -- cgit v1.2.3-54-g00ecf