added two new enums to handle returns
commands file cleanup and bug fixes, making code more readable and organized
This commit is contained in:
parent
5ca8dd48f7
commit
b0e02dd1c1
|
|
@ -138,8 +138,6 @@ fn tokenize(incoming_message: &str) -> Vec<String>{
|
|||
fn match_command(data: &Vec<String>) -> Commands{
|
||||
match data[0].as_str(){
|
||||
"?info!" => Commands::Info,
|
||||
"!success:" => Commands::Success,
|
||||
"!error:" => Commands::Error,
|
||||
"!connect:" => Commands::Connect,
|
||||
"!disconnect:" => Commands::Disconnect,
|
||||
"!clientUpdate:" => Commands::ClientUpdate,
|
||||
|
|
|
|||
|
|
@ -23,8 +23,6 @@ use std::time::Duration;
|
|||
|
||||
pub enum Commands{
|
||||
Info,
|
||||
Success,
|
||||
Error,
|
||||
Connect,
|
||||
Disconnect,
|
||||
ClientUpdate,
|
||||
|
|
@ -33,27 +31,29 @@ pub enum Commands{
|
|||
}
|
||||
|
||||
pub enum OutboundCommands{
|
||||
Success,
|
||||
Error,
|
||||
Client,
|
||||
ClientRemove,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
enum InboundReturns{
|
||||
Success,
|
||||
Error,
|
||||
}
|
||||
|
||||
enum OutboundReturns{
|
||||
Success,
|
||||
Error,
|
||||
}
|
||||
|
||||
impl Commands{
|
||||
pub fn execute(&self, mut stream: &TcpStream, buffer: &mut [u8; 1024], data: &Vec<String>, address: &String, clients_ref: &Arc<Mutex<HashMap<String,Client>>>, message_queue: &Arc<FairMutex<VecDeque<String>>>){
|
||||
match *self{
|
||||
Commands::Info => {
|
||||
let server_details = info::get_server_info();
|
||||
|
||||
let mut message = "!success: ".to_string();
|
||||
message.push_str(&server_details.to_string());
|
||||
|
||||
network::transmit_data(stream, &message);
|
||||
},
|
||||
Commands::Success => {
|
||||
},
|
||||
Commands::Error => {
|
||||
let out_success = OutboundReturns::Success;
|
||||
out_success.execute(stream, &server_details);
|
||||
},
|
||||
Commands::Connect => {
|
||||
connect::add_client(clients_ref, &data[1], &data[2], address);
|
||||
|
|
@ -66,7 +66,9 @@ impl Commands{
|
|||
message.push_str(&data[1].to_string());
|
||||
|
||||
message_queue.lock().push_back(message);
|
||||
network::transmit_data(stream, &String::from("!success:"));
|
||||
|
||||
let out_success = OutboundReturns::Success;
|
||||
out_success.execute(stream, &String::from(""));
|
||||
},
|
||||
Commands::Disconnect => {
|
||||
let client_profile = disconnect::remove_client(clients_ref, &data[1]);
|
||||
|
|
@ -75,58 +77,25 @@ impl Commands{
|
|||
message.push_str(&client_profile.get_uuid().to_string());
|
||||
|
||||
message_queue.lock().push_back(message);
|
||||
network::transmit_data(stream, &String::from("!success:"));
|
||||
|
||||
let out_success = OutboundReturns::Success;
|
||||
out_success.execute(stream, &String::from(""));
|
||||
},
|
||||
Commands::ClientUpdate => {
|
||||
stream.set_read_timeout(Some(Duration::from_millis(3000))).unwrap();
|
||||
//let mut buffer = [0; 1024];
|
||||
let mut failing = true;
|
||||
let in_success = InboundReturns::Success;
|
||||
|
||||
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);
|
||||
|
||||
while failing{
|
||||
let _ = match stream.read(&mut *buffer){
|
||||
Err(e) => {
|
||||
match e.kind() {
|
||||
io::ErrorKind::WouldBlock => {
|
||||
println!("Blocking...");
|
||||
network::transmit_data(stream, &formatted_data);
|
||||
},
|
||||
_ => panic!("Fatal Error {}", e),
|
||||
}
|
||||
},
|
||||
Ok(m) => {
|
||||
println!("{:?}", m);
|
||||
failing = false;
|
||||
},
|
||||
};
|
||||
}
|
||||
in_success.execute(stream, buffer, &formatted_data);
|
||||
}
|
||||
|
||||
let final_message = String::from("!success:");
|
||||
network::transmit_data(stream, &final_message);
|
||||
|
||||
let out_success = OutboundReturns::Success;
|
||||
out_success.execute(stream, &String::from(""));
|
||||
|
||||
failing = true;
|
||||
while failing{
|
||||
let _ = match stream.read(&mut *buffer){
|
||||
Err(e) => {
|
||||
match e.kind() {
|
||||
io::ErrorKind::WouldBlock => {
|
||||
println!("Blocking...");
|
||||
network::transmit_data(stream, &final_message);
|
||||
},
|
||||
_ => panic!("Fatal Error {}", e),
|
||||
}
|
||||
},
|
||||
Ok(m) => {
|
||||
println!("{:?}", m);
|
||||
failing = false;
|
||||
},
|
||||
};
|
||||
}
|
||||
in_success.execute(stream, buffer, &String::from("!success:"));
|
||||
},
|
||||
Commands::ClientInfo => {
|
||||
let requested_data = client_info::get_client_data(clients_ref, &data[1]);
|
||||
|
|
@ -142,53 +111,17 @@ impl Commands{
|
|||
impl OutboundCommands{
|
||||
pub fn execute(&self, mut stream: &TcpStream, buffer: &mut [u8; 1024], data: &String){
|
||||
match *self{
|
||||
OutboundCommands::Success => {},
|
||||
OutboundCommands::Error => {},
|
||||
OutboundCommands::Client => {
|
||||
stream.set_read_timeout(Some(Duration::from_millis(3000))).unwrap();
|
||||
network::transmit_data(stream, data);
|
||||
|
||||
let mut failing = true;
|
||||
while failing{
|
||||
let _ = match stream.read(&mut *buffer){
|
||||
Err(e) => {
|
||||
match e.kind() {
|
||||
io::ErrorKind::WouldBlock => {
|
||||
println!("Blocking...");
|
||||
network::transmit_data(stream, data);
|
||||
},
|
||||
_ => panic!("Fatal Error {}", e),
|
||||
}
|
||||
},
|
||||
Ok(m) => {
|
||||
println!("{:?}", m);
|
||||
failing = false;
|
||||
},
|
||||
};
|
||||
}
|
||||
let in_success = InboundReturns::Success;
|
||||
in_success.execute(stream, buffer, data);
|
||||
},
|
||||
OutboundCommands::ClientRemove => {
|
||||
stream.set_read_timeout(Some(Duration::from_millis(3000))).unwrap();
|
||||
network::transmit_data(stream, data);
|
||||
|
||||
let mut failing = true;
|
||||
while failing{
|
||||
let _ = match stream.read(&mut *buffer){
|
||||
Err(e) => {
|
||||
match e.kind() {
|
||||
io::ErrorKind::WouldBlock => {
|
||||
println!("Blocking...");
|
||||
network::transmit_data(stream, data);
|
||||
},
|
||||
_ => panic!("Fatal Error {}", e),
|
||||
}
|
||||
},
|
||||
Ok(m) => {
|
||||
println!("{:?}", m);
|
||||
failing = false;
|
||||
},
|
||||
};
|
||||
}
|
||||
let in_success = InboundReturns::Success;
|
||||
in_success.execute(stream, buffer, data);
|
||||
},
|
||||
OutboundCommands::Unknown => {
|
||||
println!("Unknown Command!");
|
||||
|
|
@ -197,15 +130,47 @@ impl OutboundCommands{
|
|||
}
|
||||
}
|
||||
|
||||
/*mod network{
|
||||
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();
|
||||
impl InboundReturns{
|
||||
pub fn execute(&self, mut stream: &TcpStream, buffer: &mut [u8; 1024], data: &String){
|
||||
stream.set_read_timeout(Some(Duration::from_millis(3000))).unwrap();
|
||||
match *self{
|
||||
InboundReturns::Success => {
|
||||
let mut failing = true;
|
||||
while failing{
|
||||
let _ = match stream.read(&mut *buffer){
|
||||
Err(e) => {
|
||||
match e.kind() {
|
||||
io::ErrorKind::WouldBlock => {
|
||||
println!("Blocking...");
|
||||
network::transmit_data(stream, data);
|
||||
},
|
||||
_ => panic!("Fatal Error {}", e),
|
||||
}
|
||||
},
|
||||
Ok(m) => {
|
||||
println!("{:?}", m);
|
||||
failing = false;
|
||||
},
|
||||
};
|
||||
}
|
||||
},
|
||||
InboundReturns::Error => {},
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
impl OutboundReturns{
|
||||
pub fn execute(&self, mut stream: &TcpStream, data: &String){
|
||||
match *self{
|
||||
OutboundReturns::Success => {
|
||||
let mut message = "!success:".to_string();
|
||||
if !data.is_empty(){
|
||||
message.push_str(&" ".to_string());
|
||||
message.push_str(&data.to_string());
|
||||
}
|
||||
network::transmit_data(stream, &message);
|
||||
},
|
||||
OutboundReturns::Error => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue