sock — Multicast#
This module provides simple facilities to multicast packets over the network for monitoring applications, via two classes:
A DAQ (data acquisition system) saving to disk but also optionally broadcasting binary packets to multiple passive monitors is a classic fit for UDP multicast. The DAQ can send out packets efficiently (one-to-many, with no duplication) and minimal overhead, without needing to know who is listening. One or more optional monitoring applications can join or leave the group without disrupting the data acquisition.
In a nutshell, the sender sends packets to a special IP address in the multicast range (224.0.0.0 to 239.255.255.255 in IPv4), and receivers can join the multicast group at that address. Routers (if configured) distribute the packet only to networks with group members.
Note
The administratively scoped multicast is a block of multicast addresses that are not routed globally, and are intended to be used and managed locally by an organization or enterprise. This is defined to be 239.0.0.0 – 239.255.255.255 in RFC 2365.
239.1.1.1 is a typical choice for local multicast applications.
UDP ports range from 0 to 65535. Although there is no reserved range just for multicast it is good practice to avoid ports below 1024 and ephemeral ports above 49152. Ports in the range 5000–6000 are a typical choice for custom applications.
What you will want to do in typical applications is fairly simple. On the DAQ side you will have something along the lines of
from astropix_analysis.sock import MulticastSender
# Create a socket to broadcast packets over the network.
sender = MulticastSender(group='239.1.1.1', port=5007)
while True: # This is your data collection loop.
# ...
sender.send_readout(readout)
while on the monitoring side all you need to intercept packets is
from astropix_analysis.fmt import AstroPix4Readout
from astropix_analysis.sock import MulticastReceiver
# Create the receiver socket---note the receiver needs to know
# the readout type you are expecting in order to be able to
# reassemble the readout objects from the binary stream.
receiver = MulticastReceiver(AstroPix4Readout, group='239.1.1.1', port=5007)
while True:
readout = receiver.receive()
print(readout)
hits = readout.decode()
for hit in hist:
print(hit)
Module documentation#
Network facilities for monitoring applications.
- class astropix_analysis.sock.MulticastSocketBase(group: str = '127.0.0.1', port: int = 5007)[source]#
Base class for UDP multicast sockets.
- Parameters:
group (str) – The multicast group.
port (int) – The multicast port.
- set_option(identifier: int, value: Any) None[source]#
Set a specific option for the socket.
- Parameters:
identifier (int) – The numerical identifier for the option.
value (any) – The value for the option.
- _io_refs#
- _closed#
- class astropix_analysis.sock.MulticastSender(group: str = '127.0.0.1', port: int = 5007, ttl: int = 2)[source]#
Simple socket class to multicast packets over the network.
- Parameters:
group (str) – The multicast group.
port (int) – The multicast port.
ttl (int) – The TTL (time-to-live) for multicast packets. TTL controls how far multicast packets can go. (Each router decrements the TTL by 1, and when TTL hits 0, the packet is dropped.) Common TTL values are 1 (stay on the local subnet), 2 (allow routing to directly connected subnets), or >2 (allow more hops)
- send_data(data: bytes) int[source]#
Send a packet over the network.
- Parameters:
data (bytes) – The data to be sent.
- send_readout(readout: AbstractAstroPixReadout) int[source]#
Send a readout over the network.
- Parameters:
readout (AbstractAstroPixReadout) – The readout to be sent.
- _io_refs#
- _closed#
- class astropix_analysis.sock.MulticastReceiver(readout_class: type, group: str = '127.0.0.1', port: int = 5007)[source]#
Simple socket class to receive multicast packets.
- Parameters:
readout_class (type) – The type of readout objects we are expecting (e.g., AstroPix4Readout`).
group (str) – The multicast group.
port (int) – The multicast port.
- DEFAULT_MAX_PACKET_SIZE = 65535#
- receive(max_size: int = 65535) AbstractAstroPixReadout[source]#
Wait for a packet to be available, read the binary data and return the corresponding readout object.
- _io_refs#
- _closed#