[zeromq-dev] Question about constness

Sebastian Lauwers sebastian.lauwers at gmail.com
Wed Jul 25 11:58:11 CEST 2012


Hi again,

On 25/07/12 03:26, Ming Ji wrote:
> Could you share your interface, or C++ API patch? I can probably do a
> test run before you release it.

I haven't written the C++ API patch yet, so that'll have to wait for the 
weekend or so, when I have a minute. I've attached two files that 
contain my so called "interface".

Please note that this is a first try / prototype, so it will need to be 
modified quite a bit (very few socket options are supported) in order to 
respect everything ZeroQ has to offer, and to actually be usable in a 
generic way. I'm only using the minimum needed for my project, and will 
add to it when I reach the need for more features. This interface also 
assumes the use of Google's protobufs; but it would be easy to adapt to 
any other kind of serialisation library.

The way I use this in my project is as follows, components that "offer" 
a service, implement their "own socket":

class InstanceManagerSocket {
     private:
     firestarter::sockets::ZMQPublisherSocket publisher;
     firestarter::sockets::ZMQResponseSocket responder;

     public:
     InstanceManagerSocket(zmq::context_t & context) :
             publisher(context, MODULE_ORDERS_SOCKET_URI),
             responder(context, MANAGER_SOCKET_URI) { };
     inline bool send(google::protobuf::Message const & pb_message) {
         return this->publisher.send(pb_message);
     };
     inline bool reply(google::protobuf::Message const & pb_message) {
         return this->responder.send(pb_message);
     };
     inline bool receive(google::protobuf::Message & pb_message,
             bool blocking = false) {
         if (this->responder.receive(pb_message, blocking))
             return this->ack();
         return false;
      };
     inline bool ack() { return this->responder.send(); };
};

Clients who then want to use this service use a client class, for 
example (usually provided by the service):

class InstanceManagerClientSocket {
     private:
     firestarter::sockets::ZMQSubscriberSocket subscriber;
     firestarter::sockets::ZMQRequestSocket requester;

     public:
     InstanceManagerClientSocket(zmq::context_t & context) :
             subscriber(context, MODULE_ORDERS_SOCKET_URI),
             requester(context, MANAGER_SOCKET_URI) { };
     inline bool send(google::protobuf::Message const & pb_message) {
         if (this->requester.send(pb_message))
             return this->receive_ack();
         return false;
     };
     inline bool receive(google::protobuf::Message & pb_message,
             bool blocking = false) {
         return this->subscriber.receive(pb_message, blocking);
     };
     inline bool receive_ack() { return this->requester.receive(true); };
};


Again, please do take into account that this is in the very early stages 
of prototyping, and will probably be modified a lot before getting my 
own stamp of approval. What I'm aiming for, in the end, is to fully 
abstract the use of Protobufs for serialisation from the perspective of 
any one using the, for example, "InstanceManager sockets".

Regarding the actual ZeroMQ part, I'll probably change the design of the 
classes, most probably through the use of templates, in order to remove 
the small artifacts that exist today (connect() or bind() end up in the 
wrong sockets, due to the use of virtual multiple inheritance, which I'm 
not happy with).

@Everyone: I welcome any comments regarding my current use of 
ZeroMQ---my understanding of it is basic to say the least, which is why 
I haven't attacked more consuming bits like devices and such.

-S.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: zmqhelper.h
Type: text/x-chdr
Size: 196 bytes
Desc: not available
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20120725/bc2dbe13/attachment.h>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: zmqsocket.cpp
Type: text/x-c++src
Size: 2269 bytes
Desc: not available
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20120725/bc2dbe13/attachment.cpp>


More information about the zeromq-dev mailing list