From 7da5495fc815c2cf255b03f100a5b800a5e5f0ff Mon Sep 17 00:00:00 2001 From: tom barrett Date: Mon, 16 Apr 2018 03:05:39 -0500 Subject: -conformed connection to naming convention --- src/bin/server.rs | 4 ++-- src/connection.rs | 57 ------------------------------------------------ src/lib.rs | 1 - src/mass.rs | 6 ++--- src/server/connection.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ src/server/dashboard.rs | 4 ++-- src/server/engines.rs | 4 ++-- src/server/mining.rs | 4 ++-- src/server/mod.rs | 1 + src/server/navigation.rs | 4 ++-- 10 files changed, 71 insertions(+), 71 deletions(-) delete mode 100644 src/connection.rs create mode 100644 src/server/connection.rs diff --git a/src/bin/server.rs b/src/bin/server.rs index 68e091e..1fad1d3 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -7,7 +7,7 @@ use std::collections::HashMap; use space::mass::Mass; use space::math::rand_name; -use space::connection::Connection; +use space::server::connection::ServerConnection; fn populate() -> HashMap { let mut masses : HashMap = HashMap::new(); @@ -28,7 +28,7 @@ fn main() { let mut connections = Vec::new(); for stream in listener.incoming() { match stream { - Ok(stream) => connections.push(Connection::new(stream, &mut masses)), + Ok(stream) => connections.push(ServerConnection::new(stream, &mut masses)), _ => { for i in 0..connections.len() { connections[i].process(&mut masses); diff --git a/src/connection.rs b/src/connection.rs deleted file mode 100644 index f4637e4..0000000 --- a/src/connection.rs +++ /dev/null @@ -1,57 +0,0 @@ -extern crate serde_json; - -use std::io::BufReader; -use std::io::prelude::*; -use std::net::TcpStream; -use std::collections::HashMap; - -use module::{Module, ModuleType}; -use mass::{Mass, MassType}; - -pub struct Connection { - pub name : String, - pub module : Module, - pub stream : TcpStream, - pub buff_r : BufReader, - pub open : bool, -} - -impl Connection { - pub fn new(mut stream : TcpStream, masses : &mut HashMap) -> Connection { - let mut buff_r = BufReader::new(stream.try_clone().unwrap()); - - let mut recv = String::new(); - buff_r.read_line(&mut recv).unwrap(); - let name = &recv[..recv.find(":").unwrap()]; - - let ship = masses.entry(name.to_string()).or_insert(Mass::new_ship()); - - let send = match ship.mass_type { - MassType::Ship{ref modules, ..} => serde_json::to_string(modules).unwrap() + "\n", - _ => String::new(), - }; - stream.write(send.as_bytes()).unwrap(); - - let mut recv = String::new(); - buff_r.read_line(&mut recv).unwrap(); - let module : Module = serde_json::from_str(&recv.replace("\n","")).unwrap(); - - stream.set_nonblocking(true).unwrap(); - Connection { - name : String::from(name), - module : module, - stream : stream, - buff_r : buff_r, - open : true, - } - } - - pub fn process(&mut self, mut masses : &mut HashMap) { - self.open = match self.module.module_type { - ModuleType::Dashboard => self.server_dashboard(&mut masses), - ModuleType::Engines => self.server_engines(&mut masses), - ModuleType::Mining{..} => self.server_mining(&mut masses), - ModuleType::Navigation{..} => self.server_navigation(&mut masses), - }; - } -} diff --git a/src/lib.rs b/src/lib.rs index 9b8050c..608a1ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,4 +8,3 @@ pub mod module; pub mod client; pub mod server; pub mod storage; -pub mod connection; diff --git a/src/mass.rs b/src/mass.rs index 762c9ab..f38ab6d 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -5,7 +5,7 @@ use self::rand::distributions::Range; use self::rand::distributions::Sample; use storage::Storage; -use module::{Module, ModuleType}; +use module::Module; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Mass { @@ -61,8 +61,8 @@ impl Mass { Mass { mass_type : ship, - position : (0.0,0.0,0.0), - velocity : (0.0,0.0,0.0), + position : (0.0, 0.0, 0.0), + velocity : (0.0, 0.0, 0.0), } } diff --git a/src/server/connection.rs b/src/server/connection.rs new file mode 100644 index 0000000..8354d70 --- /dev/null +++ b/src/server/connection.rs @@ -0,0 +1,57 @@ +extern crate serde_json; + +use std::io::BufReader; +use std::io::prelude::*; +use std::net::TcpStream; +use std::collections::HashMap; + +use module::{Module, ModuleType}; +use mass::{Mass, MassType}; + +pub struct ServerConnection { + pub name : String, + pub module : Module, + pub stream : TcpStream, + pub buff_r : BufReader, + pub open : bool, +} + +impl ServerConnection { + pub fn new(mut stream : TcpStream, masses : &mut HashMap) -> ServerConnection { + let mut buff_r = BufReader::new(stream.try_clone().unwrap()); + + let mut recv = String::new(); + buff_r.read_line(&mut recv).unwrap(); + let name = &recv[..recv.find(":").unwrap()]; + + let ship = masses.entry(name.to_string()).or_insert(Mass::new_ship()); + + let send = match ship.mass_type { + MassType::Ship{ref modules, ..} => serde_json::to_string(modules).unwrap() + "\n", + _ => String::new(), + }; + stream.write(send.as_bytes()).unwrap(); + + let mut recv = String::new(); + buff_r.read_line(&mut recv).unwrap(); + let module : Module = serde_json::from_str(&recv.replace("\n","")).unwrap(); + + stream.set_nonblocking(true).unwrap(); + ServerConnection { + name : String::from(name), + module : module, + stream : stream, + buff_r : buff_r, + open : true, + } + } + + pub fn process(&mut self, mut masses : &mut HashMap) { + self.open = match self.module.module_type { + ModuleType::Dashboard => self.server_dashboard(&mut masses), + ModuleType::Engines => self.server_engines(&mut masses), + ModuleType::Mining{..} => self.server_mining(&mut masses), + ModuleType::Navigation{..} => self.server_navigation(&mut masses), + }; + } +} diff --git a/src/server/dashboard.rs b/src/server/dashboard.rs index 81ac522..91d8831 100644 --- a/src/server/dashboard.rs +++ b/src/server/dashboard.rs @@ -4,9 +4,9 @@ use std::io::Write; use std::collections::HashMap; use mass::Mass; -use connection::Connection; +use server::connection::ServerConnection; -impl Connection { +impl ServerConnection { pub fn server_dashboard(&mut self, masses : &mut HashMap) -> bool { let ship = masses.get(&self.name).unwrap(); let send = serde_json::to_string(&ship).unwrap() + "\n"; diff --git a/src/server/engines.rs b/src/server/engines.rs index 85346f6..c2a97ef 100644 --- a/src/server/engines.rs +++ b/src/server/engines.rs @@ -6,10 +6,10 @@ use std::collections::HashMap; use mass::{Mass, MassType}; use module::{ModuleType}; -use connection::Connection; use module::NavigationStatus; +use server::connection::ServerConnection; -impl Connection { +impl ServerConnection { pub fn server_engines(&mut self, masses : &mut HashMap) -> bool { let masses_clone = masses.clone(); diff --git a/src/server/mining.rs b/src/server/mining.rs index 98803c5..2364f9f 100644 --- a/src/server/mining.rs +++ b/src/server/mining.rs @@ -7,7 +7,7 @@ use std::collections::HashMap; use math::distance; use mass::{Mass, MassType}; use module::ModuleType; -use connection::Connection; +use::server::connection::ServerConnection; #[derive(Serialize, Deserialize, Debug, Clone)] struct ServerData { @@ -17,7 +17,7 @@ struct ServerData { mining_status : bool, } -impl Connection { +impl ServerConnection { pub fn server_mining(&mut self, masses : &mut HashMap) -> bool { let masses_clone = masses.clone(); let ship = masses.get_mut(&self.name).unwrap(); diff --git a/src/server/mod.rs b/src/server/mod.rs index baabf96..444e487 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -2,3 +2,4 @@ pub mod mining; pub mod engines; pub mod dashboard; pub mod navigation; +pub mod connection; diff --git a/src/server/navigation.rs b/src/server/navigation.rs index 58d88e7..1d332f9 100644 --- a/src/server/navigation.rs +++ b/src/server/navigation.rs @@ -8,9 +8,9 @@ use std::collections::HashMap; use math::distance; use module::{ModuleType, NavigationStatus}; use mass::{Mass, MassType}; -use connection::Connection; +use server::connection::ServerConnection; -impl Connection { +impl ServerConnection { pub fn server_navigation(&mut self, masses : &mut HashMap) -> bool { let masses_clone = masses.clone(); let ship = masses.get_mut(&self.name).unwrap(); -- cgit v1.2.3