-
Notifications
You must be signed in to change notification settings - Fork 10
/
lib.rs
64 lines (55 loc) · 1.9 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
mod force_local;
mod force_node;
use bluesea_identity::{ConnId, NodeId};
pub use force_local::ForceLocalRouter;
pub use force_node::ForceNodeRouter;
#[cfg(any(test, feature = "mock"))]
use mockall::automock;
/// ServiceMeta is using for determine which node will be routed, example node with lowest price or lowest latency, which for future use
pub type ServiceMeta = u32;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RouteRule {
Direct,
ToNode(NodeId),
ToService(ServiceMeta),
ToKey(NodeId),
}
/// Determine the destination of an action/message
pub enum RouteAction {
/// Reject the message
Reject,
/// Will be processed locally
Local,
/// Will be forward to the given node
Next(ConnId, NodeId),
}
impl RouteAction {
pub fn is_local(&self) -> bool {
matches!(self, RouteAction::Local)
}
pub fn is_reject(&self) -> bool {
matches!(self, RouteAction::Reject)
}
pub fn is_remote(&self) -> bool {
matches!(self, RouteAction::Next(_, _))
}
}
#[cfg_attr(any(test, feature = "mock"), automock)]
pub trait RouterTable: Send + Sync {
/// Determine the next action for the given destination node
fn path_to_node(&self, dest: NodeId) -> RouteAction;
/// Determine the next action for the given key
fn path_to_key(&self, key: NodeId) -> RouteAction;
/// Determine the next action for the given service
fn path_to_service(&self, service_id: u8) -> RouteAction;
/// Determine next action for incoming messages
/// given the route rule and service id
fn derive_action(&self, route: &RouteRule, service_id: u8) -> RouteAction {
match route {
RouteRule::Direct => RouteAction::Local,
RouteRule::ToNode(dest) => self.path_to_node(*dest),
RouteRule::ToKey(key) => self.path_to_key(*key),
RouteRule::ToService(_) => self.path_to_service(service_id),
}
}
}