some refactoring

This commit is contained in:
Horhik 2021-01-24 20:06:06 +02:00
parent a42e2aaa10
commit 3b8c3c9438
3 changed files with 22 additions and 75 deletions

View file

@ -24,23 +24,22 @@ struct FrontMsg {
time: String,
}
pub fn listen_client(server_sender: SP, to_client_receiver: RP) -> io::Result<()> {
task::block_on(connect_to_client(server_sender, to_client_receiver))
pub fn listen_client(server_sender: SP, client_receiver: RP) -> io::Result<()> {
task::block_on(connect_to_client(server_sender, client_receiver))
}
async fn request_repeater(ss: SP) -> io::Result<()> {
loop {
let time = std::time::Duration::from_millis(1000);
std::thread::sleep(time);
println!("sleep1");
match ss.send(PackedMessage {
message: format!(
"ClientGet\n\
URI=KSK@msg23.txt\n\
Identifier=doesnt_matter?\n\
Verbosity=0\n\
ReturnType=direct\n\
EndMessage\n\n"
URI=KSK@msg23.txt\n\
Identifier=doesnt_matter?\n\
Verbosity=0\n\
ReturnType=direct\n\
EndMessage\n\n"
),
}) {
Ok(_) => {}
@ -49,7 +48,7 @@ async fn request_repeater(ss: SP) -> io::Result<()> {
}
}
async fn connect_to_client(server_sender: SP, to_client_receiver: RP) -> io::Result<()> {
async fn connect_to_client(server_sender: SP, client_receiver: RP) -> io::Result<()> {
let addr = env::args()
.nth(1)
.unwrap_or_else(|| "127.0.0.1:5948".to_string());
@ -70,7 +69,7 @@ async fn connect_to_client(server_sender: SP, to_client_receiver: RP) -> io::Res
println!("connected to: {}", addr);
let t1 = task::spawn(connection_for_sending(receiver, server_sender));
connection_for_receiving(sender, to_client_receiver, client_repeater).await?;
connection_for_receiving(sender, client_receiver, client_repeater).await?;
t1.await?;
}
@ -79,14 +78,14 @@ async fn connect_to_client(server_sender: SP, to_client_receiver: RP) -> io::Res
async fn connection_for_receiving(
mut sender: SplitSink<WebSocketStream<TcpStream>, Message>,
to_client_receiver: RP,
client_repeater: SP,
client_receiver: RP,
server_sender: SP,
) -> io::Result<()> {
while let Ok(res) = to_client_receiver.recv() {
while let Ok(res) = client_receiver.recv() {
//TODO call client get after receiving NodeHello
if res.message.lines().next() == Some("NodeHello") {
let client_repeater = client_repeater.clone();
task::spawn(request_repeater(client_repeater)).await?;
let server_sender = server_sender.clone();
task::spawn(request_repeater(server_sender)).await?;
println!("HEY NODE HELLO \n {}", res.message);
}
@ -112,7 +111,6 @@ async fn connection_for_sending(
if let Ok(received_msg) = res {
let msg = received_msg.message;
server_sender.send(PackedMessage { message: msg }).unwrap();
/* message example
{
"user_id": 123456789,
@ -120,7 +118,7 @@ async fn connection_for_sending(
"message": "hey dude",
"time": "Tue Oct 13 2020 18:31:22 GMT+0300 (Eastern European Summer Time)"
}
*/
*/
} else {
println!("seems, that messsage formatted wrong");
println!("{:?}", res);

View file

@ -35,7 +35,6 @@ async fn connect_to_server(client_sender: SP, server_receiver: RP) -> io::Result
}
}
async fn server_responce_getter(mut receiver: OwnedReadHalf, client_sender: SP) -> io::Result<()> {
println!("Running");
loop {
let mut buffer = [0; 512];
match receiver.read(&mut buffer).await {
@ -51,11 +50,11 @@ async fn server_responce_getter(mut receiver: OwnedReadHalf, client_sender: SP)
Err(e) => println!("Error: {} ", e),
}
}
//TODO HANDLE ERROR
Ok(())
}
async fn to_server_sender(mut sender: OwnedWriteHalf, server_receiver: RP) -> io::Result<()> {
while let Ok(res) = server_receiver.recv() {
//TODO from_core_to_server_handler
if res.message == "STARTAPP!" {
let _ = sender
.write(("ClientHello\nName=ggg\nExpectedVersion=2.0\nEndMessage\n\n").as_bytes())

View file

@ -45,78 +45,28 @@ use std::{
fn main() -> io::Result<()> {
let (to_server_sender, server_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
mpsc::channel();
let (client_sender, client_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
mpsc::channel();
let server_thread = thread::spawn(move || {
let cs = client_sender;
let sr = server_receiver;
// let cs1 = cs.clone();
// let cs2 = cs.clone();
let t1 = thread::spawn(move || listen_server(cs, sr));
// let t2 = thread::spawn(move || responding_to_client(cs2, sr));
let t = thread::spawn(move || listen_server(cs, sr));
t1.join().expect("failed server thread").unwrap();
// t2.join();
// while let Ok(res) = sr.recv() {
// println!("From Server:\n {}", res.message);
// }
t.join().expect("failed server thread").unwrap();
});
let client_thread = thread::spawn(move || {
let ss = to_server_sender;
let cr = client_receiver;
let t1 = thread::spawn(move || listen_client(ss, cr));
let t = thread::spawn(move || listen_client(ss, cr));
t1.join().expect("failed client thread").unwrap();
t.join().expect("failed client thread").unwrap();
});
server_thread.join().unwrap();
client_thread.join().unwrap();
Ok(())
}
/*
fn main() {
let server = TcpListener::bind("127.0.0.1:9001").unwrap();
println!("{:?}", &server);
for stream in server.incoming() {
spawn(move || {
println!("{:?}", &stream);
let mut websocket: WebSocket<std::net::TcpStream> = accept(stream.unwrap()).unwrap();
loop {
let msg = websocket.read_message().unwrap();
// We do not want to send back ping/pong messages.
if msg.is_binary() || msg.is_text() {
websocket.write_message(msg).unwrap();
}
}
});
}
}*/
/*
let (one, two) = keys;
let value =String::from_utf8_lossy(&*one);
let strVal = String::from(value);
let newbytes = strVal.into_bytes();
print!("{:?}", newbytes);
let newkey = PrivateKey::import(newbytes);
Let conn = Connection::open("myfile.db").unwrap();
conn.execute("CREATE TABLE person (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL
)", NO_PARAMS).unwrap();
let name: String = "Steve Example".to_string();
let email: String = "steve@example.org".to_string();
conn.execute("INSERT INTO person (name, email) VALUES (?1, ?2)",
&[&name, &email]).unwrap();
}
*/
//let mut std = cli::cli_base::get_stdin();