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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

//! # Connection Module Connectivity Collection Table
//!
//! This file contains the collected connectivity information
//! per connection module
//!
//! * each connection module has it's own routing table containing
//!   all currently reachable users via this module.
//! * Each user has an entry for each node over which it can be reached.
//! * Out of this information the global table is constructed,
//!   containing only the best entry.

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

use super::proto;
use crate::connections::ConnectionModule;
use crate::node;
use crate::router::router_net_proto;
use crate::router::{
    neighbours::Neighbours,
    table::{RoutingConnectionEntry, RoutingTable, RoutingUserEntry},
};
use crate::rpc::Rpc;
use crate::utilities::qaul_id::QaulId;
use crate::utilities::timestamp::Timestamp;

/// Mutable module state
/// Tables with all stats for each connection module
static LOCAL: Storage<RwLock<RoutingTable>> = Storage::new();
static INTERNET: Storage<RwLock<ConnectionTable>> = Storage::new();
static LAN: Storage<RwLock<ConnectionTable>> = Storage::new();

/// Connection entry for UserEntry
struct NeighbourEntry {
    /// node id of the neighbour
    id: PeerId,
    /// round trip time
    rtt: u32,
    /// hop count
    hc: u8,
    /// link quality
    lq: u32,
    /// time when the node was last updated
    last_update: u64,
}

/// user entry for ConnectionTable
/// containing a BTreeMap of all neighbours over
/// which this user can be reached
struct UserEntry {
    /// user q8id
    id: Vec<u8>,
    /// propagation id
    pub pgid: u32,
    /// when was the propagation id last updated
    pub pgid_update: u64,
    /// DEPRECATED: do we really need this?
    pub pgid_update_hc: u8,
    /// online time
    pub online_time: u64,
    /// connection entries
    connections: BTreeMap<PeerId, NeighbourEntry>,
}

/// Connection table contains a hashed map of all reachable users.
/// The connectivity updates from each neighbour are collected here.
/// Out of this information the global routing table is generated.
pub struct ConnectionTable {
    table: HashMap<Vec<u8>, UserEntry>,
}

impl ConnectionTable {
    /// Initialize connection tables
    /// Creates a table for each ConnectionModule
    /// and saves it to state.
    pub fn init() {
        {
            let internet = ConnectionTable {
                table: HashMap::new(),
            };
            INTERNET.set(RwLock::new(internet));

            let lan = ConnectionTable {
                table: HashMap::new(),
            };
            LAN.set(RwLock::new(lan));

            let local = RoutingTable {
                table: HashMap::new(),
            };
            LOCAL.set(RwLock::new(local));
        }

        // create filled state for locally registered users
        {
            for user in node::user_accounts::UserAccounts::get_user_info() {
                Self::add_local_user(user.id);
            }
        }
    }

    /// add a new local user to state
    pub fn add_local_user(user_id: PeerId) {
        let node_id = node::Node::get_id();
        let mut routing_table = LOCAL.get().write().unwrap();

        let mut connections = Vec::new();

        // routing table creating is done every 1 seconds.
        // by considerate neighbour sending is done before creating routing Table.
        // we set local user online time forward 3 seconds
        let now_ts = Timestamp::get_timestamp() + 3000;
        connections.push(RoutingConnectionEntry {
            module: ConnectionModule::Local,
            node: node_id,
            rtt: 0,
            hc: 0,
            lq: 0,
            last_update: now_ts,
        });

        let user_q8id = QaulId::to_q8id(user_id);

        let routing_user_entry = RoutingUserEntry {
            id: user_q8id.clone(),
            pgid: 1,
            pgid_update: now_ts,
            pgid_update_hc: 1,
            online_time: now_ts,
            connections,
        };
        routing_table.table.insert(user_q8id, routing_user_entry);
    }

