Commit afb07f0b authored by Ralf's avatar Ralf
Browse files

some refactoring

parent 9000a3e8
use std::time::Duration;
use chan::{Receiver, Sender};
use util::timeout_chan;
use input::Event;
use super::event_handler::*;
#[must_use]
#[derive(Copy,Clone,Debug,PartialEq,Eq)]
pub enum EventsResult {
ShuttingDown,
Timeout,
Quit,
}
pub struct Environment {
events: Receiver<Event>,
}
impl Environment {
pub fn handle_events(&mut self,
d: Duration,
mut handlers: Vec<EventHandler>)
-> EventsResult {
let timeout_chan = timeout_chan(d);
loop {
let events = self.events.clone();
chan_select! {
timeout_chan.recv() => return EventsResult::Timeout,
events.recv() -> ev => {
match ev {
Some(ev) => {
self.record_state(&ev);
for handler in handlers.iter_mut() {
match handler.run(ev) {
HandlerResult::Continue => {},
HandlerResult::QuitLoop => return EventsResult::Quit,
}
}
}
None => return EventsResult::ShuttingDown,
}
}
}
}
unreachable!()
}
fn record_state(&mut self, ev: &Event) {}
pub fn door_locked(&self) -> bool {
true
}
}
use input::Event;
#[must_use]
#[derive(Copy,Clone,Debug,PartialEq,Eq)]
pub enum HandlerResult {
Continue,
QuitLoop,
}
pub struct EventHandler<'a> {
run: Box<FnMut(Event) -> HandlerResult + 'a>,
}
impl<'a> EventHandler<'a> {
pub fn new<F>(f: F) -> Self
where F: FnMut(Event) -> HandlerResult + 'a
{
EventHandler { run: Box::new(f) }
}
pub fn new_boxed(f: Box<FnMut(Event) -> HandlerResult + 'a>) -> Self {
EventHandler { run: f }
}
pub fn new_safe<F>(mut f: F) -> Self
where F: FnMut(Event) + 'a
{
EventHandler {
run: Box::new(move |ev| {
f(ev);
HandlerResult::Continue
}),
}
}
pub fn new_safe_boxed(mut f: Box<FnMut(Event) + 'a>) -> Self {
EventHandler {
run: Box::new(move |ev| {
f(ev);
HandlerResult::Continue
}),
}
}
#[inline]
pub fn run(&mut self, ev: Event) -> HandlerResult {
(self.run)(ev)
}
}
struct SphinxHead; struct SphinxHead;
mod environment;
mod event_handler;
use std::time::Duration;
use self::environment::*;
use self::event_handler::*;
use actors::Actor; use actors::Actor;
use input::Event; use input::Event;
use std::time::Duration; use util::{Wakeable, Thread};
use util::{Wakeable, Thread, timeout_chan};
use chan::{Receiver, Sender};
enum LockAction { enum LockAction {
PressLock, PressLock,
...@@ -22,98 +27,6 @@ struct State { ...@@ -22,98 +27,6 @@ struct State {
run: Box<FnOnce(Actors, &mut Environment) -> (Actors, Option<State>) + 'static>, run: Box<FnOnce(Actors, &mut Environment) -> (Actors, Option<State>) + 'static>,
} }
#[must_use]
#[derive(Copy,Clone,Debug,PartialEq,Eq)]
enum EventHandlerResult {
Continue,
QuitLoop,
}
#[must_use]
#[derive(Copy,Clone,Debug,PartialEq,Eq)]
enum EventsHandledResult {
ShuttingDown,
Timeout,
Quit,
}
struct Environment {
events: Receiver<Event>,
}
impl Environment {
pub fn handle_events(&mut self,
d: Duration,
mut handlers: Vec<EventHandler>)
-> EventsHandledResult {
let timeout_chan = timeout_chan(d);
loop {
let events = self.events.clone();
chan_select! {
timeout_chan.recv() => return EventsHandledResult::Timeout,
events.recv() -> ev => {
match ev {
Some(ev) => {
self.record_state(&ev);
for handler in handlers.iter_mut() {
match (handler.run)(ev) {
EventHandlerResult::Continue => {},
EventHandlerResult::QuitLoop => return EventsHandledResult::Quit,
}
}
}
None => return EventsHandledResult::Timeout,
}
}
}
}
unreachable!()
}
fn record_state(&mut self, ev: &Event) {}
pub fn door_locked(&self) -> bool {
true
}
}
struct EventHandler<'a> {
run: Box<FnMut(Event) -> EventHandlerResult + 'a>,
}
impl<'a> EventHandler<'a> {
fn new<F>(f: F) -> Self
where F: FnMut(Event) -> EventHandlerResult + 'a
{
EventHandler { run: Box::new(f) }
}
fn new_boxed(f: Box<FnMut(Event) -> EventHandlerResult + 'a>) -> Self {
EventHandler { run: f }
}
fn new_safe<F>(mut f: F) -> Self
where F: FnMut(Event) + 'a
{
EventHandler {
run: Box::new(move |ev| {
f(ev);
EventHandlerResult::Continue
}),
}
}
fn new_safe_boxed(mut f: Box<FnMut(Event) + 'a>) -> Self {
EventHandler {
run: Box::new(move |ev| {
f(ev);
EventHandlerResult::Continue
}),
}
}
}
fn user_command_reject_handler(ev: Event) {} fn user_command_reject_handler(ev: Event) {}
impl State { impl State {
...@@ -139,15 +52,15 @@ impl State { ...@@ -139,15 +52,15 @@ impl State {
EventHandler::new_safe(user_command_reject_handler), EventHandler::new_safe(user_command_reject_handler),
EventHandler::new(|ev| { EventHandler::new(|ev| {
match ev { match ev {
Event::DoorLocked(true) => EventHandlerResult::QuitLoop, Event::DoorLocked(true) => HandlerResult::QuitLoop,
_ => EventHandlerResult::Continue, _ => HandlerResult::Continue,
} }
}), }),
]); ]);
// take back lock_actor to feel complete again // take back lock_actor to feel complete again
lock_actor = locker.terminate(); lock_actor = locker.terminate();
if r != EventsHandledResult::Timeout { if r != EventsResult::Timeout {
assert!(r == EventsHandledResult::ShuttingDown || env.door_locked()); assert!(r == EventsResult::ShuttingDown || env.door_locked());
break; break;
} }
} }
......
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