zmq
ZMQ utilities to broadcast data from an OnDA Monitor.
This module contains classes and functions that allow OnDA Monitors to broadcast data to external programs over a ZMQ socket.
ZmqDataBroadcaster
See documentation of the __init__
function.
__init__(*, parameters)
Data-broadcasting socket for OnDA Monitors.
This class manages a broadcasting socket that can be used by OnDA Monitors to transmit data to external programs. The class must be initialized with the URL, in ZeroMQ format, were the socket should operate. The created socket can then be used to broadcast data. Each data item broadcast by the socket can be tagged with a different label, and external programs can use this label to filter their incoming data. The socket can also transmit to multiple clients at the same time but has no queuing system: broadcast data will be lost to the clients if not received before the next transmission takes place.
This class creates a ZMQ PUB socket that accepts connections from ZMQ PUB sockets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parameters |
Dict[str, Any]
|
A set of OM configuration parameters collected together in a parameter group. The parameter group must contain the following entries:
|
required |
send_data(*, tag, message)
Broadcasts data from the ZMQ PUB socket.
This function transmits the provided data from the broadcasting socket. The data must have the format of a python dictionary strictly containing only Python objects. When broadcast, the data is tagged with the specified label.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tag |
str
|
The label used to tag the broadcast data. |
required |
message |
Dict[str, Any]
|
A dictionary storing the data to be transmitted.
|
required |
ZmqResponder
See documentation of the __init__
function.
__init__(*, parameters, blocking=False)
ZMQ-based responding socket for OnDA Monitors.
This class manages a socket that can be used by an OnDA Monitor to receive requests from external programs, and to respond to them. The class must be initialized with the URL, in ZeroMQ format, were the socket should operate. The socket can then be used to receive requests and to satisfy them by sending data, if necessary. The socket can be of blocking or non-blocking type. In the first case, the socket waits for a request and does not allow the monitor to proceed until one is received. In the second case, the socket retrieves a request if one is available, but proceeds otherwise. Unless a blocking socket is requested when the class is initialized, the class creates a non-blocking socket.
This class creates a ZMQ ROUTER socket that can accept requests from REQ sockets in external programs and respond to them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parameters |
Dict[str, Any]
|
A set of OM configuration parameters collected together in a parameter group. The parameter group must contain the following entries:
|
required |
blocking |
bool
|
whether the socket should be of blocking type. Defaults to False. |
False
|
get_request()
Gets a request from the responding socket, if present.
This function checks if a request has been received by the responding socket. If the socket has been set up as blocking, this function does not return until a request is received. The function then returns a tuple storing the identity of the requester and the content of the request. If the socket is instead non-blocking, the function return the same information if a request is present when the function is called, and None otherwise. The identity of the requester must be stored by the program invoking the function and provided later to the send_data function to answer the request, if necessary.
Returns:
Name | Type | Description |
---|---|---|
request |
Union[Tuple[bytes, bytes], None]
|
If a request was received by the socket, a tuple storing the identity of the caller as the first entry, and a string with the request's content as the second entry. If no request has been received by the socket, None. |
send_data(*, identity, message)
Send data from the ZMQ REP socket.
This function transmits data to an external program that has previously sent a request to the socket. The response must either have the format of a python dictionary or of a sequence of bytes (an ASCII string, for example)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
identity |
bytes
|
The identity of the requester to which the data should sent. This information is returned by the get_request. |
required |
message |
Union[Dict[str, Any], bytes]
|
A dictionary containing the information to be transmitted. |
required |
get_current_machine_ip()
Retrieves the IP address of the local machine.
This function uses Python's socket
module to autodetect the IP address of the
the machine where it is invoked.
Returns:
Type | Description |
---|---|
str
|
A string storing the IP address of the machine where the function was invoked. |