summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bin/client.rs35
-rw-r--r--src/bin/server.rs27
-rw-r--r--src/connection.rs31
-rw-r--r--src/dashboard.rs18
-rw-r--r--src/engines.rs31
-rw-r--r--src/lib.rs10
-rw-r--r--src/module.rs5
7 files changed, 114 insertions, 43 deletions
diff --git a/src/bin/client.rs b/src/bin/client.rs
index 2dd4d7d..6f4a8fc 100644
--- a/src/bin/client.rs
+++ b/src/bin/client.rs
@@ -1,15 +1,15 @@
use std::io;
-use std::thread;
-use std::io::BufReader;
use std::io::prelude::*;
-use std::net::{TcpStream, Shutdown};
+use std::io::BufReader;
+use std::net::TcpStream;
extern crate space;
-use space::ship::Ship;
+use space::dashboard::Dashboard;
+use space::engines::Engines;
+use space::module::{Module, from_primitive};
-extern crate serde_json;
-fn get_login_info() -> String {
+fn get_info() -> String {
let mut name = String::new();
println!("Ship Name:");
io::stdin().read_line(&mut name).expect("Failed");
@@ -22,29 +22,30 @@ fn get_login_info() -> String {
}
fn main() {
+ let send = get_info();
+
let mut stream = TcpStream::connect("localhost:6000").unwrap();
let mut buff_r = BufReader::new(stream.try_clone().unwrap());
- let send = get_login_info();
- stream.write(send.as_bytes());
+ stream.write(send.as_bytes()).unwrap();
let mut data = String::new();
- buff_r.read_line(&mut data);
+ buff_r.read_line(&mut data).unwrap();
let modules : Vec<&str> = data.split(",").collect();
println!("Choose your module:");
for (i, module) in modules.iter().enumerate() {
- println!("{}) {}", i, module);
+ println!("{}) {}", i, module.replace("\n", ""));
}
let mut choice = String::new();
io::stdin().read_line(&mut choice).expect("Failed");
- stream.write(choice.as_bytes());
+ stream.write(choice.as_bytes()).unwrap();
- let mut data = String::new();
- buff_r.read_line(&mut data);
- let ship : Ship = serde_json::from_str(&data).unwrap();
- println!("{:?}", ship.location.0);
-
- stream.shutdown(Shutdown::Both);
+ let module = from_primitive(choice);
+ match module {
+ Module::Dashboard => Dashboard(buff_r),
+ Module::Engines => Engines(stream),
+ _ => (),
+ }
}
diff --git a/src/bin/server.rs b/src/bin/server.rs
index 71541f8..871315e 100644
--- a/src/bin/server.rs
+++ b/src/bin/server.rs
@@ -1,24 +1,27 @@
-use std::io::prelude::*;
-use std::net::{TcpListener, TcpStream};
+use std::thread::sleep;
+use std::time::Duration;
+use std::net::TcpListener;
extern crate space;
use space::connection::Connection;
fn main() {
let listener = TcpListener::bind("localhost:6000").unwrap();
- listener.set_nonblocking(true);
+ listener.set_nonblocking(true).unwrap();
let mut connections = Vec::new();
- loop {
- for stream in listener.incoming() {
- match stream {
- Ok(stream) => connections.push(Connection::new(stream)),
- _ => (),
+ for stream in listener.incoming() {
+ match stream {
+ Ok(stream) => connections.push(Connection::new(stream)),
+ _ => {
+ println!("looped");
+ for i in 0..connections.len() {
+ connections[i].process();
+ }
+ connections.retain(|connection| connection.open );
+ sleep(Duration::from_millis(100));
}
- for i in 0..connections.len() {
- connections[i].process();
- }
- connections.retain(|connection| connection.open );
}
}
+
}
diff --git a/src/connection.rs b/src/connection.rs
index 0276558..26993fd 100644
--- a/src/connection.rs
+++ b/src/connection.rs
@@ -1,6 +1,6 @@
use std::io::prelude::*;
use std::io::BufReader;
-use std::net::{TcpListener, TcpStream};
+use std::net::TcpStream;
extern crate serde_json;
@@ -8,7 +8,7 @@ use ship::Ship;
use module::{Module, from_primitive};
pub struct Connection {
- name : String,
+// name : String,
ship : Ship,
module : Module,
stream : TcpStream,
@@ -21,19 +21,20 @@ impl Connection {
let mut buff_r = BufReader::new(stream.try_clone().unwrap());
let mut data = String::new();
- buff_r.read_line(&mut data);
- let name = &data[..data.find(":").unwrap()];
+ buff_r.read_line(&mut data).unwrap();
+ //let name = &data[..data.find(":").unwrap()];
let modules = b"dashboard,navigation,engine\n";
- stream.write(modules);
+ stream.write(modules).unwrap();
let mut data = String::new();
- buff_r.read_line(&mut data);
- let num = data.replace("\n", "");
- let module = from_primitive(num.parse::<isize>().unwrap());
+ buff_r.read_line(&mut data).unwrap();
+ let module = from_primitive(data);
+
+ stream.set_nonblocking(true).unwrap();
Connection {
- name : String::from(name),
+// name : String::from(name),
ship : Ship::new(),
module : module,
stream : stream,
@@ -47,7 +48,17 @@ impl Connection {
Module::Dashboard => {
let mut send = serde_json::to_string(&self.ship).unwrap();
send.push_str("\n");
- self.stream.write(send.as_bytes());
+ match self.stream.write(send.as_bytes()) {
+ Ok(_result) => (),
+ Err(_error) => self.open = false,
+ }
+ }
+ Module::Engines => {
+ let mut data = String::new();
+ match self.buff_r.read_line(&mut data) {
+ Ok(_result) => println!("{}", data),
+ Err(_error) => println!("{}", _error)
+ }
}
_ => ()
}
diff --git a/src/dashboard.rs b/src/dashboard.rs
new file mode 100644
index 0000000..3a3ec2b
--- /dev/null
+++ b/src/dashboard.rs
@@ -0,0 +1,18 @@
+use std::io::BufReader;
+use std::io::BufRead;
+use std::net::TcpStream;
+
+extern crate serde_json;
+
+use ship::Ship;
+
+pub fn Dashboard(mut buff_r : BufReader<TcpStream>) {
+ loop {
+ 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);
+ }
+}
diff --git a/src/engines.rs b/src/engines.rs
new file mode 100644
index 0000000..fdf5047
--- /dev/null
+++ b/src/engines.rs
@@ -0,0 +1,31 @@
+use std::net::TcpStream;
+use termion::raw::IntoRawMode;
+use termion::async_stdin;
+use std::thread::sleep;
+use std::io::{Read, Write, stdout};
+use std::time::Duration;
+
+pub fn Engines(mut stream : TcpStream) {
+ let stdout = stdout();
+ let mut stdout = stdout.lock().into_raw_mode().unwrap();
+ let mut stdin = async_stdin().bytes();
+
+ loop {
+ match stdin.next() {
+ Some(c) => {
+ let c = c.unwrap();
+ let mut send = String::new();
+ send.push(c as char);
+ if send.as_bytes() == b"q" {
+ break;
+ }
+ send.push_str("\n");
+ stream.write(send.as_bytes());
+ }
+ None => ()
+ }
+
+ stdout.flush().unwrap();
+ sleep(Duration::from_millis(100));
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index 1740463..d64d760 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,6 +1,10 @@
-#[macro_use]
+#[macro_use]
extern crate serde_derive;
+extern crate termion;
-pub mod connection;
pub mod ship;
-mod module;
+pub mod module;
+pub mod engines;
+pub mod dashboard;
+pub mod connection;
+
diff --git a/src/module.rs b/src/module.rs
index 0a1d30a..7e99329 100644
--- a/src/module.rs
+++ b/src/module.rs
@@ -4,7 +4,10 @@ pub enum Module {
Engines,
}
-pub fn from_primitive(num : isize) -> Module {
+pub fn from_primitive(data : String) -> Module {
+ let data = data.replace("\n", "");
+ let num = data.parse::<isize>().unwrap();
+
match num {
0 => Module::Dashboard,
1 => Module::Navigation,