diff --git a/client-gui/src/gui_app.rs b/client-gui/src/gui_app.rs index 3f79c78..6559dc9 100644 --- a/client-gui/src/gui_app.rs +++ b/client-gui/src/gui_app.rs @@ -22,7 +22,6 @@ enum ServerStatus { pub struct P2PClientApp { remaining: std::time::Duration, // temps restant last_update: std::time::Instant, // pour calculer delta - timer_started: bool, network_cmd_tx: Sender, network_event_rx: Receiver, @@ -65,7 +64,6 @@ impl P2PClientApp { Self { remaining: std::time::Duration::from_secs(0), - timer_started: false, last_update: std::time::Instant::now(), network_cmd_tx: cmd_tx, network_event_rx: event_rx, @@ -106,10 +104,9 @@ impl P2PClientApp { impl eframe::App for P2PClientApp { fn update(&mut self, ctx: &Context, _frame: &mut eframe::Frame) { - if matches!(self.server_status, ServerStatus::Connected) && !self.timer_started { + if matches!(self.server_status, ServerStatus::Connected) { self.remaining = std::time::Duration::from_secs(30 * 60); self.last_update = std::time::Instant::now(); - self.timer_started = true; } let now = std::time::Instant::now(); @@ -437,18 +434,27 @@ impl eframe::App for P2PClientApp { if self.show_network_window { match self.server_status { ServerStatus::Connected | ServerStatus::ConnectedHandshake => { - let desired = egui::vec2(300.0, 0.0); // width 300, auto-height if 0 - ui.set_min_size(desired); - ui.vertical(|ui| { - if ui.button("Disconnect").clicked() { - println!("Disconnecting..."); - let _ = self.network_cmd_tx.send(NetworkCommand::Disconnect()); - self.server_status = ServerStatus::NotConnected; - self.remaining = std::time::Duration::from_secs(0); - self.timer_started = false; - self.show_network_window = false; - } - }); + egui::Window::new("Network") + .resizable(false) + .collapsible(false) + .title_bar(false) + .show(ctx, |ui| { + let desired = egui::vec2(300.0, 0.0); // width 300, auto-height if 0 + ui.set_min_size(desired); + ui.vertical(|ui| { + if ui.button("Disconnect").clicked() { + println!("Disconnecting..."); + let _ = self + .network_cmd_tx + .send(NetworkCommand::Disconnect()); + self.server_status = ServerStatus::NotConnected; + self.remaining = std::time::Duration::from_secs(0); + self.show_network_window = false; + self.loaded_fs.clear(); + self.active_peer = None; + } + }); + }); } ServerStatus::NotConnected => { egui::Window::new("Network") diff --git a/client-network/src/data.rs b/client-network/src/data.rs index 43745e4..a6bb9cb 100644 --- a/client-network/src/data.rs +++ b/client-network/src/data.rs @@ -62,18 +62,12 @@ impl ChunkNode { pub fn new_random() -> Self { let mut rng = rand::rng(); - // Determine a random length between 1 and MAX_CHUNK_DATA_SIZE (inclusive). - // Using +1 ensures the range is up to 1024. let random_len = rng.random_range(1..=MAX_CHUNK_DATA_SIZE); - // Initialize a vector with the random length let mut data = vec![0u8; random_len]; - // Fill the vector with random bytes rng.fill(&mut data[..]); - // Since we generated the length based on MAX_CHUNK_DATA_SIZE, - // this is guaranteed to be valid and doesn't need to return a Result. ChunkNode { data } } } @@ -188,17 +182,6 @@ fn hash(data: &[u8]) -> NodeHash { println!("root hash: {:?}", root_hash); let res: NodeHash = root_hash.try_into().expect("incorrect size"); res - /*let mut hasher = DefaultHasher::new(); - data.hash(&mut hasher); - let hash_u64 = hasher.finish(); - - let mut hash_array = [0u8; FILENAME_HASH_SIZE]; - // Simple way to spread a 64-bit hash across 32 bytes for a unique-ish ID - for i in 0..8 { - hash_array[i] = (hash_u64 >> (i * 8)) as u8; - } - hash_array // The rest remains 0, satisfying the 32-byte requirement - */ } fn generate_random_filename() -> [u8; FILENAME_HASH_SIZE] { diff --git a/client-network/src/datum_parsing.rs b/client-network/src/datum_parsing.rs index 6b4bbfc..295667e 100644 --- a/client-network/src/datum_parsing.rs +++ b/client-network/src/datum_parsing.rs @@ -13,7 +13,6 @@ pub fn parse_received_datum( let hash_name: [u8; 32] = recevied_datum[..32].try_into().expect("error"); let value = &recevied_datum[32..datum_length]; let value_slice = value.to_vec(); - // println!("valueslice: {:?}, {}", value_slice, value_slice.len()); println!( "((value_slice.len() - 1) / 32) {} ", diff --git a/client-network/src/lib.rs b/client-network/src/lib.rs index c94fb55..15f369e 100644 --- a/client-network/src/lib.rs +++ b/client-network/src/lib.rs @@ -218,17 +218,12 @@ use crossbeam_channel::{Receiver, Sender}; use sha2::{Digest, Sha256}; pub fn calculate_chunk_id(data: &[u8]) -> String { - // 1. Create a new Sha256 hasher instance let mut hasher = Sha256::new(); - // 2. Write the input data into the hasher hasher.update(data); - // 3. Finalize the hash computation and get the resulting bytes let hash_bytes = hasher.finalize(); - // 4. Convert the hash bytes (array of u8) into a hexadecimal string - // This is the common, human-readable format for cryptographic IDs. hex::encode(hash_bytes) } @@ -237,15 +232,11 @@ pub fn start_p2p_executor( event_tx: Sender, mut shared_data: Option, ) -> tokio::task::JoinHandle<()> { - // Use tokio to spawn the asynchronous networking logic tokio::task::spawn(async move { - // P2P/Networking Setup goes here - println!("Network executor started."); // Main network loop loop { - // Check for commands from the GUI if let Ok(cmd) = cmd_rx.try_recv() { match cmd { NetworkCommand::InitDownload(hash, ip, name) => { @@ -384,9 +375,6 @@ pub fn start_p2p_executor( NetworkCommand::ConnectPeer((username, _)) => { println!("[Network] ConnectPeer() called"); println!("[Network] Attempting to connect to: {}", username); - // Network logic to connect... - // If successful, send an event back: - // event_tx.send(NetworkEvent::PeerConnected(addr)).unwrap(); } NetworkCommand::RequestFileTree(_) => { println!("[Network] RequestFileTree() called"); @@ -549,18 +537,7 @@ pub fn start_p2p_executor( }; println!("username created: {}", sd.cryptopair().username); } - //println!("ip: {}", ip); } - - //tokio::time::sleep(std::time::Duration::from_millis(5000)).await; - - /*let res = event_tx.send(NetworkEvent::Connected()); - if let Some(error) = res.err() { - println!( - "[Network] Couldn't send crossbeam message to GUI: {}", - error.to_string() - ); - }*/ } NetworkCommand::FetchPeerList(ip) => { println!("[Network] FetchPeerList() called"); @@ -716,12 +693,6 @@ pub fn start_p2p_executor( } } - // 2. Poll network for new events (e.g., an incoming connection) - // ... - // When a new peer is found: - // event_tx.send(NetworkEvent::PeerConnected("NewPeerID".to_string())).unwrap(); - - // Avoid spinning too fast sleep(std::time::Duration::from_millis(50)).await; } }) @@ -762,7 +733,6 @@ async fn quick_ping(addr: &SocketAddr, timeout_ms: u64, sd: &P2PSharedData) -> b /// /// sends a get request to the server to get the socket address of the given peer /// - pub async fn get_socket_address( username: String, ip: String, diff --git a/client-network/src/message_handling.rs b/client-network/src/message_handling.rs index 6a61542..afb64f5 100644 --- a/client-network/src/message_handling.rs +++ b/client-network/src/message_handling.rs @@ -266,10 +266,6 @@ pub fn parse_message( let ilength = u16::from_be_bytes(length_bytes); let received_address = &received_message[LENGTH..LENGTH + ilength as usize]; - println!("received_address:{:?}", received_message); - //let addressv4 = IpAddr::V4(Ipv4Addr::from_octets( - // received_address[0..4].try_into().expect("incorrect size"), - //)); let bytes: [u8; 4] = received_address[0..4].try_into().expect("incorrect size"); let addr_v4 = Ipv4Addr::from(bytes); let addressv4 = IpAddr::V4(addr_v4); diff --git a/client-network/src/registration.rs b/client-network/src/registration.rs index cde6748..388f744 100644 --- a/client-network/src/registration.rs +++ b/client-network/src/registration.rs @@ -50,7 +50,6 @@ pub fn parse_addresses(input: &String) -> Vec { /// /// registers the IP addresses by sending a Hello request to the server. /// - pub async fn perform_handshake( sd: &P2PSharedData, username: String,