very simple multithreading
This commit is contained in:
parent
97568a29db
commit
4916f96345
34
src/main.rs
34
src/main.rs
|
@ -20,53 +20,47 @@ fn main() -> io::Result<()> {
|
|||
Nope,
|
||||
}
|
||||
struct MFA {
|
||||
encoded_msg: String
|
||||
// TODO add User field
|
||||
encoded_msg: String, // TODO add User field
|
||||
}; //Message from above
|
||||
|
||||
|
||||
struct ClientHandler {
|
||||
message: ClientMessage,
|
||||
}
|
||||
|
||||
struct ServerHandler {
|
||||
messages: Vec<MFA>
|
||||
messages: Vec<MFA>,
|
||||
}
|
||||
|
||||
let (server_sender, server_receiver) : (Sender<ClientHandler>, Receiver<ClientHandler>) = mpsc::channel(); // server sender, server receiver
|
||||
let (client_sender, client_receiver) : (Sender<ServerHandler>, Receiver<ServerHandler>) = mpsc::channel(); // client sender, client receiver
|
||||
let (server_sender, server_receiver): (Sender<MFA>, Receiver<MFA>) = mpsc::channel(); // server sender, server receiver
|
||||
let (client_sender, client_receiver): (Sender<MFA>, Receiver<MFA>) = 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(())
|
||||
}
|
||||
/*
|
||||
|
|
Loading…
Reference in a new issue