Dispatcher API Reference¶
This section documents the Dispatcher class and related functionality in pyMC_Core.
Dispatcher¶
pymc_core.node.dispatcher.Dispatcher
¶
Handles all the packet routing and radio communication.
This class doesn't do much packet processing itself - it just routes incoming packets to the right handler that knows what to do with them.
expect_ack
¶
Register an ACK CRC we're waiting for and return an asyncio.Event that will be set as soon as the ACK arrives (or is already cached).
register_default_handlers
¶
register_default_handlers(*, contacts=None, local_identity=None, channel_db=None, event_service=None, node_name=None)
Quick setup for all the standard packet handlers.
register_fallback_handler
¶
Register a fallback handler for unhandled payload types.
register_handler
¶
Register a handler for a specific type of packet.
run_forever
async
¶
Run the dispatcher maintenance loop indefinitely (call this in an asyncio task).
send_packet
async
¶
Send a packet and optionally wait for an ACK.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
packet
|
Packet
|
The packet to send |
required |
wait_for_ack
|
bool
|
Whether to wait for an ACK |
True
|
expected_crc
|
Optional[int]
|
The expected CRC for ACK matching. If None, will be calculated from packet. |
None
|
set_raw_packet_callback
¶
Set callback for raw packet data (includes both parsed packet and raw bytes).
wait_for_ack
async
¶
Wait for a specific ACK CRC for up to timeout
seconds.
Dispatcher Components¶
Packet Routing¶
The Dispatcher handles routing of packets to appropriate handlers based on packet type and content.
Handler Registration¶
# Register a handler for specific packet types
dispatcher.register_handler(PacketType.DATA, data_handler)
dispatcher.register_handler(PacketType.ACK, ack_handler)
Packet Transmission¶
Key Methods¶
register_handler(packet_type, handler)
- Register a handler for a packet typeunregister_handler(packet_type, handler)
- Remove a handler registrationsend_packet(packet)
- Send a packet through the dispatcherhandle_packet(packet)
- Process an incoming packetget_registered_handlers()
- Get list of registered handlers
Handler Interface¶
All packet handlers should implement the following interface:
class PacketHandler:
async def handle_packet(self, packet: Packet) -> None:
"""Handle an incoming packet."""
pass
Packet Flow¶
- Incoming Packet → Dispatcher.receive_packet()
- Handler Lookup → Find registered handler for packet type
- Handler Execution → Call handler.handle_packet(packet)
- Response Handling → Handle any response packets generated
Error Handling¶
The Dispatcher includes built-in error handling for: - Invalid packet types - Handler execution errors - Packet validation failures - Transmission timeouts
Thread Safety¶
The Dispatcher is designed to be thread-safe for concurrent packet handling and registration operations.