diff options
author | Hanoh Haim <hhaim@cisco.com> | 2015-08-26 15:50:06 +0300 |
---|---|---|
committer | Hanoh Haim <hhaim@cisco.com> | 2015-08-26 15:50:06 +0300 |
commit | 53f0e28d7f30c7175cbb15884c309613593859d8 (patch) | |
tree | e75ff499233519baa6c50c60fa0c726da7b42e03 /scripts/external_libs/zmq/utils/monitor.py | |
parent | a628a35b10fbd38211c353f506a8c49c2cc3dd7e (diff) | |
parent | 00d6f001971b324bb9c884aaf0384a4cee076550 (diff) |
Merge branch 'lib_fix'
Diffstat (limited to 'scripts/external_libs/zmq/utils/monitor.py')
-rw-r--r-- | scripts/external_libs/zmq/utils/monitor.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/scripts/external_libs/zmq/utils/monitor.py b/scripts/external_libs/zmq/utils/monitor.py new file mode 100644 index 00000000..734d54b1 --- /dev/null +++ b/scripts/external_libs/zmq/utils/monitor.py @@ -0,0 +1,68 @@ +# -*- coding: utf-8 -*- +"""Module holding utility and convenience functions for zmq event monitoring.""" + +# Copyright (C) PyZMQ Developers +# Distributed under the terms of the Modified BSD License. + +import struct +import zmq +from zmq.error import _check_version + +def parse_monitor_message(msg): + """decode zmq_monitor event messages. + + Parameters + ---------- + msg : list(bytes) + zmq multipart message that has arrived on a monitor PAIR socket. + + First frame is:: + + 16 bit event id + 32 bit event value + no padding + + Second frame is the endpoint as a bytestring + + Returns + ------- + event : dict + event description as dict with the keys `event`, `value`, and `endpoint`. + """ + + if len(msg) != 2 or len(msg[0]) != 6: + raise RuntimeError("Invalid event message format: %s" % msg) + event = {} + event['event'], event['value'] = struct.unpack("=hi", msg[0]) + event['endpoint'] = msg[1] + return event + +def recv_monitor_message(socket, flags=0): + """Receive and decode the given raw message from the monitoring socket and return a dict. + + Requires libzmq ≥ 4.0 + + The returned dict will have the following entries: + event : int, the event id as described in libzmq.zmq_socket_monitor + value : int, the event value associated with the event, see libzmq.zmq_socket_monitor + endpoint : string, the affected endpoint + + Parameters + ---------- + socket : zmq PAIR socket + The PAIR socket (created by other.get_monitor_socket()) on which to recv the message + flags : bitfield (int) + standard zmq recv flags + + Returns + ------- + event : dict + event description as dict with the keys `event`, `value`, and `endpoint`. + """ + _check_version((4,0), 'libzmq event API') + # will always return a list + msg = socket.recv_multipart(flags) + # 4.0-style event API + return parse_monitor_message(msg) + +__all__ = ['parse_monitor_message', 'recv_monitor_message'] |