Commit 78b08081 authored by Ralf's avatar Ralf
Browse files

moar comments

parent db9f5297
......@@ -12,9 +12,13 @@ pub trait Actor<T> : Send {
fn act(&mut self, t: T);
}
/// Possibleactions on the door lock
#[derive(Copy,Clone,Debug,PartialEq,Eq)]
pub enum LockAction {
/// Press the "lock" button (releasing all the others)
PressLock,
/// Press the "unlock" button (releasing all the others)
PressUnlock,
/// Release all the buttons
Release,
}
......@@ -10,22 +10,25 @@ use input::Event;
/// An EventHandler is a function that handles events.
pub type EventHandler<'a> = Box<FnMut(Event) -> HandlerResult + 'a>;
/// The result of a handler is either: Go on working, or:
/// Quit the current situation
/// The result of a handler
#[must_use]
#[derive(Copy,Clone,Debug,PartialEq,Eq)]
pub enum HandlerResult {
/// Go on working
Continue,
/// Quit the current situation
QuitSituation,
}
/// The result of handling a bunch of events: Shut down the world, a timeout,
/// or quit the current situation.
/// The result of handling a bunch of events
#[must_use]
#[derive(Copy,Clone,Debug,PartialEq,Eq)]
pub enum EventsResult {
/// The worlds is shutting down
ShuttingDown,
/// Time went out
Timeout,
/// Quit the current situation
QuitSituation,
}
......
use std::fmt;
//! The brain is the core of the daemon: It manages the current [Situation]
//! we are in, as well as getting the events handled properly.
#[macro_use]
mod environment;
pub mod environment;
mod handlers;
mod situations;
use std::fmt;
use actors::{Actor, LockAction};
use self::environment::Environment;
......
......@@ -4,9 +4,13 @@
pub mod fake;
/// The events we can see, coming from our inputs
#[derive(Copy,Clone,Debug)]
pub enum Event {
/// The door has been (un)locked
DoorLocked(bool),
/// The bell started/stopped ringing
Bell(bool),
/// A command came in from the UNIX socket
UserCommand,
}
//! rinx is the Rust-based implementation of the Sphinx: The daemon that
//! manages the door of our hackerspace.
#![deny(unsafe_code)]
#![warn(missing_docs)]
// FIXME: While we are still prototyping, allow dead code
#![allow(dead_code)]
// FIXME: While we are still prototyping, allow missing docs
#![allow(missing_docs)]
extern crate take_mut;
#[macro_use]
......@@ -16,210 +18,11 @@ pub mod actors;
pub mod brain;
pub mod testing;
// FIXME everything below here is just a sketch
// struct Commander {
// TODO connection information
// dummy: i32,
// }
//
// impl Commander {
// fn send(statuscode:i32, message:&str) {
// TODO
// }
// }
//
// enum CommandType {
// CmdUnlock,
// CmdLock,
// CmdBuzz,
// CmdFallbackOn,
// CmdFallbackOff,
// }
//
// struct CommandEvent {
// commander: Commander,
// command_type: CommandType,
// }
//
// enum Event {
// Command(CommandEvent)
// }
//
// struct State {
// is_buzzing: Option<bool>,
// is_door_closed: Option<bool>,
// is_door_locked: Option<bool>,
// is_switch_on: Option<bool>,
// }
//
// impl State {
// pub fn new() -> State {
// State {
// is_buzzing: None,
// is_door_closed: None,
// is_door_locked: None,
// is_switch_on: None,
// }
// }
// }
//
// #[derive(PartialEq,Eq)]
// enum ShouldThreadRun {
// ContinueRunning,
// Terminate,
// }
//
// struct SleepInfo {
// data: Mutex<ShouldThreadRun>,
// notifyer: Condvar,
// }
//
// impl SleepInfo {
// fn new_arc() -> Arc<SleepInfo> {
// Arc::new(SleepInfo {
// data: Mutex::new(ShouldThreadRun::ContinueRunning),
// notifyer: Condvar::new(),
// })
// }
// }
//
// struct ActorThread<F>
// where F: FnOnce(ActorThread<F>) -> Actor + Send
// {
// f: Option<F>,
// sleep_info: Arc<SleepInfo>,
// }
//
// impl<F> ActorThread<F>
// where F: FnOnce(ActorThread<F>) -> Actor + Send + 'static
// {
// fn new(f: F, sleep_info: Arc<SleepInfo>) -> ActorThread<F> {
// ActorThread {
// f: Some(f),
// sleep_info: sleep_info,
// }
// }
// fn is_started(&self) -> bool {
// self.f.is_none()
// }
// fn spawn(mut self) -> Result<JoinHandle<Actor>, ()> {
// if let Some(f) = self.f {
// self.f = None;
// Ok(thread::spawn(move || f(self)))
// } else {
// Err(())
// }
// }
// fn sleep(&mut self, dur: Duration) -> WaitTimeoutResult {
// unwrap is okay because another thread must have already panicked for an
// error value to be returned here
// let sleep_result = self.sleep_info
// .notifyer
// .wait_timeout(self.sleep_info.data.lock().unwrap(), dur)
// .unwrap();
// sleep_result.1
// }
// fn should_terminate(&self) -> bool {
// (self.sleep_info.data.lock().unwrap()) == ShouldThreadRun::Terminate
// }
// }
//
// struct SphinxHead {
// state: State,
// }
//
// impl SphinxHead {
// fn new() -> SphinxHead {
// SphinxHead { state: State::new() }
// }
// fn run(&mut self) {
// let mut red_led = Actor::new(10, false);
// let sleep_info = SleepInfo::new_arc();
// let blinker = ActorThread::new(move |mut s| {
// debug_assert!(s.is_started());
// while !s.should_terminate() {
// red_led.flip();
// println!("waiting in thread");
// let sleep_result =
// s.sleep(Duration::from_millis(500));
// println!("waiting in thread done");
// println!("timeout has elapsed: {}",
// sleep_result.timed_out());
// }
// red_led
// },
// sleep_info.clone())
// .spawn()
// .unwrap();
// thread::sleep_ms(1750);
// {
// let mut endthread = sleep_info.data.lock().unwrap();
// endthread = ShouldThreadRun::Terminate;
// sleep_info.notifyer.notify_one();
// } // block is important so the lock is released again
// println!("joining...");
// red_led = blinker.join().unwrap();
// println!("joined");
// red_led.flip();
// }
// }
//
// struct Actor {
// pin: u32,
// last_set_to: bool, // TODO string name for debugging
// }
//
// impl Actor {
// pub fn new(pin: u32, initial_state: bool) -> Actor {
// let mut ret = Actor {
// pin: pin,
// last_set_to: !initial_state,
// };
// ret.set(initial_state);
// ret
// }
// pub fn set(&mut self, value: bool) {
// if self.last_set_to != value {
// self.force_set(value);
// } else {
// println!("GPIO was already {}", value);
// }
// }
// pub fn force_set(&mut self, value: bool) {
// TODO GPIO stuff
// println!("GPIO set to {}", value);
// self.last_set_to = value;
// }
// pub fn flip(&mut self) -> bool {
// let new_value = !self.last_set_to;
// self.set(new_value);
// debug_assert_eq!(new_value, self.last_set_to);
// new_value
// }
// }
// fn sphinx (old_state: &State, new_state: &State) -> i32 {
// match new_state {
// (is_buzzing:_;is_door_closed:_;is_door_locked:_;is_switch_on:_) => 0
// }
// }
//
fn main() {
// match logger::init() {
// Ok(_) => (),
// Err(e) => {
// let mut stderr = std::io::stderr();
// writeln!(&mut stderr,
// "Logger initialization failed: {}\nThere will be no logging.",
// e)
// .unwrap_or_else(|_| {
// println!("Logger init failed AND printing to stderr doesn't work. Seems like \
// we're all out of luck today :-(");
// });
// }
// }
// debug!("Logging initialised");
// let mut sphinx_head = SphinxHead::new();
// sphinx_head.run();
let _ = brain::environment::Environment::new();
// TODO: get Actors from somewhere, which use the I2C
// TODO: Spawn some input threads, using env.sender()
// TODO: obtain initiali situation
// brain::run(s, actors, &mut env);
// TODO: Shut everything down again
}
//! This module collects various utilities that are not directly tied to
//! the rest of the infrastructure.
pub mod wakeable;
......@@ -21,11 +21,12 @@ pub struct Wakeable<T, F> {
fun: Arc<F>,
}
/// The state of a Wakeable: Either the thread has not been spawned yet,
/// or we have a handle to get the T when returned by the thread, plus
/// a way to talk to it (the ThreadState).
/// The state of a Wakeable
enum WakeableState<T> {
/// We have not been spawned yet
Lazy(T),
/// We have a handle to get the T when returned by the thread, plus
/// a way to talk to it (the ThreadState).
Running(thread::JoinHandle<T>, Arc<ThreadState>),
}
......@@ -106,11 +107,12 @@ pub struct WakeableThread {
state: Arc<ThreadState>,
}
/// Defines whether the thread should be running right now, or whether it
/// should try to terminate as quickly as possible.
/// Defines whether the thread should be running right now.
#[derive(PartialEq,Eq,Debug,Copy,Clone)]
pub enum ShouldThreadRun {
/// We can go on
Yes,
/// Shut down ASAP!
No,
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment