From c8d308a566c9e3e5be85e0e67082a06c3420a72b Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 22 Feb 2018 04:32:51 -0600 Subject: -got now a trait which resembles polymorphism for new objects --- src/bin/server.rs | 9 +++++---- src/connection.rs | 33 +++++++++++++++++++-------------- src/dashboard.rs | 4 +--- src/lib.rs | 1 + src/mass.rs | 7 +++++++ src/ship.rs | 29 ++++++++++++++++++++++++----- 6 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 src/mass.rs diff --git a/src/bin/server.rs b/src/bin/server.rs index 7c6a63e..d2923d8 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -3,23 +3,24 @@ use std::time::Duration; use std::net::TcpListener; extern crate space; +use space::mass::Mass; use space::connection::Connection; fn main() { let listener = TcpListener::bind("localhost:6000").unwrap(); listener.set_nonblocking(true).unwrap(); - let mut ships = Vec::new(); + let mut masses : Vec>= Vec::new(); let mut connections = Vec::new(); for stream in listener.incoming() { match stream { - Ok(stream) => connections.push(Connection::new(stream, &mut ships)), + Ok(stream) => connections.push(Connection::new(stream, &mut masses)), _ => { for i in 0..connections.len() { - connections[i].process(&mut ships); + connections[i].process(&mut masses); } - connections.retain(|connection| connection.open ); + connections.retain(|connection| connection.open); sleep(Duration::from_millis(100)); } diff --git a/src/connection.rs b/src/connection.rs index 648e1e3..b84c4e8 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -5,6 +5,7 @@ use std::net::TcpStream; extern crate serde_json; use ship::Ship; +use mass::Mass; use module::{Module, from_primitive}; pub struct Connection { @@ -16,20 +17,20 @@ pub struct Connection { } impl Connection { - pub fn new(mut stream : TcpStream, ships : &mut Vec) -> Connection { + pub fn new(mut stream : TcpStream, masses : &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 result = ships.into_iter().position(|ship| ship.name == name); + let result = masses.into_iter().position(|ship| ship.get_name() == name); let index = match result { Some(index) => index, None => { - let ship = Ship::new(name); - ships.push(ship); - ships.len() - 1 + let ship = Box::new(Ship::new(name, (0,0,0))); + masses.push(ship); + masses.len() - 1 }, }; @@ -52,10 +53,10 @@ impl Connection { } - pub fn process(&mut self, ships : &mut Vec) { + pub fn process(&mut self, masses : &mut Vec>) { match self.module { Module::Dashboard => { - let mut send = serde_json::to_string(&ships[self.index]).unwrap(); + let mut send = masses[self.index].serialize(); send.push_str("\n"); match self.stream.write(send.as_bytes()) { Ok(_result) => (), @@ -63,21 +64,25 @@ impl Connection { } } Module::Engines => { + let mut location = masses[self.index].get_location(); let mut data = String::new(); match self.buff_r.read_line(&mut data) { Ok(_result) => match data.as_bytes() { - b"5\n" => ships[self.index].location.0 += 1, - b"0\n" => ships[self.index].location.0 -= 1, - b"8\n" => ships[self.index].location.1 += 1, - b"2\n" => ships[self.index].location.1 -= 1, - b"4\n" => ships[self.index].location.2 += 1, - b"6\n" => ships[self.index].location.2 -= 1, + b"5\n" => location.0 += 1, + b"0\n" => location.0 -= 1, + b"8\n" => location.1 += 1, + b"2\n" => location.1 -= 1, + b"4\n" => location.2 += 1, + b"6\n" => location.2 -= 1, _ => (), }, Err(_error) => println!("b{}", _error) } + masses[self.index].give_location(location); + } + Module::Navigation => { + () } - _ => () } } } diff --git a/src/dashboard.rs b/src/dashboard.rs index 3a3ec2b..2227251 100644 --- a/src/dashboard.rs +++ b/src/dashboard.rs @@ -11,8 +11,6 @@ pub fn Dashboard(mut buff_r : BufReader) { let mut data = String::new(); buff_r.read_line(&mut data).unwrap(); let ship : Ship = serde_json::from_str(&data).unwrap(); - println!("Location: ({},{},{})", ship.location.0, - ship.location.1, - ship.location.2); + println!("{:?}", ship); } } diff --git a/src/lib.rs b/src/lib.rs index d64d760..90fd64b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ extern crate serde_derive; extern crate termion; +pub mod mass; pub mod ship; pub mod module; pub mod engines; diff --git a/src/mass.rs b/src/mass.rs new file mode 100644 index 0000000..d1f9d3e --- /dev/null +++ b/src/mass.rs @@ -0,0 +1,7 @@ +pub trait Mass { + fn new(name : &str, location : (isize, isize, isize)) -> Self where Self: Sized; + fn get_name(&self) -> &String; + fn get_location(&self) -> (isize, isize, isize); + fn give_location(&mut self, location : (isize, isize, isize)); + fn serialize(&self) -> String; +} diff --git a/src/ship.rs b/src/ship.rs index f791809..715f019 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -1,14 +1,33 @@ +use mass::Mass; +extern crate serde_json; + #[derive(Serialize, Deserialize, Debug)] pub struct Ship { - pub name : String, - pub location : (isize, isize, isize), + name : String, + location : (isize, isize, isize), } -impl Ship { - pub fn new(name : &str) -> Ship { +impl Mass for Ship { + fn new(name : &str, location : (isize, isize, isize)) -> Ship { Ship { name : String::from(name), - location : (0,0,0), + location : location, } } + + fn get_name(&self) -> &String { + &self.name + } + + fn get_location(&self) -> (isize, isize, isize) { + self.location + } + + fn give_location(&mut self, location : (isize, isize, isize)) { + self.location = location; + } + + fn serialize(&self) ->String { + serde_json::to_string(self).unwrap() + } } -- cgit v1.2.3