diff options
Diffstat (limited to 'src')
-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 |
6 files changed, 162 insertions, 0 deletions
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) + } + } +} |