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. 33
      src/main.rs
  4. 27
      src/protocol.rs

10
.idea/misc.xml

@ -1,16 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CargoProjects">
<cargoProject FILE="$PROJECT_DIR$/Cargo.toml" />
</component>
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</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>

2
Cargo.lock

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

33
src/main.rs

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

27
src/protocol.rs

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

Loading…
Cancel
Save