Riskybiz

MultiPartMessage.h

Sep 14th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.93 KB | None | 0 0
  1. #ifndef MULTIPARTMESSAGE
  2. #define MULTIPARTMESSAGE
  3.  
  4. #include <string>
  5. #include <vector>
  6. #include <zmq.h>
  7.  
  8. class MultiPartMessage
  9. {
  10.  
  11. public:
  12.     //Constructor
  13.     MultiPartMessage(void* &socket, const std::string &identity, const std::vector<std::string> &framesOut) : id(identity) //sends multipart message at instantiation.
  14.     {
  15.         if (framesOut.size() > 0)//if there are frames to send
  16.         {
  17.             //Prepare & send identity frame
  18.             if (id != "NULL")
  19.             {
  20.                 zmq_msg_t messageOut;
  21.  
  22.                 int rc = zmq_msg_init_size(&messageOut, id.size());
  23.                 if (rc == -1)
  24.                 {
  25.                     std::string errStr = zmq_strerror(zmq_errno());
  26.                     std::string errConc = "MultiPartMessage: Error Initialising Message: " + errStr;
  27.                     const char* errOut = errConc.c_str();
  28.                     OutputDebugStringA(errOut);
  29.                 }
  30.  
  31.                 memcpy(zmq_msg_data(&messageOut), id.data(), id.size());
  32.  
  33.                 rc = zmq_msg_send(&messageOut, socket, ZMQ_SNDMORE);//part of multipart message, more to follow.
  34.                 if (rc == -1)
  35.                 {
  36.                     std::string errStr = zmq_strerror(zmq_errno());
  37.                     std::string errConc = "MultiPartMessage: Error Sending Identity Message Part: " + errStr;
  38.                     const char* errOut = errConc.c_str();
  39.                     OutputDebugStringA(errOut);
  40.  
  41.                     //Release Message, when sending only necessary to release in the case of an error.
  42.                     rc = zmq_msg_close(&messageOut);
  43.                     if (rc == -1)
  44.                     {
  45.                         std::string errStr = zmq_strerror(zmq_errno());
  46.                         std::string errConc = "MultiPartMessage: Error Releasing Sent Message: " + errStr;
  47.                         const char* errOut = errConc.c_str();
  48.                         OutputDebugStringA(errOut);
  49.                     }
  50.                 }
  51.             }//prepare & send identity frame
  52.  
  53.             //Prepare & send regular message frames. Each part of a multipart message is a separate zmq_msg_t object.  Each frame (a.k.a a message part) is put into a message object.  A message object is a length specified piece of data.
  54.             for (auto frm = framesOut.cbegin(); frm < framesOut.cend(); ++frm)
  55.             {
  56.                 zmq_msg_t messageOut;
  57.  
  58.                 int rc = zmq_msg_init_size(&messageOut, frm->size());
  59.                 if (rc == -1)
  60.                 {
  61.                     std::string errStr = zmq_strerror(zmq_errno());
  62.                     std::string errConc = "MultiPartMessage: Error Initialising Message: " + errStr;
  63.                     const char* errOut = errConc.c_str();
  64.                     OutputDebugStringA(errOut);
  65.                 }
  66.  
  67.                 memcpy(zmq_msg_data(&messageOut), frm->data(), frm->size());
  68.  
  69.                 if (frm + 1 != framesOut.cend())
  70.                 {
  71.                     rc = zmq_msg_send(&messageOut, socket, ZMQ_SNDMORE);//part of multipart message, more to follow.
  72.                     if (rc == -1)
  73.                     {
  74.                         std::string errStr = zmq_strerror(zmq_errno());
  75.                         std::string errConc = "MultiPartMessage: Error Sending Regular Message Part: " + errStr;
  76.                         const char* errOut = errConc.c_str();
  77.                         OutputDebugStringA(errOut);
  78.                         //return 1;//error
  79.                     }
  80.                 }
  81.                 else if (frm + 1 == framesOut.cend())
  82.                 {
  83.                     rc = zmq_msg_send(&messageOut, socket, 0);//final part.
  84.                     if (rc == -1)
  85.                     {
  86.                         std::string errStr = zmq_strerror(zmq_errno());
  87.                         std::string errConc = "MultiPartMessage: Error Sending Final Message Part: " + errStr;
  88.                         const char* errOut = errConc.c_str();
  89.                         OutputDebugStringA(errOut);
  90.                         //Release Message, when sending only necessary to release in the case of an error.
  91.                         rc = zmq_msg_close(&messageOut);
  92.                         if (rc == -1)
  93.                         {
  94.                             std::string errStr = zmq_strerror(zmq_errno());
  95.                             std::string errConc = "MultiPartMessage: Error Releasing Sent Message: " + errStr;
  96.                             const char* errOut = errConc.c_str();
  97.                             OutputDebugStringA(errOut);
  98.                         }
  99.                     }
  100.                 }
  101.             }//if there are frames to send
  102.         }//for loop
  103.     }//send constructor
  104.  
  105.     //Constructor
  106.     MultiPartMessage(void* &socket) //blocking receive of new message at instantiation.
  107.     {
  108.         id = "blank";//assign default
  109.         id_size = 0;//assign default
  110.         //Determine socket type
  111.         size_t socketType;
  112.         size_t socketType_size = sizeof(socketType);
  113.         int rc = zmq_getsockopt(socket, ZMQ_TYPE, &socketType, &socketType_size);
  114.         if (rc == -1)
  115.         {
  116.             std::string errStr = zmq_strerror(zmq_errno());
  117.             std::string errConc = "MultiPartMessage: Error Getting Socket Type: " + errStr;
  118.             const char* errOut = errConc.c_str();
  119.             OutputDebugStringA(errOut);
  120.         }
  121.  
  122.         //Determine handling of first message frame
  123.         bool firstFrame(false);
  124.         if (socketType == ZMQ_ROUTER)
  125.         {
  126.             firstFrame = true;
  127.         }
  128.  
  129.         //Receive multipart message
  130.         while (1)
  131.         {
  132.             //Initialise message object
  133.             zmq_msg_t messageIn;
  134.             int rc = zmq_msg_init(&messageIn);
  135.             if (rc == -1)
  136.             {
  137.                 std::string errStr = zmq_strerror(zmq_errno());
  138.                 std::string errConc = "MultiPartMessage: Error Initialising Message: " + errStr;
  139.                 const char* errOut = errConc.c_str();
  140.                 OutputDebugStringA(errOut);
  141.             }
  142.  
  143.             //Receive message part
  144.             rc = zmq_msg_recv(&messageIn, socket, 0);
  145.             if (rc == -1)
  146.             {
  147.                 std::string errStr = zmq_strerror(zmq_errno());
  148.                 std::string errConc = "MultiPartMessage: Error Receiving Message Part: " + errStr;
  149.                 const char* errOut = errConc.c_str();
  150.                 OutputDebugStringA(errOut);
  151.             }
  152.  
  153.             //Cast the received message data blob to a string for storage. A char type object is one byte of memory in size and holds the binary representation of one character in eight bits.
  154.             //Char is suitable for storing the raw binary data received off a network socket or a data blob (binary large object).
  155.  
  156.             if (!firstFrame)//Routine handling of non-identity message frames
  157.             {
  158.                 //Put into the frames vector
  159.                 framesIn.push_back(std::string(static_cast<char*>(zmq_msg_data(&messageIn)), zmq_msg_size(&messageIn)));
  160.             }
  161.             else if (firstFrame)//Router socket will prepend identity to the multipart message, handle this message part differently
  162.             {
  163.                 id = std::string(static_cast<char*>(zmq_msg_data(&messageIn)), zmq_msg_size(&messageIn));
  164.                 id_size = zmq_msg_size(&messageIn);//size in bytes
  165.                 firstFrame = false;//Handle subsequent message parts differently
  166.             }
  167.  
  168.             //Detect final message part
  169.             if (!zmq_msg_more(&messageIn))
  170.             {
  171.                 zmq_msg_close(&messageIn);//discard message part
  172.                 break;//Final message frame
  173.             }
  174.             else
  175.                 zmq_msg_close(&messageIn);//discard message part
  176.         }
  177.     }//receive constructor
  178.  
  179.     //Copy constructor
  180.     MultiPartMessage(const MultiPartMessage &orig) :framesIn(orig.framesIn), id(orig.id), id_size(orig.id_size){};
  181.  
  182.     //Copy assignment
  183.     MultiPartMessage& operator=(const MultiPartMessage& that)
  184.     {
  185.         if (this != &that)
  186.         {
  187.             //Release any resources; eg. those acquired in the constructor.
  188.             //Take over resources from 'that'.
  189.             framesIn = that.framesIn;
  190.             id = that.id;
  191.             id_size = that.id_size;
  192.         }
  193.         return *this;
  194.     }
  195.  
  196.     //Move constructor
  197.     MultiPartMessage(MultiPartMessage &&orig) /*noexcept NOT VS2013*/ : framesIn(orig.framesIn), id(orig.id), id_size(orig.id_size)
  198.     {
  199.         //Steals resources from the source instance.C++ Primer.p534.   
  200.         //leave orig in a state in which it is harmless to call the destructor. C++ Primer. p534.
  201.     }
  202.  
  203.     //Move assignment constructor
  204.     MultiPartMessage& operator=(MultiPartMessage &&that) /*noexcept NOT VS2013*/
  205.     {
  206.         //Direct test for self assignment.
  207.         if (this != &that)
  208.         {
  209.             //Take over resources from 'that'.  Steals resources from the source instance. C++ Primer. p534.
  210.             framesIn = that.framesIn;
  211.             id = that.id;
  212.             id_size = that.id_size;
  213.             //leave 'that' in a destructible state         
  214.         }
  215.         return *this;
  216.     }
  217.  
  218.  
  219.     //Destructor
  220.     ~MultiPartMessage(){}
  221.  
  222.     //Operator
  223.     bool operator==(const MultiPartMessage &anotherMPM) const
  224.     {
  225.         return (framesIn == anotherMPM.framesIn &&
  226.             id == anotherMPM.id &&
  227.             id_size == anotherMPM.id_size);
  228.     }
  229.  
  230.     //Getters
  231.     std::string getIdentity() const
  232.     {
  233.         return id;
  234.     }
  235.  
  236.     size_t getIdentitySize() const
  237.     {
  238.         return id_size;
  239.     }
  240.  
  241.     size_t getNumberOfFrames() const
  242.     {
  243.         return framesIn.size();
  244.     }
  245.  
  246.     std::string getFrameElement(int frmElem) const
  247.     {
  248.         return framesIn.at(frmElem);
  249.     }
  250.  
  251. private:
  252.     std::vector <std::string> framesIn;
  253.     std::string id;
  254.     size_t id_size;
  255. };
  256. #endif//header guard
Advertisement
Add Comment
Please, Sign In to add comment