Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void
- _run (gpointer *zmq_ctx)
- {
- gpointer zmq_sck_req;
- gpointer zmq_sck_push;
- /* create socket */
- zmq_sck_req = zmq_socket (zmq_ctx, ZMQ_REQ);
- if (!zmq_sck_req) {
- g_error (G_STRLOC ": Unable to create REQ socket. %s.", zmq_strerror (zmq_errno ()));
- }
- /* bind to the PUSH socket */
- if (zmq_connect (zmq_sck_req, AK_REPO_SOCKET_SEND_WORK) != 0) {
- g_error (G_STRLOC ": Unable to connect to an endpoint at '%s'. %s.", AK_REPO_SOCKET_SEND_WORK, zmq_strerror (zmq_errno ()));
- }
- /* setup poll for both sockets */
- zmq_pollitem_t items [] = {
- { zmq_sck_req, 0, ZMQ_POLLOUT, 0 },
- { zmq_sck_req, 0, ZMQ_POLLIN, 0 }
- };
- gint n_items = sizeof (items) / sizeof (*items);
- /* start the main execution loop */
- gint64 next_ts = 0;
- while (g_atomic_int_get (&is_running)) {
- gint64 cur_ts = g_get_real_time ();
- if (cur_ts < next_ts) {
- g_usleep (SOCKET_POLL_WAIT_TIMEOUT_USEC);
- continue;
- }
- /* poll zmq sockets */
- gint rc = zmq_poll (items, n_items, SOCKET_POLL_WAIT_TIMEOUT_USEC);
- if (rc == 0) {
- continue;
- }
- else if (rc < 0) {
- g_warning (G_STRLOC "(): Socket error detected: %s.", zmq_strerror (zmq_errno ()));
- continue;
- }
- if ((items[0].revents & ZMQ_POLLOUT)) {
- zmq_msg_t msg_req;
- /* request work */
- if (zmq_msg_init (&msg_req) == 0) {
- if (zmq_send (zmq_sck_req, &msg_req, 0) != 0) {
- g_critical (G_STRLOC ": %s.", zmq_strerror (zmq_errno ()));
- }
- zmq_msg_close (&msg_req);
- }
- else {
- g_critical (G_STRLOC ": %s.", zmq_strerror (zmq_errno ()));
- }
- }
- if ((items[1].revents & ZMQ_POLLIN)) {
- /* receive reply */
- zmq_msg_t msg_reply;
- if (zmq_msg_init (&msg_reply) == 0) {
- if (zmq_recv (zmq_sck_req, &msg_reply, 0) == 0) {
- gchar *req_str = (gchar *) zmq_msg_data (&msg_reply);
- /* TODO: handle reply */
- }
- else {
- g_critical (G_STRLOC ": zmq_recv() failed with code '%s'.", zmq_strerror (zmq_errno ()));
- }
- zmq_msg_close (&msg_reply);
- }
- else {
- g_critical (G_STRLOC ": %s.", zmq_strerror (zmq_errno ()));
- }
- }
- }
- /* close ZMQ_PULL socket */
- if (zmq_close (zmq_sck_req) != 0) {
- g_error (G_STRLOC ": Unable to close PULL socket.");
- }
- /* close ZMQ_REQ socket */
- if (zmq_close (zmq_sck_push) != 0) {
- g_error (G_STRLOC ": Unable to close PUSH socket.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment