Browse Source

preventing server from crashing when client crashes; added RPI constant to let the server run without GPIOs

master
Michael Preisach 7 years ago
parent
commit
9f8d2979e3
  1. 12
      src/main.rs
  2. 15
      src/protocol.rs

12
src/main.rs

@ -18,7 +18,9 @@ fn handle_connection(stream: TcpStream, prot: &mut protocol::Protocol) -> Result
let len = match reader.read_line(&mut text) {
Ok(len) => len,
Err(_) => {
if protocol::DEBUG {
println!("Reading line from client failed!");
}
0
},
};
@ -26,14 +28,18 @@ fn handle_connection(stream: TcpStream, prot: &mut protocol::Protocol) -> Result
if len == 0 {
break;
}
if protocol::DEBUG {
println!("read: {}", text);
}
match prot.parse(&text) {
Ok(response) => {
match writeln!(outstream,"{}",response) {
Ok(_) => (),
Err(_) => {
prot.stop();
println!("Write to TCP Stream failed")
if protocol::DEBUG {
println!("Write to TCP Stream failed");
}
},
}
},
@ -42,7 +48,9 @@ fn handle_connection(stream: TcpStream, prot: &mut protocol::Protocol) -> Result
Ok(_) => (),
Err(_) => {
prot.stop();
println!("Write to TCP Stream failed")
if protocol::DEBUG {
println!("Write to TCP Stream failed");
}
},
}
}

15
src/protocol.rs

@ -3,6 +3,7 @@ extern crate gpio;
use gpio::GpioOut;
pub static RPI: bool = true;
pub static DEBUG: bool = false;
struct Segment {
nextval: bool,
@ -94,9 +95,11 @@ impl Protocol {
if RPI {
self.segments[index].set_nextval(value);
} else {
if DEBUG {
println!("segment {} = {}", index, value);
}
}
}
"display" => {
//show the saved state on the display
@ -104,14 +107,22 @@ impl Protocol {
for i in 0..8 {
match self.segments[i].show() {
Ok(_) => (),
Err(e) => println!("{}", e),
Err(e) => {
if DEBUG {
println!("{}", e)
}
},
}
}
} else {
println!("showing segments")
if DEBUG {
println!("showing segments");
}
}
if DEBUG {
println!();
}
}
"exit" => {
//close connection
println!("Closing connection.");

Loading…
Cancel
Save