[zeromq-dev] C++ 0mq helper
Pieter Hintjens
ph at imatix.com
Wed Dec 24 12:09:22 CET 2014
Hi, the best way to share this with people is to make a github project
for it, and then add it to the C++ bindings page at
http://zeromq.org/bindings:cpp
-Pieter
On Wed, Dec 24, 2014 at 10:50 AM, cibercitizen1 <cibercitizen1 at gmail.com> wrote:
> Hi all,
>
> I've written a little and simple helper class that permitts easy socket
> creation,
> easy multi-send/receive, and asynchronous listener callbacks (lambdas,
> function pointers, function objects) for receivers.
>
> If someone finds it worth, I can post the file.
>
> Thanks.
>
> Some examples of use follows:
>
> --- serverREP.cpp -----------------------------------------
> SocketAdaptor< ZMQ_REP > sa;
> sa.bind ("tcp://*:5555");
>
> sa.onMessage ( [&] (zmq::socket_t & socket ) -> void {
> // Get the request.
> std::vector<std::string> lines = receiveText (socket);
> std::cout << " received -------- \n";
> for ( auto s : lines ) { std::cout << s << "\n"; }
>
> // Send the reply
> std::vector<std::string> multi = { "Welt Welt", "World World" };
> sendText ( socket, multi );
> });
>
> sa.wait (); // never happens
> ------------------------------------------------------------------
>
>
> -- clientREQ.cpp ----------------------------------------------------
> SocketAdaptor< ZMQ_REQ > sa;
> sa.connect ("tcp://localhost:5555");
>
> // Send the request
> std::vector<std::string> multi = { "Hallo Hallo", "Hello Hello" };
> sa.sendText ( multi );
>
> // Get the reply.
> auto lines = sa.receiveText ();
>
> std::cout << " received -------- \n";
> for ( auto s : lines ) { std::cout << s << "\n"; }
> ------------------------------------------------------------------
>
> -- broker.cpp ----------------------------------------------------
> SocketAdaptor< ZMQ_ROUTER > frontend_ROUTER;
> SocketAdaptor< ZMQ_DEALER > backend_DEALER;
>
> frontend_ROUTER.bind ("tcp://*:8000");
> backend_DEALER.bind ("tcp://*:8001");
>
> frontend_ROUTER.onMessage ( [&] (zmq::socket_t & socket ) {
> auto lines = receiveText (socket);
> // routing
> backend_DEALER.sendText (lines);
> });
>
> backend_DEALER.onMessage ( [&] (zmq::socket_t & socket ) {
> auto lines = backend_DEALER.receiveText ();
> // routing
> frontend_ROUTER.sendText (lines);
> });
>
> // wait (never happens)
> frontend_ROUTER.wait ();
> backend_DEALER.wait ();
> ------------------------------------------------------------------
>
>
> -- subscriptor.cpp ----------------------------------------------------
> SocketAdaptor< ZMQ_SUB > sa;
> sa.connect ("tcp://localhost:5555");
> sa.subscribe ("news");
>
> while (true) {
> // Get news
> auto lines = sa.receiveText ();
>
> std::cout << " received -------- \n";
> for ( auto s : lines ) {
> std::cout << s << "\n";
> }
> } // true
> --------------------------------------------------------------------
>
> -- publisher.cpp ----------------------------------------------------
> SocketAdaptor< ZMQ_PUB > sa;
> sa.bind ("tcp://*:5555");
>
> int i=1;
>
> while (true) {
> std::cout << " publishing " << i << "\n";
> std::vector<std::string> multi = { "news", "nachrichten" };
> sa.sendText ( multi );
> sleep (1);
> i++;
> } // true
> --------------------------------------------------------------------
>
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
More information about the zeromq-dev
mailing list