From edf468ab02503dfa907576f867625b7e96163409 Mon Sep 17 00:00:00 2001 From: carlosb1 Date: Wed, 7 Dec 2022 05:03:41 +0100 Subject: [PATCH] Multiple brainstorming Signed-off-by: carlosb1 --- p2p-playground/tracker/Cargo.lock | 294 ++++++++++++++++++++++- p2p-playground/tracker/Cargo.toml | 6 +- p2p-playground/tracker/src/devices.rs | 15 ++ p2p-playground/tracker/src/domain.rs | 112 +++++++-- p2p-playground/tracker/src/entrypoint.rs | 2 +- p2p-playground/tracker/src/lib.rs | 1 + p2p-playground/tracker/src/main.rs | 27 ++- 7 files changed, 427 insertions(+), 30 deletions(-) diff --git a/p2p-playground/tracker/Cargo.lock b/p2p-playground/tracker/Cargo.lock index b54785c..bae3dff 100644 --- a/p2p-playground/tracker/Cargo.lock +++ b/p2p-playground/tracker/Cargo.lock @@ -13,6 +13,26 @@ dependencies = [ "version_check", ] +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -25,6 +45,21 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array", +] + +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + [[package]] name = "bytes" version = "1.3.0" @@ -37,6 +72,46 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cpufeatures" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320" +dependencies = [ + "libc", +] + +[[package]] +name = "crc32fast" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01a9af1f4c2ef74bb8aa1f7e19706bc72d03598c8a570bb5de72243c7a9d9d5a" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f" +dependencies = [ + "cfg-if", +] + [[package]] name = "ctor" version = "0.1.26" @@ -53,12 +128,34 @@ version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + [[package]] name = "dyn-clone" version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4f94fa09c2aeea5b8839e414b7b841bf429fd25b9c522116ac97ee87856d88b2" +[[package]] +name = "env_logger" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "fnv" version = "1.0.7" @@ -66,10 +163,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] -name = "fragile" -version = "2.0.0" +name = "fs2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c2141d6d6c8512188a7891b4b01590a45f6dac67afb4f255c4124dbb86d4eaa" +checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213" +dependencies = [ + "libc", + "winapi", +] [[package]] name = "futures" @@ -167,6 +268,25 @@ dependencies = [ "slab", ] +[[package]] +name = "fxhash" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" +dependencies = [ + "byteorder", +] + +[[package]] +name = "generic-array" +version = "0.14.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getrandom" version = "0.2.8" @@ -196,6 +316,12 @@ dependencies = [ "libc", ] +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "http" version = "0.2.8" @@ -207,6 +333,24 @@ dependencies = [ "itoa", ] +[[package]] +name = "humantime" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" +dependencies = [ + "quick-error", +] + +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + [[package]] name = "itoa" version = "1.0.4" @@ -244,6 +388,15 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +[[package]] +name = "memoffset" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" +dependencies = [ + "autocfg", +] + [[package]] name = "mio" version = "0.8.5" @@ -272,6 +425,12 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + [[package]] name = "output_vt100" version = "0.1.3" @@ -281,6 +440,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "parking_lot" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core 0.8.5", +] + [[package]] name = "parking_lot" version = "0.12.1" @@ -288,7 +458,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" dependencies = [ "lock_api", - "parking_lot_core", + "parking_lot_core 0.9.4", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d76e8e1493bcac0d2766c42737f34458f1c8c50c0d23bcb24ea953affb273216" +dependencies = [ + "cfg-if", + "instant", + "libc", + "redox_syscall", + "smallvec", + "winapi", ] [[package]] @@ -328,6 +512,16 @@ dependencies = [ "yansi", ] +[[package]] +name = "pretty_env_logger" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d" +dependencies = [ + "env_logger", + "log", +] + [[package]] name = "proc-macro2" version = "1.0.47" @@ -337,6 +531,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quote" version = "1.0.21" @@ -355,6 +555,23 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" + [[package]] name = "rstest" version = "0.16.0" @@ -436,6 +653,29 @@ dependencies = [ "serde", ] +[[package]] +name = "sha2" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" +dependencies = [ + "block-buffer", + "cfg-if", + "cpufeatures", + "digest", + "opaque-debug", +] + +[[package]] +name = "sha256" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e334db67871c14c18fc066ad14af13f9fdf5f9a91c61af432d1e3a39c8c6a141" +dependencies = [ + "hex", + "sha2", +] + [[package]] name = "signal-hook-registry" version = "1.4.0" @@ -454,6 +694,22 @@ dependencies = [ "autocfg", ] +[[package]] +name = "sled" +version = "0.34.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f96b4737c2ce5987354855aed3797279def4ebf734436c6aa4552cf8e169935" +dependencies = [ + "crc32fast", + "crossbeam-epoch", + "crossbeam-utils", + "fs2", + "fxhash", + "libc", + "log", + "parking_lot 0.11.2", +] + [[package]] name = "smallvec" version = "1.10.0" @@ -481,6 +737,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + [[package]] name = "tokio" version = "1.22.0" @@ -493,7 +758,7 @@ dependencies = [ "memchr", "mio", "num_cpus", - "parking_lot", + "parking_lot 0.12.1", "pin-project-lite", "signal-hook-registry", "socket2", @@ -567,20 +832,28 @@ version = "0.1.0" dependencies = [ "bytes", "dyn-clone", - "fragile", "futures", "http", "log", "pretty_assertions", + "pretty_env_logger", "rstest", "serde", "serde_derive", "serde_json", + "sha256", + "sled", "tokio", "tokio-stream", "tokio-util", ] +[[package]] +name = "typenum" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" + [[package]] name = "unicode-ident" version = "1.0.5" @@ -615,6 +888,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/p2p-playground/tracker/Cargo.toml b/p2p-playground/tracker/Cargo.toml index 292bade..2718812 100644 --- a/p2p-playground/tracker/Cargo.toml +++ b/p2p-playground/tracker/Cargo.toml @@ -16,8 +16,10 @@ serde = "1.0" serde_derive = "1.0" serde_json = "1.0" log="0.4" -fragile="2.0.0" -dyn-clone = "1.0.9" +pretty_env_logger = "0.4" +dyn-clone = "1.0" +sled = "0.34" +sha256 = "1.1" [dev-dependencies] rstest = "0.16.0" diff --git a/p2p-playground/tracker/src/devices.rs b/p2p-playground/tracker/src/devices.rs index 9c3fd90..c943f0f 100644 --- a/p2p-playground/tracker/src/devices.rs +++ b/p2p-playground/tracker/src/devices.rs @@ -61,3 +61,18 @@ pub async fn send(address: &str, mesg: &str) -> Result, Box Message { + Message { + operation: "ok".to_string(), + ..Default::default() + } + } pub fn new_user(user: String, address_source: String) -> Message { let mut info: HashMap = HashMap::new(); @@ -28,35 +36,99 @@ impl Message { } } -pub struct User { - pub username: String, - pub source_address: String, +#[derive(Clone)] +pub enum Type { + User, + File, } -impl User { - pub fn new(username: String, source_address: String) -> Self { - User { - username, - source_address, +#[derive(Clone)] +struct Node { + typ: Type, + user: String, + address: String, +} +impl Node { + pub fn new(user: &str, address: &str, typ: Type) -> Self { + Node { + user: user.to_string(), + address: address.to_string(), + typ, } } } -pub fn add_new_user(user_repo: &mut HashMap, user: User) { - (*user_repo).insert(user.username.clone(), user); +use std::collections::BTreeMap; + +struct Hasher {} +impl Hasher { + pub fn hash(self, info: String) -> String { + digest(info).to_string() + } +} + +//TODO move to devices +struct NodeRepository { + data: BTreeMap, +} + +impl NodeRepository { + pub fn add(&mut self, node: &Node) { + let hasher = Hasher {}; + let key = hasher.hash(format!("{:}-{:}", node.user, node.address)); + self.data.insert(key, (*node).clone()); + } +} + +struct RegisterNewNode { + repository: Arc, +} +impl RegisterNewNode { + const USER_PARAM: &str = "user_param"; + const ADDRESS: &str = "address"; + + pub fn new(repository: Arc) -> Self { + RegisterNewNode { repository } + } + + pub fn run(message: Message) -> Option { + let user = message.info.get(&RegisterNewNode::USER_PARAM.to_string())?; + let address = message.info.get(&RegisterNewNode::ADDRESS.to_string())?; + let node = Node::new(user, address, Type::User); + + Some(Message::ok()) + } } #[cfg(test)] mod tests { - use super::*; - use pretty_assertions::assert_eq; - use rstest::*; - - #[rstest] - fn should_add_a_new_user() { - let mut user_repo = HashMap::new(); - let new_user = User::new("user1".to_string(), "192.168.1.1".to_string()); - add_new_user(&mut user_repo, new_user); - assert_eq!(user_repo.len(), 1); + mod messages { + use crate::domain::*; + use pretty_assertions::assert_eq; + use rstest::*; + #[rstest] + pub fn ok_should_be_correctly() { + assert_eq!(Message::ok().operation, "ok".to_string()); + } + } + mod cases { + pub fn register_new_node_correctly() {} + } + mod devices { + mod repositories { + pub fn add_new_node() {} + } + mod hashers { + use crate::domain::Hasher; + use rstest::*; + #[rstest] + pub fn new_hash() { + let hasher = Hasher {}; + assert_eq!( + "50d858e0985ecc7f60418aaf0cc5ab587f42c2570a884095a9e8ccacd0f6545c", + hasher.hash("example".to_string()) + ); + } + } } } diff --git a/p2p-playground/tracker/src/entrypoint.rs b/p2p-playground/tracker/src/entrypoint.rs index 54ffa3b..e724adc 100644 --- a/p2p-playground/tracker/src/entrypoint.rs +++ b/p2p-playground/tracker/src/entrypoint.rs @@ -48,7 +48,7 @@ impl Server { self, address: String, _user: String, - manager: Arc>>, + manager: Arc>, ) -> Result<(), Box> { info!("Trying to connect to {}", address); diff --git a/p2p-playground/tracker/src/lib.rs b/p2p-playground/tracker/src/lib.rs index d7aab63..9281935 100644 --- a/p2p-playground/tracker/src/lib.rs +++ b/p2p-playground/tracker/src/lib.rs @@ -3,4 +3,5 @@ mod domain; mod entrypoint; mod services; +extern crate log; extern crate serde_derive; diff --git a/p2p-playground/tracker/src/main.rs b/p2p-playground/tracker/src/main.rs index 8d24df8..8c7cb56 100644 --- a/p2p-playground/tracker/src/main.rs +++ b/p2p-playground/tracker/src/main.rs @@ -1,3 +1,28 @@ +mod devices; +mod domain; +mod entrypoint; +mod services; + +extern crate serde_derive; + +use crate::entrypoint::Server; +use crate::services::MessageManager; +use futures::lock::Mutex; +use log::info; +use std::sync::Arc; + +use tokio::runtime::Runtime; + pub fn main() { - println!("My first tracker!!"); + pretty_env_logger::init(); + let rt = Runtime::new().unwrap(); + + let user = "user".to_string(); + let address = "127.0.0.1:12345".to_string(); + + let message_manager = MessageManager::new(); + let message_manager: Arc> = Arc::new(Mutex::new(message_manager)); + let server = Server {}; + info!("Running our server"); + let _ = rt.block_on(server.run(address, user, message_manager)); }