Advertisement
chandras002

Zyre_Listener.C

Apr 26th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.64 KB | None | 0 0
  1. #include <zyre.h>
  2.  
  3. // this program starts a zyre node, joins a group,
  4. // receives messages and prints the message frame by frame
  5. int main(int argc, char *argv[])
  6. {
  7.     if (argc < 2)
  8.     {
  9.         printf("Usage: ./zyre_listener <group name>\n");
  10.         return 1;
  11.     }
  12.  
  13.     char * group_name = argv[1];
  14.    
  15.    
  16.     // create a new node
  17.      zyre_t *node = zyre_new("Leader");
  18.      zyre_set_name(node,"Leader");
  19.    
  20.      //zyre_t *name(zyre_t *self);  // Is it valid ? what it will do
  21.    
  22.     if (!node)
  23.     {
  24.         return 1;      //  Could not create new node
  25.     }
  26.  
  27.     // this sends an ENTER message
  28.     zyre_start(node);
  29.     // this sends a JOIN message
  30.     zyre_join(node, group_name);
  31.     // wait for a while
  32.     zclock_sleep(250);
  33.     // print UUID of node
  34.    
  35.     printf    (" Leader's UUID: %s\n", zyre_uuid(node));
  36.     zsys_info (" Leader node, uuid=%s, name=%s", zyre_uuid (node), zyre_name (node));
  37.     zsys_info ("   ******Leader Listening*****  ");
  38.     zyre_event_t *event = zyre_event_new (node); // needed for event
  39.    
  40.      
  41.     while (true)
  42.     {
  43.    
  44.     zmsg_t * msg = zyre_recv(node);
  45.    
  46.      if (streq (zyre_event_type (event),"ENTER"))
  47.    
  48.          {
  49.             // If new peer, say hello to it and wait for it to answer us
  50.                zsys_info ("*[%s] Follower info",zyre_event_type (event));
  51.                zyre_whispers (node,zyre_event_peer_uuid (event), "Hello...Follower one ");
  52.            
  53.         }
  54.        
  55.            
  56.         // loop through the frames in the message
  57.          while(true)
  58.          {
  59.            
  60.            
  61.             char * msg_str = zmsg_popstr(msg);
  62.             char * command = zmsg_popstr(msg);
  63.            
  64.             if (!msg_str)
  65.             {
  66.                 break;
  67.             }
  68.          
  69.             printf("frame: %s\n", msg_str);
  70.            
  71.            
  72.             /*if (streq(command,"EXIT")) // what this mean? doubt?
  73.            
  74.             {
  75.                
  76.                 printf(" ********* \n");
  77.                 zyre_whispers (node,zyre_event_peer_uuid (event), "Hello...Follower one ");
  78.             }*/
  79.            
  80.             // with zmsg_popstr, you own the frame string
  81.             // so you need to free it
  82.             free(msg_str);
  83.                
  84.          }
  85.             printf("------\n");
  86.            
  87.            
  88.          
  89.         // you own the message with zyre_recv
  90.         // so you need to destroy it
  91.         zmsg_destroy(&msg);
  92.      }
  93.     // this sends a LEAVE message
  94.     zyre_leave(node, group_name);
  95.     // this sends an EXIT message
  96.     zyre_stop(node);
  97.     // wait for node to stop
  98.     zclock_sleep(100);
  99.    
  100.    // zyre_destroy(&node);
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement