diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2014-09-17 13:00:34 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2014-09-17 13:00:34 +0200 |
commit | f60fb626f809aaf91d9be77eca3f000d1081c0bb (patch) | |
tree | 8954ae052d454ffbf856e59b7abf000d9823bb46 /src/master.c | |
parent | 6a996a2fcb97578a60142d5c9c36b867d24b7c56 (diff) | |
download | biruda-f60fb626f809aaf91d9be77eca3f000d1081c0bb.tar.gz biruda-f60fb626f809aaf91d9be77eca3f000d1081c0bb.tar.bz2 |
better error handling and termination on Ctrl-C
Diffstat (limited to 'src/master.c')
-rw-r--r-- | src/master.c | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/src/master.c b/src/master.c index 37b79d0..d2c3728 100644 --- a/src/master.c +++ b/src/master.c @@ -22,15 +22,16 @@ static void *master_func( void *thread_data ) master_sock = nn_socket( AF_SP, NN_SURVEYOR ); - int deadline = 10000; + int deadline = 5000; (void)nn_setsockopt( master_sock, NN_SURVEYOR, NN_SURVEYOR_DEADLINE, &deadline, sizeof( deadline ) ); (void)nn_bind( master_sock, control ); printf( "master connected to %s\n", control ); +NEXT_DISCOVER: sleep( 1 ); - + json_object *obj = json_object_new_object( ); json_object *op = json_object_new_string( "discover" ); json_object_object_add( obj, "op", op ); @@ -51,15 +52,22 @@ static void *master_func( void *thread_data ) char *answer = NULL; bytes = nn_recv( master_sock, &answer, NN_MSG, 0 ); - if( bytes == ETIMEDOUT ) continue; if( master_must_terminate ) continue; if( bytes >= 0 ) { printf( "master received: %s\n", answer ); nn_freemsg( answer ); } if( bytes < 0 ) { - fprintf( stderr, "ERROR: nn_recv returned %d (%s)\n", - bytes, nn_strerror( errno ) ); + /* documentation says we should get a ETIMEDOUT, but in + * fact we get EFSM here, change if fixed in nanomsg */ + if( errno == EFSM /* ETIMEDOUT */ ) { + goto NEXT_DISCOVER; + } else if( errno == EAGAIN || errno == EINTR ) { + continue; + } else { + fprintf( stderr, "ERROR: nn_recv returned %d: %s (%d)\n", + bytes, nn_strerror( errno ), errno ); + } } } @@ -93,6 +101,8 @@ int master_init( const char *control ) void master_terminate( ) { master_must_terminate = 1; + + nn_term( ); } int master_free( ) |