added modules for commands

each file represents a module for that command, which is all handled by the commands.rs file
This commit is contained in:
Mitchell 2020-06-04 13:49:50 +00:00
parent 08e4c1e324
commit db2443aa74
11 changed files with 139 additions and 0 deletions

80
src/protocols/commands.rs Normal file
View File

@ -0,0 +1,80 @@
mod request;
mod info;
mod success;
mod error;
mod connect;
mod disconnect;
mod client_update;
mod client;
mod test;
mod message;
//use connect::Connect;
//use crate::protocols::commands::connect::Connect;
//use crate::protocols::commands::client_update::ClientUpdate;
//use crate::protocols::commands::client::ClientQ;
use crate::client_management::client_profile::Client;
use std::sync::Mutex;
use std::sync::Arc;
use std::collections::HashMap;
use std::net::TcpStream;
use std::io::Write;
pub enum Commands{
Request,
Info,
Success,
Error,
Connect,
Disconnect,
ClientUpdate,
Client,
Test,
Message,
Unknown,
}
impl Commands{
pub fn execute(&self, stream: TcpStream, data: &Vec<String>, address: &String, clients_ref: &Arc<Mutex<HashMap<String,Client>>>){
match *self{
Commands::Request => {
}
Commands::Info => {
}
Commands::Success => {
}
Commands::Error => {
}
Commands::Connect => {
connect::add_new_client(clients_ref, &data[1], &data[2], address);
}
Commands::Disconnect => {
}
Commands::ClientUpdate => {
let address = client_update::get_client_address(clients_ref, &data[1]);
Commands::transmit_data(stream, &address);
}
Commands::Client => {
let address = client::retrieve_requested_clients(clients_ref, &data[1]);
Commands::transmit_data(stream, &address);
}
Commands::Test => {
}
Commands::Message => {
}
Commands::Unknown => {
println!("Uknown Command!");
}
}
}
fn transmit_data(mut stream: TcpStream, data: &str){
println!("Transmitting...");
println!("data: {}",data);
stream.write(data.to_string().as_bytes()).unwrap();
stream.flush().unwrap();
}
}

View File

@ -0,0 +1,24 @@
use crate::client_management::client_profile::Client;
use std::sync::Mutex;
use std::sync::Arc;
use std::collections::HashMap;
pub fn retrieve_requested_clients(clients_ref: &Arc<Mutex<HashMap<String,Client>>>, uuid: &String) -> String{
//let address = String::new();
//if data[1].starts_with("uuid:"){
get_client_address(clients_ref, uuid)
//}else if data[1].starts_with("name:"){
// address = ClientQ::get_clients_addresses();
//}
}
fn get_client_address(clients_ref: &Arc<Mutex<HashMap<String,Client>>>, uuid: &String) -> String{
// may not need to lock hashmap as it may cause difficulties later on
let clients_hashmap = clients_ref.lock().unwrap();
let client = clients_hashmap.get(uuid);
match client{
Some(data) => data.get_address().to_string(),
None => String::from("client not online"),
}
}

View File

@ -0,0 +1,15 @@
use crate::client_management::client_profile::Client;
use std::sync::Mutex;
use std::sync::Arc;
use std::collections::HashMap;
pub fn get_client_address(clients_ref: &Arc<Mutex<HashMap<String,Client>>>, uuid: &String) -> String{
// may not need to lock hashmap as it may cause difficulties later on
let clients_hashmap = clients_ref.lock().unwrap();
let client = clients_hashmap.get(uuid);
match client{
Some(data) => data.get_address().to_string(),
None => String::from("client not online"),
}
}

View File

@ -0,0 +1,20 @@
use crate::client_management::client_profile::Client;
use std::sync::Mutex;
use std::sync::Arc;
use std::collections::HashMap;
pub fn add_new_client(clients_ref: &Arc<Mutex<HashMap<String,Client>>>, 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(),
}
}

View File

View File

View File

View File

View File

View File

View File