Guest User

worker

a guest
Apr 11th, 2020
1,097
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include "mdp_worker.h"
  5.  
  6. #define SAFEFREE(x)                                                            \
  7.   if (x) {                                                                     \
  8.     free(x);                                                                   \
  9.     (x) = NULL;                                                                \
  10.   }
  11.  
  12. int main() {
  13.  
  14.   char service[] = "bb-test";
  15.   char endpoint[] = "ipc:///tmp/bbtest.ipc";
  16.  
  17.  
  18.   mdp_worker_t *worker_session = NULL;
  19.   zsock_t *worker_sock = NULL;
  20.   zframe_t *address = NULL;
  21.  
  22.   char *cmd = NULL;
  23.   char *request = NULL;
  24.   char *reply = NULL;
  25.   int rc = 0;
  26.  
  27.  
  28.   /* create new worker and register the service with the broker */
  29.   worker_session = mdp_worker_new(endpoint, service);
  30.   assert(worker_session != NULL);
  31.   //mdp_worker_set_verbose(worker_session);
  32.  
  33.   worker_sock = mdp_worker_msgpipe(worker_session);
  34.   assert(worker_sock != NULL);
  35.  
  36.   while (1) {
  37.  
  38.     rc = zsock_recv(worker_sock, "sfs", &cmd, &address, &request);
  39.     if (rc != 0) {
  40.       fprintf(stderr, "Failed to receive message: %s\r\n",
  41.               mdp_worker_reason(worker_session));
  42.       continue;
  43.     }
  44.  
  45.     fprintf(stdout, "Got message \"%s\"\r\n", request);
  46.  
  47.     reply = calloc(strlen(request) + 10, sizeof(char));
  48.     assert(reply != NULL);
  49.     snprintf(reply, strlen(request) + 10, "%s - reply", request);
  50.  
  51.     /*  Create reply message */
  52.     zmsg_t *msg_response = zmsg_new();
  53.     assert(msg_response != NULL);
  54.  
  55.     /* Send */
  56.     rc = zmsg_addstr(msg_response, reply);
  57.     assert(rc == 0);
  58.  
  59.     rc = mdp_worker_send_final(worker_session, &address, &msg_response);
  60.     fprintf(rc == 0 ? stdout : stderr, "Sending reply (\"%s\") was %s\r\n\r\n",
  61.             reply, rc == 0 ? "successful" : "UNSUCCESSFUL");
  62.  
  63.     zmsg_destroy(&msg_response);
  64.     SAFEFREE(cmd)
  65.     SAFEFREE(request)
  66.     SAFEFREE(reply)
  67.   }
  68.   mdp_worker_destroy(&worker_session);
  69.   exit(0);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment