diff options
author | tom barrett <spalf0@gmail.com> | 2018-02-20 09:51:10 -0600 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2018-02-20 09:51:10 -0600 |
commit | 6fb01a0cb228b1aa08e47e8dbcd6b46c43bde06b (patch) | |
tree | 65e70e23ec229decc000be66e98b13d1c5ee06e0 |
-starting out
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Cargo.lock | 108 | ||||
-rw-r--r-- | Cargo.toml | 9 | ||||
-rw-r--r-- | src/bin/client.rs | 50 | ||||
-rw-r--r-- | src/bin/server.rs | 24 | ||||
-rw-r--r-- | src/connection.rs | 56 | ||||
-rw-r--r-- | src/lib.rs | 6 | ||||
-rw-r--r-- | src/module.rs | 14 | ||||
-rw-r--r-- | src/ship.rs | 12 |
9 files changed, 281 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eccd7b4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target/ +**/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..849f913 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,108 @@ +[[package]] +name = "dtoa" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "itoa" +version = "0.3.4" +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" +dependencies = [ + "num-traits 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-traits" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "quote" +version = "0.3.15" +source = "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" + +[[package]] +name = "serde_derive" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive_internals 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_derive_internals" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", + "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_json" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "space" +version = "0.1.0" +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)", +] + +[[package]] +name = "syn" +version = "0.11.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "synom" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.0.4 (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" + +[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 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 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 unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c06b50d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "space" +version = "0.1.0" +authors = ["tom barrett <spalf0@gmail.com>"] + +[dependencies] +serde = "1.0" +serde_derive = "1.0" +serde_json = "1.0" diff --git a/src/bin/client.rs b/src/bin/client.rs new file mode 100644 index 0000000..2dd4d7d --- /dev/null +++ b/src/bin/client.rs @@ -0,0 +1,50 @@ +use std::io; +use std::thread; +use std::io::BufReader; +use std::io::prelude::*; +use std::net::{TcpStream, Shutdown}; + +extern crate space; +use space::ship::Ship; + +extern crate serde_json; + +fn get_login_info() -> String { + let mut name = String::new(); + println!("Ship Name:"); + io::stdin().read_line(&mut name).expect("Failed"); + + let mut password = String::new(); + println!("Password:"); + io::stdin().read_line(&mut password).expect("Failed"); + + name.replace("\n", "") + ":" + &password +} + +fn main() { + 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()); + + let mut data = String::new(); + buff_r.read_line(&mut data); + let modules : Vec<&str> = data.split(",").collect(); + + println!("Choose your module:"); + for (i, module) in modules.iter().enumerate() { + println!("{}) {}", i, module); + } + + let mut choice = String::new(); + io::stdin().read_line(&mut choice).expect("Failed"); + stream.write(choice.as_bytes()); + + 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); +} diff --git a/src/bin/server.rs b/src/bin/server.rs new file mode 100644 index 0000000..71541f8 --- /dev/null +++ b/src/bin/server.rs @@ -0,0 +1,24 @@ +use std::io::prelude::*; +use std::net::{TcpListener, TcpStream}; + +extern crate space; +use space::connection::Connection; + +fn main() { + let listener = TcpListener::bind("localhost:6000").unwrap(); + listener.set_nonblocking(true); + + let mut connections = Vec::new(); + loop { + for stream in listener.incoming() { + match stream { + Ok(stream) => connections.push(Connection::new(stream)), + _ => (), + } + for i in 0..connections.len() { + connections[i].process(); + } + connections.retain(|connection| connection.open ); + } + } +} diff --git a/src/connection.rs b/src/connection.rs new file mode 100644 index 0000000..0276558 --- /dev/null +++ b/src/connection.rs @@ -0,0 +1,56 @@ +use std::io::prelude::*; +use std::io::BufReader; +use std::net::{TcpListener, TcpStream}; + +extern crate serde_json; + +use ship::Ship; +use module::{Module, from_primitive}; + +pub struct Connection { + name : String, + ship : Ship, + module : Module, + stream : TcpStream, + buff_r : BufReader<TcpStream>, + pub open : bool, +} + +impl Connection { + pub fn new(mut stream : TcpStream) -> 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()]; + + let modules = b"dashboard,navigation,engine\n"; + stream.write(modules); + + let mut data = String::new(); + buff_r.read_line(&mut data); + let num = data.replace("\n", ""); + let module = from_primitive(num.parse::<isize>().unwrap()); + + Connection { + name : String::from(name), + ship : Ship::new(), + module : module, + stream : stream, + buff_r : buff_r, + open : true, + } + } + + pub fn process(&mut self) { + match self.module { + Module::Dashboard => { + let mut send = serde_json::to_string(&self.ship).unwrap(); + send.push_str("\n"); + self.stream.write(send.as_bytes()); + } + _ => () + } + } +} + diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..1740463 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,6 @@ +#[macro_use] +extern crate serde_derive; + +pub mod connection; +pub mod ship; +mod module; diff --git a/src/module.rs b/src/module.rs new file mode 100644 index 0000000..0a1d30a --- /dev/null +++ b/src/module.rs @@ -0,0 +1,14 @@ +pub enum Module { + Dashboard, + Navigation, + Engines, +} + +pub fn from_primitive(num : isize) -> Module { + match num { + 0 => Module::Dashboard, + 1 => Module::Navigation, + 2 => Module::Engines, + _ => Module::Dashboard, + } +} diff --git a/src/ship.rs b/src/ship.rs new file mode 100644 index 0000000..fd55ca9 --- /dev/null +++ b/src/ship.rs @@ -0,0 +1,12 @@ +#[derive(Serialize, Deserialize, Debug)] +pub struct Ship { + pub location : (isize, isize, isize) +} + +impl Ship { + pub fn new() -> Ship { + Ship { + location : (0,0,0) + } + } +} |