From 6ddb9d4355af9688a7337c5680cd34b227cba702 Mon Sep 17 00:00:00 2001 From: Michael Preisach Date: Thu, 11 Apr 2019 16:23:10 +0200 Subject: [PATCH] initial server commit, tcp connection established, no protocol defined --- .gitignore | 2 ++ .idea/misc.xml | 16 ++++++++++++++ .idea/modules.xml | 8 +++++++ .idea/noobctf-server.iml | 14 ++++++++++++ .idea/vcs.xml | 6 +++++ Cargo.lock | 30 +++++++++++++++++++++++++ Cargo.toml | 8 +++++++ src/main.rs | 35 +++++++++++++++++++++++++++++ src/protocol.rs | 48 ++++++++++++++++++++++++++++++++++++++++ 9 files changed, 167 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/noobctf-server.iml create mode 100644 .idea/vcs.xml create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs create mode 100644 src/protocol.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f0e3bca --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..90d8454 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..de6c84e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/noobctf-server.iml b/.idea/noobctf-server.iml new file mode 100644 index 0000000..b7b4242 --- /dev/null +++ b/.idea/noobctf-server.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..0f8ee74 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,30 @@ +[[package]] +name = "lazy_static" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.51" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "noobctf-server" +version = "0.1.0" +dependencies = [ + "rppal 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rppal" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[metadata] +"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" +"checksum libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)" = "bedcc7a809076656486ffe045abeeac163da1b558e963a31e29fbfbeba916917" +"checksum rppal 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "47fa3e09d34f2992f47f3d4315f8ee8cfffc1fe1912d4d08258b66cc18dc3b00" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3fbdbc1 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "noobctf-server" +version = "0.1.0" +authors = ["Michael Preisach "] +edition = "2018" + +[dependencies] +rppal = "0.11.1" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..b37c93a --- /dev/null +++ b/src/main.rs @@ -0,0 +1,35 @@ +use std::net::{TcpStream, TcpListener}; +use std::io::prelude::*; +use std::io; + +mod protocol; + + +fn handle_connection(stream: TcpStream) -> io::Result<()> { + let mut outstream = stream.try_clone()?; + let mut reader = io::BufReader::new(stream); + let mut text = String::new(); + reader.read_line(&mut text)?; + println!("got '{}'", text.trim_right()); + outstream.write_all("hello from the server as well!".as_bytes())?; + Ok(()) +} + +fn main() { + let address = "127.0.0.1:8000"; + let listener = TcpListener::bind(address).expect("could not start server"); + + // accept connections and get a TcpStream + for connection in listener.incoming() { + match connection { + Ok(stream) => { + stream.set_nonblocking(false); + if let Err(e) = handle_connection(stream) { + println!("error {:?}", e); + } + } + Err(e) => { println!("connection failed {}\n", e); } + } + } +} + diff --git a/src/protocol.rs b/src/protocol.rs new file mode 100644 index 0000000..c2511bf --- /dev/null +++ b/src/protocol.rs @@ -0,0 +1,48 @@ +pub struct Protocol { + segments: Vec, +} + +impl Protocol { + pub fn new() -> Protocol { + Protocol { + segments: [false, false, false, false, false, false, false, false].to_vec(), + } + } + + pub fn get_segment(&self, index: usize) -> Result { + if index < 8 && index >= 0 { + Ok(self.segments[index]) + } else { + Err("get_segment: Index out of bounds") + } + } + + pub fn move_state(&mut self, input : &str) -> Result<&'static str, &'static str> { + let arguments:Vec<&str> = input.split_whitespace().collect(); + if arguments.len() < 1 { + return Err("No argument found!"); + } + match arguments[0] { + "setsegment" => { + let index = arguments[1].parse::().unwrap(); + let value = match arguments[2] { + "on" => true, + "off" => false, + _ => self.segments[index], + }; + } + "display" => { + //show the saved state on the display + println!("Showing state on display"); + } + "exit" => { + //close connection + println!("Closing connection."); + } + "shutdown" => { + //exit program + println!("Closing connection and shut down server"); + } + } + } +} \ No newline at end of file