cppzmq icon indicating copy to clipboard operation
cppzmq copied to clipboard

Examples would be nice

Open Slesa opened this issue 6 years ago • 7 comments

Hi all, this lib seems to be great when trying to use zeromq in C++. The first problem I ran into: how to use it?

Slesa avatar Jul 04 '18 09:07 Slesa

Hi @Slesa, thanks for the feedback. By how to use it, do you mean:

  1. how to integrated with your project
  2. how to use socket_t, message_t, ...
  3. all above :smile:

kurdybacha avatar Jul 04 '18 21:07 kurdybacha

Well - I ended up this way:

NetMessaging::NetMessaging() 
: m_zmqContext(1)
{ }

bool NetMessaging::getOsVersion(const string& address, NetMsgOsVersion& version) {
    zmq::socket_t requester(m_zmqContext, ZMQ_REQ);
    requester.connect(address);
    try {
        s_send(requester, "getosversion");
        auto reply = s_recv(requester);
        /* zmq::message_t env1("getosversion", strlen("getosversion"));
        if (!requester.send(env1)) {
        return;
        } */

        //string osreply;
        /* zmq::message_t msg1;
        bool moreData;
        do {
        if (!requester.recv(&msg1)) {
        return;
        }
        //osreply += (char*)msg1.data();
        moreData = msg1.more();
        //OutputDebugStringA(msg1.gets("version"));
        } while (moreData);
        auto osreply = (char*)msg1.data(); */
        return true;
    }
    catch (zmq::error_t err)
    {
        //OutputDebugStringA(err.what);
        return false;
    }
}

Which means in detail:

  • I am completely unable to print out any exception string
  • I am still using the s_send/s_recv functions - no idea how cppzmq wants me to deal with messages.
  • I do still hope that all messages will fit in a frame

What I wanted to do:

The server uses NetMQ, so tried to use

NetMQMessage message = new NetMQMessage();
message.Append("getosversion");
message.Append...

which couldn't be read in the c++ client.

Current mood: it runs somehow - do not touch ;)

Slesa avatar Jul 04 '18 21:07 Slesa

it is hard to read your code because of formatting. You can use socket_t::recv and socket_t::send only. We plan to add some demo to the project but for now I would suggest you take a look at our current tests e.g. client_server test from tests/active_poller.cpp where socket_t::recv and socket_t::send is used.

kurdybacha avatar Jul 04 '18 23:07 kurdybacha

Thanks. I think that helps to start with messages.

Slesa avatar Jul 05 '18 09:07 Slesa

Some examples would be super helpful. I can't figure out of cppzmq automatically adds the empty delimiter on a send or if i need to add it myself.

I also cant figure out the proper way to set the ID of a message.

        std::string msg= "ExampleMessage";

        // Send JobRequest to camera to run
        zmq::message_t request (msg.length());
        memcpy (request.data (), msg.data(), msg.length());

        // Set the address, then the empty delimiter and then the request itself
        socket.send("ID", ZMQ_SNDMORE);
        socket.send("", ZMQ_SNDMORE);
        socket.send(request);

vpaladino778 avatar Feb 19 '20 21:02 vpaladino778

Any Update on more examples for different capabilities of cppzmq?

sinaaghli avatar Jul 13 '20 04:07 sinaaghli

Developing a set of ZeroMQ Guide examples with cppzmq would be, I think, a good way to focus this kind of effort. I guess that would best be best added as part of:

https://github.com/booksbyus/zguide/tree/master/examples

Probably a new "language" name of cppzmq needs to be chosen and some work to hook this into the Guide's language links itself would ultimately be desired. But, simply populating a subdirectory with example code could could happen in parallel with that web work.

BTW, I have developed the Majordomo examples to work with cppzmq, however they are also extended to allow swapping between ROUTER/DEALER and SERVER/CLIENT so as-is are not so good as basic examples. They could maybe be stripped down to go back to just the MDP related examples. Repo is here:

https://github.com/brettviren/generaldomo

I see I've not yet picked an explicit license but I'm happy to set an OSS license to best assist in their use.

@vpaladino778 I have not seen that cppzmq does any of that message packing automatically. The packing is dependent on the underlying libzmq socket type. Even though the packing scheme is "standard" (RFC'ed) it's kind of an application-level duty to assure (one could in principle violate it if needed) so personally I think such a policy is best not implemented in cppzmq.

In general, I often find reading zmq.hpp to see what it's really doing in translating between C++ and libzmq C idioms is useful, essentially required, and not that hard.

Examples, of course do help. generaldomo has an example although it uses cppzmq multipart_t support from zmq_addon.hpp instead of explicit zmq::send_flags::sndmore.

https://github.com/brettviren/generaldomo/blob/master/src/util.cpp#L101

There are also some snippets of examples at:

https://brettviren.github.io/cppzmq-tour/

brettviren avatar Jul 13 '20 13:07 brettviren