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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

//! # Global Routing Table
//!
//! This file contains the global routing table
//!
//! * contains all currently reachable users.
//! * There is an entry for each user over which connection modules
//!   it can be reached. Each connection module only contains
//!   information of the best node.

use libp2p::PeerId;
use prost::Message;
use state::Storage;
use std::collections::{BTreeMap, HashMap};
use std::sync::RwLock;

use super::proto;
use crate::connections::ConnectionModule;
use crate::router::router_net_proto;
use crate::rpc::Rpc;
use crate::utilities::qaul_id::QaulId;

/// mutable state of table
static ROUTINGTABLE: Storage<RwLock<RoutingTable>> = Storage::new();

/// table entry per user
#[derive(Debug, Clone)]
pub struct RoutingUserEntry {
    /// user q8id, 8 Byte qaul user id
    pub id: Vec<u8>,
    /// propagation id
    pub pgid: u32,
    /// propagation id update time
    pub pgid_update: u64,
    /// shortest hop count for user within this propagation id
    pub pgid_update_hc: u8,
    //online time
    pub online_time: u64,
    /// best routing entry per connection module
    pub connections: Vec<RoutingConnectionEntry>,
}

/// connection entry per connection module
#[derive(Debug, Clone)]
pub struct RoutingConnectionEntry {
    /// connections module
    pub module: ConnectionModule,
    /// node id
    /// via which the user can be reached
    pub node: PeerId,
    /// round trip time
    /// addition of all round trip times for all hops
    pub rtt: u32,
    /// hop count
    /// how many hops has the connection
    pub hc: u8,
    /// link quality
    pub lq: u32,
    /// last_update
    pub last_update: u64,
}

/// Global Routing Table Implementation
///
/// This is the table to turn to when checking where to send
/// a package.
pub struct RoutingTable {
    /// routing table key is a users q8id
    pub table: HashMap<Vec<u8>, RoutingUserEntry>,
}

impl RoutingTable {
    /// Initialize routing table
    /// Creates global routing table and saves it to state.
    pub fn init() {
        // create global routing table and save it to state
        let table = RoutingTable {
            table: HashMap::new(),
        };
        ROUTINGTABLE.set(RwLock::new(table));
    }

    /// set and replace routing table with a new table
    pub fn set(new_table: RoutingTable) {
        let mut table = ROUTINGTABLE.get().write().unwrap();
        table.table = new_table.table;
    }

    /// Create routing information for a specific neighbour node,
    /// to be sent to this neighbour node.
    pub fn create_routing_info(
        neighbour: PeerId,
        last_sent: u64,
    ) -> router_net_proto::RoutingInfoTable {
        let mut table = router_net_proto::RoutingInfoTable { entry: Vec::new() };

        // get access to routing table
        let routing_table = ROUTINGTABLE.get().read().unwrap();

        // loop through routing table
        for (user_id, user) in routing_table.table.iter() {
            if user.connections.len() == 0 {
                continue;
            }

            // choose best link quality
            let mut min_conn = user.connections[0].clone();
            for i in 0..user.connections.len() {
                if user.connections[i].lq < min_conn.lq {
                    min_conn = user.connections[i].clone();
                }
            }

            if neighbour != min_conn.node && (min_conn.last_update >= last_sent || min_conn.hc == 0)
            {
                let mut hc = Vec::new();
                hc.push(min_conn.hc);

                let table_entry = router_net_proto::RoutingInfoEntry {
                    user: user_id.to_owned(),
                    rtt: min_conn.rtt,
                    hc,
                    pgid: user.pgid,
                };
                table.entry.push(table_entry);
            }
        }

        table
    }

    /// get online users and hope count    
    pub fn get_online_users() -> BTreeMap<Vec<u8>, u8> {
        let mut user_ids: BTreeMap<Vec<u8>, u8> = BTreeMap::new();

        // get access to routing table
        let routing_table = ROUTINGTABLE.get().read().unwrap();

        // loop through routing table
        for (user_id, user) in routing_table.table.iter() {
            if user.connections.len() > 0 {
                user_ids.insert(user_id.clone(), user.connections[0].hc);
            }
        }
        user_ids
    }

    /// get online users and hope count    
    pub fn get_online_users_info() -> BTreeMap<Vec<u8>, Vec<RoutingConnectionEntry>> {
        let mut users: BTreeMap<Vec<u8>, Vec<RoutingConnectionEntry>> = BTreeMap::new();

        // get access to routing table
        let routing_table = ROUTINGTABLE.get().read().unwrap();

        // loop through routing table
        for (user_id, user) in routing_table.table.iter() {
            if user.connections.len() > 0 {
                users.insert(user_id.clone(), user.connections.clone());
            }
        }
        users
    }

