Guest User

Untitled

a guest
Jun 12th, 2014
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.88 KB | None | 0 0
  1. static void
  2. _run (gpointer *zmq_ctx)
  3. {
  4.     gpointer zmq_sck_req;
  5.     gpointer zmq_sck_push;
  6.  
  7.     /* create socket */
  8.     zmq_sck_req = zmq_socket (zmq_ctx, ZMQ_REQ);
  9.     if (!zmq_sck_req) {
  10.         g_error (G_STRLOC ": Unable to create REQ socket. %s.", zmq_strerror (zmq_errno ()));
  11.     }
  12.  
  13.     /* bind to the PUSH socket */
  14.     if (zmq_connect (zmq_sck_req, AK_REPO_SOCKET_SEND_WORK) != 0) {
  15.         g_error (G_STRLOC ": Unable to connect to an endpoint at '%s'. %s.", AK_REPO_SOCKET_SEND_WORK, zmq_strerror (zmq_errno ()));
  16.     }
  17.  
  18.  
  19.     /* setup poll for both sockets */
  20.     zmq_pollitem_t items [] = {
  21.             { zmq_sck_req, 0, ZMQ_POLLOUT, 0 },
  22.             { zmq_sck_req, 0, ZMQ_POLLIN,  0 }
  23.         };
  24.     gint n_items = sizeof (items) / sizeof (*items);
  25.  
  26.  
  27.     /* start the main execution loop */
  28.     gint64 next_ts = 0;
  29.     while (g_atomic_int_get (&is_running)) {
  30.  
  31.         gint64 cur_ts = g_get_real_time ();
  32.         if (cur_ts < next_ts) {
  33.             g_usleep (SOCKET_POLL_WAIT_TIMEOUT_USEC);
  34.             continue;
  35.         }
  36.  
  37.         /* poll zmq sockets */
  38.         gint rc = zmq_poll (items, n_items, SOCKET_POLL_WAIT_TIMEOUT_USEC);
  39.         if (rc == 0) {
  40.             continue;
  41.         }
  42.         else if (rc < 0) {
  43.             g_warning (G_STRLOC "(): Socket error detected: %s.", zmq_strerror (zmq_errno ()));
  44.             continue;
  45.         }
  46.  
  47.  
  48.         if ((items[0].revents & ZMQ_POLLOUT)) {
  49.  
  50.             zmq_msg_t msg_req;
  51.  
  52.             /* request work */
  53.             if (zmq_msg_init (&msg_req) == 0) {
  54.  
  55.                 if (zmq_send (zmq_sck_req, &msg_req, 0) != 0) {
  56.                     g_critical (G_STRLOC ": %s.", zmq_strerror (zmq_errno ()));
  57.                 }
  58.  
  59.                 zmq_msg_close (&msg_req);
  60.             }
  61.             else {
  62.                 g_critical (G_STRLOC ": %s.", zmq_strerror (zmq_errno ()));
  63.             }
  64.         }
  65.  
  66.  
  67.         if ((items[1].revents & ZMQ_POLLIN)) {
  68.  
  69.             /* receive reply */
  70.             zmq_msg_t msg_reply;
  71.  
  72.             if (zmq_msg_init (&msg_reply) == 0) {
  73.  
  74.                 if (zmq_recv (zmq_sck_req, &msg_reply, 0) == 0) {
  75.  
  76.                     gchar *req_str = (gchar *) zmq_msg_data (&msg_reply);
  77.  
  78.                     /* TODO: handle reply */
  79.  
  80.                 }
  81.                 else {
  82.                     g_critical (G_STRLOC ": zmq_recv() failed with code '%s'.", zmq_strerror (zmq_errno ()));
  83.                 }
  84.  
  85.                 zmq_msg_close (&msg_reply);
  86.             }
  87.             else {
  88.                 g_critical (G_STRLOC ": %s.", zmq_strerror (zmq_errno ()));
  89.             }
  90.         }
  91.  
  92.     }
  93.  
  94.     /* close ZMQ_PULL socket */
  95.     if (zmq_close (zmq_sck_req) != 0) {
  96.          g_error (G_STRLOC ": Unable to close PULL socket.");
  97.     }
  98.  
  99.     /* close ZMQ_REQ socket */
  100.     if (zmq_close (zmq_sck_push) != 0) {
  101.          g_error (G_STRLOC ": Unable to close PUSH socket.");
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment