Unprotected version of servers storage
This commit is contained in:
parent
9bf4b22cb1
commit
93ab48db81
@ -1,10 +1,2 @@
|
||||
mod cycled_buffer;
|
||||
|
||||
use std::collections::HashSet;
|
||||
use bgtu_networks_2_gui_abstract::ServersStorage as ServersGuiCtx;
|
||||
use bgtu_networks_2_network_abstract::{Address, ServersContext as ServersNetworkCtx};
|
||||
use crate::cycled_buffer::CycledBuffer;
|
||||
|
||||
struct ServersStorage<A: Address> {
|
||||
map: HashSet<A, (String, CycledBuffer<Option<u32>>)>
|
||||
}
|
||||
mod unprotected;
|
||||
63
data/src/unprotected.rs
Normal file
63
data/src/unprotected.rs
Normal file
@ -0,0 +1,63 @@
|
||||
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> {
|
||||
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 A = A;
|
||||
|
||||
fn add_server(&mut self, addr: Self::A, 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::A) {
|
||||
self.map.remove(addr);
|
||||
}
|
||||
|
||||
fn iter_servers(
|
||||
&self,
|
||||
) -> impl Iterator<Item = (&Self::A, &str, impl Iterator<Item = Option<u128>>)> {
|
||||
return self
|
||||
.map
|
||||
.iter()
|
||||
.map(|(k, v)| (k, v.0.as_str(), v.1.iter().map(|x| *x)));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user