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");
while let Ok(res) = client_receiver.recv() {
//TODO call client get after receiving NodeHello
/*
if res.message.lines().next() == Some("NodeHello") {
let server_sender = server_sender.clone();
task::spawn(request_repeater(server_sender)).await?;
log::info!("Client received: \n {}", res.message);
}
sender
.send(Message::Text(String::from(res.message).to_owned()))
.await
.expect("couldn't send Message");
*/
match res {
PackedMessage::FromCore(json) => {
sender
.send(Message::Text(json))
.await
.expect("Couldn't send message");
}
PackedMessage::FromFreenet(response) => {sender
// TODO freenet_response_handler
.send(Message::Text(response))
.await
.expect("Couldn't send messge");
}
_ => {},
}
}
Ok(())
}

View file

@ -18,7 +18,7 @@ async fn connect_to_server(client_sender: SP, server_receiver: RP) -> io::Result
.nth(1)
.unwrap_or_else(|| "127.0.0.1:9481".to_string());
let sr = client_sender.clone();
let sr = client_sender.clone();
let stream = TcpStream::connect(&addr).await.expect("weeror here");
let (receiver, sender) = stream.into_split();
let t = task::spawn(server_responce_getter(receiver, client_sender));
@ -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<()> {
loop {
let mut buffer = [0; 512];
let mut buffer = [0; 1024];
match receiver.read(&mut buffer).await {
Ok(_) => {
let received = String::from_utf8_lossy(&buffer[..]);
log::info!("received {}", received);
client_sender
.send(PackedMessage {
message: received.to_string(),
})
.send(PackedMessage::FromFreenet(received.to_string()))
.expect("Falied to send message to client thread");
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::{
io::{self, AsyncReadExt, AsyncWriteExt},
net::{
@ -8,11 +8,29 @@ use tokio::{
};
use fcpv2::client::fcp_types::{ClientHello, ClientPut};
use fcpv2::types::{traits::{FcpRequest, FcpParser}, SSK} ;
pub async fn to_server_sender(mut sender: OwnedWriteHalf, server_receiver: RP, client_sender: SP) -> io::Result<()> {
use fcpv2::types::{
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() {
//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
.write((ClientHello::new("name".to_string(), 2.0).convert()).as_bytes())
.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 cp = ClientPut::new_default(key, "msg23.txt", "hello", &res.message[..]).convert();
let _ = sender.write(cp.as_bytes()).await;
}
}*/
}
Ok(())

View file

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

View file

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