ipfrs_network/overlay_network_manager/
functions.rs1use std::collections::{HashMap, HashSet};
6
7use super::types::{OverlayError, OverlayLink, VirtualRoute};
8
9#[inline]
11pub fn xorshift64(state: &mut u64) -> u64 {
12 let mut x = *state;
13 x ^= x << 13;
14 x ^= x >> 7;
15 x ^= x << 17;
16 *state = x;
17 x
18}
19#[inline]
21pub fn fnv1a_64(data: &[u8]) -> u64 {
22 let mut h: u64 = 14_695_981_039_346_656_037;
23 for &b in data {
24 h ^= b as u64;
25 h = h.wrapping_mul(1_099_511_628_211);
26 }
27 h
28}
29#[inline]
31pub(super) fn canonical_key(a: &str, b: &str) -> (String, String) {
32 if a <= b {
33 (a.to_owned(), b.to_owned())
34 } else {
35 (b.to_owned(), a.to_owned())
36 }
37}
38pub(super) fn default_link(a: &str, b: &str) -> OverlayLink {
40 OverlayLink {
41 from_id: a.to_owned(),
42 to_id: b.to_owned(),
43 latency_ms: 10,
44 bandwidth_bps: 1_000_000_000,
45 reliability: 1.0,
46 is_tunnel: false,
47 }
48}
49pub(super) fn reconstruct_path(
51 prev: &HashMap<String, String>,
52 from: &str,
53 to: &str,
54) -> Result<Vec<String>, OverlayError> {
55 let mut path = Vec::new();
56 let mut cur = to.to_owned();
57 let mut visited: HashSet<String> = HashSet::new();
58 loop {
59 if visited.contains(&cur) {
60 return Err(OverlayError::NoPathExists {
61 from: from.to_owned(),
62 to: to.to_owned(),
63 });
64 }
65 visited.insert(cur.clone());
66 path.push(cur.clone());
67 if cur == from {
68 break;
69 }
70 match prev.get(&cur) {
71 Some(p) => cur = p.clone(),
72 None => {
73 return Err(OverlayError::NoPathExists {
74 from: from.to_owned(),
75 to: to.to_owned(),
76 });
77 }
78 }
79 }
80 path.reverse();
81 Ok(path)
82}
83pub(super) fn best_candidate_index(candidates: &[VirtualRoute]) -> usize {
87 let mut best = 0;
88 for i in 1..candidates.len() {
89 let a = &candidates[best];
90 let b = &candidates[i];
91 if b.total_latency_ms < a.total_latency_ms
92 || (b.total_latency_ms == a.total_latency_ms && b.path_reliability > a.path_reliability)
93 {
94 best = i;
95 }
96 }
97 best
98}