diff options
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/connection.rs | 57 | ||||
-rw-r--r-- | src/server/dashboard.rs | 4 | ||||
-rw-r--r-- | src/server/engines.rs | 4 | ||||
-rw-r--r-- | src/server/mining.rs | 4 | ||||
-rw-r--r-- | src/server/mod.rs | 1 | ||||
-rw-r--r-- | src/server/navigation.rs | 4 |
6 files changed, 66 insertions, 8 deletions
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<TcpStream>, + pub open : bool, +} + +impl ServerConnection { + pub fn new(mut stream : TcpStream, masses : &mut HashMap<String, Mass>) -> 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<String, Mass>) { + 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<String, Mass>) -> 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<String, Mass>) -> 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<String, Mass>) -> 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<String, Mass>) -> bool { let masses_clone = masses.clone(); let ship = masses.get_mut(&self.name).unwrap(); |