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-integration>
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
_______________________________________________
zeromq-dev mailing list
[email protected]
https://lists.zeromq.org/mailman/listinfo/zeromq-dev