Browse Source

server accepts connections, command reading not fully working

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

16
src/main.rs

@ -9,8 +9,20 @@ fn handle_connection(stream: TcpStream) -> io::Result<()> {
let mut outstream = stream.try_clone()?; let mut outstream = stream.try_clone()?;
let mut reader = io::BufReader::new(stream); let mut reader = io::BufReader::new(stream);
let mut text = String::new(); let mut text = String::new();
reader.read_line(&mut text)?; let mut running = true;
println!("got '{}'", text.trim_right()); 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())?; outstream.write_all("hello from the server as well!".as_bytes())?;
Ok(()) 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(); let arguments:Vec<&str> = input.split_whitespace().collect();
if arguments.len() < 1 { if arguments.len() < 1 {
return Err("No argument found!"); return Err("No argument found!");
@ -43,6 +43,11 @@ impl Protocol {
//exit program //exit program
println!("Closing connection and shut down server"); println!("Closing connection and shut down server");
} }
_ => {
println!("Err: Command not found");
return Err("Command not found\n");
}
} }
Ok("OK")
} }
} }
Loading…
Cancel
Save