Skip to main content

brainwires_network/routing/
traits.rs

1use anyhow::Result;
2use async_trait::async_trait;
3use serde::{Deserialize, Serialize};
4
5use super::peer_table::PeerTable;
6use crate::network::MessageEnvelope;
7use crate::transport::TransportAddress;
8
9/// Routing strategy identifier.
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub enum RoutingStrategy {
12    /// Point-to-point delivery to a specific peer.
13    Direct,
14    /// Send to all known peers.
15    Broadcast,
16    /// Route based on topic subscriptions.
17    ContentBased,
18    /// Custom routing with a user-defined identifier.
19    Custom(String),
20}
21
22/// The core routing trait.
23///
24/// A router takes a [`MessageEnvelope`] and the current [`PeerTable`]
25/// and returns the list of transport addresses the message should be
26/// sent to.
27#[async_trait]
28pub trait Router: Send + Sync {
29    /// Determine the transport addresses to deliver this message to.
30    async fn route(
31        &self,
32        envelope: &MessageEnvelope,
33        peers: &PeerTable,
34    ) -> Result<Vec<TransportAddress>>;
35
36    /// The routing strategy this router implements.
37    fn strategy(&self) -> RoutingStrategy;
38}