2023-09-23 01:53:45 +00:00
|
|
|
use std::{net::Ipv4Addr, sync::Arc};
|
|
|
|
|
|
2024-09-18 21:55:28 +08:00
|
|
|
use crate::common::PeerId;
|
2023-09-23 01:53:45 +00:00
|
|
|
|
2024-05-12 11:48:13 +08:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub enum NextHopPolicy {
|
|
|
|
|
LeastHop,
|
|
|
|
|
LeastCost,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for NextHopPolicy {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
NextHopPolicy::LeastHop
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 21:55:28 +08:00
|
|
|
#[async_trait::async_trait]
|
2023-09-23 01:53:45 +00:00
|
|
|
pub trait RouteInterface {
|
|
|
|
|
async fn list_peers(&self) -> Vec<PeerId>;
|
2024-03-13 00:15:22 +08:00
|
|
|
fn my_peer_id(&self) -> PeerId;
|
2023-09-23 01:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub type RouteInterfaceBox = Box<dyn RouteInterface + Send + Sync>;
|
|
|
|
|
|
2024-05-12 21:45:48 +08:00
|
|
|
#[auto_impl::auto_impl(Box , &mut)]
|
2024-05-12 11:48:13 +08:00
|
|
|
pub trait RouteCostCalculatorInterface: Send + Sync {
|
2024-05-12 21:45:48 +08:00
|
|
|
fn begin_update(&mut self) {}
|
|
|
|
|
fn end_update(&mut self) {}
|
|
|
|
|
|
2024-05-12 11:48:13 +08:00
|
|
|
fn calculate_cost(&self, _src: PeerId, _dst: PeerId) -> i32 {
|
|
|
|
|
1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn need_update(&self) -> bool {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn dump(&self) -> String {
|
|
|
|
|
"All routes have cost 1".to_string()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
|
pub struct DefaultRouteCostCalculator;
|
|
|
|
|
|
|
|
|
|
impl RouteCostCalculatorInterface for DefaultRouteCostCalculator {}
|
|
|
|
|
|
|
|
|
|
pub type RouteCostCalculator = Box<dyn RouteCostCalculatorInterface>;
|
|
|
|
|
|
2024-09-18 21:55:28 +08:00
|
|
|
#[async_trait::async_trait]
|
2024-02-06 20:17:08 +08:00
|
|
|
#[auto_impl::auto_impl(Box, Arc)]
|
2023-09-23 01:53:45 +00:00
|
|
|
pub trait Route {
|
|
|
|
|
async fn open(&self, interface: RouteInterfaceBox) -> Result<u8, ()>;
|
|
|
|
|
async fn close(&self);
|
|
|
|
|
|
2024-03-13 00:15:22 +08:00
|
|
|
async fn get_next_hop(&self, peer_id: PeerId) -> Option<PeerId>;
|
2024-05-12 11:48:13 +08:00
|
|
|
async fn get_next_hop_with_policy(
|
|
|
|
|
&self,
|
|
|
|
|
peer_id: PeerId,
|
|
|
|
|
_policy: NextHopPolicy,
|
|
|
|
|
) -> Option<PeerId> {
|
|
|
|
|
self.get_next_hop(peer_id).await
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 21:55:28 +08:00
|
|
|
async fn list_routes(&self) -> Vec<crate::proto::cli::Route>;
|
2024-02-06 20:17:08 +08:00
|
|
|
|
|
|
|
|
async fn get_peer_id_by_ipv4(&self, _ipv4: &Ipv4Addr) -> Option<PeerId> {
|
|
|
|
|
None
|
|
|
|
|
}
|
2024-05-12 11:48:13 +08:00
|
|
|
|
|
|
|
|
async fn set_route_cost_fn(&self, _cost_fn: RouteCostCalculator) {}
|
2024-07-07 15:40:46 +08:00
|
|
|
|
|
|
|
|
async fn dump(&self) -> String {
|
|
|
|
|
"this route implementation does not support dump".to_string()
|
|
|
|
|
}
|
2023-09-23 01:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub type ArcRoute = Arc<Box<dyn Route + Send + Sync>>;
|