// this class consists of a thread that will re send pings every time the first element // of the stack is at the correct unix time use std::{ collections::{HashMap, VecDeque}, net::{AddrParseError, SocketAddr}, ops::Add, process::Command, sync::{Arc, Mutex}, thread, time::{self, Duration, SystemTime}, }; pub struct PeerInfo { username: String, ip: SocketAddr, } pub struct HandshakeHistory { time_k_ip_v: HashMap, ip_k_peerinfo_v: HashMap, } impl HandshakeHistory { pub fn new() -> HandshakeHistory { HandshakeHistory { time_k_ip_v: HashMap::new(), ip_k_peerinfo_v: HashMap::new(), } } pub fn update_handshake(&mut self) { thread::spawn(move || { let mut times_to_check = VecDeque::new(); let current_time: u64 = SystemTime::now() .duration_since(time::UNIX_EPOCH) .expect("system time before UNIX EPOCH") .add(Duration::from_secs(10)) .as_secs(); // adds 10 seconds in the queue every 10 seconds loop { let mut child = Command::new("sleep").arg("9").spawn().unwrap(); let _result = child.wait().unwrap(); for n in 0..9 { // push 9 successive seconds times_to_check.push_back(current_time + n); // gestion d'erreur si verrou mort } } }); } pub fn add_new_handshake(&mut self, hash: u64, username: String, ip: SocketAddr) { let current_time: u64 = SystemTime::now() .duration_since(time::UNIX_EPOCH) .expect("system time before UNIX EPOCH") .as_secs(); println!("time:{}", current_time); /*self.time_k_hash_v.insert(current_time, hash); self.hash_k_peerinfo_v .insert(hash, PeerInfo { username, ip });*/ } } #[cfg(test)] mod tests { use std::net::{IpAddr, Ipv4Addr}; use super::*; /// /// creates a cryptographic signature /// #[test] fn creating_cryptographic_signature() { let mut hh = HandshakeHistory::new(); hh.add_new_handshake( 20, "putain".to_string(), SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 1), ); } }