From bd9a9f0e22ea5ecca6d97a8e5d7d7e8e86c91049 Mon Sep 17 00:00:00 2001 From: Michael Preisach Date: Fri, 26 Apr 2019 16:18:06 +0200 Subject: [PATCH] Now accepting all incoming IPs --- src/main.rs | 12 ++++++++---- src/protocol.rs | 5 ++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8fef712..360b318 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,11 +5,10 @@ use std::io::{BufReader, Result}; mod protocol; -fn handle_connection(stream: TcpStream) -> Result<()> { +fn handle_connection(stream: TcpStream, prot: &mut protocol::Protocol) -> Result<()> { let mut outstream = stream.try_clone()?; let mut reader = BufReader::new(stream); let mut text = String::new(); - let mut prot = protocol::Protocol::new(); prot.start(); write!(outstream,"Hello to 7seg-writer!\n").expect("could not write to TCP Stream!"); @@ -36,20 +35,25 @@ fn handle_connection(stream: TcpStream) -> Result<()> { fn main() -> Result<()> { let address = "0.0.0.0:8000"; + println!("Initialize network"); let listener = TcpListener::bind(address).expect("could not start server"); - + let mut prot = protocol::Protocol::new(); + println!("Initialize GPIO"); + prot.init(); + println!("Ready"); // accept connections and get a TcpStream for connection in listener.incoming() { match connection { Ok(stream) => { //stream.set_nonblocking(false); - if let Err(e) = handle_connection(stream) { + if let Err(e) = handle_connection(stream, &mut prot) { println!("error {}", e); } } Err(e) => { println!("connection failed {}\n", e); } } } + println!("Shutting down"); Ok(()) } diff --git a/src/protocol.rs b/src/protocol.rs index e20cd11..e6f8073 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -40,7 +40,7 @@ impl Protocol { } } - pub fn start(&mut self) { + pub fn init(&mut self) { self.segments[0] = Segment::new(5); self.segments[1] = Segment::new(6); self.segments[2] = Segment::new(13); @@ -49,6 +49,9 @@ impl Protocol { self.segments[5] = Segment::new(21); self.segments[6] = Segment::new(20); self.segments[7] = Segment::new(16); + } + + pub fn start(&mut self) { self.running = true; }