Guest User

lpclient2.c

a guest
Jun 6th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //  Lazy Pirate client
  2. //  Use zmq_poll to do a safe request-reply
  3. //  To run, start lpserver and then randomly kill/restart it
  4. //  Modified by Laurent Alebarde 2013-06-06
  5.  
  6. #include "czmq.h"
  7. #define REQUEST_TIMEOUT     2500    //  msecs, (> 1000!)
  8. #define REQUEST_RETRIES     3       //  Before we abandon
  9. #define SERVER_ENDPOINT     "tcp://localhost:5555"
  10.  
  11. int main (void)
  12. {
  13.     zctx_t *ctx = zctx_new ();
  14.     printf ("I: connecting to server...\n");
  15.     void *client = zsocket_new (ctx, ZMQ_REQ);
  16.     assert (client);
  17.     zsocket_connect (client, SERVER_ENDPOINT);
  18.  
  19.     int sequence = 0;
  20.     int retries_left = REQUEST_RETRIES;
  21.     bool is_interrupted = zctx_interrupted; // [email protected] 2013-06-06 R1
  22.     while (retries_left && !is_interrupted) { // [email protected] 2013-06-06 R1
  23.         //  We send a request, then we work to get a reply
  24.         char request [10];
  25.         sprintf (request, "%d", ++sequence);
  26.         zstr_send (client, request);
  27.  
  28.         int expect_reply = 1;
  29.         while (expect_reply) {
  30.             //  Poll socket for a reply, with timeout
  31.             zmq_pollitem_t items [] = { { client, 0, ZMQ_POLLIN, 0 } };
  32.             int rc = zmq_poll (items, 1, REQUEST_TIMEOUT * ZMQ_POLL_MSEC);
  33.             if (rc == -1) {
  34.                 is_interrupted = zctx_interrupted;  // [email protected] 2013-06-06 R1
  35.         break;        //  Interrupted
  36.         }
  37.  
  38.             //  .split process server reply
  39.             //  Here we process a server reply and exit our loop if the
  40.             //  reply is valid. If we didn't a reply we close the client
  41.             //  socket and resend the request. We try a number of times
  42.             //  before finally abandoning:
  43.            
  44.             if (items [0].revents & ZMQ_POLLIN) {
  45.                 //  We got a reply from the server, must match sequence
  46.                 char *reply = zstr_recv (client);
  47.                 if (!reply) {
  48.             is_interrupted = zctx_interrupted; // [email protected] 2013-06-06 R1
  49.                     break;      //  Interrupted
  50.         }
  51.                 if (atoi (reply) == sequence) {
  52.                     printf ("I: server replied OK (%s)\n", reply);
  53.                     retries_left = REQUEST_RETRIES;
  54.                     expect_reply = 0;
  55.                 }
  56.                else
  57.                     printf ("E: malformed reply from server: %s\n",
  58.                         reply);
  59.        
  60.                 free (reply);
  61.         }
  62.             else
  63.         if (rc >= 1) {  // [email protected] 2013-06-06 R2
  64.                 printf ("E: malformed reply from server\n");
  65.         break; // retry without destroying/rebuilding the socket  // [email protected] 2013-06-06 R3
  66.             }
  67.             else
  68.             if (--retries_left == 0) {
  69.                 printf ("E: server seems to be offline, abandoning\n");
  70.                 break;
  71.             }
  72.             else {
  73.                 printf ("W: no response from server, retrying...\n");
  74.                 //  Old socket is confused; close it and open a new one
  75.                 zsocket_destroy (ctx, client);
  76.                 printf ("I: reconnecting to server...\n");
  77.                 client = zsocket_new (ctx, ZMQ_REQ);
  78.                 zsocket_connect (client, SERVER_ENDPOINT);
  79.                 //  Send request again, on new socket
  80.                 zstr_send (client, request);
  81.             }
  82.         }
  83.     is_interrupted = zctx_interrupted;  // [email protected] 2013-06-06 R1
  84.     }
  85.     zsocket_destroy (ctx, client);  // [email protected] 2013-06-06 R1
  86.     zctx_destroy (&ctx);
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment