summaryrefslogtreecommitdiff
path: root/src/master.c
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 /src/master.c
parent6627513d260e807f99fd02cc6571601d794437de (diff)
downloadbiruda-5257e805a9499229af234180a9d68104a563cf80.tar.gz
biruda-5257e805a9499229af234180a9d68104a563cf80.tar.bz2
add json, nanomsg in master
Diffstat (limited to 'src/master.c')
-rw-r--r--src/master.c89
1 files changed, 89 insertions, 0 deletions
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;
+
+}