Browse Source

server accepts connections, command reading not fully working

master
Michael Preisach 7 years ago
parent
commit
11daa51c0d
  1. 14
      src/main.rs
  2. 7
      src/protocol.rs

14
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();
let mut running = true;
let mut prot = protocol::Protocol::new();
while running {
reader.read_line(&mut text)?;
println!("got '{}'", text.trim_right());
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(())
}

7
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")
}
}
Loading…
Cancel
Save