diff --git a/src/client_management/client_profile.rs b/src/client_management/client_profile.rs deleted file mode 100644 index 5d1ac0c..0000000 --- a/src/client_management/client_profile.rs +++ /dev/null @@ -1,20 +0,0 @@ -#[derive(Clone)] -pub struct Client{ - pub uuid: String, - pub username: String, - pub address: String, -} - -impl Client{ - pub fn get_uuid(&self) -> &String{ - &self.uuid - } - - pub fn get_username(&self) -> &String{ - &self.username - } - - pub fn get_address(&self) -> &String{ - &self.address - } -} diff --git a/src/protocols.rs b/src/protocols.rs deleted file mode 100644 index 82b6da3..0000000 --- a/src/protocols.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod commands; diff --git a/src/protocols/commands/client_info.rs b/src/protocols/commands/client_info.rs deleted file mode 100644 index 011f541..0000000 --- a/src/protocols/commands/client_info.rs +++ /dev/null @@ -1,22 +0,0 @@ -use crate::client_management::client_profile::Client; - -use std::sync::Mutex; -use std::sync::Arc; -use std::collections::HashMap; - -pub fn get_client_data(clients_ref: &Arc>>, uuid: &String) -> String{ - let clients_hashmap = clients_ref.lock().unwrap(); - let client = clients_hashmap.get(uuid); - match client{ - Some(data) => { - let mut message = String::from("!success: "); - message.push_str(&data.get_uuid().to_string()); - message.push_str(&" host:".to_string()); - message.push_str(&data.get_address().to_string()); - message.push_str(&" ".to_string()); - message.push_str(&data.get_username().to_string()); - message - }, - None => String::from("client not online"), - } -} diff --git a/src/protocols/commands/connect.rs b/src/protocols/commands/connect.rs deleted file mode 100644 index 0c8e0ff..0000000 --- a/src/protocols/commands/connect.rs +++ /dev/null @@ -1,20 +0,0 @@ -use crate::client_management::client_profile::Client; - -use std::sync::Mutex; -use std::sync::Arc; -use std::collections::HashMap; - -pub fn add_client(clients_ref: &Arc>>, username: &String, uuid: &String, address: &String){ - let client = create_client_profile(username,uuid,address); - let mut clients_hashmap = clients_ref.lock().unwrap(); - //let cloned_client = client.clone(); - clients_hashmap.insert(uuid.to_string(),client); -} - -fn create_client_profile(username: &String, uuid: &String, address: &String) -> Client{ - Client{ - uuid: uuid.to_string(), - username: username.to_string(), - address: address.to_string(), - } -} \ No newline at end of file diff --git a/src/protocols/commands/disconnect.rs b/src/protocols/commands/disconnect.rs deleted file mode 100644 index 7242985..0000000 --- a/src/protocols/commands/disconnect.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::client_management::client_profile::Client; - -use std::sync::Mutex; -use std::sync::Arc; -use std::collections::HashMap; - -pub fn remove_client(clients_ref: &Arc>>, uuid: &String) -> Client{ - let mut clients_hashmap = clients_ref.lock().unwrap(); - clients_hashmap.remove(uuid).unwrap() -} diff --git a/src/protocols/commands/network.rs b/src/protocols/commands/network.rs deleted file mode 100644 index e3ddae2..0000000 --- a/src/protocols/commands/network.rs +++ /dev/null @@ -1,10 +0,0 @@ -use std::net::TcpStream; -use std::io::Write; - -pub fn transmit_data(mut stream: &TcpStream, data: &str){ - println!("Transmitting..."); - println!("data: {}",data); - - stream.write(data.to_string().as_bytes()).unwrap(); - stream.flush().unwrap(); -} diff --git a/src/client_management.rs b/src/server/client/mod.rs similarity index 100% rename from src/client_management.rs rename to src/server/client/mod.rs diff --git a/src/protocols/commands/client.rs b/src/server/commands/client.rs similarity index 100% rename from src/protocols/commands/client.rs rename to src/server/commands/client.rs diff --git a/src/protocols/commands/client_update.rs b/src/server/commands/client_update.rs similarity index 57% rename from src/protocols/commands/client_update.rs rename to src/server/commands/client_update.rs index 688d7f4..9de5a5f 100644 --- a/src/protocols/commands/client_update.rs +++ b/src/server/commands/client_update.rs @@ -1,4 +1,5 @@ -use crate::client_management::client_profile::Client; +use crate::server::client::client_profile::Client; +//use crate::client_management::client_profile::Client; // send a list of all clients. @@ -6,5 +7,5 @@ use crate::client_management::client_profile::Client; // 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() + ["!client: username:",client.get_username(), " uuid:", uuid, " host:\"", client.get_address(), "\""].concat() } diff --git a/src/protocols/commands/error.rs b/src/server/commands/error.rs similarity index 100% rename from src/protocols/commands/error.rs rename to src/server/commands/error.rs diff --git a/src/protocols/commands/info.rs b/src/server/commands/info.rs similarity index 75% rename from src/protocols/commands/info.rs rename to src/server/commands/info.rs index b43d4e1..f1ad46a 100644 --- a/src/protocols/commands/info.rs +++ b/src/server/commands/info.rs @@ -3,8 +3,9 @@ pub fn get_server_info() -> String{ let server_name = String::from("Server-01"); let server_owner = String::from("mickyb18"); + server_details.push_str(&"name:".to_string()); server_details.push_str(&server_name.to_string()); - server_details.push_str(&" ".to_string()); + server_details.push_str(&" owner:".to_string()); server_details.push_str(&server_owner.to_string()); server_details diff --git a/src/protocols/commands/message.rs b/src/server/commands/message.rs similarity index 100% rename from src/protocols/commands/message.rs rename to src/server/commands/message.rs diff --git a/src/protocols/commands.rs b/src/server/commands/mod.rs similarity index 56% rename from src/protocols/commands.rs rename to src/server/commands/mod.rs index 8c8ada5..7a58825 100644 --- a/src/protocols/commands.rs +++ b/src/server/commands/mod.rs @@ -9,9 +9,10 @@ mod client_info; mod client; mod test; mod message; -pub mod network; -use crate::client_management::client_profile::Client; +use crate::server::client::client_profile::Client; +use crate::server::utility; + use std::collections::VecDeque; use parking_lot::FairMutex; use std::sync::Mutex; @@ -20,6 +21,7 @@ use std::collections::HashMap; use std::io::{self, Read}; use std::net::TcpStream; use std::time::Duration; +use dashmap::DashMap; pub enum Commands{ Info, @@ -47,39 +49,41 @@ enum OutboundReturns{ } impl Commands{ - pub fn execute(&self, mut stream: &TcpStream, buffer: &mut [u8; 1024], data: &Vec, address: &String, clients_ref: &Arc>>, message_queue: &Arc>>){ + pub fn execute(&self, client: &mut Client, buffer: &mut [u8; 1024], data: &HashMap, clients_ref: &Arc>>, message_queue: &Arc>>){ + let stream = client.get_stream(); match *self{ Commands::Info => { let server_details = info::get_server_info(); let out_success = OutboundReturns::Success; - out_success.execute(stream, &server_details); + out_success.execute(&stream, &server_details); }, Commands::Connect => { - connect::add_client(clients_ref, &data[1], &data[2], address); + connect::add_client(clients_ref, client); - let mut message = "!client: ".to_string(); - message.push_str(&data[2].to_string()); - message.push_str(&" address:".to_string()); - message.push_str(&address.to_string()); - message.push_str(&" ".to_string()); - message.push_str(&data[1].to_string()); + let mut message = "!client: username:".to_string(); + message.push_str(&client.get_username().to_string()); + message.push_str(&" host:".to_string()); + message.push_str(&client.get_address().to_string()); + message.push_str(&" uuid:".to_string()); + message.push_str(&client.get_uuid().to_string()); message_queue.lock().push_back(message); let out_success = OutboundReturns::Success; - out_success.execute(stream, &String::from("")); + out_success.execute(&stream, &String::from("")); }, Commands::Disconnect => { - let client_profile = disconnect::remove_client(clients_ref, &data[1]); - - let mut message = "!clientRemove: ".to_string(); - message.push_str(&client_profile.get_uuid().to_string()); + disconnect::remove_client(clients_ref, client); + let mut message = "!clientRemove: uuid:".to_string(); + message.push_str(&client.get_uuid().to_string()); message_queue.lock().push_back(message); let out_success = OutboundReturns::Success; - out_success.execute(stream, &String::from("")); + out_success.execute(&stream, &String::from("")); + client.disconnect(); + println!("disconnected!"); }, Commands::ClientUpdate => { let in_success = InboundReturns::Success; @@ -87,19 +91,19 @@ impl Commands{ let clients_hashmap = clients_ref.lock().unwrap(); for (key, value) in clients_hashmap.iter(){ let formatted_data = client_update::format_client_data(&key, &value); - network::transmit_data(stream, &formatted_data); + utility::transmit_data(&stream, &formatted_data); - in_success.execute(stream, buffer, &formatted_data); + in_success.execute(&stream, buffer, &formatted_data); } let out_success = OutboundReturns::Success; - out_success.execute(stream, &String::from("")); + out_success.execute(&stream, &String::from("")); - in_success.execute(stream, buffer, &String::from("!success:")); + in_success.execute(&stream, buffer, &String::from("!success:")); }, Commands::ClientInfo => { - let requested_data = client_info::get_client_data(clients_ref, &data[1]); - network::transmit_data(stream, &requested_data); + let requested_data = client_info::get_client_data(clients_ref, data); + utility::transmit_data(&stream, &requested_data); }, Commands::Unknown => { println!("Uknown Command!"); @@ -109,19 +113,34 @@ impl Commands{ } impl OutboundCommands{ - pub fn execute(&self, mut stream: &TcpStream, buffer: &mut [u8; 1024], data: &String){ + pub fn execute(&self, client: &Client, buffer: &mut [u8; 1024], data: &HashMap){ + let stream = client.get_stream(); match *self{ OutboundCommands::Client => { - network::transmit_data(stream, data); + let mut message = String::from(""); + message.push_str(&data.get("command").unwrap()); + message.push_str(&" username:"); + message.push_str(&data.get("username").unwrap()); + message.push_str(&" host:"); + message.push_str(&data.get("host").unwrap()); + message.push_str(&" uuid:"); + message.push_str(&data.get("uuid").unwrap()); + + utility::transmit_data(&stream, &message); let in_success = InboundReturns::Success; - in_success.execute(stream, buffer, data); + in_success.execute(&stream, buffer, &message); }, OutboundCommands::ClientRemove => { - network::transmit_data(stream, data); + let mut message = String::from(""); + message.push_str(&data.get("command").unwrap()); + message.push_str(&" uuid:"); + message.push_str(&data.get("uuid").unwrap()); + + utility::transmit_data(&stream, &message); let in_success = InboundReturns::Success; - in_success.execute(stream, buffer, data); + in_success.execute(&stream, buffer, &message); }, OutboundCommands::Unknown => { println!("Unknown Command!"); @@ -142,7 +161,7 @@ impl InboundReturns{ match e.kind() { io::ErrorKind::WouldBlock => { println!("Blocking..."); - network::transmit_data(stream, data); + utility::transmit_data(stream, data); }, _ => panic!("Fatal Error {}", e), } @@ -160,7 +179,7 @@ impl InboundReturns{ } impl OutboundReturns{ - pub fn execute(&self, mut stream: &TcpStream, data: &String){ + pub fn execute(&self, stream: &TcpStream, data: &String){ match *self{ OutboundReturns::Success => { let mut message = "!success:".to_string(); @@ -168,7 +187,7 @@ impl OutboundReturns{ message.push_str(&" ".to_string()); message.push_str(&data.to_string()); } - network::transmit_data(stream, &message); + utility::transmit_data(stream, &message); }, OutboundReturns::Error => {}, } diff --git a/src/protocols/commands/request.rs b/src/server/commands/request.rs similarity index 100% rename from src/protocols/commands/request.rs rename to src/server/commands/request.rs diff --git a/src/protocols/commands/success.rs b/src/server/commands/success.rs similarity index 100% rename from src/protocols/commands/success.rs rename to src/server/commands/success.rs diff --git a/src/protocols/commands/test.rs b/src/server/commands/test.rs similarity index 100% rename from src/protocols/commands/test.rs rename to src/server/commands/test.rs