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. 16
      src/main.rs
  2. 19
      src/protocol.rs

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

19
src/protocol.rs

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

Loading…
Cancel
Save