From 492b31ffd13d4def86050d1db31c5c2e59b670ab Mon Sep 17 00:00:00 2001 From: tom barrett Date: Wed, 21 Feb 2018 08:47:21 -0600 Subject: -moving engines shows change on dashboard --- src/bin/server.rs | 9 +++++---- src/connection.rs | 33 ++++++++++++++++++++++----------- src/engines.rs | 2 +- src/ship.rs | 8 +++++--- 4 files changed, 33 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/bin/server.rs b/src/bin/server.rs index 871315e..7c6a63e 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -9,19 +9,20 @@ fn main() { let listener = TcpListener::bind("localhost:6000").unwrap(); listener.set_nonblocking(true).unwrap(); + let mut ships = Vec::new(); + let mut connections = Vec::new(); for stream in listener.incoming() { match stream { - Ok(stream) => connections.push(Connection::new(stream)), + Ok(stream) => connections.push(Connection::new(stream, &mut ships)), _ => { - println!("looped"); for i in 0..connections.len() { - connections[i].process(); + connections[i].process(&mut ships); } connections.retain(|connection| connection.open ); + sleep(Duration::from_millis(100)); } } } - } diff --git a/src/connection.rs b/src/connection.rs index 26993fd..cfe642d 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -8,8 +8,7 @@ use ship::Ship; use module::{Module, from_primitive}; pub struct Connection { -// name : String, - ship : Ship, + index : usize, module : Module, stream : TcpStream, buff_r : BufReader, @@ -17,12 +16,22 @@ pub struct Connection { } impl Connection { - pub fn new(mut stream : TcpStream) -> Connection { + pub fn new(mut stream : TcpStream, ships : &mut Vec) -> Connection { let mut buff_r = BufReader::new(stream.try_clone().unwrap()); let mut data = String::new(); buff_r.read_line(&mut data).unwrap(); - //let name = &data[..data.find(":").unwrap()]; + let name = &data[..data.find(":").unwrap()]; + + let result = ships.into_iter().position(|ship| ship.name == name); + let index = match result { + Some(index) => index, + None => { + let mut ship = Ship::new(name); + ships.push(ship); + ships.len() - 1 + }, + }; let modules = b"dashboard,navigation,engine\n"; stream.write(modules).unwrap(); @@ -34,8 +43,7 @@ impl Connection { stream.set_nonblocking(true).unwrap(); Connection { -// name : String::from(name), - ship : Ship::new(), + index : index, module : module, stream : stream, buff_r : buff_r, @@ -43,10 +51,11 @@ impl Connection { } } - pub fn process(&mut self) { + + pub fn process(&mut self, ships : &mut Vec) { match self.module { Module::Dashboard => { - let mut send = serde_json::to_string(&self.ship).unwrap(); + let mut send = serde_json::to_string(&ships[self.index]).unwrap(); send.push_str("\n"); match self.stream.write(send.as_bytes()) { Ok(_result) => (), @@ -56,12 +65,14 @@ impl Connection { Module::Engines => { let mut data = String::new(); match self.buff_r.read_line(&mut data) { - Ok(_result) => println!("{}", data), - Err(_error) => println!("{}", _error) + Ok(_result) => match data.as_bytes() { + b"5\n" => ships[self.index].location.0 += 1, + _ => (), + }, + Err(_error) => println!("b{}", _error) } } _ => () } } } - diff --git a/src/engines.rs b/src/engines.rs index fdf5047..b50a6ae 100644 --- a/src/engines.rs +++ b/src/engines.rs @@ -20,7 +20,7 @@ pub fn Engines(mut stream : TcpStream) { break; } send.push_str("\n"); - stream.write(send.as_bytes()); + stream.write(send.as_bytes()).unwrap(); } None => () } diff --git a/src/ship.rs b/src/ship.rs index fd55ca9..f791809 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -1,12 +1,14 @@ #[derive(Serialize, Deserialize, Debug)] pub struct Ship { - pub location : (isize, isize, isize) + pub name : String, + pub location : (isize, isize, isize), } impl Ship { - pub fn new() -> Ship { + pub fn new(name : &str) -> Ship { Ship { - location : (0,0,0) + name : String::from(name), + location : (0,0,0), } } } -- cgit v1.2.3