Advertisement
Riskybiz

lazyPirateClient

Aug 25th, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. //
  2. //  Lazy Pirate client
  3. //  Use zmq_poll to do a safe request-reply
  4. //  To run, start piserver and then randomly kill/restart it
  5. //
  6. #include "zhelpers.hpp"
  7.  
  8. #include <sstream>
  9.  
  10. #define REQUEST_TIMEOUT     2500    //  msecs, (> 1000!)
  11. #define REQUEST_RETRIES     3       //  Before we abandon
  12.  
  13. //  Helper function that returns a new configured socket
  14. //  connected to the Hello World server
  15. //
  16. static zmq::socket_t * s_client_socket (zmq::context_t & context)
  17. {
  18.     //std::cout << "I: client connecting to server..." << std::endl;
  19.     zmq::socket_t * client = new zmq::socket_t (context, ZMQ_REQ);
  20.     client->connect ("tcp://localhost:5555");
  21.     std::cout << "I: client connected to server!" << std::endl;
  22.    
  23.     //  Configure socket to not wait at close time
  24.     int linger = 0;
  25.     client->setsockopt (ZMQ_LINGER, &linger, sizeof (linger));
  26.     return client;
  27. }
  28.  
  29. int main ()
  30. {
  31.     zmq::context_t context (1);
  32.  
  33.     zmq::socket_t * client = s_client_socket (context);
  34.  
  35.     int sequence = 0;
  36.     int retries_left = REQUEST_RETRIES;
  37.  
  38.     while (retries_left)
  39.     {
  40.         std::stringstream request;
  41.         request << ++sequence;
  42.         s_send (*client, request.str());
  43.         sleep (1);
  44.  
  45.         bool expect_reply = true;
  46.         while (expect_reply)
  47.         {
  48.             //  Poll socket for a reply, with timeout
  49.             zmq::pollitem_t items[] = { { *client, 0, ZMQ_POLLIN, 0 } };
  50.             zmq::poll (&items[0], 1, REQUEST_TIMEOUT);//corrected this, was REQUEST_TIMEOUT * 1000
  51.  
  52.             //  If we got a reply, process it
  53.             if (items[0].revents & ZMQ_POLLIN)
  54.             {
  55.                 //  We got a reply from the server, must match sequence
  56.                 std::string reply = s_recv (*client);
  57.                 if (atoi (reply.c_str ()) == sequence)
  58.                 {
  59.                     std::cout << "I: client received: (" << reply << ") in reply from server" << std::endl;
  60.                     retries_left = REQUEST_RETRIES;
  61.                     expect_reply = false;
  62.                 }
  63.                 else
  64.                 {
  65.                     std::cout << "E: client received malformed reply from server: " << reply << std::endl;
  66.                 }
  67.             }
  68.             else if (--retries_left == 0)
  69.             {
  70.                 std::cout << "E: client reports; server seems to be offline, abandoning" << std::endl;
  71.                 expect_reply = false;
  72.                 break;
  73.             }
  74.             else
  75.             {
  76.                 std::cout << "W: client reports; no response from server, retrying..." << std::endl;
  77.                 //  Old socket will be confused; close it and open a new one
  78.                 delete client;
  79.                 client = s_client_socket (context);
  80.                 //  Send request again, on new socket
  81.                 s_send (*client, request.str());
  82.             }
  83.         }
  84.     }
  85.     delete client;
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement