Browse Source

server protocol implemented

master
Michael Preisach 7 years ago
parent
commit
91a5a1849f
  1. 32
      src/main.rs
  2. 34
      src/protocol.rs

32
src/main.rs

@ -1,33 +1,40 @@
use std::net::{TcpStream, TcpListener}; use std::net::{TcpStream, TcpListener};
use std::io::prelude::*; use std::io::prelude::*;
use std::io; use std::io::{BufReader, Result};
mod protocol; mod protocol;
fn handle_connection(stream: TcpStream) -> io::Result<()> { fn handle_connection(mut stream: TcpStream) -> Result<()> {
let mut outstream = stream.try_clone()?; let mut outstream = stream.try_clone()?;
let mut reader = io::BufReader::new(stream); let mut reader = BufReader::new(stream);
let mut text = String::new(); let mut text = String::new();
let mut running = true;
let mut prot = protocol::Protocol::new(); let mut prot = protocol::Protocol::new();
while running { prot.start();
reader.read_line(&mut text)?; write!(outstream,"Hello to 7seg-writer!\n");
while prot.is_running() {
let len = reader.read_line(&mut text).expect("Reading line from client failed!");
if len == 0 {
break;
}
println!("read: {}", text);
match prot.parse(&text) { match prot.parse(&text) {
Ok(response) => { Ok(response) => {
outstream.write_all(response.as_bytes()); writeln!(outstream,"{}",response);
//stream.write_line(response.as_bytes());
}, },
Err(e) => { Err(e) => {
outstream.write_all(e.as_bytes()); writeln!(outstream,"{}",e);
//stream.write_line(e.as_bytes());
} }
} }
text = String::from("");
} }
outstream.write_all("hello from the server as well!".as_bytes())?;
Ok(()) Ok(())
} }
fn main() { fn main() -> Result<()> {
let address = "127.0.0.1:8000"; let address = "127.0.0.1:8000";
let listener = TcpListener::bind(address).expect("could not start server"); let listener = TcpListener::bind(address).expect("could not start server");
@ -35,13 +42,14 @@ fn main() {
for connection in listener.incoming() { for connection in listener.incoming() {
match connection { match connection {
Ok(stream) => { Ok(stream) => {
stream.set_nonblocking(false); //stream.set_nonblocking(false);
if let Err(e) = handle_connection(stream) { if let Err(e) = handle_connection(stream) {
println!("error {:?}", e); println!("error {}", e);
} }
} }
Err(e) => { println!("connection failed {}\n", e); } Err(e) => { println!("connection failed {}\n", e); }
} }
} }
Ok(())
} }

34
src/protocol.rs

@ -1,14 +1,24 @@
pub struct Protocol { pub struct Protocol {
segments: Vec<bool>, segments: Vec<bool>,
running: bool,
} }
impl Protocol { impl Protocol {
pub fn new() -> Protocol { pub fn new() -> Protocol {
Protocol { Protocol {
segments: [false, false, false, false, false, false, false, false].to_vec(), segments: [false, false, false, false, false, false, false, false].to_vec(),
running: false,
} }
} }
pub fn start(&mut self) {
self.running = true;
}
pub fn is_running(&self) -> bool{
self.running
}
pub fn get_segment(&self, index: usize) -> Result<bool, &'static str> { pub fn get_segment(&self, index: usize) -> Result<bool, &'static str> {
if index < 8 && index >= 0 { if index < 8 && index >= 0 {
Ok(self.segments[index]) Ok(self.segments[index])
@ -20,32 +30,40 @@ impl Protocol {
pub fn parse(&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 {
self.running = false;
return Err("No argument found!"); return Err("No argument found!");
} }
match arguments[0] { match arguments[0] {
"setsegment" => { "setsegment" => {
if arguments.len() != 3 {
self.running = false;
return Err("Wrong number of arguments!");
}
let index = arguments[1].parse::<usize>().unwrap(); let index = arguments[1].parse::<usize>().unwrap();
let value = match arguments[2] { let value = match arguments[2] {
"on" => true, "on" => true,
"off" => false, "off" => false,
_ => self.segments[index], _ => self.segments[index],
}; };
self.segments[index] = value;
} }
"display" => { "display" => {
//show the saved state on the display //show the saved state on the display
println!("Showing state on display"); print!("Showing state on display: ");
for seg in self.segments.clone() {
print!("{}, ", seg);
}
println!();
} }
"exit" => { "exit" => {
//close connection //close connection
println!("Closing connection."); println!("Closing connection.");
} self.running = false;
"shutdown" => {
//exit program
println!("Closing connection and shut down server");
} }
_ => { _ => {
println!("Err: Command not found"); println!("Err: Command not found, closing connection!");
return Err("Command not found\n"); self.running = false;
return Err("Command not found!");
} }
} }
Ok("OK") Ok("OK")

Loading…
Cancel
Save