pub struct PeerMessageRouter {
pub rules: Vec<RouteRule>,
pub subscriptions: HashMap<String, Vec<String>>,
pub next_msg_id: u64,
pub stats: MessageRouterStats,
}Expand description
Routes messages to appropriate peer handlers based on message type and topic subscriptions, supporting both direct and broadcast routing.
§Routing semantics
- Direct (
to_peer = Some): a singlePeerRoutedMessageis produced addressed to the specified peer. - Broadcast (
to_peer = None):MessageType::Gossip: delivered to all peers subscribed to the gossip topic, excludingfrom_peer.- All other types: delivered to all peers that appear as keys in the
subscription map and match at least one rule with
target_peer = None. - If no recipients are found the message is counted as dropped.
Fields§
§rules: Vec<RouteRule>Routing rules kept sorted by priority descending.
subscriptions: HashMap<String, Vec<String>>Topic → list of subscribed peer IDs.
next_msg_id: u64Next message ID to assign (post-increment semantics).
stats: MessageRouterStatsAggregate statistics.
Implementations§
Source§impl PeerMessageRouter
impl PeerMessageRouter
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new, empty PeerMessageRouter.
Sourcepub fn add_rule(&mut self, rule: RouteRule)
pub fn add_rule(&mut self, rule: RouteRule)
Insert rule, maintaining the invariant that self.rules is sorted by
priority descending.
Sourcepub fn remove_rule(&mut self, rule_id: u64) -> bool
pub fn remove_rule(&mut self, rule_id: u64) -> bool
Remove the rule with the given rule_id.
Returns true if a rule was found and removed, false otherwise.
Sourcepub fn subscribe(&mut self, peer_id: &str, topic: &str)
pub fn subscribe(&mut self, peer_id: &str, topic: &str)
Subscribe peer_id to topic.
If the peer is already subscribed this is a no-op.
Sourcepub fn unsubscribe(&mut self, peer_id: &str, topic: &str) -> bool
pub fn unsubscribe(&mut self, peer_id: &str, topic: &str) -> bool
Unsubscribe peer_id from topic.
Returns true if the peer was subscribed, false if it was not.
Sourcepub fn route(
&mut self,
msg_type: MessageType,
from_peer: &str,
to_peer: Option<&str>,
payload_bytes: u64,
tick: u64,
) -> Vec<PeerRoutedMessage>
pub fn route( &mut self, msg_type: MessageType, from_peer: &str, to_peer: Option<&str>, payload_bytes: u64, tick: u64, ) -> Vec<PeerRoutedMessage>
Route a message, returning one PeerRoutedMessage per recipient.
msg_id is assigned from next_msg_id (pre-increment — the assigned
value is used, then next_msg_id is incremented).
Sourcepub fn subscribers(&self, topic: &str) -> Vec<&str>
pub fn subscribers(&self, topic: &str) -> Vec<&str>
Return all peer IDs subscribed to topic, sorted lexicographically.
Sourcepub fn stats(&self) -> &MessageRouterStats
pub fn stats(&self) -> &MessageRouterStats
Return a reference to the current statistics.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PeerMessageRouter
impl RefUnwindSafe for PeerMessageRouter
impl Send for PeerMessageRouter
impl Sync for PeerMessageRouter
impl Unpin for PeerMessageRouter
impl UnsafeUnpin for PeerMessageRouter
impl UnwindSafe for PeerMessageRouter
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more