    /// process received routing info table
    /// enter it into all modules where we are connected to
    pub fn process_received_routing_info(
        neighbour_id: PeerId,
        info: &Vec<router_net_proto::RoutingInfoEntry>,
    ) {
        // log::trace!("process_received_routing_info count={}", info.len());
        // for inf in &info{
        //     let c: &[u8] = &inf.user;
        //     let userid = PeerId::from_bytes(c).unwrap();
        //     log::trace!("receive_routing_info user={}, hc={}, propg_id={}", userid, inf.hc[0], inf.pgid);
        // }

        // try Lan module
        if let Some(rtt) = Neighbours::get_rtt(&neighbour_id, &ConnectionModule::Lan) {
            Self::fill_received_routing_info(ConnectionModule::Lan, neighbour_id, rtt, info);
        }

        // try Internet module
        if let Some(rtt) = Neighbours::get_rtt(&neighbour_id, &ConnectionModule::Internet) {
            Self::fill_received_routing_info(ConnectionModule::Internet, neighbour_id, rtt, info);
        }
    }

    /// populate connection table with incoming routing information
    fn fill_received_routing_info(
        conn: ConnectionModule,
        neighbour_id: PeerId,
        rtt: u32,
        info: &Vec<router_net_proto::RoutingInfoEntry>,
    ) {
        log::trace!("fill_received_routing_info {}", info.len());
        // loop through results and enter them to the table
        for entry in info {
            // calculate hop count
            // if hop count is > 255, return
            let hc;
            if entry.hc[0] < 255 {
                hc = entry.hc[0] + 1;
            } else {
                return;
            }

            // fill structure
            let neighbour = NeighbourEntry {
                id: neighbour_id,
                rtt: entry.rtt + rtt,
                hc,
                lq: Self::calculate_linkquality(entry.rtt + rtt, hc),
                last_update: Timestamp::get_timestamp(),
            };

            // add it to state
            Self::add_connection(entry.user.clone(), entry.pgid, neighbour, conn.clone());
        }
    }

    /// calculate link quality
    ///
    /// returns the calculated link quality for a connection.
    ///
    /// The link quality is currently calculated using the
    /// round trip time (rtt) and adding a penalty for each hop
    /// according hop count (hc).
    ///
    /// The smaller the value is better is the link quality.
    pub fn calculate_linkquality(rtt: u32, hc: u8) -> u32 {
        // get the router configuration
        let config = super::Router::get_configuration();

        // calculate link quality
        // `hop_count_penalty` is seconds unit, thus it must be converted micro seconds
        let lq = rtt + (hc as u32 * (config.hop_count_penalty as u32) * 1000_000);

        // return link quality
        lq
    }

    /// add connection to local state
    fn add_connection(
        user_q8id: Vec<u8>,
        pgid: u32,
        connection: NeighbourEntry,
        module: ConnectionModule,
    ) {
        // get access to the connection table
        let mut connection_table;
        match module {
            ConnectionModule::Internet => connection_table = INTERNET.get().write().unwrap(),
            ConnectionModule::Lan => connection_table = LAN.get().write().unwrap(),
            ConnectionModule::Ble => return,
            ConnectionModule::Local => return,
            ConnectionModule::None => return,
        }

        let now_ts = Timestamp::get_timestamp();
        // check if user already exists
        if let Some(user) = connection_table.table.get_mut(&user_q8id) {
            if connection.hc == 1 || pgid > user.pgid {
                user.pgid = pgid;
                user.pgid_update = now_ts;
                user.pgid_update_hc = connection.hc;
                log::trace!("add_connection={}", connection.last_update);
                user.connections.remove(&connection.id);
                user.connections.insert(connection.id, connection);
            } else if pgid == user.pgid {
                //check last update
                if (now_ts - user.pgid_update <= (10 * 1000)) && connection.hc < user.pgid_update_hc
                {
                    user.pgid_update = now_ts;
                    user.pgid_update_hc = connection.hc;
                    user.connections.remove(&connection.id);
                    user.connections.insert(connection.id, connection);
                } else if let Some(conn) = user.connections.get_mut(&connection.id) {
                    if connection.lq < conn.lq {
                        conn.lq = connection.lq;
                        conn.hc = connection.hc;
                        conn.last_update = now_ts;
                        user.connections.remove(&connection.id);
                        user.connections.insert(connection.id, connection);
                    }
                }
            } else if pgid < user.pgid {
                if user.pgid_update_hc == connection.hc {
                    //reboot node case
                    if (user.pgid - pgid) > (connection.hc as u32) {
                        user.pgid = pgid;
                        user.pgid_update = now_ts;
                        user.pgid_update_hc = connection.hc;
                        user.connections.remove(&connection.id);
                        user.connections.insert(connection.id, connection);
                    }
                }
            }
        } else {
            let mut connections_map = BTreeMap::new();
            let hc = connection.hc;
            connections_map.insert(connection.id, connection);

            let user = UserEntry {
                id: user_q8id.clone(),
                pgid: pgid,
                pgid_update: now_ts,
                pgid_update_hc: hc,
                online_time: now_ts,
                connections: connections_map,
            };

            connection_table.table.insert(user_q8id, user);
        }
    }

