Эх сурвалжийг харах

dchat: move render() into main to avoid mismatched lifetime error

lunar-mining 4 жил өмнө
parent
commit
1394a21d59

+ 0 - 1
Cargo.lock

@@ -1380,7 +1380,6 @@ dependencies = [
  "async-executor",
  "async-std",
  "darkfi",
- "dchat",
  "easy-parallel",
  "futures",
  "log",

+ 43 - 44
example/dchat/src/main.rs

@@ -5,9 +5,9 @@ use log::{error, info};
 use simplelog::WriteLogger;
 use std::{
     fs::File,
-    io::{self, Write},
+    io::{self, Read, Write},
 };
-use termion::{event::Key, input::TermRead};
+use termion::{async_stdin, event::Key, input::TermRead};
 use url::Url;
 
 use darkfi::{
@@ -27,15 +27,14 @@ struct Dchat {
 }
 
 impl Dchat {
-    fn new(p2p: net::P2pPtr) -> Self {
-        Self { p2p }
+    fn new(p2p: net::P2pPtr) -> Arc<Self> {
+        Arc::new(Self { p2p })
     }
 
     async fn render(&self, ex: Arc<Executor<'_>>) -> Result<()> {
         info!("DCHAT::render()::start");
         let mut stdout = io::stdout().lock();
-        let stdin = termion::async_stdin();
-        let mut keys = stdin.keys();
+        let mut stdin = async_stdin();
 
         stdout.write_all(
             b"Welcome to dchat
@@ -44,33 +43,33 @@ impl Dchat {
     q: quit \n",
         )?;
 
-        ex.spawn(async move {
-            loop {
-                let k = keys.next();
-                match k {
-                    Some(k) => match k {
-                        Ok(k) => match k {
-                            Key::Char('q') => {
-                                info!("DCHAT::Q pressed.... exiting");
-                                break
-                            }
-                            Key::Char('i') => {}
-                            Key::Char('s') => {
-                                // TODO
-                                //self.send().await?;
-                            }
-                            _ => {}
-                        },
-                        Err(e) => {
-                            error!("found error: {}", e);
-                        }
-                    },
-                    None => {}
+        loop {
+            for k in stdin.by_ref().keys() {
+                match k.unwrap() {
+                    Key::Char('q') => {
+                        info!("DCHAT::Q pressed.... exiting");
+                        return Ok(())
+                    }
+                    Key::Char('i') => {}
+
+                    Key::Char('s') => {
+                        let msg = self.get_input().await?;
+                        self.send(msg).await?;
+                    }
+                    _ => {}
                 }
             }
-        })
-        .detach();
-        Ok(())
+        }
+    }
+
+    async fn get_input(&self) -> Result<String> {
+        let mut stdout = io::stdout().lock();
+        stdout.write_all(b"type your message and then press enter\n")?;
+        let mut input = String::new();
+        io::stdin().read_line(&mut input)?;
+        stdout.write_all(b"you entered:")?;
+        stdout.write_all(input.as_bytes())?;
+        return Ok(input)
     }
 
     async fn register_protocol(&self) -> Result<()> {
@@ -85,28 +84,26 @@ impl Dchat {
         Ok(())
     }
 
-    async fn start(&self, executor: Arc<Executor<'_>>) -> Result<()> {
+    async fn start(&self, ex: Arc<Executor<'_>>) -> Result<()> {
         info!("DCHAT::start()::start");
+        let ex2 = ex.clone();
+
         let dchat = Dchat::new(self.p2p.clone());
 
         dchat.register_protocol().await?;
 
-        self.p2p.clone().start(executor.clone()).await?;
-        let executor_cloned = executor.clone();
-        executor_cloned.spawn(self.p2p.clone().run(executor.clone())).detach();
-
-        let result = dchat.render(executor.clone()).await;
-
-        if let Err(e) = result {
-            error!("Rendering failed {}", e);
-        };
+        self.p2p.clone().start(ex.clone()).await?;
+        ex2.spawn(self.p2p.clone().run(ex.clone())).detach();
 
         info!("DCHAT::start()::stop");
         Ok(())
     }
 
-    async fn _send(&self) -> Result<()> {
-        let dchatmsg = Dchatmsg { message: "helloworld".to_string() };
+    async fn send(&self, message: String) -> Result<()> {
+        let mut stdout = io::stdout().lock();
+        stdout.write_all(b"sending: ")?;
+        stdout.write_all(message.as_bytes())?;
+        let dchatmsg = Dchatmsg { message };
         self.p2p.broadcast(dchatmsg).await?;
         Ok(())
     }
@@ -148,6 +145,7 @@ async fn main() -> Result<()> {
 
     let ex = Arc::new(Executor::new());
     let ex2 = ex.clone();
+    let ex3 = ex.clone();
 
     let dchat = Dchat::new(p2p.clone());
 
@@ -155,7 +153,8 @@ async fn main() -> Result<()> {
         .each(0..nthreads, |_| smol::future::block_on(ex.run(shutdown.recv())))
         .finish(|| {
             smol::future::block_on(async move {
-                dchat.start(ex2).await?;
+                dchat.start(ex3).await?;
+                dchat.render(ex2).await?;
                 drop(signal);
                 Ok(())
             })

+ 5 - 5
example/dchat/src/protocol_dchat.rs

@@ -2,7 +2,7 @@ use async_executor::Executor;
 use async_std::sync::Arc;
 use async_trait::async_trait;
 use darkfi::{net, Result};
-use log::debug;
+use log::info;
 
 use crate::dchatmsg::Dchatmsg;
 
@@ -14,7 +14,7 @@ pub struct ProtocolDchat {
 
 impl ProtocolDchat {
     pub async fn init(channel: net::ChannelPtr, p2p: net::P2pPtr) -> net::ProtocolBasePtr {
-        debug!(target: "dchat", "ProtocolDchat::init() [START]");
+        info!(target: "dchat", "ProtocolDchat::init() [START]");
 
         let message_subsytem = channel.get_message_subsystem();
         message_subsytem.add_dispatch::<Dchatmsg>().await;
@@ -30,7 +30,7 @@ impl ProtocolDchat {
     }
 
     async fn handle_receive_msg(self: Arc<Self>) -> Result<()> {
-        debug!(target: "dchat", "ProtocolDchat::handle_receive_msg() [START]");
+        info!(target: "dchat", "ProtocolDchat::handle_receive_msg() [START]");
 
         loop {
             let msg = self.msg_sub.receive().await?;
@@ -47,10 +47,10 @@ impl ProtocolDchat {
 #[async_trait]
 impl net::ProtocolBase for ProtocolDchat {
     async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> Result<()> {
-        debug!(target: "dchat", "ProtocolDchat::ProtocolBase::start() [START]");
+        info!(target: "dchat", "ProtocolDchat::ProtocolBase::start() [START]");
         self.jobsman.clone().start(executor.clone());
         self.jobsman.clone().spawn(self.clone().handle_receive_msg(), executor.clone()).await;
-        debug!(target: "dchat", "ProtocolDchat::ProtocolBase::start() [STOP]");
+        info!(target: "dchat", "ProtocolDchat::ProtocolBase::start() [STOP]");
         Ok(())
     }