Basic communication between two threads

This commit is contained in:
Horhik 2020-11-22 19:03:34 +02:00
parent ddcc5e48a8
commit 7803aeab02
4 changed files with 59 additions and 77 deletions

View file

@ -1,3 +1,4 @@
use crate::chat::types::PackedMessage;
use crate::db;
use crate::encrypting;
use async_std::{
@ -9,11 +10,15 @@ use async_tungstenite::{accept_async, tungstenite::Message, WebSocketStream};
use futures::{SinkExt, StreamExt};
use serde_derive::Deserialize;
use std::env;
pub fn listen_client(sync: std::sync::mpsc::Sender) -> io::Result<()> {
task::block_on(connect_to_client())
use std::sync::mpsc::Sender;
type SP = Sender<PackedMessage>;
pub fn listen_client(server_sender: SP) -> io::Result<()> {
task::block_on(connect_to_client(server_sender))
}
async fn connect_to_client() -> io::Result<()> {
async fn connect_to_client(server_sender: SP) -> io::Result<()> {
let addr = env::args()
.nth(1)
.unwrap_or_else(|| "127.0.0.1:8080".to_string());
@ -22,7 +27,8 @@ async fn connect_to_client() -> io::Result<()> {
println!("Debugging!");
while let Ok((stream, _)) = listener.accept().await {
task::spawn(accept_client(stream));
let ss = server_sender.clone();
task::spawn(accept_client(stream, ss));
}
println!("Debugging 2!");
@ -37,7 +43,7 @@ struct FrontMsg {
time: String,
}
async fn accept_client(stream: TcpStream) -> io::Result<()> {
async fn accept_client(stream: TcpStream, server_sender: SP) -> io::Result<()> {
let addr = stream
.peer_addr()
.expect("connected streams should have a peer address");
@ -59,6 +65,7 @@ async fn accept_client(stream: TcpStream) -> io::Result<()> {
serde_json::from_str(jsoned.to_text().unwrap());
if let Ok(received_msg) = res {
let msg = received_msg.message;
server_sender.send(PackedMessage { message: msg }).unwrap();
/* message example
{

View file

@ -1,2 +1,3 @@
pub mod chat_api_base;
pub mod front_conn;
pub mod types;

View file

@ -1,32 +1,36 @@
type Decoded = String;
type Encoded = String;
//type Decoded = String;
//type Encoded = String;
trait Handler<State> {
fn process(code: State) -> Message;
fn send(socket: &WebSocketStream<TcpStream>, msg: Message);
}
pub struct MessageServer {
new_message: bool,
text: String,
}
// trait Handler<State> {
// fn process(code: State) -> Message;
// fn send(socket: &WebSocketStream<TcpStream>, msg: Message);
//}
// pub struct MessageServer {
// new_message: bool,
// text: String,
// }
pub struct MessageClient {
message_queue: Vec<Message>,
}
// pub struct MessageClient {
// message_queue: Vec<Message>,
// }
impl MessageServer {
fn new() -> MessageServer {
MessageServer {
new_message: false,
text: String::from(""),
}
}
}
// impl MessageServer {
// fn new() -> MessageServer {
// MessageServer {
// new_message: false,
// text: String::from(""),
// }
// }
// }
impl MessageClient {
fn new() -> MessageClient {
MessageClient {
message_queue: vec![],
}
}
// impl MessageClient {
// fn new() -> MessageClient {
// MessageClient {
// message_queue: vec![],
// }
// }
//}
pub struct PackedMessage {
pub message: String,
}

View file

@ -4,6 +4,7 @@ mod db;
mod encrypting;
use async_std::io;
use chat::front_conn::listen_client;
use chat::types::PackedMessage;
use std::{
sync::{
@ -41,56 +42,25 @@ use std::{
+-----------------------------------------------------------------------------------+
*/
fn main() -> io::Result<()> {
// listen_client();
let (server_sender, server_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
mpsc::channel();
let (client_sender, client_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
mpsc::channel();
enum ClientMessage {
Message(String),
Nope,
}
struct MFA {
encoded_msg: String, // TODO add User field
}; //Message from above
let server_thread = thread::spawn(move || {
let cr = client_sender;
let sr = server_receiver;
struct ClientHandler {
message: ClientMessage,
}
struct ServerHandler {
messages: Vec<MFA>,
}
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
thread::spawn(listen_client(server_sender));
let client_listener = thread::spawn(move || {
let new_msg: MFA = client_receiver.recv().unwrap();
println!("{:?}", new_msg.encoded_msg);
println!("Multithreadding YAY!!! {}", sr.recv().unwrap().message);
});
let client_thread = thread::spawn(move || {
let ss = server_sender;
let cr = client_receiver;
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)]};
// 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!!"),
};
thread::spawn(|| listen_client(ss));
});
server_listener.join().expect("fail listening server");
client_listener.join().expect("fail listening client");
server_thread.join();
client_thread.join();
Ok(())
}
/*