change PackedMessage type

This commit is contained in:
Horhik 2021-03-27 09:09:59 +03:00
parent a1f77298c7
commit be2705640c
5 changed files with 53 additions and 22 deletions

View file

@ -77,16 +77,28 @@ async fn connection_for_receiving(
log::info!("Connection for receiving launched"); log::info!("Connection for receiving launched");
while let Ok(res) = 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 server_sender = server_sender.clone(); let server_sender = server_sender.clone();
task::spawn(request_repeater(server_sender)).await?; task::spawn(request_repeater(server_sender)).await?;
log::info!("Client received: \n {}", res.message); log::info!("Client received: \n {}", res.message);
} }
*/
match res {
PackedMessage::FromCore(json) => {
sender sender
.send(Message::Text(String::from(res.message).to_owned())) .send(Message::Text(json))
.await .await
.expect("couldn't send Message"); .expect("Couldn't send message");
}
PackedMessage::FromFreenet(response) => {sender
// TODO freenet_response_handler
.send(Message::Text(response))
.await
.expect("Couldn't send messge");
}
_ => {},
}
} }
Ok(()) Ok(())
} }

View file

@ -30,15 +30,12 @@ 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<()> {
loop { loop {
let mut buffer = [0; 512]; let mut buffer = [0; 1024];
match receiver.read(&mut buffer).await { match receiver.read(&mut buffer).await {
Ok(_) => { Ok(_) => {
let received = String::from_utf8_lossy(&buffer[..]); let received = String::from_utf8_lossy(&buffer[..]);
log::info!("received {}", received);
client_sender client_sender
.send(PackedMessage { .send(PackedMessage::FromFreenet(received.to_string()))
message: received.to_string(),
})
.expect("Falied to send message to client thread"); .expect("Falied to send message to client thread");
log::info!("Sended to client!"); log::info!("Sended to client!");
} }

View file

@ -1,4 +1,4 @@
use crate::chat::types::{RP, SP}; use crate::chat::types::{PackedMessage, RP, SP};
use tokio::{ use tokio::{
io::{self, AsyncReadExt, AsyncWriteExt}, io::{self, AsyncReadExt, AsyncWriteExt},
net::{ net::{
@ -8,11 +8,29 @@ use tokio::{
}; };
use fcpv2::client::fcp_types::{ClientHello, ClientPut}; use fcpv2::client::fcp_types::{ClientHello, ClientPut};
use fcpv2::types::{traits::{FcpRequest, FcpParser}, SSK} ; use fcpv2::types::{
pub async fn to_server_sender(mut sender: OwnedWriteHalf, server_receiver: RP, client_sender: SP) -> io::Result<()> { traits::{FcpParser, FcpRequest},
SSK,
};
pub async fn to_server_sender(
mut sender: OwnedWriteHalf,
server_receiver: RP,
client_sender: SP,
) -> io::Result<()> {
while let Ok(res) = server_receiver.recv() { while let Ok(res) = server_receiver.recv() {
//TODO from_core_to_server_handler //TODO from_core_to_server_handler
if res.message == "STARTAPP!" { match res {
PackedMessage::ToClient(json) => {
client_sender.send(PackedMessage::FromCore(json)).unwrap();
log::info!("Message sended to client thread");
}
PackedMessage::ToFreenet(req) => {
sender.write(req.as_bytes()).await?;
log::info!("Message sended to freenet");
}
_ => {},
}
/*if res == "STARTAPP!" {
let _ = sender let _ = sender
.write((ClientHello::new("name".to_string(), 2.0).convert()).as_bytes()) .write((ClientHello::new("name".to_string(), 2.0).convert()).as_bytes())
.await?; .await?;
@ -23,7 +41,7 @@ pub async fn to_server_sender(mut sender: OwnedWriteHalf, server_receiver: RP, c
let key = SSK::parse("KSK@msg23.txt").unwrap(); let key = SSK::parse("KSK@msg23.txt").unwrap();
let cp = ClientPut::new_default(key, "msg23.txt", "hello", &res.message[..]).convert(); let cp = ClientPut::new_default(key, "msg23.txt", "hello", &res.message[..]).convert();
let _ = sender.write(cp.as_bytes()).await; let _ = sender.write(cp.as_bytes()).await;
} }*/
} }
Ok(()) Ok(())

View file

@ -1,6 +1,6 @@
use crate::chat::types::PackedMessage; use crate::chat::types::PackedMessage;
use async_std::io; use async_std::io;
use fcpv2::client::fcp_types::{ClientGet}; use fcpv2::client::fcp_types::ClientGet;
use fcpv2::types::{traits::FcpRequest, ReturnType, SSK}; use fcpv2::types::{traits::FcpRequest, ReturnType, SSK};
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
@ -11,8 +11,8 @@ pub async fn request_repeater(ss: SP) -> io::Result<()> {
//TODO create a field with tracked users //TODO create a field with tracked users
let time = std::time::Duration::from_millis(1000); let time = std::time::Duration::from_millis(1000);
std::thread::sleep(time); std::thread::sleep(time);
match ss.send(PackedMessage{ match ss.send(PackedMessage::FromFreenet(
message: ClientGet::new_default(SSK{sign_key: "9Zq-H7vg1iN6852rcL3mQQaIfPZODnIJnKyIy1dE6mk".to_string(), decrypt_key: "n-vQibdLXPDMtW7k5ftbR9HVz4Tb184lUc~MiUGHWAM".to_string(),settings: Some("AQACAAE".to_string())}, ClientGet::new_default(SSK{sign_key: "9Zq-H7vg1iN6852rcL3mQQaIfPZODnIJnKyIy1dE6mk".to_string(), decrypt_key: "n-vQibdLXPDMtW7k5ftbR9HVz4Tb184lUc~MiUGHWAM".to_string(),settings: Some("AQACAAE".to_string())},
"check", "check",
ReturnType::Direct).convert() ReturnType::Direct).convert()
// message: format!( // message: format!(
@ -23,7 +23,7 @@ pub async fn request_repeater(ss: SP) -> io::Result<()> {
// ReturnType=direct\n\ // ReturnType=direct\n\
// EndMessage\n\n" // EndMessage\n\n"
// ), // ),
}) { )) {
Ok(_) => {} Ok(_) => {}
Err(e) => log::error!("{:?}", e), Err(e) => log::error!("{:?}", e),
} }

View file

@ -32,8 +32,12 @@ use std::sync::mpsc::{Receiver, Sender};
// } // }
//} //}
pub struct PackedMessage { pub enum PackedMessage {
pub message: String, ToFreenet(String),
FromFreenet(String),
ToClient(String),
FromCore(String),
} }
pub type SP = Sender<PackedMessage>; pub type SP = Sender<PackedMessage>;