    /// update propagation id for local users
    pub fn update_propagation_id(propagation_id: u32) {
        //update local user's propagation id
        let mut local = LOCAL.get().write().unwrap();
        for (_user_id, user) in local.table.iter_mut() {
            user.pgid = propagation_id;
            // QUESTION: is this of any use?
            user.pgid_update = Timestamp::get_timestamp();
            user.connections.get_mut(0).unwrap().last_update = Timestamp::get_timestamp();
        }
    }

    /// Create a routing table and set it to active routing table
    pub fn create_routing_table() {
        // create a new table
        let mut table = RoutingTable {
            table: HashMap::new(),
        };

        // set static routes for local users
        // create them first, for that they are always routed to ourselves
        {
            table = Self::local_routes_to_intermediary_table(table);
        }

        // calculate from lan module
        table = Self::calculate_intermediary_table(table, ConnectionModule::Lan);

        // calculate from internet module
        table = Self::calculate_intermediary_table(table, ConnectionModule::Internet);

        // set table as new active routing table
        RoutingTable::set(table);
    }

    /// insert local routes into routing table
    fn local_routes_to_intermediary_table(mut table: RoutingTable) -> RoutingTable {
        // get local routes
        let local = LOCAL.get().read().unwrap();

        // fill it into routing table
        for (user_id, user) in &local.table {
            table.table.insert(user_id.to_owned(), user.to_owned());
        }

        table
    }

    /// calculate a routing table for a module
    fn calculate_intermediary_table(
        mut table: RoutingTable,
        conn: ConnectionModule,
    ) -> RoutingTable {
        // create vector for users to remove
        let mut expired_users: Vec<Vec<u8>> = Vec::new();

        // get connections table
        let mut connection_table;
        match conn.clone() {
            ConnectionModule::Internet => connection_table = INTERNET.get().write().unwrap(),
            ConnectionModule::Lan => connection_table = LAN.get().write().unwrap(),
            ConnectionModule::Ble => return table,
            ConnectionModule::Local => return table,
            ConnectionModule::None => return table,
        }

        // iterate over connection table
        for (user_id, user) in connection_table.table.iter_mut() {
            let (b_expired_pgid, connection_entry) = Self::find_best_connection(user);
            if b_expired_pgid == false {
                if let Some(connection) = connection_entry {
                    // fill entry into routing table
                    let routing_connection_entry = RoutingConnectionEntry {
                        module: conn.clone(),
                        node: connection.id,
                        rtt: connection.rtt,
                        hc: connection.hc,
                        lq: connection.lq,
                        last_update: connection.last_update,
                    };

                    // check if user entry already exists hashmap
                    if let Some(routing_user_entry) = table.table.get_mut(&user.id) {
                        routing_user_entry
                            .connections
                            .push(routing_connection_entry);
                    } else {
                        let mut connections = Vec::new();
                        connections.push(routing_connection_entry);

                        let routing_user_entry = RoutingUserEntry {
                            id: user_id.to_owned(),
                            pgid: user.pgid,
                            pgid_update: user.pgid_update,
                            pgid_update_hc: user.pgid_update_hc,
                            online_time: user.online_time,
                            connections,
                        };
                        table.table.insert(user_id.to_owned(), routing_user_entry);
                    }
                } else {
                    if let None = table.table.get(&user.id) {
                        let routing_user_entry = RoutingUserEntry {
                            id: user_id.to_owned(),
                            pgid: user.pgid,
                            pgid_update: user.pgid_update,
                            pgid_update_hc: user.pgid_update_hc,
                            online_time: user.online_time,
                            connections: Vec::new(),
                        };
                        table.table.insert(user_id.to_owned(), routing_user_entry);
                    }
                }
            } else {
                expired_users.push(user_id.clone());
            }
        }

        // remove expired users
        for user_id in expired_users {
            connection_table.table.remove(&user_id);
        }

        table
    }

