aboutsummaryrefslogtreecommitdiff
path: root/p2p/examples/chat.rs
diff options
context:
space:
mode:
authorhozan23 <hozan23@karyontech.net>2024-06-30 01:13:45 +0200
committerhozan23 <hozan23@karyontech.net>2024-06-30 01:13:45 +0200
commit4dc6ae61c66d2ecedb3dbd519dde89e8afc727a9 (patch)
treed5ecff0aed228119f3cb6e21f91f4e749456f13b /p2p/examples/chat.rs
parent5c0abab1b7bf0f2858c451d6f0efc7ca0e138fc6 (diff)
p2p: remove async-std from dev dependencies & clean up examples
Diffstat (limited to 'p2p/examples/chat.rs')
-rw-r--r--p2p/examples/chat.rs24
1 files changed, 12 insertions, 12 deletions
diff --git a/p2p/examples/chat.rs b/p2p/examples/chat.rs
index 1ad215c..5867c8b 100644
--- a/p2p/examples/chat.rs
+++ b/p2p/examples/chat.rs
@@ -2,7 +2,6 @@ mod shared;
use std::sync::Arc;
-use async_std::io;
use async_trait::async_trait;
use clap::Parser;
use smol::{channel, Executor};
@@ -14,7 +13,7 @@ use karyon_p2p::{
Backend, Config, Error, Peer, Version,
};
-use shared::run_executor;
+use shared::{read_line_async, run_executor};
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
@@ -59,13 +58,11 @@ impl ChatProtocol {
#[async_trait]
impl Protocol for ChatProtocol {
async fn start(self: Arc<Self>) -> Result<(), Error> {
- let stdin = io::stdin();
let task = self.executor.spawn({
let this = self.clone();
async move {
loop {
- let mut input = String::new();
- stdin.read_line(&mut input).await.unwrap();
+ let input = read_line_async().await.expect("Read line from stdin");
let msg = format!("> {}: {}", this.username, input.trim());
this.peer.broadcast(&Self::id(), &msg).await;
}
@@ -74,11 +71,11 @@ impl Protocol for ChatProtocol {
let listener = self.peer.register_listener::<Self>().await;
loop {
- let event = listener.recv().await.unwrap();
+ let event = listener.recv().await.expect("Receive new protocol event");
match event {
ProtocolEvent::Message(msg) => {
- let msg = String::from_utf8(msg).unwrap();
+ let msg = String::from_utf8(msg).expect("Convert received bytes to string");
println!("{msg}");
}
ProtocolEvent::Shutdown => {
@@ -125,8 +122,8 @@ fn main() {
let backend = Backend::new(&key_pair, config, ex.clone().into());
let (ctrlc_s, ctrlc_r) = channel::unbounded();
- let handle = move || ctrlc_s.try_send(()).unwrap();
- ctrlc::set_handler(handle).unwrap();
+ let handle = move || ctrlc_s.try_send(()).expect("Send ctrlc signal");
+ ctrlc::set_handler(handle).expect("ctrlc set handler");
run_executor(
{
@@ -136,13 +133,16 @@ fn main() {
// Attach the ChatProtocol
let c = move |peer| ChatProtocol::new(&username, peer, ex.clone().into());
- backend.attach_protocol::<ChatProtocol>(c).await.unwrap();
+ backend
+ .attach_protocol::<ChatProtocol>(c)
+ .await
+ .expect("Attach chat protocol to the p2p backend");
// Run the backend
- backend.run().await.unwrap();
+ backend.run().await.expect("Run the backend");
// Wait for ctrlc signal
- ctrlc_r.recv().await.unwrap();
+ ctrlc_r.recv().await.expect("Receive ctrlc signal");
// Shutdown the backend
backend.shutdown().await;