2

I am using ( Client )REQ->ROUTER(Router)<-ROUTER(Worker) Socket-scheme.

I could able to send the Client request to the particular worker, but unable to send the response back to Client and as it's a REQ socket and hence the client hangs waiting for a response.

Tried a lot but failed to resolve.

Client.cpp

#include "zhelpers.hpp"
#include <string>

int main (int argc, char *argv[])
{
    zmq::context_t context(1);
    zmq::socket_t requester(context, ZMQ_REQ);
    requester.setsockopt(ZMQ_IDENTITY,"M");
    requester.connect("tcp://localhost:5559");

    for( int request = 0 ; request < 10 ; request++) {
        std::string cmd;
        std::cin>>cmd;
        s_sendmore (requester, "B");//addressing workers
        s_sendmore (requester, "");
        s_send (requester, cmd);
        s_dump(requester);
    }
}

router.cpp

#include "zhelpers.hpp"
int main(int argc, char *argv[]) {
    zmq::context_t context(1);
    zmq::socket_t frontend(context, ZMQ_ROUTER);
    frontend.setsockopt(ZMQ_ROUTER_MANDATORY, 1);

    frontend.bind("tcp://*:5559");

    zmq::pollitem_t items[] = {
            {frontend, 0, ZMQ_POLLIN, 0}
    };

    while (1) {
        zmq::message_t source;
        zmq::message_t empty1;
        zmq::message_t destination;
        zmq::message_t empty2;
        zmq::message_t message;
        int more;
        zmq::poll(&items[0], 1, -1);
        std::cout << "DEBUG MSGS NOT PRINTING";// Another issue
        if (items[0].revents & ZMQ_POLLIN) {
            while (1) {

                //  Process all parts of the message

                frontend.recv(&source);
                frontend.recv(&empty1);
                frontend.recv(&destination);
                frontend.recv(&empty2);
                frontend.recv(&message);
                size_t more_size = sizeof(more);
                frontend.getsockopt(ZMQ_RCVMORE, &more, &more_size);
                frontend.send(destination, ZMQ_SNDMORE);
                frontend.send(empty1, ZMQ_SNDMORE);
                frontend.send(source, ZMQ_SNDMORE);
                frontend.send(empty2, ZMQ_SNDMORE);
                frontend.send(message);
                if (!more)
                    break;
            }}}
    return 0;
}

Also the cout statements in router.cpp do not print, Reason unknown ?

worker.cpp

#include "zhelpers.hpp"
int main (int argc, char *argv[])
{
    zmq::context_t context(1);
    zmq::socket_t responder(context, ZMQ_ROUTER);
    responder.setsockopt(ZMQ_IDENTITY, "B", 1);
    responder.connect("tcp://localhost:5559");

    while(1)
    {
        s_dump(responder);  
        sleep (1);
        s_sendmore (responder, "B");
        s_sendmore (responder, "");
        s_sendmore (responder, "M");
        s_sendmore (responder, "");
        s_send (responder, "FromSlaveB");
    }}

I'm on Ubuntu 16.04 and Current 0MQ version is 4.0.5

Bugs
  • 4,491
  • 9
  • 32
  • 41
Kumar Roshan Mehta
  • 3,078
  • 2
  • 27
  • 50
  • Are your debug message not even printing after exiting? Did you try flushing? You can simply add `std::endl` and try. Example: `std::cout << "Message" << std::endl;` – Azeem Jun 27 '17 at 11:54
  • Please ensure you post solutions as answers not as updates to your question. This is to help future visitors and to avoid confusion. Thank you. – Bugs Jun 28 '17 at 12:18

1 Answers1

1

Simpler part - why cout never executes:

A brief check of the ZeroMQ API specification tells you:

int zmq_poll (zmq_pollitem_t *items, int nitems,long timeout );
...
If the value of timeout is -1, zmq_poll() shall block indefinitely until a requested event has occurred on at least one zmq_pollitem_t.

So for this it is enough to check the API documentation ( which Martin SUSTRIK has spent some remarkable time on since the early release of v2.1.x ).


The rest is in REQ-ROUTER-ROUTER

For the sake of simplicity, start and prototype any piece of software first with a standard, well documented scenario - be it REQ-REP, XREQ-XREP ( == ROUTER-DEALER ) or any other documented Scalable Formal Communication Pattern archetype, before advancing into more complex multi-socket & multi-party composite mix of blocking & non-blocking advanced messaging & signalling scenarios, that form some desired, but highly advanced mix of features.

If possible, read the full book, as advised in other of your several questions, before asking another un-tested or un-supported setup and rather do not jump into things before having mastered those trivial use-cases, that the book helps to understand and build on.


Post Festum: In case of issues with the alleged cout does not print

1) Test this below
2) post the observed output as another Question on S/O #READYTOHELP:


router_a_proof_of_print.cpp

#include "zhelpers.hpp"
int main( int argc, char *argv[] ) {
    std::cout << "DEBUG: [PASS|FAIL] ON AN ATTEMPT TO PRINT"; // Another issue
    return 0;
}
user3666197
  • 1
  • 6
  • 50
  • 92