This commit is contained in:
Tiago Batista Cardoso
2025-11-30 14:49:16 +01:00
parent 85e78447a7
commit 721d52a028
5 changed files with 111 additions and 45 deletions

View File

@@ -1,5 +1,4 @@
use rand::{Rng, rng};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::hash::{DefaultHasher, Hash, Hasher};
@@ -283,29 +282,24 @@ impl MerkleNode {
pub fn serialize(&self) -> Vec<u8> {
let mut bytes = Vec::new();
// 1. Add the type byte
bytes.push(self.get_type_byte());
// 2. Add the node-specific data
match self {
MerkleNode::Chunk(node) => {
bytes.extend_from_slice(&node.data);
}
MerkleNode::Directory(node) => {
// The data is the sequence of directory entries
for entry in &node.entries {
bytes.extend_from_slice(&entry.filename);
bytes.extend_from_slice(&entry.content_hash);
}
}
MerkleNode::Big(node) => {
// The data is the list of child hashes
for hash in &node.children_hashes {
bytes.extend_from_slice(hash);
}
}
MerkleNode::BigDirectory(node) => {
// The data is the list of child hashes
for hash in &node.children_hashes {
bytes.extend_from_slice(hash);
}

View File

@@ -2,8 +2,12 @@ mod data;
/// Messages sent to the Network thread by the GUI.
pub enum NetworkCommand {
ConnectPeer(String), // e.g., IP:PORT
RequestFileTree(String), // e.g., peer_id
ConnectToServer(String), // ServerIP
FetchPeerList(String), // ServerIP
RegisterAsPeer(String),
Ping(),
ConnectPeer(String), // IP:PORT
RequestFileTree(String), // peer_id
RequestDirectoryContent(String, String),
RequestChunk(String, String),
// ...
@@ -11,19 +15,19 @@ pub enum NetworkCommand {
/// Messages sent to the GUI by the Network thread.
pub enum NetworkEvent {
Connected(),
Disconnected(),
Error(),
PeerConnected(String),
PeerListUpdated(Vec<String>),
FileTreeReceived(String, Vec<MerkleNode>), // peer_id, content
DataReceived(String, MerkleNode),
FileTreeRootReceived(String, String),
// ...
}
use crossbeam_channel::{Receiver, Sender};
pub use crate::data::*;
use crossbeam_channel::{Receiver, Sender};
use sha2::{Digest, Sha256};
pub fn calculate_chunk_id(data: &[u8]) -> String {
@@ -41,16 +45,12 @@ pub fn calculate_chunk_id(data: &[u8]) -> String {
hex::encode(hash_bytes)
}
pub fn start_p2p_executor(
cmd_rx: Receiver<NetworkCommand>,
event_tx: Sender<NetworkEvent>,
) -> 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.");
@@ -66,18 +66,40 @@ pub fn start_p2p_executor(
// Network logic to connect...
// If successful, send an event back:
// event_tx.send(NetworkEvent::PeerConnected(addr)).unwrap();
},
}
NetworkCommand::RequestFileTree(_) => {
println!("[Network] RequestFileTree() called");
},
// ... handle other commands
}
NetworkCommand::RequestDirectoryContent(_, _) => {
println!("[Network] RequestDirectoryContent() called");
},
}
NetworkCommand::RequestChunk(_, _) => {
println!("[Network] RequestChunk() called");
},
}
NetworkCommand::ConnectToServer(ip) => {
println!("[Network] ConnectToServer() called");
// Actual server connection
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");
}
NetworkCommand::RegisterAsPeer(_) => {
println!("[Network] RegisterAsPeer() called");
}
NetworkCommand::Ping() => {
println!("[Network] Ping() called");
}
}
}
@@ -90,4 +112,4 @@ pub fn start_p2p_executor(
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}
})
}
}

View File