very clumsy solution of sending and getting messages
This commit is contained in:
parent
4594b0f750
commit
a42e2aaa10
|
@ -28,6 +28,27 @@ pub fn listen_client(server_sender: SP, to_client_receiver: RP) -> io::Result<()
|
||||||
task::block_on(connect_to_client(server_sender, to_client_receiver))
|
task::block_on(connect_to_client(server_sender, to_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"
|
||||||
|
),
|
||||||
|
}) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(e) => println!("{:?}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn connect_to_client(server_sender: SP, to_client_receiver: RP) -> io::Result<()> {
|
async fn connect_to_client(server_sender: SP, to_client_receiver: RP) -> io::Result<()> {
|
||||||
let addr = env::args()
|
let addr = env::args()
|
||||||
.nth(1)
|
.nth(1)
|
||||||
|
@ -35,6 +56,7 @@ async fn connect_to_client(server_sender: SP, to_client_receiver: RP) -> io::Res
|
||||||
|
|
||||||
let listener = TcpListener::bind(&addr).await?;
|
let listener = TcpListener::bind(&addr).await?;
|
||||||
|
|
||||||
|
let client_repeater = server_sender.clone();
|
||||||
if let Ok((stream, _)) = listener.accept().await {
|
if let Ok((stream, _)) = listener.accept().await {
|
||||||
let addr = stream
|
let addr = stream
|
||||||
.peer_addr()
|
.peer_addr()
|
||||||
|
@ -48,7 +70,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).await?;
|
connection_for_receiving(sender, to_client_receiver, client_repeater).await?;
|
||||||
t1.await?;
|
t1.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,9 +80,16 @@ 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,
|
to_client_receiver: RP,
|
||||||
|
client_repeater: SP,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
while let Ok(res) = to_client_receiver.recv() {
|
while let Ok(res) = to_client_receiver.recv() {
|
||||||
println!("HEYNEHHEFSJDHFKLSDJ \n {}", res.message);
|
//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?;
|
||||||
|
println!("HEY NODE HELLO \n {}", res.message);
|
||||||
|
}
|
||||||
|
|
||||||
sender
|
sender
|
||||||
.send(Message::Text(String::from(res.message).to_owned()))
|
.send(Message::Text(String::from(res.message).to_owned()))
|
||||||
.await
|
.await
|
||||||
|
@ -86,8 +115,8 @@ async fn connection_for_sending(
|
||||||
|
|
||||||
/* message example
|
/* message example
|
||||||
{
|
{
|
||||||
"userID": 123456789,
|
"user_id": 123456789,
|
||||||
"receiverID": 123456789,
|
"receiver_id": 123456789,
|
||||||
"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)"
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,10 @@ use std::env;
|
||||||
use std::sync::mpsc::{Receiver, Sender};
|
use std::sync::mpsc::{Receiver, Sender};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
io::{self, AsyncReadExt, AsyncWriteExt},
|
io::{self, AsyncReadExt, AsyncWriteExt},
|
||||||
net::{TcpStream, tcp::{OwnedReadHalf, OwnedWriteHalf}}
|
net::{
|
||||||
|
tcp::{OwnedReadHalf, OwnedWriteHalf},
|
||||||
|
TcpStream,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
type SP = Sender<PackedMessage>;
|
type SP = Sender<PackedMessage>;
|
||||||
|
@ -28,13 +31,12 @@ async fn connect_to_server(client_sender: SP, server_receiver: RP) -> io::Result
|
||||||
to_server_sender(sender, server_receiver).await?;
|
to_server_sender(sender, server_receiver).await?;
|
||||||
match t.await {
|
match t.await {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(e) => Err(e)
|
Err(e) => Err(e),
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
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");
|
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 {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
|
@ -52,16 +54,25 @@ loop {
|
||||||
//TODO HANDLE ERROR
|
//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(){
|
if res.message == "STARTAPP!" {
|
||||||
|
let _ = sender
|
||||||
println!("{}", res.message);
|
.write(("ClientHello\nName=ggg\nExpectedVersion=2.0\nEndMessage\n\n").as_bytes())
|
||||||
println!("ALIVE\nALIVE\nALIVE\nALIVE\n");
|
.await?;
|
||||||
let _ = sender
|
} else if res.message.lines().next() == Some("ClientGet") {
|
||||||
.write(("ClientHello\nName=ggg\nExpectedVersion=2.0\nEndMessage\n\n").as_bytes())
|
let _ = sender.write(res.message.as_bytes()).await?;
|
||||||
.await?;
|
} else {
|
||||||
println!("SENDED\nSENDED\nSENDED\nSENDED\n");
|
//println!("{:?}", res.message);
|
||||||
|
let _ = sender.write(
|
||||||
|
format!(
|
||||||
|
"ClientPut\nIdentifier=hello\nURI=KSK@msg23.txt\nDataLength={}\nUploadFrom=direct\nEndMessage\n{}\n\n",
|
||||||
|
res.message.len(),
|
||||||
|
res.message
|
||||||
|
)
|
||||||
|
.as_bytes(),
|
||||||
|
).await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -74,4 +85,3 @@ struct FrontMsg {
|
||||||
message: String,
|
message: String,
|
||||||
time: String,
|
time: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue