add unimplemented request_handler

This commit is contained in:
Horhik 2021-03-24 21:35:20 +03:00
parent 0a34a0b210
commit cc92837d64
6 changed files with 47 additions and 31 deletions

View file

@ -1 +0,0 @@
use rusqlite;

9
src/api/handlers.rs Normal file
View file

@ -0,0 +1,9 @@
use rusqlite;
use crate::chat::types::SP;
use async_std::io::Result;
use rusqlite::Connection;
// server_sender sending data to server thread;
pub async fn request_handler(json: String, server_sender: &SP, conn: &Connection) -> Result<()>{
unimplemented!()
}

View file

@ -1,3 +1,3 @@
pub mod request; pub mod request;
pub mod response; pub mod response;
pub mod handlers;

View file

@ -1 +0,0 @@
use

View file

@ -16,8 +16,9 @@ use std::sync::mpsc::{Receiver, Sender};
use super::stay_awake::request_repeater; use super::stay_awake::request_repeater;
type SP = Sender<PackedMessage>; use super::types::{RP, SP};
type RP = Receiver<PackedMessage>;
use crate::api::handlers::request_handler;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct FrontMsg { struct FrontMsg {
@ -27,12 +28,19 @@ struct FrontMsg {
time: String, time: String,
} }
pub fn listen_client(server_sender: SP, client_receiver: RP) -> io::Result<()> { pub fn listen_client(
task::block_on(connect_to_client(server_sender, client_receiver)) server_sender: SP,
client_receiver: RP,
conn: rusqlite::Connection,
) -> io::Result<()> {
task::block_on(connect_to_client(server_sender, client_receiver, conn))
} }
async fn connect_to_client(
async fn connect_to_client(server_sender: SP, client_receiver: RP) -> io::Result<()> { server_sender: SP,
client_receiver: RP,
conn: rusqlite::Connection,
) -> io::Result<()> {
let addr = env::args() let addr = env::args()
.nth(1) .nth(1)
.unwrap_or_else(|| "127.0.0.1:5948".to_string()); .unwrap_or_else(|| "127.0.0.1:5948".to_string());
@ -53,7 +61,7 @@ async fn connect_to_client(server_sender: SP, client_receiver: RP) -> io::Result
log::info!("connected to: {}", addr); log::info!("connected to: {}", addr);
let (sender, receiver) = ws.split(); let (sender, receiver) = ws.split();
let t1 = task::spawn(connection_for_sending(receiver, server_sender)); let t1 = task::spawn(connection_for_sending(receiver, server_sender, conn));
connection_for_receiving(sender, client_receiver, client_repeater).await?; connection_for_receiving(sender, client_receiver, client_repeater).await?;
t1.await?; t1.await?;
} }
@ -86,22 +94,22 @@ async fn connection_for_receiving(
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,
conn: rusqlite::Connection,
) -> io::Result<()> { ) -> io::Result<()> {
log::info!("Connection for sending launched"); log::info!("Connection for sending launched");
let mut new_msg = receiver.next(); let mut new_msg = receiver.next();
loop { loop {
match new_msg.await { if let Some(msg) = new_msg.await {
Some(msg) => {
let jsoned = msg.expect("Falied to unwrap gotted message"); let jsoned = msg.expect("Falied to unwrap gotted message");
log::info!("The message is: {:?}", &jsoned); request_handler(jsoned.to_string(), &server_sender, &conn);
//TODO handle_request(jsoned, server_sender,); /*
let res: serde_json::Result<FrontMsg> = let res: serde_json::Result<FrontMsg> =
serde_json::from_str(jsoned.to_text().expect("Falied to parse JSON")); serde_json::from_str(jsoned.to_text().expect("Falied to parse JSON"));
if let Ok(received_msg) = res { if let Ok(received_msg) = res {
let msg = received_msg.message; let msg = received_msg.message;
//db::start_db().expect("Failed to start db"); //db::start_db().expect("Failed to start db");
server_sender.send(PackedMessage { message: msg }).expect("Falied to send message"); ss.send(PackedMessage { message: msg }).expect("Falied to send message");
/* message example /* message example
{ {
"user_id": 123456789, "user_id": 123456789,
@ -114,10 +122,11 @@ async fn connection_for_sending(
log::info!("seems, that messsage formatted wrong"); log::info!("seems, that messsage formatted wrong");
log::info!("{:?}", res); log::info!("{:?}", res);
} }
*/
new_msg = receiver.next(); new_msg = receiver.next();
} } else {
None => { {
break; break;
} }
} }

View file

@ -49,7 +49,7 @@ use std::{
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
SimpleLogger::new().init().unwrap(); SimpleLogger::new().init().unwrap();
let db = db::start_db(); let db = db::start_db().unwrap();
let (to_server_sender, server_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) = let (to_server_sender, server_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
mpsc::channel(); mpsc::channel();
@ -70,7 +70,7 @@ fn main() -> io::Result<()> {
let ss = to_server_sender; let ss = to_server_sender;
let cr = client_receiver; let cr = client_receiver;
let t = thread::spawn(move || listen_client(ss, cr)); let t = thread::spawn(move || listen_client(ss, cr, db));
t.join().expect("failed client thread").unwrap(); t.join().expect("failed client thread").unwrap();
}); });