From d42a28c46057c386d161bfe438302e2314f0a6f6 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Wed, 21 Feb 2018 06:27:02 -0600 Subject: -got engine and dashboard data sending --- Cargo.lock | 33 +++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/bin/client.rs | 35 ++++++++++++++++++----------------- src/bin/server.rs | 27 +++++++++++++++------------ src/connection.rs | 31 +++++++++++++++++++++---------- src/dashboard.rs | 18 ++++++++++++++++++ src/engines.rs | 31 +++++++++++++++++++++++++++++++ src/lib.rs | 10 +++++++--- src/module.rs | 5 ++++- 9 files changed, 148 insertions(+), 43 deletions(-) create mode 100644 src/dashboard.rs create mode 100644 src/engines.rs diff --git a/Cargo.lock b/Cargo.lock index 849f913..748be5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,11 @@ name = "itoa" 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" @@ -26,6 +31,19 @@ name = "quote" 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" @@ -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]] @@ -88,6 +107,16 @@ dependencies = [ "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[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" @@ -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" diff --git a/Cargo.toml b/Cargo.toml index c06b50d..689c76f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ authors = ["tom barrett "] 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::().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) { + 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::().unwrap(); + match num { 0 => Module::Dashboard, 1 => Module::Navigation, -- cgit v1.2.3