dispatcher.rs

Overview

This file implements a message dispatching mechanism for routing network messages within a multi-threaded environment, where messages are associated with specific threads identified by a ThreadIdentifier. The Dispatcher struct manages routing information and directs incoming messages to the appropriate thread-specific channels for processing, distinguishing between regular node messages and those related to authority (consensus or control) protocols.

The dispatcher handles various types of network messages (NetworkMessage) by evaluating their thread association and routing them accordingly. It supports dynamic route addition and checks for existing routes before dispatching messages.

Types and Enumerations

Payload

type Payload = (NetworkMessage, SocketAddr);

DispatchError

pub enum DispatchError {
    NoRoute(ThreadIdentifier, Payload),
    DestinationClosed(ThreadIdentifier, Payload),
}

Structs

Dispatcher

pub struct Dispatcher {
    routes: HashMap<ThreadIdentifier, (XInstrumentedSender<Payload>, XInstrumentedSender<Payload>)>,
}

Implementation Details

Trait Implementations

Default for Dispatcher

impl Default for Dispatcher {
    fn default() -> Self {
        Self::new()
    }
}

Methods

new

pub fn new() -> Self

has_route

pub fn has_route(&mut self, thread_identifier: &ThreadIdentifier) -> bool
if dispatcher.has_route(&thread_id) {
    // Route exists, safe to dispatch messages
}

add_route

pub fn add_route(
    &mut self,
    thread_identifier: ThreadIdentifier,
    node: XInstrumentedSender<Payload>,
    authority: XInstrumentedSender<Payload>,
)
dispatcher.add_route(thread_id, node_sender, authority_sender);

dispatch

pub fn dispatch(&self, message: Payload) -> anyhow::Result<(), DispatchError>
match dispatcher.dispatch(message) {
    Ok(_) => println!("Message dispatched successfully"),
    Err(DispatchError::NoRoute(thread, _)) => eprintln!("No route for thread {:?}", thread),
    Err(DispatchError::DestinationClosed(thread, _)) => eprintln!("Channel closed for thread {:?}", thread),
}

Important Implementation Notes

Interactions with Other Components

Diagram: Dispatcher Structure and Message Flow

flowchart TD
A["Incoming Payload (NetworkMessage + SocketAddr)"]
B{Message Type?}
C[Authority Switch Protocol]
D[Other Network Messages]
E[Extract ThreadIdentifier]
F{Route Exists for Thread?}
G[Send via Authority Sender]
H[Send via Node Sender]
I[Return Ok]
J[Return DispatchError::NoRoute]
K[Return DispatchError::DestinationClosed]
A --> B
B -->|AuthoritySwitchProtocol| C
B -->|Others| D
C --> E
D --> E
E --> F
F -->|Yes| G
F -->|No| J
G --> I
G -->|Send Error| K
H --> I
H -->|Send Error| K
E -->|Is Authority| G
E -->|Is Node| H

This file is central to the message routing subsystem, enabling thread-specific message handling with clear separation between authority and node message flows, while incorporating instrumentation hooks for monitoring. For thread and message type definitions, see ThreadIdentifier and NetworkMessage. For details on authority protocol routing logic, see authority_switch::routing.