start working with multithreading
This commit is contained in:
parent
605319e158
commit
97568a29db
89
src/chat/#front_conn.rs#
Normal file
89
src/chat/#front_conn.rs#
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
use crate::encrypting;
|
||||||
|
use crate::db;
|
||||||
|
use async_std::{
|
||||||
|
io,
|
||||||
|
net::{TcpListener, TcpStream},
|
||||||
|
task,
|
||||||
|
};
|
||||||
|
use async_tungstenite::{accept_async, tungstenite::Message, WebSocketStream};
|
||||||
|
use futures::{SinkExt, StreamExt};
|
||||||
|
use serde_derive::Deserialize;
|
||||||
|
use std::env;
|
||||||
|
pub fn listen_client() -> io::Result<()> {
|
||||||
|
task::block_on(connect_to_client())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn connect_to_client() -> io::Result<()> {
|
||||||
|
let addr = env::args()
|
||||||
|
.nth(1)
|
||||||
|
.unwrap_or_else(|| "127.0.0.1:8080".to_string());
|
||||||
|
|
||||||
|
let listener = TcpListener::bind(&addr).await?;
|
||||||
|
|
||||||
|
println!("Debugging!");
|
||||||
|
while let Ok((stream, _)) = listener.accept().await {
|
||||||
|
task::spawn(accept_client(stream));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
struct FrontMsg {
|
||||||
|
userID: u32,
|
||||||
|
receiverID: u32,
|
||||||
|
message: String,
|
||||||
|
time: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn accept_client(stream: TcpStream) -> io::Result<()> {
|
||||||
|
let addr = stream
|
||||||
|
.peer_addr()
|
||||||
|
.expect("connected streams should have a peer address");
|
||||||
|
println!("Peer address: {}", addr);
|
||||||
|
|
||||||
|
let ws = accept_async(stream)
|
||||||
|
.await
|
||||||
|
.expect("err during the ws handshake");
|
||||||
|
|
||||||
|
println!("connected to: {}", addr);
|
||||||
|
|
||||||
|
let (mut sender, mut receiver) = ws.split();
|
||||||
|
let mut new_msg = receiver.next();
|
||||||
|
loop {
|
||||||
|
match new_msg.await {
|
||||||
|
Some(msg) => {
|
||||||
|
let jsoned = msg.unwrap();
|
||||||
|
let res: serde_json::Result<FrontMsg> =
|
||||||
|
serde_json::from_str(jsoned.to_text().unwrap());
|
||||||
|
if let Ok(received_msg) = res {
|
||||||
|
let id = received_msg.receiverID;
|
||||||
|
|
||||||
|
/* message example
|
||||||
|
{
|
||||||
|
"userID": 123456789,
|
||||||
|
"receiverID": 123456789,
|
||||||
|
"message": "hey dude",
|
||||||
|
"time": "Tue Oct 13 2020 18:31:22 GMT+0300 (Eastern European Summer Time)",
|
||||||
|
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
sender
|
||||||
|
.send(Message::Text(String::from("Sended").to_owned()))
|
||||||
|
.await
|
||||||
|
.expect("ooops");
|
||||||
|
} else {
|
||||||
|
println!("seems, that messsage formatted wrong");
|
||||||
|
println!("{:?}", res);
|
||||||
|
}
|
||||||
|
|
||||||
|
new_msg = receiver.next();
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::db;
|
||||||
use crate::encrypting;
|
use crate::encrypting;
|
||||||
use async_std::{
|
use async_std::{
|
||||||
io,
|
io,
|
||||||
|
@ -19,10 +20,11 @@ async fn connect_to_client() -> io::Result<()> {
|
||||||
|
|
||||||
let listener = TcpListener::bind(&addr).await?;
|
let listener = TcpListener::bind(&addr).await?;
|
||||||
|
|
||||||
|
println!("Debugging!");
|
||||||
while let Ok((stream, _)) = listener.accept().await {
|
while let Ok((stream, _)) = listener.accept().await {
|
||||||
task::spawn(accept_client(stream));
|
task::spawn(accept_client(stream));
|
||||||
}
|
}
|
||||||
println!("HEY");
|
println!("Debugging 2!");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -56,8 +58,7 @@ async fn accept_client(stream: TcpStream) -> io::Result<()> {
|
||||||
let res: serde_json::Result<FrontMsg> =
|
let res: serde_json::Result<FrontMsg> =
|
||||||
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 id = received_msg.receiverID;
|
let msg = received_msg.message;
|
||||||
println!("{:?}", id);
|
|
||||||
|
|
||||||
/* message example
|
/* message example
|
||||||
{
|
{
|
||||||
|
@ -69,7 +70,7 @@ async fn accept_client(stream: TcpStream) -> io::Result<()> {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
sender
|
sender
|
||||||
.send(Message::Text(String::from("msg").to_owned()))
|
.send(Message::Text(String::from("Sended").to_owned()))
|
||||||
.await
|
.await
|
||||||
.expect("ooops");
|
.expect("ooops");
|
||||||
} else {
|
} else {
|
||||||
|
|
0
src/db/messaging.rs
Normal file
0
src/db/messaging.rs
Normal file
4
src/db/mod.rs
Normal file
4
src/db/mod.rs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
pub mod messaging;
|
||||||
|
fn main () {
|
||||||
|
|
||||||
|
}
|
4
src/db/mod.rs~
Normal file
4
src/db/mod.rs~
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
pub mod messaging
|
||||||
|
fn main () {
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod encrypting;
|
pub mod encrypting;
|
||||||
|
pub mod db;
|
||||||
|
|
||||||
mod chat;
|
mod chat;
|
||||||
pub use chat::front_conn;
|
pub use chat::front_conn;
|
||||||
|
|
65
src/main.rs
65
src/main.rs
|
@ -1,10 +1,73 @@
|
||||||
mod chat;
|
mod chat;
|
||||||
|
|
||||||
|
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 std::{
|
||||||
|
sync::{
|
||||||
|
mpsc,
|
||||||
|
mpsc::{Receiver, Sender},
|
||||||
|
},
|
||||||
|
thread,
|
||||||
|
};
|
||||||
fn main() -> io::Result<()> {
|
fn main() -> io::Result<()> {
|
||||||
listen_client()
|
// listen_client();
|
||||||
|
|
||||||
|
enum ClientMessage {
|
||||||
|
Message(String),
|
||||||
|
Nope,
|
||||||
|
}
|
||||||
|
struct MFA{
|
||||||
|
encoded_msg: String
|
||||||
|
// TODO add User field
|
||||||
|
}; //Message from above
|
||||||
|
|
||||||
|
|
||||||
|
struct ClientHandler {
|
||||||
|
message: ClientMessage,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ServerHandler {
|
||||||
|
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 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 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(lol)| ;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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!!"),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
Loading…
Reference in a new issue