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
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;
static ROUTINGTABLE: Storage<RwLock<RoutingTable>> = Storage::new();
#[derive(Debug, Clone)]
pub struct RoutingUserEntry {
pub id: Vec<u8>,
pub pgid: u32,
pub pgid_update: u64,
pub pgid_update_hc: u8,
pub online_time: u64,
pub connections: Vec<RoutingConnectionEntry>,
}
#[derive(Debug, Clone)]
pub struct RoutingConnectionEntry {
pub module: ConnectionModule,
pub node: PeerId,
pub rtt: u32,
pub hc: u8,
pub lq: u32,
pub last_update: u64,
}
pub struct RoutingTable {
pub table: HashMap<Vec<u8>, RoutingUserEntry>,
}
impl RoutingTable {
pub fn init() {
let table = RoutingTable {
table: HashMap::new(),
};
ROUTINGTABLE.set(RwLock::new(table));
}
pub fn set(new_table: RoutingTable) {
let mut table = ROUTINGTABLE.get().write().unwrap();
table.table = new_table.table;
}
pub fn create_routing_info(
neighbour: PeerId,
last_sent: u64,
) -> router_net_proto::RoutingInfoTable {
let mut table = router_net_proto::RoutingInfoTable { entry: Vec::new() };
let routing_table = ROUTINGTABLE.get().read().unwrap();
for (user_id, user) in routing_table.table.iter() {
if user.connections.len() == 0 {
continue;
}
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
}
pub fn get_online_users() -> BTreeMap<Vec<u8>, u8> {
let mut user_ids: BTreeMap<Vec<u8>, u8> = BTreeMap::new();
let routing_table = ROUTINGTABLE.get().read().unwrap();
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
}
pub fn get_online_users_info() -> BTreeMap<Vec<u8>, Vec<RoutingConnectionEntry>> {
let mut users: BTreeMap<Vec<u8>, Vec<RoutingConnectionEntry>> = BTreeMap::new();
let routing_table = ROUTINGTABLE.get().read().unwrap();
for (user_id, user) in routing_table.table.iter() {
if user.connections.len() > 0 {
users.insert(user_id.clone(), user.connections.clone());
}
}
users
}
pub fn get_online_user_ids(last_sent: u64) -> Vec<Vec<u8>> {
let mut user_ids: Vec<Vec<u8>> = vec![];
let routing_table = ROUTINGTABLE.get().read().unwrap();
for (user_id, user) in routing_table.table.iter() {
if user.online_time >= last_sent {
user_ids.push(user_id.clone());
}
}
user_ids
}
pub fn rpc_send_routing_table() {
let mut table_list: Vec<proto::RoutingTableEntry> = Vec::new();
let routing_table = ROUTINGTABLE.get().read().unwrap();
for (id, entry) in &routing_table.table {
let mut table_entry = proto::RoutingTableEntry {
user_id: id.to_owned(),
connections: Vec::new(),
};
for connection in &entry.connections {
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,
}
table_entry.connections.push(proto::RoutingTableConnection {
module,
rtt: connection.rtt,
hop_count: connection.hc as u32,
via: connection.node.to_bytes(),
});
}
table_list.push(table_entry);
}
let proto_message = proto::Router {
message: Some(proto::router::Message::RoutingTable(
proto::RoutingTableList {
routing_table: table_list,
},
)),
};
let mut buf = Vec::with_capacity(proto_message.encoded_len());
proto_message
.encode(&mut buf)
.expect("Vec<u8> provides capacity as needed");
Rpc::send_message(
buf,
crate::rpc::proto::Modules::Router.into(),
"".to_string(),
Vec::new(),
);
}
pub fn get_route_to_user(user_id: PeerId) -> Option<RoutingConnectionEntry> {
let routing_table = ROUTINGTABLE.get().read().unwrap();
let user_q8id = QaulId::to_q8id(user_id);
if let Some(user_entry) = routing_table.table.get(&user_q8id) {
let mut compare: Option<&RoutingConnectionEntry> = None;
for connection in &user_entry.connections {
match compare {
Some(current) => {
if Self::compare_connections(current, connection) {
compare = Some(connection);
}
}
None => compare = Some(connection),
}
}
match compare {
None => return None,
Some(connection) => return Some(connection.to_owned()),
}
}
None
}
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
}
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,
}
}
}