diff --git a/src/main.rs b/src/main.rs index b37c93a..b579d55 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,8 +9,20 @@ 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()); + let mut running = true; + let mut prot = protocol::Protocol::new(); + + while running { + reader.read_line(&mut text)?; + match prot.parse(&text) { + Ok(response) => { + outstream.write_all(response.as_bytes()); + }, + Err(e) => { + outstream.write_all(e.as_bytes()); + } + } + } outstream.write_all("hello from the server as well!".as_bytes())?; Ok(()) } diff --git a/src/protocol.rs b/src/protocol.rs index c2511bf..e0dcccc 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -17,7 +17,7 @@ impl Protocol { } } - pub fn move_state(&mut self, input : &str) -> Result<&'static str, &'static str> { + pub fn parse(&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!"); @@ -43,6 +43,11 @@ impl Protocol { //exit program println!("Closing connection and shut down server"); } + _ => { + println!("Err: Command not found"); + return Err("Command not found\n"); + } } + Ok("OK") } } \ No newline at end of file