From 4916f963456f0f90b834006bf55985b4c56f7048 Mon Sep 17 00:00:00 2001 From: Horhik Date: Sun, 22 Nov 2020 16:48:24 +0200 Subject: [PATCH] very simple multithreading --- src/main.rs | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6c9fe72..eebffcd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,54 +19,48 @@ fn main() -> io::Result<()> { Message(String), Nope, } - struct MFA{ - encoded_msg: String - // TODO add User field + struct MFA { + encoded_msg: String, // TODO add User field }; //Message from above - struct ClientHandler { message: ClientMessage, } struct ServerHandler { - messages: Vec + messages: Vec, } - let (server_sender, server_receiver) : (Sender, Receiver) = mpsc::channel(); // server sender, server receiver - let (client_sender, client_receiver) : (Sender, Receiver) = mpsc::channel(); // client sender, client receiver + let (server_sender, server_receiver): (Sender, Receiver) = mpsc::channel(); // server sender, server receiver + let (client_sender, client_receiver): (Sender, Receiver) = mpsc::channel(); // client sender, client receiver let client_listener = thread::spawn(move || { - let a = server_receiver.recv(); - let it_will_drop = match a.message { - ClientMessage::Message(m) => { - println!("{}", &m); - m - }, - ClientMessage::Nope => String::from("fail!!"), - }; - server_sender.send(ClientHandler{message: ClientMessage::Message(String::from("I come from frontend"))}); + let new_msg: MFA = client_receiver.recv().unwrap(); + println!("{:?}", new_msg.encoded_msg); }); let server_listener = thread::spawn(move || loop { let m1 = String::from("It's a encoded message from Jim"); let m2 = String::from("It's a encoded message from one killer, who trying to find you"); - let mut fromabove = ServerHandler{messages: vec![MFA(m1), MFA(m2)]}; + // let mut fromabove = ServerHandler{messages: vec![MFA(m1), MFA(m2)]}; - for msg in fromabove.messages.iter() { - let msg = |MFA(lol)| ; + // for msg in fromabove.messages.iter() { + let msg = MFA { encoded_msg: m1 }; - } + client_sender.send(msg); + // } let mut a: ClientHandler = ClientHandler { message: ClientMessage::Message(String::from("yup")), - } + }; let it_will_drop = match a.message { ClientMessage::Message(m) => m, ClientMessage::Nope => String::from("fail!!"), }; }); + server_listener.join().expect("fail listening server"); + client_listener.join().expect("fail listening client"); Ok(()) } /*