some refactoring
This commit is contained in:
parent
a42e2aaa10
commit
3b8c3c9438
|
@ -24,23 +24,22 @@ struct FrontMsg {
|
||||||
time: String,
|
time: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn listen_client(server_sender: SP, to_client_receiver: RP) -> io::Result<()> {
|
pub fn listen_client(server_sender: SP, client_receiver: RP) -> io::Result<()> {
|
||||||
task::block_on(connect_to_client(server_sender, to_client_receiver))
|
task::block_on(connect_to_client(server_sender, client_receiver))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn request_repeater(ss: SP) -> io::Result<()> {
|
async fn request_repeater(ss: SP) -> io::Result<()> {
|
||||||
loop {
|
loop {
|
||||||
let time = std::time::Duration::from_millis(1000);
|
let time = std::time::Duration::from_millis(1000);
|
||||||
std::thread::sleep(time);
|
std::thread::sleep(time);
|
||||||
println!("sleep1");
|
|
||||||
match ss.send(PackedMessage {
|
match ss.send(PackedMessage {
|
||||||
message: format!(
|
message: format!(
|
||||||
"ClientGet\n\
|
"ClientGet\n\
|
||||||
URI=KSK@msg23.txt\n\
|
URI=KSK@msg23.txt\n\
|
||||||
Identifier=doesnt_matter?\n\
|
Identifier=doesnt_matter?\n\
|
||||||
Verbosity=0\n\
|
Verbosity=0\n\
|
||||||
ReturnType=direct\n\
|
ReturnType=direct\n\
|
||||||
EndMessage\n\n"
|
EndMessage\n\n"
|
||||||
),
|
),
|
||||||
}) {
|
}) {
|
||||||
Ok(_) => {}
|
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()
|
let addr = env::args()
|
||||||
.nth(1)
|
.nth(1)
|
||||||
.unwrap_or_else(|| "127.0.0.1:5948".to_string());
|
.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);
|
println!("connected to: {}", addr);
|
||||||
|
|
||||||
let t1 = task::spawn(connection_for_sending(receiver, server_sender));
|
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?;
|
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(
|
async fn connection_for_receiving(
|
||||||
mut sender: SplitSink<WebSocketStream<TcpStream>, Message>,
|
mut sender: SplitSink<WebSocketStream<TcpStream>, Message>,
|
||||||
to_client_receiver: RP,
|
client_receiver: RP,
|
||||||
client_repeater: SP,
|
server_sender: SP,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
while let Ok(res) = to_client_receiver.recv() {
|
while let Ok(res) = client_receiver.recv() {
|
||||||
//TODO call client get after receiving NodeHello
|
//TODO call client get after receiving NodeHello
|
||||||
if res.message.lines().next() == Some("NodeHello") {
|
if res.message.lines().next() == Some("NodeHello") {
|
||||||
let client_repeater = client_repeater.clone();
|
let server_sender = server_sender.clone();
|
||||||
task::spawn(request_repeater(client_repeater)).await?;
|
task::spawn(request_repeater(server_sender)).await?;
|
||||||
println!("HEY NODE HELLO \n {}", res.message);
|
println!("HEY NODE HELLO \n {}", res.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,7 +111,6 @@ async fn connection_for_sending(
|
||||||
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();
|
server_sender.send(PackedMessage { message: msg }).unwrap();
|
||||||
|
|
||||||
/* message example
|
/* message example
|
||||||
{
|
{
|
||||||
"user_id": 123456789,
|
"user_id": 123456789,
|
||||||
|
@ -120,7 +118,7 @@ async fn connection_for_sending(
|
||||||
"message": "hey dude",
|
"message": "hey dude",
|
||||||
"time": "Tue Oct 13 2020 18:31:22 GMT+0300 (Eastern European Summer Time)"
|
"time": "Tue Oct 13 2020 18:31:22 GMT+0300 (Eastern European Summer Time)"
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
} else {
|
} else {
|
||||||
println!("seems, that messsage formatted wrong");
|
println!("seems, that messsage formatted wrong");
|
||||||
println!("{:?}", res);
|
println!("{:?}", res);
|
||||||
|
|
|
@ -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<()> {
|
async fn server_responce_getter(mut receiver: OwnedReadHalf, client_sender: SP) -> io::Result<()> {
|
||||||
println!("Running");
|
|
||||||
loop {
|
loop {
|
||||||
let mut buffer = [0; 512];
|
let mut buffer = [0; 512];
|
||||||
match receiver.read(&mut buffer).await {
|
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),
|
Err(e) => println!("Error: {} ", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//TODO HANDLE ERROR
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
async fn to_server_sender(mut sender: OwnedWriteHalf, server_receiver: RP) -> io::Result<()> {
|
async fn to_server_sender(mut sender: OwnedWriteHalf, server_receiver: RP) -> io::Result<()> {
|
||||||
while let Ok(res) = server_receiver.recv() {
|
while let Ok(res) = server_receiver.recv() {
|
||||||
|
//TODO from_core_to_server_handler
|
||||||
if res.message == "STARTAPP!" {
|
if res.message == "STARTAPP!" {
|
||||||
let _ = sender
|
let _ = sender
|
||||||
.write(("ClientHello\nName=ggg\nExpectedVersion=2.0\nEndMessage\n\n").as_bytes())
|
.write(("ClientHello\nName=ggg\nExpectedVersion=2.0\nEndMessage\n\n").as_bytes())
|
||||||
|
|
62
src/main.rs
62
src/main.rs
|
@ -45,78 +45,28 @@ use std::{
|
||||||
fn main() -> io::Result<()> {
|
fn main() -> io::Result<()> {
|
||||||
let (to_server_sender, server_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
|
let (to_server_sender, server_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
|
||||||
mpsc::channel();
|
mpsc::channel();
|
||||||
|
|
||||||
let (client_sender, client_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
|
let (client_sender, client_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
|
||||||
mpsc::channel();
|
mpsc::channel();
|
||||||
|
|
||||||
let server_thread = thread::spawn(move || {
|
let server_thread = thread::spawn(move || {
|
||||||
let cs = client_sender;
|
let cs = client_sender;
|
||||||
let sr = server_receiver;
|
let sr = server_receiver;
|
||||||
// let cs1 = cs.clone();
|
|
||||||
// let cs2 = cs.clone();
|
|
||||||
|
|
||||||
let t1 = thread::spawn(move || listen_server(cs, sr));
|
let t = thread::spawn(move || listen_server(cs, sr));
|
||||||
// let t2 = thread::spawn(move || responding_to_client(cs2, sr));
|
|
||||||
|
|
||||||
t1.join().expect("failed server thread").unwrap();
|
t.join().expect("failed server thread").unwrap();
|
||||||
// t2.join();
|
|
||||||
// while let Ok(res) = sr.recv() {
|
|
||||||
// println!("From Server:\n {}", res.message);
|
|
||||||
// }
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let client_thread = thread::spawn(move || {
|
let client_thread = thread::spawn(move || {
|
||||||
let ss = to_server_sender;
|
let ss = to_server_sender;
|
||||||
let cr = client_receiver;
|
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();
|
server_thread.join().unwrap();
|
||||||
client_thread.join().unwrap();
|
client_thread.join().unwrap();
|
||||||
Ok(())
|
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();
|
|
||||||
|
|
Loading…
Reference in a new issue