This commit is contained in:
TIBERGHIEN corentin
2026-01-21 02:45:48 +01:00
parent dacedd1ceb
commit 8b2ab4861b
10 changed files with 351 additions and 460 deletions

View File

@@ -1,6 +1,8 @@
// 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
pub use crate::message_handling::*;
use std::{
collections::{HashMap, VecDeque},
net::{AddrParseError, Ipv4Addr, SocketAddr},
@@ -11,7 +13,10 @@ use std::{
time::{self, Duration, SystemTime},
};
use crate::{NetworkEvent, threads_handling::Worker};
use crate::{
NetworkEvent, cryptographic_signature::CryptographicSignature,
messages_channels::MultipleSenders, threads_handling::Worker,
};
use crate::{
P2PSharedData, construct_message, generate_id, messages_structure,
registration::perform_handshake,
@@ -26,59 +31,35 @@ pub struct PeerInfo {
pub ip: SocketAddr,
}
#[derive(Debug, Clone)]
pub struct HandshakeHistory {
//time_k_ip_v: HashMap<u64, u64>,
username_k_peerinfo_v: HashMap<String, PeerInfo>,
ip_k_peerinfo_v: HashMap<String, PeerInfo>,
pub username_k_peerinfo_v: Arc<Mutex<HashMap<String, PeerInfo>>>,
ip_k_peerinfo_v: Arc<Mutex<HashMap<String, PeerInfo>>>,
}
impl HandshakeHistory {
pub fn new() -> HandshakeHistory {
HandshakeHistory {
//time_k_ip_v: HashMap::new(),
//ip_k_peerinfo_v: HashMap::new(),
username_k_peerinfo_v: HashMap::new(),
ip_k_peerinfo_v: HashMap::new(),
username_k_peerinfo_v: Arc::new(Mutex::new(HashMap::new())),
ip_k_peerinfo_v: Arc::new(Mutex::new(HashMap::new())),
}
}
/*pub fn update_handshake(&self) {
let hashmap_shared = Arc::new(self.username_k_peerinfo_v);
thread::spawn(move || {
let selfhashmap = hashmap_shared.clone();
loop {
for peer in selfhashmap.keys() {
let peer_ip = selfhashmap.get(peer);
// send ping
}
let mut child = Command::new("sleep").arg("10").spawn().unwrap();
let _result = child.wait().unwrap();
}
});
}*/
pub fn get_peer_info_username(&self, username: String) -> Option<PeerInfo> {
//self.username_k_peerinfo_v.get(&username).clone()
pub fn get_peer_info_username(&self, username: String) -> Option<&PeerInfo> {
self.username_k_peerinfo_v.get(&username).clone()
let guard = self.username_k_peerinfo_v.lock().unwrap();
guard.get(&username).cloned()
}
pub fn get_peer_info_ip(&self, ip: String) -> Option<&PeerInfo> {
self.ip_k_peerinfo_v.get(&ip).clone()
pub fn get_peer_info_ip(&self, ip: String) -> Option<PeerInfo> {
let guard = self.ip_k_peerinfo_v.lock().unwrap();
guard.get(&ip).cloned()
}
pub fn update_handshake(&self) -> Worker {
let map_clone: Arc<HashMap<String, PeerInfo>> =
Arc::new(self.username_k_peerinfo_v.clone());
let map_for_thread = Arc::clone(&map_clone);
let handle = thread::spawn(move || {
loop {
for (peer, peerinfo) in map_for_thread.iter() {}
thread::sleep(Duration::from_secs(10));
}
});
Worker::spawn(handle, crate::threads_handling::WorkerType::PING)
}
pub fn update_peer_info(&mut self, ip: String, username: String) {
pub fn update_peer_info(&self, ip: String, username: String) {
let peerinfo = self.get_peer_info_ip(ip.clone());
match peerinfo {
Some(peer_info) => match ip.parse::<SocketAddr>() {
@@ -88,8 +69,18 @@ impl HandshakeHistory {
pubkey: peer_info.pubkey,
ip: addr,
};
self.ip_k_peerinfo_v.insert(ip, new_peer_info.clone());
self.username_k_peerinfo_v.insert(username, new_peer_info);
let mut guardb = self.ip_k_peerinfo_v.lock().unwrap();
guardb.insert(ip.to_string(), new_peer_info.clone());
let mut guard = self.username_k_peerinfo_v.lock().unwrap();
guard.insert(username.to_string(), new_peer_info);
println!(
"handshake added: {}, {}, {}",
username.to_string(),
ip.to_string(),
guard.len(),
);
}
Err(e) => eprintln!("parse error: {}", e),
},
@@ -99,43 +90,56 @@ impl HandshakeHistory {
}
}
pub fn add_new_handshake(&mut self, hash: VerifyingKey, username: String, ip: SocketAddr) {
pub fn add_new_handshake(&self, hash: VerifyingKey, username: String, ip: SocketAddr) {
let peerinfo = PeerInfo {
username: username.clone(),
pubkey: hash,
ip,
};
self.username_k_peerinfo_v
.insert(username, peerinfo.clone());
self.ip_k_peerinfo_v
.insert(ip.to_string(), peerinfo.clone());
let mut guard = self.username_k_peerinfo_v.lock().unwrap();
guard.insert(username, peerinfo.clone());
let mut guardb = self.ip_k_peerinfo_v.lock().unwrap();
guardb.insert(ip.to_string(), peerinfo.clone());
}
}
pub fn perform_discover(
username: String,
hash: String,
sd: &P2PSharedData,
server_ip: String,
event_tx: Sender<NetworkEvent>,
) {
// first, sends handshake
if hash == "root" {
perform_handshake(sd, username, server_ip, event_tx, false);
/*if let Some(data) = construct_message(
messages_structure::ROOTREQUEST,
Vec::new(),
generate_id(),
sd.cryptopair_ref(),
) {
if let Some(peerinfo) = sd.handshake_ref() {
sd.senders_ref()
.send_via(0, data, peerinfo.ip.to_string(), false);
pub fn update_handshake(
senders: Arc<MultipleSenders>,
crypto_pair: Arc<CryptographicSignature>,
messages_list: Arc<Mutex<HashMap<i32, EventType>>>,
username_k_peerinfo_v: Arc<Mutex<HashMap<String, PeerInfo>>>,
) -> Worker {
let map_for_thread = username_k_peerinfo_v.clone();
let handle = thread::spawn(move || {
loop {
println!("loop boucle");
let guard = map_for_thread.lock().unwrap();
println!("len:{}", guard.len());
for (peer, peerinfo) in guard.iter() {
let id = generate_id();
let mut map = messages_list.lock().unwrap();
map.insert(id, EventType::Ping);
drop(map);
let pingrequest = construct_message(PING, Vec::new(), id, &crypto_pair);
if let Some(ping) = pingrequest {
senders.add_message_to_retry_queue(
ping.clone(),
peerinfo.ip.to_string(),
false,
);
senders.send_dispatch(
ping,
peerinfo.ip.to_string(),
false,
messages_list.clone(),
);
println!("ping envoye a {}", peer);
}
}
}*/
} else {
// envoyer un datum request
}
thread::sleep(Duration::from_secs(2));
}
});
Worker::spawn(handle, crate::threads_handling::WorkerType::PING)
}
#[cfg(test)]