    /// Create routing information for a specific neighbour node,
    /// to be sent to this neighbour node.
    pub fn get_online_user_ids(last_sent: u64) -> Vec<Vec<u8>> {
        let mut user_ids: Vec<Vec<u8>> = vec![];

        // get access to routing table
        let routing_table = ROUTINGTABLE.get().read().unwrap();

        // loop through routing table
        for (user_id, user) in routing_table.table.iter() {
            if user.online_time >= last_sent {
                user_ids.push(user_id.clone());
            }
        }
        user_ids
    }

    /// send protobuf RPC neighbours list
    pub fn rpc_send_routing_table() {
        // create list
        let mut table_list: Vec<proto::RoutingTableEntry> = Vec::new();

        // get routing table state
        let routing_table = ROUTINGTABLE.get().read().unwrap();

        // loop through all user table entries
        for (id, entry) in &routing_table.table {
            let mut table_entry = proto::RoutingTableEntry {
                user_id: id.to_owned(),
                connections: Vec::new(),
            };

            // loop through all connection entries in a user entry
            for connection in &entry.connections {
                // check module
                let module: i32;
                match connection.module {
                    ConnectionModule::Lan => module = proto::ConnectionModule::Lan as i32,
                    ConnectionModule::Internet => module = proto::ConnectionModule::Internet as i32,
                    ConnectionModule::Ble => module = proto::ConnectionModule::Ble as i32,
                    ConnectionModule::Local => module = proto::ConnectionModule::Local as i32,
                    _ => module = proto::ConnectionModule::None as i32,
                }

                // create entry
                table_entry.connections.push(proto::RoutingTableConnection {
                    module,
                    rtt: connection.rtt,
                    hop_count: connection.hc as u32,
                    via: connection.node.to_bytes(),
                });
            }

            // add user entry to table list
            table_list.push(table_entry);
        }

        // create table list message
        let proto_message = proto::Router {
            message: Some(proto::router::Message::RoutingTable(
                proto::RoutingTableList {
                    routing_table: table_list,
                },
            )),
        };

        // encode message
        let mut buf = Vec::with_capacity(proto_message.encoded_len());
        proto_message
            .encode(&mut buf)
            .expect("Vec<u8> provides capacity as needed");

        // send message
        Rpc::send_message(
            buf,
            crate::rpc::proto::Modules::Router.into(),
            "".to_string(),
            Vec::new(),
        );
    }

    /// Get the routing connection entry for a specific user
    ///
    /// The connection entry for the provided user_id contains
    /// the neighbour id as well as the connection module via
    /// which to send the packages.
    ///
    /// It selects the best route according to the rank_routing_connection function.
    ///
    pub fn get_route_to_user(user_id: PeerId) -> Option<RoutingConnectionEntry> {
        // get routing table state
        let routing_table = ROUTINGTABLE.get().read().unwrap();

        // get q8id for qaul user
        let user_q8id = QaulId::to_q8id(user_id);

        // find user
        if let Some(user_entry) = routing_table.table.get(&user_q8id) {
            let mut compare: Option<&RoutingConnectionEntry> = None;

            // find best route
            for connection in &user_entry.connections {
                match compare {
                    Some(current) => {
                        if Self::compare_connections(current, connection) {
                            compare = Some(connection);
                        }
                    }
                    None => compare = Some(connection),
                }
            }

            // return route
            match compare {
                None => return None,
                Some(connection) => return Some(connection.to_owned()),
            }
        }
        None
    }

    /// Compare two routing connections and decides which one is better
    ///
    /// This function decides which connection to favour based on the
    /// rank_routing_connection function
    ///
    /// Return values:
    ///
    /// * returns true, when the new connection is better
    /// * returns false, when the current connection is better
    ///
    fn compare_connections(current: &RoutingConnectionEntry, new: &RoutingConnectionEntry) -> bool {
        let current_value = Self::rank_routing_connection(current);
        let new_value = Self::rank_routing_connection(new);

        if current_value < new_value {
            return true;
        }

        false
    }

    /// give a ranking to the routing connection
    ///
    /// This function decides which connection to favour based on the following qualities:
    ///
    /// * Hierarchy of connection modules in the following order:
    ///   Local, LAN, Internet, BLE, None
    ///
    fn rank_routing_connection(connection: &RoutingConnectionEntry) -> u8 {
        match connection.module {
            ConnectionModule::None => return 0,
            ConnectionModule::Ble => return 1,
            ConnectionModule::Internet => return 2,
            ConnectionModule::Lan => return 3,
            ConnectionModule::Local => return 4,
        }
    }
}