From 2b032229e46293af92db798a36793c6b8b97baee Mon Sep 17 00:00:00 2001 From: hozan23 Date: Wed, 29 Nov 2023 00:15:10 +0300 Subject: p2p/protocol: improve the Protocol API --- p2p/examples/chat.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'p2p/examples') diff --git a/p2p/examples/chat.rs b/p2p/examples/chat.rs index d94bca4..925c832 100644 --- a/p2p/examples/chat.rs +++ b/p2p/examples/chat.rs @@ -44,23 +44,25 @@ struct Cli { pub struct ChatProtocol { username: String, peer: ArcPeer, + executor: Arc>, } impl ChatProtocol { - fn new(username: &str, peer: ArcPeer) -> ArcProtocol { + fn new(username: &str, peer: ArcPeer, executor: Arc>) -> ArcProtocol { Arc::new(Self { peer, username: username.to_string(), + executor, }) } } #[async_trait] impl Protocol for ChatProtocol { - async fn start(self: Arc, ex: Arc>) -> Result<(), P2pError> { + async fn start(self: Arc) -> Result<(), P2pError> { let selfc = self.clone(); let stdin = io::stdin(); - let task = ex.spawn(async move { + let task = self.executor.spawn(async move { loop { let mut input = String::new(); stdin.read_line(&mut input).await.unwrap(); @@ -111,6 +113,7 @@ fn main() { peer_endpoints: cli.peer_endpoints, bootstrap_peers: cli.bootstrap_peers, discovery_port: cli.discovery_port.unwrap_or(0), + enable_tls: true, ..Default::default() }; @@ -124,12 +127,13 @@ fn main() { let handle = move || ctrlc_s.try_send(()).unwrap(); ctrlc::set_handler(handle).unwrap(); + let ex_cloned = ex.clone(); run_executor( async { let username = cli.username; // Attach the ChatProtocol - let c = move |peer| ChatProtocol::new(&username, peer); + let c = move |peer| ChatProtocol::new(&username, peer, ex_cloned.clone()); backend.attach_protocol::(c).await.unwrap(); // Run the backend -- cgit v1.2.3