Update server_profile.rs

added spaces and other changes
This commit is contained in:
michael-bailey 2020-09-27 08:47:47 +01:00 committed by michael bailey
parent 8aa499ab03
commit c233138ca6
1 changed files with 14 additions and 13 deletions

View File

@ -39,9 +39,9 @@ pub enum ServerMessages {
// MARK: - server struct // MARK: - server struct
pub struct Server { pub struct Server {
name: Arc<String>, pub name: String,
address: Arc<String>, pub address: String,
author: Arc<String>, pub author: String,
connected_clients: Arc<Mutex<HashMap<String, Client>>>, connected_clients: Arc<Mutex<HashMap<String, Client>>>,
@ -115,6 +115,7 @@ impl Server {
let listener = TcpListener::bind(self.get_address())?; let listener = TcpListener::bind(self.get_address())?;
listener.set_nonblocking(true)?; listener.set_nonblocking(true)?;
println!("server: spawning threads"); println!("server: spawning threads");
let _ = thread::Builder::new().name("Server Thread".to_string()).spawn(move || { let _ = thread::Builder::new().name("Server Thread".to_string()).spawn(move || {
@ -138,7 +139,7 @@ impl Server {
if Server::read_data(&mut stream, &mut buffer).unwrap_or(Commands::Error(None)) == Commands::Success(None) { if Server::read_data(&mut stream, &mut buffer).unwrap_or(Commands::Error(None)) == Commands::Success(None) {
println!("Success Confirmed"); println!("Success Confirmed");
} else { } else {
println!("no success read"); println!("No success read");
let error = Commands::Error(None); let error = Commands::Error(None);
let _ = Server::transmit_data(&mut stream, error.to_string().as_str()); let _ = Server::transmit_data(&mut stream, error.to_string().as_str());
} }
@ -146,7 +147,7 @@ impl Server {
}, },
ServerMessages::RequestInfo(uuid, stream_arc) => { ServerMessages::RequestInfo(uuid, stream_arc) => {
let mut stream = stream_arc.lock().unwrap(); let mut stream = stream_arc.lock().unwrap();
if let Some(client) = connected_clients.lock().unwrap().get(&uuid) { if let Some(client) = connected_clients.lock().unwrap().get(&uuid) {
let params: HashMap<String, String> = [(String::from("uuid"), client.get_uuid()), (String::from("name"), client.get_username()), (String::from("host"), client.get_address())].iter().cloned().collect(); let params: HashMap<String, String> = [(String::from("uuid"), client.get_uuid()), (String::from("name"), client.get_username()), (String::from("host"), client.get_address())].iter().cloned().collect();
let command = Commands::Success(Some(params)); let command = Commands::Success(Some(params));
@ -182,24 +183,24 @@ impl Server {
let uuid = data.get("uuid").unwrap(); let uuid = data.get("uuid").unwrap();
let username = data.get("name").unwrap(); let username = data.get("name").unwrap();
let address = data.get("host").unwrap(); let address = data.get("host").unwrap();
println!("{}", format!("Server: new Client connection: _addr = {}", address )); println!("{}", format!("Server: new Client connection: _addr = {}", address ));
let client = Client::new(stream, sender.clone(), &uuid, &username, &address); let client = Client::new(stream, sender.clone(), &uuid, &username, &address);
connected_clients.lock().unwrap().insert(uuid.to_string(), client); connected_clients.lock().unwrap().insert(uuid.to_string(), client);
let params: HashMap<String, String> = [(String::from("name"), username.clone()), (String::from("host"), address.clone()), (String::from("uuid"), uuid.clone())].iter().cloned().collect(); let params: HashMap<String, String> = [(String::from("name"), username.clone()), (String::from("host"), address.clone()), (String::from("uuid"), uuid.clone())].iter().cloned().collect();
let new_client = Commands::Client(Some(params)); let new_client = Commands::Client(Some(params));
let _ = connected_clients.lock().unwrap().iter().map(|(_k, v)| v.sender.send(new_client.clone())); let _ = connected_clients.lock().unwrap().iter().map(|(_k, v)| v.sender.send(new_client.clone()));
}, },
// TODO: - correct connection reset error when getting info. // TODO: - correct connection reset error when getting info.
Commands::Info(None) => { Commands::Info(None) => {
println!("Server: info requested"); println!("Server: info requested");
let params: HashMap<String, String> = [(String::from("name"), name.to_string().clone()), (String::from("owner"), author.to_string().clone())].iter().cloned().collect(); let params: HashMap<String, String> = [(String::from("name"), name.to_string().clone()), (String::from("owner"), author.to_string().clone())].iter().cloned().collect();
let command = Commands::Info(Some(params)); let command = Commands::Info(Some(params));
let _ = Server::transmit_data(&mut stream, command.to_string().as_str()); let _ = Server::transmit_data(&mut stream, command.to_string().as_str());
}, },
_ => { _ => {
@ -265,7 +266,7 @@ impl Drop for Server {
} }
/* The new version of the server no long works with these unit /* The new version of the server No long works with these unit
* tests. * tests.
* They will be fixed soon! * They will be fixed soon!
* TODO: fix unit tests * TODO: fix unit tests
@ -290,7 +291,7 @@ mod tests{
static START: Once = Once::new(); static START: Once = Once::new();
/* /*
* These tests must be executed individually to ensure that no errors * These tests must be executed individually to ensure that No errors
* occur, this is due to the fact that the server is created everytime. * occur, this is due to the fact that the server is created everytime.
* Setup a system for the server to close after every test. * Setup a system for the server to close after every test.
*/ */