very simple multithreading

This commit is contained in:
Horhik 2020-11-22 16:48:24 +02:00
parent 97568a29db
commit 4916f96345

View file

@ -19,54 +19,48 @@ fn main() -> io::Result<()> {
Message(String), Message(String),
Nope, Nope,
} }
struct MFA{ struct MFA {
encoded_msg: String encoded_msg: String, // TODO add User field
// TODO add User field
}; //Message from above }; //Message from above
struct ClientHandler { struct ClientHandler {
message: ClientMessage, message: ClientMessage,
} }
struct ServerHandler { struct ServerHandler {
messages: Vec<MFA> messages: Vec<MFA>,
} }
let (server_sender, server_receiver) : (Sender<ClientHandler>, Receiver<ClientHandler>) = mpsc::channel(); // server sender, server receiver let (server_sender, server_receiver): (Sender<MFA>, Receiver<MFA>) = mpsc::channel(); // server sender, server receiver
let (client_sender, client_receiver) : (Sender<ServerHandler>, Receiver<ServerHandler>) = mpsc::channel(); // client sender, client receiver let (client_sender, client_receiver): (Sender<MFA>, Receiver<MFA>) = mpsc::channel(); // client sender, client receiver
let client_listener = thread::spawn(move || { let client_listener = thread::spawn(move || {
let a = server_receiver.recv(); let new_msg: MFA = client_receiver.recv().unwrap();
let it_will_drop = match a.message { println!("{:?}", new_msg.encoded_msg);
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 server_listener = thread::spawn(move || loop { let server_listener = thread::spawn(move || loop {
let m1 = String::from("It's a encoded message from Jim"); 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 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() { // for msg in fromabove.messages.iter() {
let msg = |MFA(lol)| ; let msg = MFA { encoded_msg: m1 };
} client_sender.send(msg);
// }
let mut a: ClientHandler = ClientHandler { let mut a: ClientHandler = ClientHandler {
message: ClientMessage::Message(String::from("yup")), message: ClientMessage::Message(String::from("yup")),
} };
let it_will_drop = match a.message { let it_will_drop = match a.message {
ClientMessage::Message(m) => m, ClientMessage::Message(m) => m,
ClientMessage::Nope => String::from("fail!!"), ClientMessage::Nope => String::from("fail!!"),
}; };
}); });
server_listener.join().expect("fail listening server");
client_listener.join().expect("fail listening client");
Ok(()) Ok(())
} }
/* /*