zmq proxy capture not receiving messages

  c++, qt, zeromq

I tried to get the capture socket running, without success (based on https://zguide.zeromq.org/docs/chapter5/#Pub-Sub-Tracing-Espresso-Pattern):

#include <QCoreApplication>

#include <QDebug>
#include <QtConcurrent>
#include <QThread>

#include <zmq.hpp>

static void sub(zmq::context_t* pCtx)
{
    qDebug() << Q_FUNC_INFO;
    auto subscriber = zmq::socket_t (*pCtx, zmq::socket_type::sub);
    subscriber.connect("tcp://localhost:6000");
    subscriber.set(zmq::sockopt::subscribe, "A");
    subscriber.set(zmq::sockopt::subscribe, "B");

    int count = 0;
    while (count < 5)
    {
        zmq::message_t msg;
        auto res = subscriber.recv(msg);
        if(!res)
            break; //  Interrupted
        count++;
        qDebug() << Q_FUNC_INFO << "recv" << count;
    }

    qDebug() << Q_FUNC_INFO;
}

static void pub(zmq::context_t* pCtx)
{
    qDebug() << Q_FUNC_INFO;
    auto publisher = zmq::socket_t (*pCtx, zmq::socket_type::pub);
    publisher.bind("tcp://*:6000");
    QThread::currentThread()->msleep(1000);
    while(1)
    {
        zmq::message_t msg("B");
        if(!publisher.send(msg))
            break;
        QThread::currentThread()->msleep(1000);
    }

    qDebug() << Q_FUNC_INFO;
}

static void caps(zmq::context_t* pCtx)
{
    auto capture = zmq::socket_t(*pCtx, zmq::socket_type::pair);
    capture.connect("inproc://listener");
    qDebug() << Q_FUNC_INFO;
    zmq::message_t msg1("lala");
    capture.send(msg1);
    while(1)
    {
        zmq::message_t msg;
        auto res = capture.recv(msg);
        if(!res)
            break;
        qDebug() << Q_FUNC_INFO << msg.to_string().c_str();
    }
    qDebug() << Q_FUNC_INFO;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    zmq::context_t ctx;

    QFuture<void> caper = QtConcurrent::run(caps, &ctx);
    QFuture<void> puber = QtConcurrent::run(pub, &ctx);
    QFuture<void> suber = QtConcurrent::run(sub, &ctx);

    QThread::currentThread()->msleep(100);

    auto subscriber = zmq::socket_t (ctx, zmq::socket_type::xsub);

    auto publisher = zmq::socket_t (ctx, zmq::socket_type::xpub);

    auto capture = zmq::socket_t (ctx, zmq::socket_type::pair);

    capture.bind("inproc://listener");
    zmq::message_t msg;
    capture.recv(msg);
    qDebug() << Q_FUNC_INFO << msg.to_string().c_str();
    subscriber.connect("tcp://localhost:6000");
    publisher.bind("tcp://*:6001");
    QThread::currentThread()->msleep(100);
    zmq::proxy((zmq::socket_ref)subscriber,(zmq::socket_ref) publisher, (zmq::socket_ref)capture);


    return a.exec();
}

Output:

void pub(zmq::context_t*)
void sub(zmq::context_t*)
void caps(zmq::context_t*)
int main(int, char**) lala
void sub(zmq::context_t*) recv 1
void sub(zmq::context_t*) recv 2
void sub(zmq::context_t*) recv 3
void sub(zmq::context_t*) recv 4
void sub(zmq::context_t*) recv 5
void sub(zmq::context_t*)

The sleeps were put in to avoid connection time issues, but the capture socket does apparently not receive any messages.
Any idea? Thanks.

Source: Windows Questions C++

LEAVE A COMMENT