Advertisement
chandras002

ZYRE_Listener.C

Apr 24th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. #include <zyre.h>
  2. // this program Zyre_listener.c  -- starts a zyre node, joins a group,
  3. // receives messages and prints the message frame by frame
  4. int main(int argc, char *argv[])
  5. {
  6.     if (argc < 2)
  7.     {
  8.         printf("Usage: ./zyre_listener <group name>\n");
  9.         return 1;
  10.     }
  11.     char * group_name = argv[1];
  12.  
  13.     // create a new node
  14.      zyre_t *node = zyre_new("Leader");
  15.    
  16.        
  17.     if (!node)
  18.     {
  19.         return 1;      //  Could not create new node
  20.     }
  21.  
  22.     // this sends an ENTER message
  23.     zyre_start(node);
  24.     // this sends a JOIN message
  25.     zyre_join(node, group_name);
  26.     // wait for a while
  27.     zclock_sleep(250);
  28.     // print UUID of node
  29.     printf("UUID: %s\n", zyre_uuid(node));
  30.     zsys_info ("Create Zyre node, uuid=%s, name=%s", zyre_uuid (node), zyre_name (node));
  31.     while (true)
  32.     {
  33.         zmsg_t * msg = zyre_recv(node);
  34.  
  35.         // loop through the frames in the message
  36.         while(true)
  37.         {
  38.             char * msg_str = zmsg_popstr(msg);
  39.             if (!msg_str)
  40.             {
  41.                 break;
  42.             }
  43.             printf("frame: %s\n", msg_str);
  44.             // with zmsg_popstr, you own the frame string
  45.             // so you need to free it
  46.             free(msg_str);
  47.         }
  48.         printf("------\n");
  49.         // you own the message with zyre_recv
  50.         // so you need to destroy it
  51.         zmsg_destroy(&msg);
  52.     }
  53.     // this sends a LEAVE message
  54.     zyre_leave(node, group_name);
  55.     // this sends an EXIT message
  56.     zyre_stop(node);
  57.     // wait for node to stop
  58.     zclock_sleep(100);
  59.     zyre_destroy(&node);
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement