Guest User

Untitled

a guest
Apr 15th, 2013
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.48 KB | None | 0 0
  1. //  Pubsub envelope publisher
  2. //  Note that the zhelpers.h file also provides s_sendmore
  3.  
  4. #include "zhelpers.h"
  5.  
  6. int main (void)
  7. {
  8.     char *endpoint = "tcp://localhost:5678";
  9.     // char *endpoint = "ipc:///tmp/wat/5563";
  10.  
  11.     //  Prepare our context and publisher
  12.     pid_t child_pid = fork();
  13.  
  14.     if (child_pid >= 0) {
  15.       if (child_pid == 0) {
  16.         sleep(5);
  17.         //  Prepare our context and subscriber
  18.         void *context = zmq_ctx_new ();
  19.         void *subscriber = zmq_socket (context, ZMQ_SUB);
  20.         zmq_connect (subscriber, endpoint);
  21.         zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, "B", 1);
  22.  
  23.         while (1) {
  24.             //  Read envelope with address
  25.             char *address = s_recv (subscriber);
  26.             //  Read message contents
  27.             char *contents = s_recv (subscriber);
  28.             printf ("[%s] %s\n", address, contents);
  29.             free (address);
  30.             free (contents);
  31.         }
  32.       }
  33.       else {
  34.         void *context = zmq_ctx_new ();
  35.         void *publisher = zmq_socket (context, ZMQ_PUB);
  36.         zmq_bind (publisher, endpoint);
  37.  
  38.         while (1) {
  39.             //  Write two messages, each with an envelope and content
  40.             s_sendmore (publisher, "A");
  41.             s_send (publisher, "We don't want to see this");
  42.             s_sendmore (publisher, "B");
  43.             s_send (publisher, "We would like to see this");
  44.             printf ("published 2 messages\n");
  45.         }
  46.       }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment