Guest User

client.c

a guest
Apr 11th, 2020
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include "mdp_client.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.   char request_str[128];
  17.   char *cmd = NULL, *reply = NULL;
  18.   int i = 0, loops = 10;
  19.   mdp_client_t **clients = NULL;
  20.   zmsg_t *request = NULL;
  21.  
  22.  
  23.   /* Create array of ptr for <loop> clients */
  24.   clients = calloc(loops, sizeof(mdp_client_t *));
  25.   assert(clients != NULL);
  26.  
  27.   /* create <loops> client sessions and send a request on each */
  28.   for (i = 0; i < loops; i++) {
  29.     /* create a new MDP client session */
  30.     clients[i] = mdp_client_new(endpoint);
  31.     if (!clients[i]) {
  32.       fprintf(stderr, "Error %s\r\n", mdp_client_reason(clients[i]));
  33.       exit(-1);
  34.     }
  35.     /* create new request message */
  36.     request = zmsg_new();
  37.     assert(request != NULL);
  38.     memset(request_str, 0, 128);
  39.     sprintf(request_str, "Request %d", i);
  40.     zmsg_addstr(request, request_str);
  41.     /* send the message as an MDP client request */
  42.     if(mdp_client_request(clients[i], service, &request) ==0 ) {
  43.       fprintf(stdout, "%s sent\r\n", request_str);
  44.     } else {
  45.       fprintf(stderr, "%s NOT SENT (%s)\r\n", request_str, mdp_client_reason(clients[i]));
  46.     }
  47.  
  48.     zmsg_destroy(&request);
  49.  
  50.     /* If I add sleep time here, so the worker can process the
  51.      * request and send the reply back, it works just fine.
  52.      * As soon as a drop all requests to the broker, the worker gets
  53.      * stuck at zsock_recv() stuck after processing only one, or a
  54.      * subset of the requests )
  55.      * */
  56.     //sleep(1);
  57.   }
  58.  
  59.   /*  collect the replies       */
  60.   for (i = 0; i < loops; i++) {
  61.  
  62.     /* create a message pipe to read the replies */
  63.     zsock_t *client_sock = mdp_client_msgpipe(clients[i]);
  64.     assert(client_sock);
  65.     /* set receive timeout (60s) */
  66.     zsock_set_rcvtimeo(client_sock, 10000);
  67.     /* get the message as "ss" (string and string) into cmd and reply*/
  68.     if (zsock_recv(client_sock, "ss", &cmd, &reply) == 0) {
  69.       fprintf(stdout, "Received: %s: %s\r\n", cmd, reply);
  70.     } else {
  71.       fprintf(stderr, "Failed to receive reply %s\r\n",
  72.               mdp_client_reason(clients[i]));
  73.     }
  74.  
  75.     /* close the message pipe */
  76.     zmq_close(client_sock);
  77.  
  78.     /* destroy the client session */
  79.     if (clients[i]) {
  80.       mdp_client_destroy(&clients[i]);
  81.     }
  82.  
  83.     SAFEFREE(cmd);
  84.     SAFEFREE(reply);
  85.   }
  86.  
  87.   return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment