|
|
@ -1,10 +1,12 @@ |
|
|
use std::net::{TcpStream, TcpListener}; |
|
|
use std::net::{TcpStream, TcpListener}; |
|
|
use std::io::prelude::*; |
|
|
use std::io::prelude::*; |
|
|
use std::io::{BufReader, Result}; |
|
|
use std::io::{BufReader, Result}; |
|
|
|
|
|
//use crate::protocol::Protocol;
|
|
|
|
|
|
|
|
|
mod protocol; |
|
|
mod protocol; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn handle_connection(stream: TcpStream, prot: &mut protocol::Protocol) -> Result<()> { |
|
|
fn handle_connection(stream: TcpStream, prot: &mut protocol::Protocol) -> Result<()> { |
|
|
let mut outstream = stream.try_clone()?; |
|
|
let mut outstream = stream.try_clone()?; |
|
|
let mut reader = BufReader::new(stream); |
|
|
let mut reader = BufReader::new(stream); |
|
|
@ -13,19 +15,36 @@ fn handle_connection(stream: TcpStream, prot: &mut protocol::Protocol) -> Result |
|
|
prot.start(); |
|
|
prot.start(); |
|
|
write!(outstream,"Hello to 7seg-writer!\n").expect("could not write to TCP Stream!"); |
|
|
write!(outstream,"Hello to 7seg-writer!\n").expect("could not write to TCP Stream!"); |
|
|
while prot.is_running() { |
|
|
while prot.is_running() { |
|
|
let len = reader.read_line(&mut text).expect("Reading line from client failed!"); |
|
|
let len = match reader.read_line(&mut text) { |
|
|
|
|
|
Ok(len) => len, |
|
|
|
|
|
Err(_) => { |
|
|
|
|
|
println!("Reading line from client failed!"); |
|
|
|
|
|
0 |
|
|
|
|
|
}, |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
if len == 0 { |
|
|
if len == 0 { |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
println!("read: {}", text); |
|
|
println!("read: {}", text); |
|
|
match prot.parse(&text) { |
|
|
match prot.parse(&text) { |
|
|
Ok(response) => { |
|
|
Ok(response) => { |
|
|
writeln!(outstream,"{}",response).expect("could not write to TCP Stream!"); |
|
|
match writeln!(outstream,"{}",response) { |
|
|
//stream.write_line(response.as_bytes());
|
|
|
Ok(_) => (), |
|
|
|
|
|
Err(_) => { |
|
|
|
|
|
prot.stop(); |
|
|
|
|
|
println!("Write to TCP Stream failed") |
|
|
|
|
|
}, |
|
|
|
|
|
} |
|
|
}, |
|
|
}, |
|
|
Err(e) => { |
|
|
Err(e) => { |
|
|
writeln!(outstream,"{}",e).expect("could not write to TCP Stream!"); |
|
|
match writeln!(outstream,"{}",e) { |
|
|
//stream.write_line(e.as_bytes());
|
|
|
Ok(_) => (), |
|
|
|
|
|
Err(_) => { |
|
|
|
|
|
prot.stop(); |
|
|
|
|
|
println!("Write to TCP Stream failed") |
|
|
|
|
|
}, |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
text = String::from(""); |
|
|
text = String::from(""); |
|
|
@ -39,7 +58,9 @@ fn main() -> Result<()> { |
|
|
let listener = TcpListener::bind(address).expect("could not start server"); |
|
|
let listener = TcpListener::bind(address).expect("could not start server"); |
|
|
let mut prot = protocol::Protocol::new(); |
|
|
let mut prot = protocol::Protocol::new(); |
|
|
println!("Initialize GPIO"); |
|
|
println!("Initialize GPIO"); |
|
|
|
|
|
if protocol::RPI { |
|
|
prot.init(); |
|
|
prot.init(); |
|
|
|
|
|
} |
|
|
println!("Ready"); |
|
|
println!("Ready"); |
|
|
// accept connections and get a TcpStream
|
|
|
// accept connections and get a TcpStream
|
|
|
for connection in listener.incoming() { |
|
|
for connection in listener.incoming() { |
|
|
|