50 lines
1.5 KiB
Rust
50 lines
1.5 KiB
Rust
|
use lox_library::bridge_table::BridgeLine;
|
||
|
use std::collections::HashMap;
|
||
|
|
||
|
pub struct Bridge {
|
||
|
pub fingerprint: [u8; 20],
|
||
|
real_connections: HashMap<String, u32>,
|
||
|
total_connections: HashMap<String, u32>,
|
||
|
}
|
||
|
|
||
|
impl Bridge {
|
||
|
pub fn new(fingerprint: &[u8; 20]) -> Self {
|
||
|
Self {
|
||
|
fingerprint: *fingerprint,
|
||
|
real_connections: HashMap::<String, u32>::new(),
|
||
|
total_connections: HashMap::<String, u32>::new(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn from_bridge_line(bridgeline: &BridgeLine) -> Self {
|
||
|
Self::new(&bridgeline.fingerprint)
|
||
|
}
|
||
|
|
||
|
pub fn connect_real(&mut self, country: &str) {
|
||
|
if self.real_connections.contains_key(country) {
|
||
|
let prev = self.real_connections.get(country).unwrap();
|
||
|
self.real_connections
|
||
|
.insert(country.to_string(), prev + 1)
|
||
|
.unwrap();
|
||
|
} else {
|
||
|
self.real_connections
|
||
|
.insert(country.to_string(), 1)
|
||
|
.unwrap();
|
||
|
}
|
||
|
self.connect_total(country);
|
||
|
}
|
||
|
|
||
|
pub fn connect_total(&mut self, country: &str) {
|
||
|
if self.total_connections.contains_key(country) {
|
||
|
let prev = self.total_connections.get(country).unwrap();
|
||
|
self.total_connections
|
||
|
.insert(country.to_string(), prev + 1)
|
||
|
.unwrap();
|
||
|
} else {
|
||
|
self.total_connections
|
||
|
.insert(country.to_string(), 1)
|
||
|
.unwrap();
|
||
|
}
|
||
|
}
|
||
|
}
|