Browse Source

Now accepting all incoming IPs

master
Michael Preisach 7 years ago
parent
commit
bd9a9f0e22
  1. 12
      src/main.rs
  2. 5
      src/protocol.rs

12
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(())
}

5
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;
}

Loading…
Cancel
Save