76 lines
2.2 KiB
Rust
76 lines
2.2 KiB
Rust
use crate::cycled_buffer::CycledBuffer;
|
|
use bgtu_networks_2_gui_abstract::ServersStorage as ServersGuiCtx;
|
|
use bgtu_networks_2_network_abstract::{Address, ServersContext as ServersNetworkCtx};
|
|
use std::collections::HashMap;
|
|
use std::hash::Hash;
|
|
|
|
pub struct ServersStorage<A: Address + Hash + Eq> {
|
|
history_capacity: usize,
|
|
last_stored_req_id: u64,
|
|
map: HashMap<A, (String, CycledBuffer<Option<u128>>)>,
|
|
}
|
|
|
|
impl<A: Address + Hash + Eq> ServersStorage<A> {
|
|
pub fn new(history_capacity: usize) -> Self {
|
|
return Self {
|
|
history_capacity,
|
|
last_stored_req_id: 0,
|
|
map: HashMap::new(),
|
|
};
|
|
}
|
|
|
|
fn _get_offset(&self, req_id: u64) -> usize {
|
|
return self.last_stored_req_id.wrapping_sub(req_id) as usize;
|
|
}
|
|
}
|
|
|
|
impl<A: Address + Hash + Eq> ServersNetworkCtx<A> for ServersStorage<A> {
|
|
fn start_measuring(&mut self) -> (u64, impl Iterator<Item = A>) {
|
|
self.last_stored_req_id = self.last_stored_req_id.wrapping_add(1);
|
|
for (_, b) in self.map.values_mut() {
|
|
b.push(None);
|
|
}
|
|
|
|
return (self.last_stored_req_id, self.map.keys().map(|z| z.clone()));
|
|
}
|
|
|
|
fn report_ping(&mut self, addr: A, id: u64, ping_ms: u128) {
|
|
let offset = self._get_offset(id);
|
|
if let Some((_, b)) = self.map.get_mut(&addr) {
|
|
b[offset] = Some(ping_ms);
|
|
}
|
|
}
|
|
|
|
fn report_error(&mut self, _addr: A, _id: u64) {}
|
|
}
|
|
|
|
impl<A: Address + Hash + Eq> ServersGuiCtx for ServersStorage<A> {
|
|
type Address = A;
|
|
|
|
fn add_server(&mut self, addr: Self::Address, memo: String) {
|
|
if self.map.get(&addr).is_some() {
|
|
todo!()
|
|
};
|
|
|
|
self.map
|
|
.insert(addr, (memo, CycledBuffer::new(self.history_capacity)));
|
|
}
|
|
|
|
fn remove_server(&mut self, addr: &Self::Address) {
|
|
self.map.remove(addr);
|
|
}
|
|
|
|
fn iter_servers(
|
|
&self,
|
|
) -> impl Iterator<Item = (&Self::Address, &str, impl Iterator<Item = Option<u128>>)> {
|
|
return self
|
|
.map
|
|
.iter()
|
|
.map(|(k, v)| (k, v.0.as_str(), v.1.iter().map(|x| *x)));
|
|
}
|
|
|
|
fn graph_sections_count(&self) -> usize {
|
|
return self.history_capacity;
|
|
}
|
|
}
|