Search code examples
pythonzeromqmessaging

ZeroMQ for ring topology - how to configure sockets?


I am trying to connect several hosts in a ring topology with ZeroMQ. But I after trying for a long time I hope to get some help now.

Basically I have a client C that sends message M to itself S through several hosts H1...Hn. It is a ring communication.

C -> H2 -> H1 -> S    # where C and S are on the same host.

IP addresses:
C, S:  192.168.100.165
H2:    192.168.100.102
H1:    192.168.100.101

H1 and H2 need to take the message do something on it and forward it to the next host.

I tried to follow the extended request-reply pattern example of the official ZeroMQ doc.

This is my client C:

main_context = zmq.Context()
client_socket = main_context.socket(zmq.REQ)
client_socket.connect("tcp://192.168.100.102:5556")
while 1:
    client_socket.send("hello!")
    ug_response = client_socket.recv()
    time.sleep(3)

Here comes my server S (that is on the same host. Even same program, just another thread):

ug_server_context = zmq.Context()
server_socket = ug_server_context.socket(zmq.REP)
server_socket.bind("tcp://*:5558")
while 1:
    updated_ug_message = server_socket.recv()
    server_socket.send("ug received.")

And this are my "brokers" H1 and H2 that use ROUTER and DEALER:

context = zmq.Context()
# Prepare broker sockets
frontend = context.socket(zmq.ROUTER)
backend = context.socket(zmq.DEALER)
frontend.bind("tcp://*:5556")    # for H1: 5557
backend.bind("tcp://*:5557")     # for H1: 5558

# Initialize broker poll set
poller = zmq.Poller()
poller.register(frontend, zmq.POLLIN)
poller.register(backend, zmq.POLLIN)

# Switch messages between sockets
while 1:
    socks = dict(poller.poll())

    if socks.get(frontend) == zmq.POLLIN:
        message = frontend.recv_multipart()
        backend.send_multipart(message)

    if socks.get(backend) == zmq.POLLIN:
        message = backend.recv_multipart()
        frontend.send_multipart(message)

The thing is, it doesn't work and my question are:

1. How does H2 know that it has to send message to H1?

2. Am I going towards the right direction?


Solution

  • OK, in the meantime I figured it out on my own. I had to change:

    backend.bind("tcp://*:5557")
    

    to:

    backend.connect("tcp://192.168.100.101:5557")
    

    Now my communication ring works very well.