summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bin/server.rs9
-rw-r--r--src/connection.rs33
-rw-r--r--src/engines.rs2
-rw-r--r--src/ship.rs8
4 files changed, 33 insertions, 19 deletions
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<TcpStream>,
@@ -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<Ship>) -> 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<Ship>) {
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),
}
}
}