From 9f8d2979e3411f3d4eedfdd584dc8c07dba8411c Mon Sep 17 00:00:00 2001 From: Michael Preisach Date: Tue, 30 Apr 2019 23:37:58 +0200 Subject: [PATCH] preventing server from crashing when client crashes; added RPI constant to let the server run without GPIOs --- src/main.rs | 16 ++++++++++++---- src/protocol.rs | 19 +++++++++++++++---- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 06983a1..15f4f39 100644 --- a/src/main.rs +++ b/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(_) => { - println!("Reading line from client failed!"); + 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; } - println!("read: {}", text); + 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"); + } }, } } diff --git a/src/protocol.rs b/src/protocol.rs index 3646a06..beb9b59 100644 --- a/src/protocol.rs +++ b/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,7 +95,9 @@ impl Protocol { if RPI { self.segments[index].set_nextval(value); } else { - println!("segment {} = {}", index, value); + if DEBUG { + println!("segment {} = {}", index, value); + } } } "display" => { @@ -104,13 +107,21 @@ 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!(); } - println!(); } "exit" => { //close connection