update message receiver
This commit is contained in:
parent
60f5ed6c6e
commit
f980d5e08c
|
@ -22,6 +22,7 @@ use uuid::Uuid;
|
||||||
use crate::db::types::Id;
|
use crate::db::types::Id;
|
||||||
|
|
||||||
pub fn start_app(server_sender: SP) -> Result<()> {
|
pub fn start_app(server_sender: SP) -> Result<()> {
|
||||||
|
log::debug!("Sending ClientHello to freenet");
|
||||||
server_sender
|
server_sender
|
||||||
.send(PackedMessage::ToFreenet(
|
.send(PackedMessage::ToFreenet(
|
||||||
ClientHello::new("start_app_request".to_string(), 2.0).convert(),
|
ClientHello::new("start_app_request".to_string(), 2.0).convert(),
|
||||||
|
@ -42,9 +43,12 @@ pub fn start_app(server_sender: SP) -> Result<()> {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
|
log::debug!("Reading config path... ");
|
||||||
let conf = std::fs::read_to_string(&config_path).unwrap();
|
let conf = std::fs::read_to_string(&config_path).unwrap();
|
||||||
log::debug!("Responsing to start_app: {}", &conf);
|
log::debug!("Parsing config to toml.. ");
|
||||||
let toml: crate::chat::Config = toml::from_str(&conf[..]).unwrap();
|
let toml: crate::chat::Config = toml::from_str(&conf[..]).unwrap();
|
||||||
|
|
||||||
|
log::debug!("Sending config to client thread...");
|
||||||
server_sender
|
server_sender
|
||||||
.send(PackedMessage::ToClient(
|
.send(PackedMessage::ToClient(
|
||||||
serde_json::to_string(&crate::api::response::ResponseType::InitialConfig {
|
serde_json::to_string(&crate::api::response::ResponseType::InitialConfig {
|
||||||
|
@ -68,15 +72,18 @@ pub fn stop_app(conn: &Connection, server_sender: SP) -> Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_users(conn: &Connection, server_sender: SP) -> Result<()> {
|
pub fn load_users(conn: &Connection, server_sender: SP) -> Result<()> {
|
||||||
|
log::debug!("Getting user list from DB... ");
|
||||||
let jsoned_users: Vec<_> = users::load_all_users(conn)
|
let jsoned_users: Vec<_> = users::load_all_users(conn)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|x| x.to_jsonable())
|
.map(|x| x.to_jsonable())
|
||||||
.collect();
|
.collect();
|
||||||
|
log::debug!("Creatin user list JSON... ");
|
||||||
let users: String = serde_json::to_string(&crate::api::response::ResponseType::UserList {
|
let users: String = serde_json::to_string(&crate::api::response::ResponseType::UserList {
|
||||||
users: jsoned_users,
|
users: jsoned_users,
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
log::debug!("Sending users to client thread... ");
|
||||||
let _ = server_sender.send(PackedMessage::ToClient(users)).unwrap();
|
let _ = server_sender.send(PackedMessage::ToClient(users)).unwrap();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -93,7 +100,9 @@ pub fn send_message(
|
||||||
let message_id: u32 = user_data.my_messages_count;
|
let message_id: u32 = user_data.my_messages_count;
|
||||||
let id = Id(uuid::Uuid::parse_str(identifier).expect("failed to parse user ID"));
|
let id = Id(uuid::Uuid::parse_str(identifier).expect("failed to parse user ID"));
|
||||||
|
|
||||||
|
log::debug!("Reading .hole.toml");
|
||||||
let config: String = String::from_utf8_lossy(&std::fs::read(".hole.toml")?).parse().unwrap();
|
let config: String = String::from_utf8_lossy(&std::fs::read(".hole.toml")?).parse().unwrap();
|
||||||
|
log::debug!("Parsing .hole.toml");
|
||||||
let parsed: crate::chat::Config = toml::from_str(&config[..]).unwrap();
|
let parsed: crate::chat::Config = toml::from_str(&config[..]).unwrap();
|
||||||
let my_id = parsed.id.0.to_string();
|
let my_id = parsed.id.0.to_string();
|
||||||
let db_message = db::types::Message {
|
let db_message = db::types::Message {
|
||||||
|
@ -103,17 +112,22 @@ pub fn send_message(
|
||||||
message: message.clone(),
|
message: message.clone(),
|
||||||
from_me: true,
|
from_me: true,
|
||||||
};
|
};
|
||||||
let _ = db::messages::add_my_message(db_message, conn).unwrap();
|
log::debug!("Adding sended message to DB");
|
||||||
|
match db::messages::add_my_message(db_message, conn) {
|
||||||
|
Ok(_) => {},
|
||||||
|
Err(e) => {log::error!("Failed to add message to DB");},
|
||||||
|
}
|
||||||
log::debug!("Sending new message to freent...");
|
log::debug!("Sending new message to freent...");
|
||||||
let fcp_req: String =
|
let fcp_req: String =
|
||||||
ClientPut::new_default_direct(fcpv2::types::USK{ ssk: key, path: format!("{}/{}", &my_id, message_id)}, &format!("new-messge-{}/{}", &identifier, &message_id )[..], &message[..]).convert();
|
ClientPut::new_default_direct(fcpv2::types::USK{ ssk: key, path: format!("{}/{}", &my_id, message_id)}, &format!("new-messge-{}/{}", &identifier, &message_id )[..], &message[..]).convert();
|
||||||
server_sender
|
server_sender
|
||||||
.send(PackedMessage::ToFreenet(fcp_req))
|
.send(PackedMessage::ToFreenet(fcp_req))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let _ = db::users::increase_my_messages_count(id.clone(), conn);
|
let _ = db::users::increase_my_messages_count(id.clone(), conn).unwrap();
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
// create error types
|
// create error types
|
||||||
|
log::error!("No such user in DB..");
|
||||||
server_sender
|
server_sender
|
||||||
.send(PackedMessage::ToClient(
|
.send(PackedMessage::ToClient(
|
||||||
json!(super::response::AppError {
|
json!(super::response::AppError {
|
||||||
|
@ -134,6 +148,7 @@ pub fn load_messages(
|
||||||
conn: &Connection,
|
conn: &Connection,
|
||||||
server_sender: SP,
|
server_sender: SP,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
log::debug!("Loading {} messages from user {:?}...", &count, &user_id);
|
||||||
let messages: Vec<DbMessage> = db::messages::select_n_last_messages(user_id.clone(), start_index, count, conn).unwrap();
|
let messages: Vec<DbMessage> = db::messages::select_n_last_messages(user_id.clone(), start_index, count, conn).unwrap();
|
||||||
let jsoned = json!(
|
let jsoned = json!(
|
||||||
ResponseType::MessageList{
|
ResponseType::MessageList{
|
||||||
|
@ -146,6 +161,7 @@ pub fn load_messages(
|
||||||
id: user_id.0
|
id: user_id.0
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
log::debug!("Sending loaded messages to client...");
|
||||||
let _ = server_sender.send(PackedMessage::ToClient(jsoned.to_string())).unwrap();
|
let _ = server_sender.send(PackedMessage::ToClient(jsoned.to_string())).unwrap();
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
||||||
|
@ -160,6 +176,7 @@ pub fn add_user(
|
||||||
conn: &Connection,
|
conn: &Connection,
|
||||||
server_sender: SP,
|
server_sender: SP,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
log::debug!("Retreiving user data from DB...");
|
||||||
let user = db::types::User {
|
let user = db::types::User {
|
||||||
id: Id(id.clone()),
|
id: Id(id.clone()),
|
||||||
name: name.clone(),
|
name: name.clone(),
|
||||||
|
@ -176,10 +193,12 @@ pub fn add_user(
|
||||||
messages_count: 0,
|
messages_count: 0,
|
||||||
my_messages_count: 0
|
my_messages_count: 0
|
||||||
};
|
};
|
||||||
|
log::debug!("Adding new user to DB...");
|
||||||
db::users::add_user(user, &conn).unwrap();
|
db::users::add_user(user, &conn).unwrap();
|
||||||
// Sending "Ok" response to client
|
// Sending "Ok" response to client
|
||||||
//
|
//
|
||||||
//loading all users to frontend
|
//loading all users to frontend
|
||||||
|
log::debug!("Loading all users to client...");
|
||||||
load_users(conn, server_sender).unwrap();
|
load_users(conn, server_sender).unwrap();
|
||||||
|
|
||||||
// TODO senging only one user to client{
|
// TODO senging only one user to client{
|
||||||
|
|
|
@ -74,6 +74,7 @@ async fn connect_to_client(
|
||||||
client_repeater,
|
client_repeater,
|
||||||
conn.clone(),
|
conn.clone(),
|
||||||
));
|
));
|
||||||
|
log::debug!("launching connection for sending...");
|
||||||
let t2 = task::spawn(connection_for_sending(
|
let t2 = task::spawn(connection_for_sending(
|
||||||
receiver,
|
receiver,
|
||||||
server_sender,
|
server_sender,
|
||||||
|
@ -106,25 +107,28 @@ async fn connection_for_receiving(
|
||||||
// log::debug!("they are different");
|
// log::debug!("they are different");
|
||||||
match res {
|
match res {
|
||||||
PackedMessage::FromCore(json) => {
|
PackedMessage::FromCore(json) => {
|
||||||
|
log::debug!("Sending message FromCore to frontend...");
|
||||||
async_std::task::block_on(sender.send(Message::Text(json.clone())))
|
async_std::task::block_on(sender.send(Message::Text(json.clone())))
|
||||||
.expect("Couldn't send message");
|
.expect("Couldn't send message");
|
||||||
log::debug!("Message sended to frontend: {}", json.clone());
|
|
||||||
}
|
}
|
||||||
PackedMessage::FromFreenet(response) => {
|
PackedMessage::FromFreenet(response) => {
|
||||||
let r = response.clone();
|
let r = response.clone();
|
||||||
let res_type = r.lines().next();
|
let res_type = r.lines().next();
|
||||||
log::debug!("Got:\n {}", &response);
|
log::debug!("Got message {:?} from freenet:\n", &res_type);
|
||||||
match res_type {
|
match res_type {
|
||||||
Some("AllData") => {
|
Some("AllData") => {
|
||||||
|
log::debug!("Parsing AllData...");
|
||||||
let data = AllData::parse(&r).unwrap();
|
let data = AllData::parse(&r).unwrap();
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"GOT mESSAGE {}\n FROM FREENET: {}",
|
"GOT mESSAGE {}\n FROM FREENET: {}",
|
||||||
&data.identifier,
|
&data.identifier,
|
||||||
&data.data
|
&data.data
|
||||||
);
|
);
|
||||||
|
log::debug!("Sending data to client...");
|
||||||
server_sender.send(PackedMessage::ToClient(data.data.clone())).unwrap();
|
server_sender.send(PackedMessage::ToClient(data.data.clone())).unwrap();
|
||||||
let (_, id) =
|
let (_, id) =
|
||||||
crate::api::identifier::parse_message_identifier(&data.identifier);
|
crate::api::identifier::parse_message_identifier(&data.identifier);
|
||||||
|
log::debug!("Parsing data to json...");
|
||||||
let jsoned: crate::api::types::Message =
|
let jsoned: crate::api::types::Message =
|
||||||
serde_json::from_str(&data.data[..]).unwrap();
|
serde_json::from_str(&data.data[..]).unwrap();
|
||||||
let uid = Id(jsoned.id);
|
let uid = Id(jsoned.id);
|
||||||
|
@ -139,6 +143,7 @@ async fn connection_for_receiving(
|
||||||
&db,
|
&db,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
log::debug!("Increasing messages count...");
|
||||||
crate::db::users::increase_my_messages_count(uid.clone(), &db).unwrap();
|
crate::db::users::increase_my_messages_count(uid.clone(), &db).unwrap();
|
||||||
/*async_std::task::block_on(
|
/*async_std::task::block_on(
|
||||||
sender
|
sender
|
||||||
|
@ -162,6 +167,7 @@ async fn connection_for_receiving(
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
// gets Request from frontend
|
||||||
// sending ToClient messages to frontend
|
// sending ToClient messages to frontend
|
||||||
async fn connection_for_sending(
|
async fn connection_for_sending(
|
||||||
mut receiver: SplitStream<WebSocketStream<TcpStream>>,
|
mut receiver: SplitStream<WebSocketStream<TcpStream>>,
|
||||||
|
|
|
@ -91,7 +91,15 @@ async fn server_responce_getter(mut receiver: OwnedReadHalf, client_sender: SP)
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"DataFound" => {
|
"aDataFound" => {
|
||||||
|
log::debug!("Receive a new message!!! {:?}", &received);
|
||||||
|
let message = fcpv2::node::fcp_response::AllData::parse(&received[..]).unwrap();
|
||||||
|
log::debug!("Parse new message!!!! {:?}", &message);
|
||||||
|
},
|
||||||
|
"AllData" => {
|
||||||
|
log::debug!("Receive a new message!!! {:?}", &received);
|
||||||
|
let message = fcpv2::node::fcp_response::AllData::parse(&received[..]).unwrap();
|
||||||
|
log::debug!("Parse new message!!!! {:?}", &message);
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
log::debug!("unhandled: {}", &req);
|
log::debug!("unhandled: {}", &req);
|
||||||
|
|
Loading…
Reference in a new issue