Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZMQ: Sending custom CPP object over the ZMQ queue

Tags:

c++

zeromq

memcpy

I have a class named GenericMessage shown in the 1st code snippet below (defined in GenericMessage.hxx).

I have a .cpp file named TestFE.cpp (see the 2nd code snippet below) that attempts to send an instance of class GenericMessage via a ZMQ queue (See also the 4th code snippet very below - ZmqHandler.hxx). TesfFE.cpp implements the ZMQ push pattern here by including ZmqHandler.hxx.

I have yet another .cpp file named TestBE.cpp (see the 3rd code snippet below) that receives so mentioned GenericMessage instance via the ZMQ queue. TestBE.cpp implements the ZMQ pull pattern here to retrive the GenericMessage instance over the ZMQ queue.

In the TestFE.cpp, I use the standard memcpy function in order to convert the GenericMessage object into a form that can be accepted by the ZMQ queue. On the line 21 of TestBE.cpp (marked in the 3rd code snippet in comments), I get a segmentation fault because it looks the memcpy does not work properly on the sender side which is TestFE.cpp. I got the below message when TestBE is executed. I am also providing the gdb backtrace just below. Could you please tell me what's wrong here? Why do you think memcpy cannot copy my GenericMessage object to ZMQ message_t format properly? Or do you think the problem is st else? Any comments would be appreciated.

ERROR MESSAGE

  $ ./TestBE
  Connecting to FE...
  RECEIVED: 1
  Segmentation fault (core dumped)

GDB Backtrace

  (gdb) r
   Starting program: /home/holb/HOLB_DESIGN/ZMQ/WORK1/TestBE 
  [Thread debugging using libthread_db enabled]
  Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
  [New Thread 0xb7c84b40 (LWP 4252)]
  [New Thread 0xb7483b40 (LWP 4253)]
  Connecting to FE...
  RECEIVED: 1

  Program received signal SIGSEGV, Segmentation fault.
  0xb7f371cc in std::basic_string<char, std::char_traits<char>, std::allocator<char>       >::basic_string(std::string const&) ()
  from /usr/lib/i386-linux-gnu/libstdc++.so.6
  (gdb) bt
  #0  0xb7f371cc in std::basic_string<char, std::char_traits<char>,      std::allocator<char> >::basic_string(std::string const&) ()
  from /usr/lib/i386-linux-gnu/libstdc++.so.6
  #1  0x08049621 in GenericMessage<std::string>::getData (this=0xbffff06c)
  at GenericMessage.hxx:18
  #2  0x08049075 in main () at TestBE.cxx:21
 (gdb) 

CODE SNIPPET 1 (GenericMessage.hxx) #include #include #include

  template <class T>
  class GenericMessage {
  public:

         GenericMessage(int id, T msg): 
         beId(id), 
         data(msg)
         {}

         ~GenericMessage(){}

         T getData()
         {
           //LINE 18 is the following line!
           return data;
         }

          std::string toString()
          {
              std::ostringstream ss;
              ss << getBeId();
              std::string ret =  ss.str();

              return ret;
          }

          void setBeId(int id)
          {
              beId = id;
          }

          int getBeId()
          {
             return beId;
          }
 private:
        int beId;
        T data;
 };

CODE SNIPPET 2 (TestFE.cxx ==> The sender) #include "ZmqHandler.hxx" //SEE THE 4th snippet at the bottom for the content of ZmqHandler.hxx

 int main ()
 {
     ZmqHandler<std::string> zmqHandler;
     int counter = 1;

     while(1)
     {  
        std::string data = "Hello there!\0";
        GenericMessage<std::string> msg(counter, data);
        zmqHandler.sendToBE(&msg);
        counter++;
        sleep(1);
     }

     return 0;
 }

CODE SNIPPET 3 (TestBE.cxx ==> The receiver)

  #include "zmq.hpp"
  #include "GenericMessage.hxx"
  #include <string>
  #include <iostream>

  int main ()
  {
      //  Prepare our context and socket
      zmq::context_t context (1);
      zmq::socket_t socket (context, ZMQ_PULL);

     std::cout << "Connecting to FE..." << std::endl;
     socket.connect ("tcp://localhost:5555");

    while(1){
        zmq::message_t reply;
            socket.recv (&reply);
            GenericMessage<std::string> *msg = (GenericMessage<std::string>*)(reply.data());                
            std::cout << "RECEIVED: " << msg->toString() << std::endl;

           /* *********************************  */
           /*  SEGMENTATION FAULT HAPPENS HERE   */
           /* The member "data" in class GenericMessage cannot be received while the  member "id" in the previous line can be received. */
           std::cout << "DATA: " << ((std::string)msg->getData())  << std::endl;
           /* ********************************** */
     }

     return 0;
 }

CODE SNIPPET 4 (ZMQHandler.hxx)

  #include "zmq.hpp"  
  #include "GenericMessage.hxx"
  #include <pthread.h>
  #include <unistd.h>
  #include <cassert>

  template <class T>
  class ZmqHandler {
  public:
        ZmqHandler():
    mContext(1),
    mOutbHandlerSocket(mContext, ZMQ_PUSH)
        {    
          mOutbHandlerSocket.bind ("tcp://*:5555");       
        }

       ~ZmqHandler() {}

       void *sendToBE(GenericMessage<T> *theMsg)
       {
         //  Place the new request to the zmq queue for BE consumption
         zmq::message_t msgToSend(sizeof(*theMsg));

         memcpy ( msgToSend.data(), ((GenericMessage<T>*)theMsg), sizeof(*  ((GenericMessage<T>*)theMsg)));

         mOutbHandlerSocket.send(msgToSend);

         std::cout << "SENT request: [" << theMsg->toString() << "]" << std::endl;

         return (NULL);
        }   

   private:  
        zmq::context_t mContext;
        zmq::socket_t mOutbHandlerSocket; 

};
like image 210
F. Aydemir Avatar asked Jan 21 '13 11:01

F. Aydemir


1 Answers

I'm beginning to see the problem. It's that you send the complete "structure", which contains a member variable that have pointers (the std::string). This you can not do, as pointers are only valid in the program that created them.

You have to serialize the structure before sending it, and then de-serialize on the receiving end.

You can use libraries such as Boost serialization for this, or Google protocol buffers, or any other number of libraries.

like image 196
Some programmer dude Avatar answered Sep 22 '22 01:09

Some programmer dude