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::io::prelude::*;
use std::io;
use std::io::{BufReader, Result};
mod protocol;
fn handle_connection(stream: TcpStream) -> io::Result<()> {
fn handle_connection(mut stream: TcpStream) -> Result<()> {
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 running = true;
let mut prot = protocol::Protocol::new();
while running {
reader.read_line(&mut text)?;
prot.start();
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) {
Ok(response) => {
outstream.write_all(response.as_bytes());
writeln!(outstream,"{}",response);
//stream.write_line(response.as_bytes());
},
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(())
}
fn main() {
fn main() -> Result<()> {
let address = "127.0.0.1:8000";
let listener = TcpListener::bind(address).expect("could not start server");
@ -35,13 +42,14 @@ fn main() {
for connection in listener.incoming() {
match connection {
Ok(stream) => {
stream.set_nonblocking(false);
//stream.set_nonblocking(false);
if let Err(e) = handle_connection(stream) {
println!("error {:?}", e);
println!("error {}", e);
}
}
Err(e) => { println!("connection failed {}\n", e); }
}
}
Ok(())
}

34
src/protocol.rs

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

Loading…
Cancel
Save