diff --git a/pi-nexus-blockchain/src/network.rs b/pi-nexus-blockchain/src/network.rs index 8871f094e..500044fef 100644 --- a/pi-nexus-blockchain/src/network.rs +++ b/pi-nexus-blockchain/src/network.rs @@ -1,4 +1,4 @@ -// network.rs (new) +// network.rs (update) use crate::blockchain::Blockchain; use tokio::net::TcpListener; @@ -19,8 +19,17 @@ impl Node { println!("Node started on {}", self.listener.local_addr().unwrap()); while let Ok((mut socket, _)) = self.listener.accept().await { tokio::spawn(async move { - // TO DO: implement handle incoming connection logic - println!("Incoming connection"); + let mut buf = [0; 1024]; + let n = socket.read(&mut buf).await.unwrap(); + let message = String::from_utf8_lossy(&buf[..n]); + match message.as_str() { + "get_chain" => { + let chain = self.blockchain.chain.clone(); + let response = serde_json::to_string(&chain).unwrap(); + socket.write(response.as_bytes()).await.unwrap(); + } + _ => println!("Invalid message"), + } }); } }