Advertisement
maqister

C++ (ZMQ SERVER - REP)

Jul 13th, 2015
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. /**
  2.  * @brief Standard system include files and namespaces
  3.  */
  4. #include <iostream>
  5. #include <string>
  6. #include <fstream>
  7. #include <sys/sysinfo.h>
  8. #include <unistd.h>
  9. using namespace std;
  10.  
  11. /**
  12.  * @brief Project libraries headers and namespaces
  13.  */
  14. #include <google/protobuf/message_lite.h>
  15. #include <zmq.hpp>
  16.  
  17. /**
  18.  * @brief Project include files and namespaces
  19.  */
  20. #include "rpi_protocol.pb.h"
  21.  
  22. static void DispatchMessage(RPiProtocol::Message rpi_msg)
  23. {
  24.     std::cout << "type= " << rpi_msg.type() << std::endl;
  25.     std::cout << "command= " << rpi_msg.command() << std::endl;
  26.     std::cout << "version= " << rpi_msg.version() << std::endl;
  27.     if (rpi_msg.has_correlator())
  28.     {
  29.         std::cout << "correlator= " << rpi_msg.correlator() << std::endl;
  30.     }
  31.  
  32.     // Handle RPi Protocol request
  33.     if (rpi_msg.type() == RPiProtocol::Message::REQUEST)
  34.     {
  35.         cout << "REQUEST received." << endl;
  36.         switch (rpi_msg.command())
  37.         {
  38.         case RPiProtocol::Message::GET_SYS_INFO:
  39.             cout << "GET_SYS_INFO received." << endl;
  40.             break;
  41.  
  42.         default:
  43.             cout << "Command not supported." << endl;
  44.             break;
  45.         }
  46.     }
  47.     // Handle RPi Protocol response
  48.     else if (rpi_msg.type() == RPiProtocol::Message::RESPONSE)
  49.     {
  50.         cout << "RESPONSE received." << endl;
  51.     }
  52.     // Unsupported RPi Protocol message type received
  53.     else
  54.     {
  55.         cout << "Unsupported message type received." << endl;
  56.     }
  57. }
  58.  
  59. static void HandleMessage(zmq::message_t &message)
  60. {
  61.     RPiProtocol::Message rpi_msg;
  62.     std::string rpi_msg_str;
  63.  
  64.     std::cout << "New message received" << std::endl;
  65.  
  66.     // Convert ZMQ message to string.
  67.     rpi_msg_str = std::string(static_cast<char*>(message.data()), message.size());
  68.  
  69.     // Deserialize RPi Protocol message from string.
  70.     rpi_msg.ParseFromString(rpi_msg_str);
  71.  
  72.     // Dispatch received RPi Protocol message
  73.     DispatchMessage(rpi_msg);
  74. }
  75.  
  76. int main(int argc, char *argv[])
  77. {
  78.     // Verify that the version of the library that we linked against is
  79.     // compatible with the version of the headers we compiled against.
  80.     GOOGLE_PROTOBUF_VERIFY_VERSION;
  81.  
  82.     //  Prepare our context and socket.
  83.     zmq::context_t context(1);
  84.     zmq::socket_t socket(context, ZMQ_REP);
  85.     socket.bind("tcp://*:5555");
  86.  
  87.     // Server thread.
  88.     while (true) {
  89.         zmq::message_t request;
  90.  
  91.         //  Wait for next request from client.
  92.         socket.recv(&request);
  93.  
  94.         // Handle ZMQ message.
  95.         HandleMessage(request);
  96. #if 1
  97.         // Test Code.
  98.         // Try to send some 'demo' response back
  99.         RPiProtocol::Message response;
  100.         std::string response_string;
  101.         response.set_type(RPiProtocol::Message::RESPONSE);
  102.         response.set_command(RPiProtocol::Message::GET_SYS_INFO);
  103.         response.set_version(0);
  104.  
  105.         // Serialize ZMQ message to string.
  106.         if (response.SerializeToString(&response_string))
  107.         {
  108.             // Debug prints.
  109.             printf("%#010x\n", response_string.c_str());
  110.             cout << "Response string length= " << response_string.length() << endl;
  111.  
  112.             //  Send response message back to the client.
  113.             zmq::message_t reply(response_string.length());
  114.             memcpy((void *)reply.data(), &response_string, response_string.length());
  115.             socket.send(reply);
  116.         }
  117. #else
  118.         //  Do some 'work'
  119.         sleep(1);
  120.  
  121.         //  Send reply back to client
  122.         zmq::message_t reply(5);
  123.         memcpy((void *)reply.data(), "World", 5);
  124.         socket.send(reply);
  125. #endif
  126.     }
  127.  
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement