From 2a75d73827586e392185c899af536a25ddce5d6e Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sun, 11 Aug 2024 14:52:54 +0700 Subject: [PATCH] Update network.rs --- pi-nexus-blockchain/src/network.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) 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"), + } }); } }