0

On initialize of my router application I call the following code. It binds fine, receives messages fine but refuses to work for the On_ReceiveXXX methods unless it's a direct response. I want to know why

    public void Initialize(string frontEndAddress, string backEndAddress)
    {
        _poller = new Poller();
        _timeAllowedBetweenPings = TimeSpan.FromMinutes(1);
        _lastPingResponse = DateTime.Now;

        using (var ctx = NetMQContext.Create())
        {
            _frontEnd = ctx.CreateRouterSocket();
            _backEnd = ctx.CreateRouterSocket();

            _frontEnd.Bind(frontEndAddress);
            Console.WriteLine(string.Format("[Router]: Connected to {0}", frontEndAddress));
            _backEnd.Bind(backEndAddress);
            Console.WriteLine(string.Format("[Router]: Connected to {0}", backEndAddress));

            _frontEnd.ReceiveReady += On_ReceiveFrontEnd;
            _backEnd.ReceiveReady += On_ReceiveBackEnd;

            _poller.AddSocket(_frontEnd);
            _poller.AddSocket(_backEnd);


            var timer = new NetMQTimer(TimeSpan.FromSeconds(1));
            timer.Elapsed += On_Ping;
            _poller.AddTimer(timer);
            _poller.PollTillCancelled();
        }
    }

This fails to call the dealer ReceiveReady event:

private void On_ReceiveFrontEnd(object sender, NetMQSocketEventArgs e) { _lastPingResponse = DateTime.Now; var frontEndMsg = e.Socket.ReceiveMultipartBytes(); var streamData = frontEndMsg.Last(); ApplicationMessage msg = PackageHelper.DeserializeOutgoing(streamData); Console.WriteLine(string.Format("Command received: {0}", msg.CO));

        _backEnd.SendMultipartBytes(frontEndMsg);
    }

BUT if I change the line

 _backEnd.SendMultipartBytes(frontEndMsg);

to

_frontEnd.SendMultipartBytes(frontEndMsg);

It suddenly works... so messages coming from my front end application can only be responded to, not passed on to the back end application. The same is true the other way round, for the back end messages.

Fred Johnson
  • 2,539
  • 3
  • 26
  • 52

1 Answers1

0

When working with router the first frame is the routing id and it specific to the socket. So you can't pass the entire message from router to router. Change the backend to dealer and it will work, or prefix the message with routing id of the backend socket.

somdoron
  • 4,653
  • 2
  • 16
  • 24
  • Hi somodoron, I think I didnt explain well enough. I have one dealer which sends a message to this router code, the _frontEnd router that is bound to the same port, gets this message but it doesn't work when i try to use _backend router to message a second dealer on another port – Fred Johnson Oct 27 '15 at 22:30
  • Because both your backend and frontend are router sockets you cannot forward messages between them as is, you must include the routing id/identity of the socket. So when you forward a message from frontend to backend you do it with the routing/identity of a frontend client which is irrelevant to the backend. The important question is why your backend socket is a router and not a dealer? I think it should be a dealer if all you want is a simple round robin. – somdoron Oct 28 '15 at 07:47
  • Thanks, I will try that tonight, I didnt think I would need to add the identity to send, only to receive again on the loop back! RE why: I plan on adding in a pinging system and fail over. If you think it's the wrong approach I will open another question once I begin it, I would appreciate any experience on the matter as I've not found examples in NetMQ further than 1:1 socket communication – Fred Johnson Oct 28 '15 at 08:11
  • Try the netmq group if you need more help: https://groups.google.com/forum/#!forum/netmq-dev – somdoron Oct 28 '15 at 08:47
  • ok thanks. Just to reiterate my pattern is frontend dealer(:5556) --> Routers _frontEnd (:5556) _backEnd (:5557) ---> backend dealer (:5557) – Fred Johnson Oct 28 '15 at 11:29
  • it should be dealer -> router (frontend) and dealer->router (the _backend should be a dealer) the last one should be a router. – somdoron Oct 28 '15 at 14:57
  • oh right. could you expand with why? I did the D -> R R -> D in zeromq and it worked perfectly. I thought the idea of a dealer is just one connection async and router was multiple async? – Fred Johnson Oct 28 '15 at 15:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93602/discussion-between-somdoron-and-user2330270). – somdoron Oct 28 '15 at 15:22