    /// find best entry
    /// and remove all old entries
    fn find_best_connection(user: &mut UserEntry) -> (bool, Option<NeighbourEntry>) {
        // initialize helper variables
        let mut expired_connections: Vec<PeerId> = Vec::new();
        let mut return_entry = None;
        let mut lq = u32::MAX;

        //remove user after 5min from last pgid updated
        //config.maintain_period_limit is seconds unit, need to convert into mili seconds
        let config = super::Router::get_configuration();
        if Timestamp::get_timestamp() - user.pgid_update >= (config.maintain_period_limit * 1000) {
            return (true, None);
        }

        // create return value
        {
            let mut entry_found = None;

            // loop through all connections
            for (key, value) in &user.connections {
                let mut expired = true;

                // check if entry is expired
                // entry expires after 20 seconds, unit is mili seconds
                let now = Timestamp::get_timestamp();
                //if now - value.last_update < (20 * 1000 * (value.hc as u64)){
                if now - value.last_update
                    //< (2 * (config.sending_table_period * 1000) * (value.hc as u64))
                    < (config.sending_table_period * 1000 * (value.hc as u64 + 1))
                {
                    expired = false;

                    if value.lq < lq {
                        lq = value.lq;
                        entry_found = Some(value);
                    }
                }

                // put connection for removal if expired
                if expired {
                    log::info!(
                        "expired entry={},  hc={}",
                        (now - value.last_update),
                        value.hc
                    );
                    expired_connections.push(key.clone());
                }
            }

            if let Some(entry) = entry_found {
                return_entry = Some(NeighbourEntry {
                    id: entry.id.clone(),
                    rtt: entry.rtt,
                    hc: entry.hc,
                    lq: entry.lq,
                    last_update: entry.last_update.clone(),
                })
            }
        }

        // remove expired connections
        for node_id in expired_connections {
            user.connections.remove(&node_id);
        }

        (false, return_entry)
    }

    /// send protobuf RPC connections list
    pub fn rpc_send_connections_list() {
        // create connections list
        let connections_list = proto::ConnectionsList {
            lan: Self::rpc_create_connection_module_list(ConnectionModule::Lan),
            internet: Self::rpc_create_connection_module_list(ConnectionModule::Internet),
            ble: Self::rpc_create_connection_module_list(ConnectionModule::Ble),
            local: Self::rpc_create_connection_module_list(ConnectionModule::Local),
        };

        // create rpc connections list protobuf message
        let proto_message = proto::Router {
            message: Some(proto::router::Message::ConnectionsList(connections_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(),
        );
    }

    /// create rpc connection module list
    fn rpc_create_connection_module_list(
        conn: ConnectionModule,
    ) -> Vec<proto::ConnectionsUserEntry> {
        // create entry vector
        let mut connections_list: Vec<proto::ConnectionsUserEntry> = Vec::new();

        // request connection table from state
        let connection_table;
        match conn {
            ConnectionModule::Lan => connection_table = LAN.get().read().unwrap(),
            ConnectionModule::Internet => connection_table = INTERNET.get().read().unwrap(),
            ConnectionModule::Ble => return connections_list,
            ConnectionModule::Local => return connections_list,
            ConnectionModule::None => return connections_list,
        }

        // loop through all table entries per user
        for (id, entry) in &connection_table.table {
            // create user entry
            let mut user_entry = proto::ConnectionsUserEntry {
                user_id: id.to_owned(),
                connections: Vec::new(),
            };

            // loop through all neighbour entries of a user entry
            for (id, neighbour) in &entry.connections {
                // add connection
                user_entry.connections.push(proto::ConnectionEntry {
                    rtt: neighbour.rtt,
                    hop_count: neighbour.hc as u32,
                    via: id.to_bytes(),
                });
            }

            // add user entry to list
            connections_list.push(user_entry);
        }

        // return list
        connections_list
    }
}