Broker by ZeroMQ built-in device

We have seen in the previous post how is conceptually easy to implement a ZMQ broker that connects request/reply sockets using a pair of router/dealer sockets.

One could notice that the core of the broker consists in setting up a polling on its sockets and then repeatedly call that chunk of code we appropriately moved in a specific function we called receiveAndSend(). And he could say that, being this structure so common, it could be worth move it in an utility function and make it generally available.

The ZeroMQ guys had the same feeling, so we have an API function, zmq_device() that has exactely this purpose.

The C++ code for our broker could be simplified like this:
zmq::context_t context(1);

zmq::socket_t frontend(context, ZMQ_ROUTER);

zmq::socket_t backend(context, ZMQ_DEALER);

zmq::device(ZMQ_QUEUE, frontend, backend);
catch(const zmq::error_t& ze)
std::cout << "Exception: " << ze.what() << std::endl;

We simply call zmq::device(), specifying that we want the messages managed in a queue, and that is it.

The Z-Guide gives you more information on the ØMQ devices.

No comments:

Post a Comment