Skip to content

Commit

Permalink
Update network.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 11, 2024
1 parent 637b346 commit 2a75d73
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pi-nexus-blockchain/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// network.rs (new)
// network.rs (update)

use crate::blockchain::Blockchain;
use tokio::net::TcpListener;
Expand All @@ -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"),
}
});
}
}
Expand Down

0 comments on commit 2a75d73

Please sign in to comment.