Basic communication between two threads
This commit is contained in:
parent
ddcc5e48a8
commit
7803aeab02
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::chat::types::PackedMessage;
|
||||||
use crate::db;
|
use crate::db;
|
||||||
use crate::encrypting;
|
use crate::encrypting;
|
||||||
use async_std::{
|
use async_std::{
|
||||||
|
@ -9,11 +10,15 @@ use async_tungstenite::{accept_async, tungstenite::Message, WebSocketStream};
|
||||||
use futures::{SinkExt, StreamExt};
|
use futures::{SinkExt, StreamExt};
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
use std::env;
|
use std::env;
|
||||||
pub fn listen_client(sync: std::sync::mpsc::Sender) -> io::Result<()> {
|
use std::sync::mpsc::Sender;
|
||||||
task::block_on(connect_to_client())
|
|
||||||
|
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()
|
let addr = env::args()
|
||||||
.nth(1)
|
.nth(1)
|
||||||
.unwrap_or_else(|| "127.0.0.1:8080".to_string());
|
.unwrap_or_else(|| "127.0.0.1:8080".to_string());
|
||||||
|
@ -22,7 +27,8 @@ async fn connect_to_client() -> io::Result<()> {
|
||||||
|
|
||||||
println!("Debugging!");
|
println!("Debugging!");
|
||||||
while let Ok((stream, _)) = listener.accept().await {
|
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!");
|
println!("Debugging 2!");
|
||||||
|
|
||||||
|
@ -37,7 +43,7 @@ struct FrontMsg {
|
||||||
time: String,
|
time: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn accept_client(stream: TcpStream) -> io::Result<()> {
|
async fn accept_client(stream: TcpStream, server_sender: SP) -> io::Result<()> {
|
||||||
let addr = stream
|
let addr = stream
|
||||||
.peer_addr()
|
.peer_addr()
|
||||||
.expect("connected streams should have a peer address");
|
.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());
|
serde_json::from_str(jsoned.to_text().unwrap());
|
||||||
if let Ok(received_msg) = res {
|
if let Ok(received_msg) = res {
|
||||||
let msg = received_msg.message;
|
let msg = received_msg.message;
|
||||||
|
server_sender.send(PackedMessage { message: msg }).unwrap();
|
||||||
|
|
||||||
/* message example
|
/* message example
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
pub mod chat_api_base;
|
pub mod chat_api_base;
|
||||||
pub mod front_conn;
|
pub mod front_conn;
|
||||||
|
pub mod types;
|
||||||
|
|
|
@ -1,32 +1,36 @@
|
||||||
type Decoded = String;
|
//type Decoded = String;
|
||||||
type Encoded = String;
|
//type Encoded = String;
|
||||||
|
|
||||||
trait Handler<State> {
|
// trait Handler<State> {
|
||||||
fn process(code: State) -> Message;
|
// fn process(code: State) -> Message;
|
||||||
fn send(socket: &WebSocketStream<TcpStream>, msg: Message);
|
// fn send(socket: &WebSocketStream<TcpStream>, msg: Message);
|
||||||
}
|
//}
|
||||||
pub struct MessageServer {
|
// pub struct MessageServer {
|
||||||
new_message: bool,
|
// new_message: bool,
|
||||||
text: String,
|
// text: String,
|
||||||
}
|
// }
|
||||||
|
|
||||||
pub struct MessageClient {
|
// pub struct MessageClient {
|
||||||
message_queue: Vec<Message>,
|
// message_queue: Vec<Message>,
|
||||||
}
|
// }
|
||||||
|
|
||||||
impl MessageServer {
|
// impl MessageServer {
|
||||||
fn new() -> MessageServer {
|
// fn new() -> MessageServer {
|
||||||
MessageServer {
|
// MessageServer {
|
||||||
new_message: false,
|
// new_message: false,
|
||||||
text: String::from(""),
|
// text: String::from(""),
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
impl MessageClient {
|
// impl MessageClient {
|
||||||
fn new() -> MessageClient {
|
// fn new() -> MessageClient {
|
||||||
MessageClient {
|
// MessageClient {
|
||||||
message_queue: vec![],
|
// message_queue: vec![],
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
pub struct PackedMessage {
|
||||||
|
pub message: String,
|
||||||
}
|
}
|
||||||
|
|
60
src/main.rs
60
src/main.rs
|
@ -4,6 +4,7 @@ mod db;
|
||||||
mod encrypting;
|
mod encrypting;
|
||||||
use async_std::io;
|
use async_std::io;
|
||||||
use chat::front_conn::listen_client;
|
use chat::front_conn::listen_client;
|
||||||
|
use chat::types::PackedMessage;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
sync::{
|
sync::{
|
||||||
|
@ -41,56 +42,25 @@ use std::{
|
||||||
+-----------------------------------------------------------------------------------+
|
+-----------------------------------------------------------------------------------+
|
||||||
*/
|
*/
|
||||||
fn main() -> io::Result<()> {
|
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 {
|
let server_thread = thread::spawn(move || {
|
||||||
Message(String),
|
let cr = client_sender;
|
||||||
Nope,
|
let sr = server_receiver;
|
||||||
}
|
|
||||||
struct MFA {
|
|
||||||
encoded_msg: String, // TODO add User field
|
|
||||||
}; //Message from above
|
|
||||||
|
|
||||||
struct ClientHandler {
|
println!("Multithreadding YAY!!! {}", sr.recv().unwrap().message);
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
|
let client_thread = thread::spawn(move || {
|
||||||
|
let ss = server_sender;
|
||||||
|
let cr = client_receiver;
|
||||||
|
|
||||||
let server_listener = thread::spawn(move || loop {
|
thread::spawn(|| listen_client(ss));
|
||||||
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!!"),
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
server_listener.join().expect("fail listening server");
|
server_thread.join();
|
||||||
client_listener.join().expect("fail listening client");
|
client_thread.join();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in a new issue