From 830e5c9d64627c961b069966bfa45da3126e101e Mon Sep 17 00:00:00 2001 From: Mitchell Date: Sun, 7 Jun 2020 20:16:45 +0100 Subject: [PATCH] added fetch all clients system --- Cargo.toml | 6 ++++++ src/lib.rs | 5 +---- src/main.rs | 4 +--- src/protocols/commands.rs | 11 +++++++++++ src/protocols/commands/client_update.rs | 11 ++++++++--- src/protocols/commands/connect.rs | 2 +- 6 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 457d548..aacbbd9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,9 @@ edition = "2018" [dependencies] regex = "1" + +[profile.dev] +opt-level = 0 + +[profile.release] +opt-level = 3 diff --git a/src/lib.rs b/src/lib.rs index b69a038..89cbe0e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,10 +43,7 @@ impl ThreadPool{ } } - pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static - { + pub fn execute(&self, f: F) where F: FnOnce() + Send + 'static { let job = Box::new(f); self.sender.send(Message::NewJob(job)).unwrap(); diff --git a/src/main.rs b/src/main.rs index fcbf9dc..77baa6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ * If no success, request ip from server to double check if theyre online, if ip match, assume * theyre offline. If no response from server assume some error has occured or sender is offline. * Save messages to be sent and check every few mins to see if they are online. + * */ mod client_management; @@ -30,9 +31,6 @@ use regex::Regex; fn main(){ let listener = TcpListener::bind("127.0.0.1:6001").unwrap(); let pool = ThreadPool::new(10); - /* - * Always dedicate 1/4 size of the connected users for updating clients - */ let connected_clients = Arc::new(Mutex::new(HashMap::new())); loop{ diff --git a/src/protocols/commands.rs b/src/protocols/commands.rs index 6145e54..ce9bc65 100644 --- a/src/protocols/commands.rs +++ b/src/protocols/commands.rs @@ -57,6 +57,17 @@ impl Commands{ disconnect::remove_client(clients_ref, &data[1]); } Commands::ClientUpdate => { + let message = String::from("!success:"); + Commands::transmit_data(&stream, &message); + + let clients_hashmap = clients_ref.lock().unwrap(); + for (key, value) in clients_hashmap.iter(){ + let formatted_data = client_update::format_client_data(&key, &value); + Commands::transmit_data(&stream, &formatted_data); + } + + let final_message = String::from("!finished:"); + Commands::transmit_data(&stream, &final_message); } Commands::ClientInfo => { let message = String::from("!success:"); diff --git a/src/protocols/commands/client_update.rs b/src/protocols/commands/client_update.rs index c5ad4ff..688d7f4 100644 --- a/src/protocols/commands/client_update.rs +++ b/src/protocols/commands/client_update.rs @@ -1,5 +1,10 @@ use crate::client_management::client_profile::Client; -use std::sync::Mutex; -use std::sync::Arc; -use std::collections::HashMap; +// send a list of all clients. + +// client !clientupdate: +// server !client: name:"vobo" uuid:24653526-23464562-46346-3563563 host:"127.0.0.1" +// server !client: name:"bovo" uuid:24643526-23464562-46346-3563563 host:"127.0.0.1" +pub fn format_client_data(uuid: &String, client: &Client) -> String{ + ["!client: ",client.get_username(), uuid, " host:\"", client.get_address(), "\""].concat() +} diff --git a/src/protocols/commands/connect.rs b/src/protocols/commands/connect.rs index e0fe817..0c8e0ff 100644 --- a/src/protocols/commands/connect.rs +++ b/src/protocols/commands/connect.rs @@ -17,4 +17,4 @@ fn create_client_profile(username: &String, uuid: &String, address: &String) -> username: username.to_string(), address: address.to_string(), } -} +} \ No newline at end of file