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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#![allow(clippy::bool_assert_comparison)]

use atm0s_sdn_identity::{NodeId, NodeIdType};
pub mod core;
pub mod shadow;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ServiceBroadcastLevel {
    Global,
    Geo1,
    Geo2,
    Group,
}

impl ServiceBroadcastLevel {
    pub fn same_level(&self, node1: NodeId, node2: NodeId) -> bool {
        match self {
            ServiceBroadcastLevel::Global => true,
            ServiceBroadcastLevel::Geo1 => node1.geo1() == node2.geo1(),
            ServiceBroadcastLevel::Geo2 => node1.geo1() == node2.geo1() && node1.geo2() == node2.geo2(),
            ServiceBroadcastLevel::Group => node1.geo1() == node2.geo1() && node1.geo2() == node2.geo2() && node1.group() == node2.group(),
        }
    }
}

impl From<ServiceBroadcastLevel> for u8 {
    fn from(val: ServiceBroadcastLevel) -> Self {
        match val {
            ServiceBroadcastLevel::Global => 0,
            ServiceBroadcastLevel::Geo1 => 1,
            ServiceBroadcastLevel::Geo2 => 2,
            ServiceBroadcastLevel::Group => 3,
        }
    }
}

impl From<u8> for ServiceBroadcastLevel {
    fn from(val: u8) -> Self {
        match val {
            0 => ServiceBroadcastLevel::Global,
            1 => ServiceBroadcastLevel::Geo1,
            2 => ServiceBroadcastLevel::Geo2,
            _ => ServiceBroadcastLevel::Group,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RouteRule {
    Direct,
    ToNode(NodeId),
    ToService(u8),
    /// First is service id, second is the level, and third is seq of message
    ToServices(u8, ServiceBroadcastLevel, u16),
    ToKey(NodeId),
}

/// Determine the destination of an action/message
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RouteAction<Remote> {
    /// Reject the message
    Reject,
    /// Will be processed locally
    Local,
    /// Will be forward to the given connection
    Next(Remote),
    /// Will be forward to the given connection, first is local or not, next is the list of remote dests
    Broadcast(bool, Vec<Remote>),
}

impl<Remote> RouteAction<Remote> {
    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(_))
    }
}

pub trait RouterTable<Remote> {
    /// Find the closest node for the given key
    fn closest_for(&self, key: NodeId) -> Option<Remote>;
    /// Find the next node for the given destination node
    fn next(&self, dest: NodeId) -> Option<Remote>;
    /// Determine the next action for the given destination node
    fn path_to_node(&self, dest: NodeId) -> RouteAction<Remote>;
    /// Determine the next action for the given key
    fn path_to_key(&self, key: NodeId) -> RouteAction<Remote>;
    /// Determine the next action for the given service
    fn path_to_service(&self, service_id: u8) -> RouteAction<Remote>;
    /// Determine the next action if we need broadcast to all node running a service.
    /// If relay_from is set, it should not sending back for avoiding loop
    fn path_to_services(&self, service_id: u8, seq: u16, level: ServiceBroadcastLevel, source: Option<NodeId>, relay_from: Option<NodeId>) -> RouteAction<Remote>;
    /// Determine next action for incoming messages
    /// given the route rule and service id
    fn derive_action(&self, route: &RouteRule, source: Option<NodeId>, relay_from: Option<NodeId>) -> RouteAction<Remote> {
        match route {
            RouteRule::Direct => RouteAction::Local,
            RouteRule::ToNode(dest) => self.path_to_node(*dest),
            RouteRule::ToKey(key) => self.path_to_key(*key),
            RouteRule::ToService(service) => self.path_to_service(*service),
            RouteRule::ToServices(service, level, seq) => self.path_to_services(*service, *seq, *level, source, relay_from),
        }
    }
}

#[cfg(test)]
mod tests {
    use atm0s_sdn_identity::ConnId;
    type RouteAction = super::RouteAction<ConnId>;

    #[test]
    fn test_is_local() {
        let local = RouteAction::Local;
        let remote = RouteAction::Next(ConnId::from_in(1, 1));
        let reject = RouteAction::Reject;

        assert!(local.is_local());
        assert!(!remote.is_local());
        assert!(!reject.is_local());
    }

    #[test]
    fn test_is_reject() {
        let local = RouteAction::Local;
        let remote = RouteAction::Next(ConnId::from_in(1, 1));
        let reject = RouteAction::Reject;

        assert!(!local.is_reject());
        assert!(!remote.is_reject());
        assert!(reject.is_reject());
    }

    #[test]
    fn test_is_remote() {
        let local = RouteAction::Local;
        let remote = RouteAction::Next(ConnId::from_in(1, 1));
        let reject = RouteAction::Reject;

        assert!(!local.is_remote());
        assert!(remote.is_remote());
        assert!(!reject.is_remote());
    }
}