ZeroMQ (cppzmq) subscriber with filters which start with the same string

♀尐吖头ヾ 提交于 2021-02-08 10:30:51

问题


I'm using two topics in my sample publisher. Both start with the same string. When I filter the message in a subscriber using only one of the two topics, the subscriber receives both topics

If I use two different topics, it works

My sample publisher

try (ZContext context = new ZContext()) {
    final ZMQ.Socket socket = context.createSocket(SocketType.PUB);
    socket.bind("tcp://*:5555");
    int i = 0;
    while (!Thread.currentThread().isInterrupted() && !stopped) {

        logger.debug("sending C1 message");
        final String env = "topic";
        final String msg = "Hello, world #" + i++;
        socket.sendMore(env);
        socket.send(msg);

    logger.debug("sending C2 message");

        final String env2 = "topic2";
        final String msg2 = "Hello, world #" + i++;
        socket.sendMore(env2);
        socket.send(msg2);

        try {
            sleep(5000);
        } catch (final InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

And my sample subscriber :

zmq::context_t ctx;
zmq::socket_t sock(ctx, zmq::socket_type::sub);
sock.connect("tcp://127.0.0.1:5555");
std::string filter="topic";
sock.setsockopt(ZMQ_SUBSCRIBE,filter.c_str(),filter.length());
while(true) {
    zmq::message_t env;
    sock.recv(&env);
    std::string env_str = std::string(static_cast<char*>(env.data()), env.size());
    std::cout << "Received Enveloppe '" << env_str << "'" << std::endl;

    zmq::message_t msg;
    sock.recv(&msg);
    std::string msg_str = std::string(static_cast<char*>(msg.data()), msg.size());
    std::cout << "Received '" << msg_str << "'" << std::endl;
}

My subscriber should display only the message associated with the topic "topic" and not both.


回答1:


Statement : "My subscriber should display only the message associated to the topic "topic" and not both."

Not correct, exactly the opposite is true.

Documentation is clear in this. ZeroMQ API explicitly states:

A non-empty option_value shall subscribe to all messages beginning with the specified prefix. Multiple filters may be attached to a single ZMQ_SUB socket, in which case a message shall be accepted if it matches at least one filter.

+-----------------------+---------------+
| Option value type | binary data |
+-----------------------+---------------+

Example: a message, dispatched on the PUB-side as:

PUB.send( "123456------------" );

will get .recv()-ed on either of below subscribed SUB-s:

SUB.setsockopt( zmq.SUBSCRIBE, "" );      // this one .recv()-es EVERY message
SUB.setsockopt( zmq.SUBSCRIBE, "1" );    //  this one .recv()-es "1{0+[*]}"
SUB.setsockopt( zmq.SUBSCRIBE, "12" );  //   this one .recv()-es "12{0+[*]}"
SUB.setsockopt( zmq.SUBSCRIBE, "123" );//    this one .recv()-es "123{0+[*]}"


来源:https://stackoverflow.com/questions/58396040/zeromq-cppzmq-subscriber-with-filters-which-start-with-the-same-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!