send gotted messags from freenet to frontend

This commit is contained in:
Horhik 2021-04-23 13:17:30 +03:00
parent 09f26108c8
commit 7c28f99546
11 changed files with 1798 additions and 27 deletions

43
Cargo.lock generated
View file

@ -639,6 +639,7 @@ dependencies = [
"futures-util", "futures-util",
"log", "log",
"ntru", "ntru",
"r2d2_sqlite",
"regex", "regex",
"rusqlite", "rusqlite",
"serde", "serde",
@ -744,9 +745,9 @@ checksum = "f2f96b10ec2560088a8e76961b00d47107b3a625fecb76dedb29ee7ccbf98235"
[[package]] [[package]]
name = "libsqlite3-sys" name = "libsqlite3-sys"
version = "0.20.1" version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64d31059f22935e6c31830db5249ba2b7ecd54fd73a9909286f0a67aa55c2fbd" checksum = "19cb1effde5f834799ac5e5ef0e40d45027cd74f271b1de786ba8abb30e2164d"
dependencies = [ dependencies = [
"pkg-config", "pkg-config",
"vcpkg", "vcpkg",
@ -1077,6 +1078,27 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "r2d2"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "545c5bc2b880973c9c10e4067418407a0ccaa3091781d1671d46eb35107cb26f"
dependencies = [
"log",
"parking_lot",
"scheduled-thread-pool",
]
[[package]]
name = "r2d2_sqlite"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d24607049214c5e42d3df53ac1d8a23c34cc6a5eefe3122acb2c72174719959"
dependencies = [
"r2d2",
"rusqlite",
]
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.7.3" version = "0.7.3"
@ -1207,9 +1229,9 @@ dependencies = [
[[package]] [[package]]
name = "rusqlite" name = "rusqlite"
version = "0.24.2" version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5f38ee71cbab2c827ec0ac24e76f82eca723cee92c509a65f67dee393c25112" checksum = "fbc783b7ddae608338003bac1fa00b6786a75a9675fbd8e87243ecfdea3f6ed2"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"chrono", "chrono",
@ -1263,6 +1285,15 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "scheduled-thread-pool"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc6f74fd1204073fa02d5d5d68bec8021be4c38690b61264b2fdb48083d0e7d7"
dependencies = [
"parking_lot",
]
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.1.0" version = "1.1.0"
@ -1377,9 +1408,9 @@ checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
[[package]] [[package]]
name = "smallvec" name = "smallvec"
version = "1.4.2" version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252" checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e"
[[package]] [[package]]
name = "socket2" name = "socket2"

View file

@ -8,7 +8,7 @@ edition = "2018"
[dependencies] [dependencies]
ntru = "0.5.6" ntru = "0.5.6"
rusqlite = {version = "0.24.2", features = ["chrono"]} rusqlite = {version = "0.25.1", features = ["chrono"]}
toml = "0.5.8" toml = "0.5.8"
tungstenite = "0.12.0" tungstenite = "0.12.0"
tokio = { version = "0.3.3", features = ["full"] } tokio = { version = "0.3.3", features = ["full"] }
@ -25,6 +25,7 @@ simple_logger = "1.11.0"
chrono = {version = "0.4.19", features = ["serde"]} chrono = {version = "0.4.19", features = ["serde"]}
uuid = {version = "0.8.2", features = ["serde" , "v4"]} uuid = {version = "0.8.2", features = ["serde" , "v4"]}
regex = "1.4.5" regex = "1.4.5"
r2d2_sqlite = "0.18.0"
[dependencies.fcpv2] [dependencies.fcpv2]
path = "../FCPv2" path = "../FCPv2"

1705
log Normal file

File diff suppressed because one or more lines are too long

View file

@ -98,7 +98,6 @@ pub fn send_message(
let identifier = &user_data.id.0.to_string()[..]; let identifier = &user_data.id.0.to_string()[..];
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"));
let _ = db::users::increase_my_messages_count(id.clone(), conn);
let db_message = db::types::Message { let db_message = db::types::Message {
id: message_id, id: message_id,
date: chrono::offset::Local::now(), date: chrono::offset::Local::now(),
@ -113,8 +112,7 @@ pub fn send_message(
server_sender server_sender
.send(PackedMessage::ToFreenet(fcp_req)) .send(PackedMessage::ToFreenet(fcp_req))
.unwrap(); .unwrap();
log::debug!("Increasing messages count"); let _ = db::users::increase_my_messages_count(id.clone(), conn);
let _ = db::users::increase_my_messages_count(id.clone(), &conn);
Ok(()) Ok(())
} else { } else {
// create error types // create error types

17
src/api/identifier.rs Normal file
View file

@ -0,0 +1,17 @@
use crate::db::types::Id;
use regex::Regex;
use uuid::{self, Uuid};
pub fn parse_message_identifier(identifier: &str) -> (Id, u32) {
let reg = Regex::new(
r"new-message-(.*)/(.*)",
)
.unwrap();
let res = reg.captures(identifier).unwrap();
let uuid = Id(Uuid::parse_str(&res[1]).unwrap());
let id = u32::from_str_radix(&res[2], 10).unwrap();
return (uuid, id)
}

View file

@ -2,3 +2,4 @@ pub mod request;
pub mod response; pub mod response;
pub mod handlers; pub mod handlers;
pub mod selector; pub mod selector;
pub mod identifier;

View file

@ -1,4 +1,5 @@
use crate::chat::types::PackedMessage; use crate::chat::types::PackedMessage;
use fcpv2::{types::traits::FcpParser, node::fcp_response::AllData};
use crate::db; use crate::db;
use async_std::{ use async_std::{
io, io,
@ -101,14 +102,27 @@ async fn connection_for_receiving(
} }
PackedMessage::FromFreenet(response) => { PackedMessage::FromFreenet(response) => {
let r = response.clone(); let r = response.clone();
let res_type = r.lines().next();
log::debug!("Got:\n {}", &response); log::debug!("Got:\n {}", &response);
async_std::task::block_on( match res_type {
sender Some("AllData") => {
// TODO freenet_response_handler let data = AllData::parse(&r).unwrap();
.send(Message::Text(r.to_string())), log::debug!("GOT mESSAGE {}\n FROM FREENET: {}",data.identifier, data.data );
) server_sender.send(PackedMessage::ToClient(data.data));
.expect("Couldn't send messge"); //TOOD parse identifier
// .await let (uuid, id) = crate::api::identifier::parse_message_identifier(&data.identifier);
log::debug!("parsed identifier: {:?} {:?}", uuid, id);
/*async_std::task::block_on(
sender
// TODO freenet_response_handler
.send(Message::Text(r.to_string())),
)
.expect("Couldn't send messge");
*/
}
None => {}
_ => {}
}
// .expect("Couldn't send messge"); // .expect("Couldn't send messge");
} }
_ => {} _ => {}
@ -117,7 +131,7 @@ async fn connection_for_receiving(
} }
Ok(()) Ok(())
} }
// 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>>,
server_sender: SP, server_sender: SP,

View file

@ -25,6 +25,7 @@ pub async fn to_server_sender(
log::info!("Message sended to client thread"); log::info!("Message sended to client thread");
} }
PackedMessage::ToFreenet(req) => { PackedMessage::ToFreenet(req) => {
let res_type = req.lines().next().unwrap();
log::debug!("SENDED {}", req); log::debug!("SENDED {}", req);
sender.write(req.as_bytes()).await?; sender.write(req.as_bytes()).await?;
log::info!("Message sended to freenet"); log::info!("Message sended to freenet");

View file

@ -28,15 +28,17 @@ pub async fn request_repeater(ss: SP) -> io::Result<()> {
let id = user.id.0.to_string(); let id = user.id.0.to_string();
let index = user.messages_count + 1; let index = user.messages_count + 1;
let key = USK {
ssk: parsed.private_key.clone(),
path: format!("{}/{}", &id, &index),
};
log::debug!("sending {:?}", &key.convert());
match ss.send(PackedMessage::ToFreenet( match ss.send(PackedMessage::ToFreenet(
ClientGet::new_default( ClientGet::new_default(
KEY::USK( KEY::USK(
USK { key
ssk: parsed.private_key.clone(),
path: format!("{}/{}", &id, &index),
}
), ),
&format!("rec;{};{}", &id, &index)[..], // TODO create Identifier type &format!("new-message-{}/{}", &id, &index)[..], // TODO create Identifier type
ReturnType::Direct, ReturnType::Direct,
) )
.convert(), .convert(),

View file

@ -41,7 +41,7 @@ pub fn select_n_last_messages(
conn: &Connection, conn: &Connection,
) -> Result<Vec<Message>> { ) -> Result<Vec<Message>> {
let mut selected = conn.prepare( let mut selected = conn.prepare(
"SELECT * FROM messages CROSS JOIN my_messages WHERE messages.user_id = ?1 or my_messages.user_id = ?1 ORDER BY date DESC LIMIT ?2", "SELECT * FROM messages WHERE user_id = ?1 ORDER BY date DESC LIMIT ?2",
)?; )?;
let message_iter = selected.query_map(params![user_id, count], |row| ret_mes(row))?; let message_iter = selected.query_map(params![user_id, count], |row| ret_mes(row))?;
let mut messages: Vec<Message> = Vec::new(); let mut messages: Vec<Message> = Vec::new();
@ -62,8 +62,8 @@ pub fn add_message(message: Message, conn: &Connection) -> Result<()> {
date, date,
message, message,
from_me from_me
) VALUES (?1, ?2, ?3, ?4)", ) VALUES (?1, ?2, ?3, ?4, ?5)",
params![message.id, message.user_id, message.date, message.message], params![message.id, message.user_id, message.date, message.message, false],
) { ) {
Ok(_) => log::info!("message {:} added succsessfully!", message.id), Ok(_) => log::info!("message {:} added succsessfully!", message.id),
Err(e) => { Err(e) => {
@ -76,7 +76,7 @@ pub fn add_message(message: Message, conn: &Connection) -> Result<()> {
pub fn add_my_message(message: Message, conn: &Connection) -> Result<()> { pub fn add_my_message(message: Message, conn: &Connection) -> Result<()> {
match conn.execute( match conn.execute(
"INSERT INTO my_messages ( "INSERT INTO messages (
id, id,
user_id, user_id,
date, date,

View file

@ -49,6 +49,7 @@ fn main() -> io::Result<()> {
SimpleLogger::new().init().unwrap(); SimpleLogger::new().init().unwrap();
let db = db::start_db().unwrap(); let db = db::start_db().unwrap();
//let manager = r2d2_foodb::FooConnectionManager::new("localhost:1234");
let (to_server_sender, server_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) = let (to_server_sender, server_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
mpsc::channel(); mpsc::channel();