Identifying the origin of ZMQ messages?

三世轮回 提交于 2019-12-10 17:29:24

问题


If I receive a message via the recv() method on a ZeroMQ (0MQ) socket...

data = s.recv()

...is there any way for me to get at the value of getpeername() for the underlying socket? My goal is to identify the origin of the message in a way that does not rely on the sender to provide accurate information.

I'm using ZMQ (via Python) to collect host metrics, and the address of the sender from the perspective of the receiver is a useful identifier.

Or is this just a Bad Idea?


回答1:


No, you cannot get the address of the sender from ZeroMq. You basically have two choices; add the senders address information to the message itself (not a bad option if you are allowed to modify existing message structures) or add the sender address as a message part, i.e. use ZeroMq multi-part messages.

A multi-part message will still be delivered as a whole (all parts or not at all), but you can extract the parts individually at the receiving end, thus you can append or prepend the sender address to any existing messages without actually touching them (and still deliver both address + message as an atomic operation).

I am not sure how this is implemented in the pyzmq binding, but check out the socket.pyx source for details (basically, use the SNDMORE flag in the send(..) method).

Also, have a look at the ZeroMq zmq_send() Api docs (3.2.2).

In C++, it would look something like this:

// Send a multi-part message consisting of sender IP plus another message
zmq_msg_send (&my_ip, my_socket, ZMQ_SNDMORE);
zmq_msg_send (&my_message, my_socket, 0);



回答2:


Looks like this has recently landed in github: https://github.com/zeromq/libzmq/commit/3aeaa6fab135aced3e762031621491c4779285c0



来源:https://stackoverflow.com/questions/9604966/identifying-the-origin-of-zmq-messages

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