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
499cc4113e
  1. 10
      .idea/misc.xml
  2. 2
      Cargo.lock
  3. 31
      src/main.rs
  4. 19
      src/protocol.rs

10
.idea/misc.xml

@ -1,16 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="CargoProjects">
<cargoProject FILE="$PROJECT_DIR$/Cargo.toml" />
</component>
<component name="JavaScriptSettings"> <component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" /> <option name="languageLevel" value="ES6" />
</component> </component>
<component name="MacroExpansionManager">
<option name="directoryName" value="zuWvPryl" />
</component>
<component name="RustProjectSettings">
<option name="toolchainHomeDirectory" value="/usr/bin" />
<option name="version" value="2" />
</component>
</project> </project>

2
Cargo.lock

@ -1,3 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]] [[package]]
name = "gpio" name = "gpio"
version = "0.4.1" version = "0.4.1"

31
src/main.rs

@ -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() {

19
src/protocol.rs

@ -2,6 +2,8 @@ extern crate gpio;
use gpio::GpioOut; use gpio::GpioOut;
pub static RPI: bool = true;
struct Segment { struct Segment {
nextval: bool, nextval: bool,
port: gpio::sysfs::SysFsGpioOutput, port: gpio::sysfs::SysFsGpioOutput,
@ -26,7 +28,6 @@ impl Segment {
} }
} }
pub struct Protocol { pub struct Protocol {
segments: Vec<Segment>, segments: Vec<Segment>,
running: bool, running: bool,
@ -49,13 +50,16 @@ impl Protocol {
self.segments.push(Segment::new(21)); self.segments.push(Segment::new(21));
self.segments.push(Segment::new(20)); self.segments.push(Segment::new(20));
self.segments.push(Segment::new(16)); self.segments.push(Segment::new(16));
} }
pub fn start(&mut self) { pub fn start(&mut self) {
self.running = true; self.running = true;
} }
pub fn stop(&mut self) {
self.running = false;
}
pub fn is_running(&self) -> bool{ pub fn is_running(&self) -> bool{
self.running self.running
} }
@ -87,18 +91,25 @@ impl Protocol {
"off" => false, "off" => false,
_ => return Err("ERR no value for segment") _ => return Err("ERR no value for segment")
}; };
if RPI {
self.segments[index].set_nextval(value); self.segments[index].set_nextval(value);
} else {
println!("segment {} = {}", index, value);
}
} }
"display" => { "display" => {
//show the saved state on the display //show the saved state on the display
for i in 0 .. 8 { if RPI {
for i in 0..8 {
match self.segments[i].show() { match self.segments[i].show() {
Ok(_) => (), Ok(_) => (),
Err(e) => println!("{}", e), Err(e) => println!("{}", e),
} }
} }
} else {
println!("showing segments")
}
println!(); println!();
} }
"exit" => { "exit" => {

Loading…
Cancel
Save