mirror of
https://mirror.suhoan.cn/https://github.com/EasyTier/EasyTier.git
synced 2025-12-16 22:57:24 +08:00
support custom cost calculate func when generating route table
This commit is contained in:
@@ -5,6 +5,18 @@ use tokio_util::bytes::Bytes;
|
||||
|
||||
use crate::common::{error::Error, PeerId};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum NextHopPolicy {
|
||||
LeastHop,
|
||||
LeastCost,
|
||||
}
|
||||
|
||||
impl Default for NextHopPolicy {
|
||||
fn default() -> Self {
|
||||
NextHopPolicy::LeastHop
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait RouteInterface {
|
||||
async fn list_peers(&self) -> Vec<PeerId>;
|
||||
@@ -19,6 +31,28 @@ pub trait RouteInterface {
|
||||
|
||||
pub type RouteInterfaceBox = Box<dyn RouteInterface + Send + Sync>;
|
||||
|
||||
#[auto_impl::auto_impl(Box, Arc, &)]
|
||||
pub trait RouteCostCalculatorInterface: Send + Sync {
|
||||
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>;
|
||||
|
||||
#[async_trait]
|
||||
#[auto_impl::auto_impl(Box, Arc)]
|
||||
pub trait Route {
|
||||
@@ -26,11 +60,21 @@ pub trait Route {
|
||||
async fn close(&self);
|
||||
|
||||
async fn get_next_hop(&self, peer_id: PeerId) -> Option<PeerId>;
|
||||
async fn get_next_hop_with_policy(
|
||||
&self,
|
||||
peer_id: PeerId,
|
||||
_policy: NextHopPolicy,
|
||||
) -> Option<PeerId> {
|
||||
self.get_next_hop(peer_id).await
|
||||
}
|
||||
|
||||
async fn list_routes(&self) -> Vec<crate::rpc::Route>;
|
||||
|
||||
async fn get_peer_id_by_ipv4(&self, _ipv4: &Ipv4Addr) -> Option<PeerId> {
|
||||
None
|
||||
}
|
||||
|
||||
async fn set_route_cost_fn(&self, _cost_fn: RouteCostCalculator) {}
|
||||
}
|
||||
|
||||
pub type ArcRoute = Arc<Box<dyn Route + Send + Sync>>;
|
||||
|
||||
Reference in New Issue
Block a user