diff options
author | hozan23 <hozan23@proton.me> | 2023-11-29 00:15:10 +0300 |
---|---|---|
committer | hozan23 <hozan23@proton.me> | 2023-11-29 00:15:10 +0300 |
commit | 2b032229e46293af92db798a36793c6b8b97baee (patch) | |
tree | 7b1304c952ff34604e9114d9d15c4687775c714b /p2p/examples | |
parent | 21e76cf87153c038909d95ff40d982b70003e2fa (diff) |
p2p/protocol: improve the Protocol API
Diffstat (limited to 'p2p/examples')
-rw-r--r-- | p2p/examples/chat.rs | 12 |
1 files changed, 8 insertions, 4 deletions
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<Executor<'static>>, } impl ChatProtocol { - fn new(username: &str, peer: ArcPeer) -> ArcProtocol { + fn new(username: &str, peer: ArcPeer, executor: Arc<Executor<'static>>) -> ArcProtocol { Arc::new(Self { peer, username: username.to_string(), + executor, }) } } #[async_trait] impl Protocol for ChatProtocol { - async fn start(self: Arc<Self>, ex: Arc<Executor<'_>>) -> Result<(), P2pError> { + async fn start(self: Arc<Self>) -> 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::<ChatProtocol>(c).await.unwrap(); // Run the backend |