diff --git a/bin/u-agent/src/main.rs b/bin/u-agent/src/main.rs index 835e528..a250c74 100644 --- a/bin/u-agent/src/main.rs +++ b/bin/u-agent/src/main.rs @@ -8,26 +8,25 @@ // самоуничтожение //mod jobs; -use std::process::Command; + +//use std::process::Command; use std::thread::sleep; use std::time::Duration; mod network; use network::ClientHandler; - #[tokio::main] async fn main() { //daemonize(); let instance = ClientHandler::new(); instance.init().await; - instance.list().await; - /*loop { - + loop { + instance.list().await; sleep(Duration::from_secs(2)); /*Command::new("touch") .arg("/tmp/win2") .output() .unwrap();*/ - }*/ + } } diff --git a/bin/u-agent/src/network.rs b/bin/u-agent/src/network.rs index 2f1fd1e..c1b28d7 100644 --- a/bin/u-agent/src/network.rs +++ b/bin/u-agent/src/network.rs @@ -8,8 +8,6 @@ use reqwest::{ Url, Response }; -use uuid::Uuid; -use std::collections::HashMap; pub struct ClientHandler { @@ -24,10 +22,7 @@ impl ClientHandler { client: Client::new(), cli_info: ClientInfo::gather(), base_url: Url::parse( - &format!("http://{}:{}", - MASTER_SERVER, - MASTER_PORT - ) + &format!("http://{}:{}", MASTER_SERVER, MASTER_PORT) ).unwrap() } } @@ -35,10 +30,7 @@ impl ClientHandler { pub async fn init(&self) { let response: Response = self.client .post(self.base_url.join("/new").unwrap()) - .json(&Message::new( - &self.cli_info.id, - self.cli_info.clone() - )) + .json(&self.cli_info.as_message()) .send() .await .unwrap(); @@ -47,7 +39,6 @@ impl ClientHandler { response .json::>() .await - .unwrap() ); } @@ -58,11 +49,12 @@ impl ClientHandler { .await .unwrap(); let clients = response - .json::>() + .json::>>() .await .unwrap(); - for cli in clients.item { - println!("{}: {:?}", cli.0, cli.1); + println!("***********"); + for cli in clients.item.iter() { + println!("{:?}", cli); } } -} \ No newline at end of file +} diff --git a/bin/u-server/src/main.rs b/bin/u-server/src/main.rs index f721963..ba6a8d7 100644 --- a/bin/u-server/src/main.rs +++ b/bin/u-server/src/main.rs @@ -2,13 +2,11 @@ use warp::{ Filter, Rejection, Reply, - body, - http::StatusCode + body }; use std::{ collections::HashMap, - sync::Arc, - marker::Send + sync::Arc }; use env_logger; use tokio::sync::Mutex; @@ -20,34 +18,28 @@ use u_lib::{ contracts::* }; -use serde::Deserialize; - type SharedStorage = Arc>; -fn get_content() --> impl Filter,), Error = Rejection> + Clone { +fn get_content() -> impl Filter< + Extract = (Message<'static, ClientInfo>,), + Error = Rejection +> + Clone { body::content_length_limit(1024*64).and(body::json::>()) } -async fn add_client(msg: Message, db: SharedStorage) -> +async fn add_client(msg: Message<'_, ClientInfo>, db: SharedStorage) -> Result { let new_cli = msg.item; let mut clients = db.lock().await; if clients.contains_key(&new_cli.id) { Ok(warp::reply::json( - &Message::new( - &new_cli.id, - &RawMsg("Already exist".to_string()) - ) + &RawMsg("Already exist".to_string()).into_message() )) } else { - let id = new_cli.id.clone(); - clients.insert(new_cli.id.clone(), new_cli); + clients.insert(new_cli.id.clone(), new_cli.into_owned()); Ok(warp::reply::json( - &Message::new( - &id, - &RawMsg("Added".to_string()) - )) + &RawMsg("Added".to_string()).into_message() + ) ) } } @@ -55,8 +47,12 @@ Result { async fn listing(db: SharedStorage) -> Result { let clients = db.lock().await; + let mut result: Vec = Vec::with_capacity(clients.len()); + for cli in clients.values() { + result.push(cli.clone()); + } Ok(warp::reply::json( - &Message::new(&Uuid::nil(), clients) + &Message::new_owned(result) )) } @@ -69,9 +65,6 @@ async fn main() { ); let db = warp::any().map(move || Arc::clone(&base_db)); - let hello = warp::get().and(warp::path::param()) - .map(|p: String| format!("Hello, {}", p)); - let new_client = warp::post() .and(warp::path("new")) .and(get_content()) @@ -83,10 +76,9 @@ async fn main() { .and(db.clone()) .and_then(listing); - let routes = hello - .or(new_client) + let routes = new_client .or(ls) .with(warp::log("warp")); warp::serve(routes) - .run((MASTER_SERVER.octets(), MASTER_PORT)).await; + .run(([0,0,0,0], MASTER_PORT)).await; } diff --git a/lib/u_lib/Cargo.toml b/lib/u_lib/Cargo.toml index 2ada440..2ebea39 100644 --- a/lib/u_lib/Cargo.toml +++ b/lib/u_lib/Cargo.toml @@ -11,3 +11,4 @@ serde = { version = "1.0.114", features = ["derive"] } uuid = { version = "^0.8.1", features = ["serde", "v4"] } nix = "0.17" libc = "^0.2" +lazy_static = "1.4.0" diff --git a/lib/u_lib/src/config.rs b/lib/u_lib/src/config.rs index 1f46e96..382840a 100644 --- a/lib/u_lib/src/config.rs +++ b/lib/u_lib/src/config.rs @@ -1,4 +1,9 @@ use std::net::Ipv4Addr; +use uuid::Uuid; -pub const MASTER_SERVER: Ipv4Addr = Ipv4Addr::LOCALHOST; -pub const MASTER_PORT: u16 = 63714; \ No newline at end of file +pub const MASTER_SERVER: Ipv4Addr = Ipv4Addr::new(3,9,16,40); //Ipv4Addr::LOCALHOST; +pub const MASTER_PORT: u16 = 63714; + +lazy_static! { + pub static ref UID: Uuid = Uuid::new_v4(); +} \ No newline at end of file diff --git a/lib/u_lib/src/contracts/client.rs b/lib/u_lib/src/contracts/client.rs index 1ab2a6a..2d2bd03 100644 --- a/lib/u_lib/src/contracts/client.rs +++ b/lib/u_lib/src/contracts/client.rs @@ -2,9 +2,18 @@ use serde::{ Deserialize, Serialize }; +use std::{ + collections::HashMap +}; use uuid::Uuid; +use super::{ + ToMsg +}; +use crate::UID; + +pub type CliStorage = HashMap; -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct ClientInfo { pub local_ip: String, pub hostname: String, @@ -19,11 +28,12 @@ impl ClientInfo { ClientInfo { local_ip: String::from("1.2.3.4"), hostname: String::from("polokonzerva"), - username: String::from("plazmoid (lol what a stupid name)"), + username: String::from("plazmoid"), os: String::from("pinux"), platform: String::from("x86_64"), - id: Uuid::new_v4() + id: *UID } } } +impl ToMsg for ClientInfo {} \ No newline at end of file diff --git a/lib/u_lib/src/contracts/messaging.rs b/lib/u_lib/src/contracts/messaging.rs index 18c1aa1..64c9a20 100644 --- a/lib/u_lib/src/contracts/messaging.rs +++ b/lib/u_lib/src/contracts/messaging.rs @@ -3,28 +3,51 @@ use serde::{ Serialize, Deserialize }; -use super::ClientInfo; -use std::collections::HashMap; +use std::{ + borrow::Cow +}; +use crate::UID; + +pub trait ToMsg +where Self: Clone { + fn as_message<'m>(&'m self) -> Message<'m, Self> + where Cow<'m, Self>: From<&'m Self> { + Message::new(self) + } -pub type CliStorage = HashMap; + fn into_message(self) -> Message<'static, Self> { + Message::new_owned(self) + } +} #[derive(Serialize, Deserialize, Debug)] -pub struct Message { +pub struct Message<'cow, I> +where I: Clone { pub id: Uuid, - pub item: I + pub item: Cow<'cow, I> } -impl Message { - pub fn new(id: &Uuid, item: I) -> Self { +impl<'cow, I> Message<'cow, I> +where I: Clone { + pub fn new(item: C) -> Self + where C: Into> { Self { - id: id.clone(), - item + id: *UID, + item: item.into() + } + } + + // crutch + // because Vec is stored as &[] (wtf?) + pub fn new_owned(item: I) -> Self { + Self { + id: *UID, + item: Cow::Owned(item) } } } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct RawMsg(pub String); -#[derive(Serialize, Deserialize, Debug)] -pub struct Empty; \ No newline at end of file +impl ToMsg for RawMsg {} \ No newline at end of file diff --git a/lib/u_lib/src/contracts/mod.rs b/lib/u_lib/src/contracts/mod.rs index 01122a4..b96b2e8 100644 --- a/lib/u_lib/src/contracts/mod.rs +++ b/lib/u_lib/src/contracts/mod.rs @@ -4,4 +4,30 @@ pub mod messaging; pub use { client::*, messaging::* -}; \ No newline at end of file +}; + +use std::{ + borrow::Cow +}; + + +macro_rules! to_cow { + ($type:ty) => { + impl<'cow> From<$type> for Cow<'cow, $type> { + #[inline] + fn from(obj: $type) -> Cow<'cow, $type> { + Cow::Owned(obj) + } + } + + impl<'cow> From<&'cow $type> for Cow<'cow, $type> { + #[inline] + fn from(obj: &'cow $type) -> Cow<'cow, $type> { + Cow::Borrowed(obj) + } + } + } +} + +to_cow!(ClientInfo); +to_cow!(RawMsg); \ No newline at end of file diff --git a/lib/u_lib/src/lib.rs b/lib/u_lib/src/lib.rs index 8c40c85..dcb2823 100644 --- a/lib/u_lib/src/lib.rs +++ b/lib/u_lib/src/lib.rs @@ -4,3 +4,6 @@ pub mod utils; pub use utils::*; pub use config::*; + +#[macro_use] +extern crate lazy_static;