[zeromq-dev] ZeroMQ integration with GLib main event loop

Luca Boccassi luca.boccassi at gmail.com
Thu Jul 27 14:55:37 CEST 2017


On Thu, 2017-07-27 at 15:02 +0300, Alexander Yanin wrote:
> I am trying to integrate ZeroMQ into my Glib main event loop. As I
> have
> seen on the official ZeroMQ FAQ and API reference and on some
> stackoverflow
> threads (zeromq glib main loop integration
> <https://stackoverflow.com/questions/6695863/zeromq-glib-main-loop-in
> tegration>
> and How to use ZeroMQ in an GTK/QT/Clutter application?
> <https://stackoverflow.com/questions/6452131/how-to-use-zeromq-in-an-
> gtk-qt-clutter-application>),
> to do this I need to get ZeroMQ FD and then connect it to existing
> event
> loop. But, unfortunately, In my case this does not work.
> 
> here is the code:
> 
> RefPtr<IOChannel> zmq_in;
> zmq::context_t context (1);
> zmq::socket_t subscriber (context, ZMQ_SUB);
> 
> subscriber.connect("tcp://localhost:5556");const char *filter =
> "10001 ";
> subscriber.setsockopt(ZMQ_SUBSCRIBE, filter, strlen (filter));
> const auto read_zmq = [this, &subscriber](Glib::IOCondition c) ->
> bool {
>     std::cout << " Received message on zmq!" << std::endl;
>     int zipcode, temperature, relhumidity;
>     zmq::message_t update;
>     subscriber.recv(&update);
>     std::istringstream iss(static_cast<char*>(update.data()));
>     iss >> zipcode >> temperature >> relhumidity;
>     std::cout << "Zipcode: " << zipcode << " Temperature: " <<
> temperature
>               << "Humidity: " << relhumidity;
>     return true;};
> for (int i = 0; i < 10 ; i++) {
>     int zipcode, temperature, relhumidity;
>     zmq::message_t update;
>     subscriber.recv(&update);
>     std::istringstream iss(static_cast<char*>(update.data()));
>     iss >> zipcode >> temperature >> relhumidity;
>     std::cout << "Zipcode: " << zipcode << "  Temperature: " <<
> temperature
>               << "  Humidity: " << relhumidity << std::endl;}
> int fd = subscriber.getsockopt<int>(ZMQ_FD);
> zmq_in = IOChannel::create_from_fd(fd);
> Glib::signal_io().connect(read_zmq, zmq_in, Glib::IO_IN);
> 
> What really happens when the code is executed: 10 messages are
> successfully
> received from Publisher (another process) in the for loop, but when
> it
> comes to receiving messages from the Glib main event loop, my c++
> lambda is
> never called :(
> 
> Here is the Publisher code in C almost completely taken from ZeroMQ
> Guide:
> 
> #include <zmq.h>#include <stdio.h>#include <unistd.h>#include
> <string.h>#include <assert.h>#include <stdlib.h>
> int main (void){
>         //  Prepare our context and publisher
>         void *context = zmq_ctx_new ();
>         void *publisher = zmq_socket (context, ZMQ_PUB);
>         int rc = zmq_bind (publisher, "tcp://*:5556");
>         assert (rc == 0);
> 
>         //  Initialize random number generator
>         while (1) {
>                 //  Get values that will fool the boss
>                 int zipcode, temperature, relhumidity;
>                 zipcode     = rand () % 100000;
>                 temperature = (rand () % 215) - 80;
>                 relhumidity = (rand () % 50) + 10;
> 
>                 //  Send message to all subscribers
>                 char update [20];
>                 sprintf (update, "%05d %d %d", zipcode, temperature,
> relhumidity);
>                 zmq_send (publisher, update, 20, 0);
>                 int ev;
>                 size_t sizeof_ev = sizeof(ev);
>         }
>         zmq_close (publisher);
>         zmq_ctx_destroy (context);
>         return 0;}
> 
> So where is the problem?
> 
> P.S.:I am using glibmm c++ binding to Glib. My OS is Fedora 26. My
> ZeroMQ
> version is 4.1.6

Hi,

I have no idea about that specific c++ glib binding, but as the answer
on the first link you posted says you can't just poll a ZMQ socket, you
need to get the file descriptor via the ZMQ_FD socket option and feed
that to the poll, and then read in a loop as long as the ZMQ_EVENTS
socket options returns ZMQ_POLLIN.

This is because those FDs are edge triggered:

https://funcptr.net/2012/09/10/zeromq---edge-triggered-notification/

-- 
Kind regards,
Luca Boccassi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: This is a digitally signed message part
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20170727/b83ac726/attachment.sig>


More information about the zeromq-dev mailing list