diff options
author | tom barrett <spalf0@gmail.com> | 2018-02-21 06:27:02 -0600 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2018-02-21 06:27:02 -0600 |
commit | d42a28c46057c386d161bfe438302e2314f0a6f6 (patch) | |
tree | 9802462761e4e41bc4419b700e4a42b06919f5b5 | |
parent | 6fb01a0cb228b1aa08e47e8dbcd6b46c43bde06b (diff) |
-got engine and dashboard data sending
-rw-r--r-- | Cargo.lock | 33 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/bin/client.rs | 35 | ||||
-rw-r--r-- | src/bin/server.rs | 27 | ||||
-rw-r--r-- | src/connection.rs | 31 | ||||
-rw-r--r-- | src/dashboard.rs | 18 | ||||
-rw-r--r-- | src/engines.rs | 31 | ||||
-rw-r--r-- | src/lib.rs | 10 | ||||
-rw-r--r-- | src/module.rs | 5 |
9 files changed, 148 insertions, 43 deletions
@@ -9,6 +9,11 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "libc" +version = "0.2.36" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "num-traits" version = "0.1.43" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -27,6 +32,19 @@ version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "redox_syscall" +version = "0.1.37" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "redox_termios" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "serde" version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -68,6 +86,7 @@ dependencies = [ "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", + "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -89,6 +108,16 @@ dependencies = [ ] [[package]] +name = "termion" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "unicode-xid" version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -96,13 +125,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" "checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c" +"checksum libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "1e5d97d6708edaa407429faa671b942dc0f2727222fb6b6539bf1db936e4b121" "checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" "checksum num-traits 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e7de20f146db9d920c45ee8ed8f71681fd9ade71909b48c3acbd766aa504cf10" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" +"checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd" +"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "db99f3919e20faa51bb2996057f5031d8685019b5a06139b1ce761da671b8526" "checksum serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "f4ba7591cfe93755e89eeecdbcc668885624829b020050e6aec99c2a03bd3fd0" "checksum serde_derive_internals 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6e03f1c9530c3fb0a0a5c9b826bdd9246a5921ae995d75f512ac917fc4dd55b5" "checksum serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c9db7266c7d63a4c4b7fe8719656ccdd51acf1bed6124b174f933b009fb10bcb" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" +"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" @@ -7,3 +7,4 @@ authors = ["tom barrett <spalf0@gmail.com>"] serde = "1.0" serde_derive = "1.0" serde_json = "1.0" +termion = "1.5.1" 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)); + } +} @@ -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, |