From 9bdb153c17e09e7df07addf5fef83d73f34e4d21 Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 12:13:13 +0100 Subject: [PATCH 01/58] Timer: simplify loop Signed-off-by: Tibor Benke --- src/timer.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/timer.rs b/src/timer.rs index 647271f..d4ce60d 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -19,11 +19,8 @@ pub struct Timer; impl Timer { pub fn from_chan(duration: Duration, tx: mpsc::Sender) { thread::spawn(move || { - loop { + while let Ok(_) = tx.send(Request::Timer(TimerEvent(duration))) { thread::sleep(duration); - if tx.send(Request::Timer(TimerEvent(duration))).is_err() { - break; - } } }); } From d728aa9a1834555f4e21ce1a6b3da5a77f264ec0 Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 12:19:09 +0100 Subject: [PATCH 02/58] Event: rename Handler associated type to Handle Signed-off-by: Tibor Benke --- src/dispatcher/mod.rs | 4 ++-- src/dispatcher/request.rs | 4 ++-- src/reactor.rs | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/dispatcher/mod.rs b/src/dispatcher/mod.rs index d0c4c1e..cebab0f 100644 --- a/src/dispatcher/mod.rs +++ b/src/dispatcher/mod.rs @@ -24,8 +24,8 @@ pub enum ResponseHandler { } impl Event for Response { - type Handler = ResponseHandler; - fn handler(&self) -> Self::Handler { + type Handle = ResponseHandler; + fn handler(&self) -> Self::Handle { match *self { Response::Exit => ResponseHandler::Exit, Response::Alert(_) => ResponseHandler::Message, diff --git a/src/dispatcher/request.rs b/src/dispatcher/request.rs index 6b2f9bd..15dccb1 100644 --- a/src/dispatcher/request.rs +++ b/src/dispatcher/request.rs @@ -22,8 +22,8 @@ pub enum RequestHandler { } impl reactor::Event for Request> { - type Handler = RequestHandler; - fn handler(&self) -> Self::Handler { + type Handle = RequestHandler; + fn handler(&self) -> Self::Handle { match *self { Request::Message(_) => RequestHandler::Message, Request::Timer(_) => RequestHandler::Timer, diff --git a/src/reactor.rs b/src/reactor.rs index cafe5b6..4a351cc 100644 --- a/src/reactor.rs +++ b/src/reactor.rs @@ -1,6 +1,6 @@ pub trait EventHandler { fn handle_event(&mut self, event: T, shared_data: &mut D); - fn handler(&self) -> T::Handler; + fn handler(&self) -> T::Handle; } pub trait EventDemultiplexer { @@ -13,10 +13,10 @@ pub trait Reactor { fn handle_events(&mut self); fn register_handler(&mut self, handler: Box>); fn remove_handler_by_handler(&mut self, - handler: &<>::Event as Event>::Handler); + handler: &<>::Event as Event>::Handle); } pub trait Event { - type Handler; - fn handler(&self) -> Self::Handler; + type Handle; + fn handler(&self) -> Self::Handle; } From 68d466eec5624555fc1471e2451c7f88ea5b760c Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 12:21:13 +0100 Subject: [PATCH 03/58] EventHandler: rename handler() method to handle() Signed-off-by: Tibor Benke --- src/correlator/exit_handler.rs | 2 +- src/correlator/mod.rs | 2 +- src/dispatcher/handlers/exit.rs | 2 +- src/dispatcher/handlers/message.rs | 2 +- src/dispatcher/handlers/timer.rs | 2 +- src/dispatcher/reactor.rs | 2 +- src/reactor.rs | 2 +- src/test_utils/correlator.rs | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/correlator/exit_handler.rs b/src/correlator/exit_handler.rs index af06d31..cc79092 100644 --- a/src/correlator/exit_handler.rs +++ b/src/correlator/exit_handler.rs @@ -34,7 +34,7 @@ impl EventHandler>> for ExitHandler { } } } - fn handler(&self) -> ResponseHandler { + fn handle(&self) -> ResponseHandler { ResponseHandler::Exit } } diff --git a/src/correlator/mod.rs b/src/correlator/mod.rs index 8a993c3..3163ab2 100644 --- a/src/correlator/mod.rs +++ b/src/correlator/mod.rs @@ -118,7 +118,7 @@ impl Correlator { pub fn register_handler(&mut self, handler: Box>>>) { - self.handlers.insert(handler.handler(), handler); + self.handlers.insert(handler.handle(), handler); } pub fn push_message(&mut self, diff --git a/src/dispatcher/handlers/exit.rs b/src/dispatcher/handlers/exit.rs index 1f59c3d..7b4387d 100644 --- a/src/dispatcher/handlers/exit.rs +++ b/src/dispatcher/handlers/exit.rs @@ -39,7 +39,7 @@ impl EventHandler>, ContextMap> for ExitEventHandler { unreachable!("An ExitEventHandler should only receive Exit events"); } } - fn handler(&self) -> RequestHandler { + fn handle(&self) -> RequestHandler { RequestHandler::Exit } } diff --git a/src/dispatcher/handlers/message.rs b/src/dispatcher/handlers/message.rs index 1889406..f03843c 100644 --- a/src/dispatcher/handlers/message.rs +++ b/src/dispatcher/handlers/message.rs @@ -25,7 +25,7 @@ impl reactor::EventHandler for MessageEventHandler unreachable!("MessageEventHandler should only handle Message events"); } } - fn handler(&self) -> RequestHandler { + fn handle(&self) -> RequestHandler { RequestHandler::Message } } diff --git a/src/dispatcher/handlers/timer.rs b/src/dispatcher/handlers/timer.rs index b72ceea..8d624c6 100644 --- a/src/dispatcher/handlers/timer.rs +++ b/src/dispatcher/handlers/timer.rs @@ -16,7 +16,7 @@ impl EventHandler for TimerEventHandler { i.on_event(event.clone()); } } - fn handler(&self) -> RequestHandler { + fn handle(&self) -> RequestHandler { RequestHandler::Timer } } diff --git a/src/dispatcher/reactor.rs b/src/dispatcher/reactor.rs index b790999..11ae87a 100644 --- a/src/dispatcher/reactor.rs +++ b/src/dispatcher/reactor.rs @@ -36,7 +36,7 @@ impl Reactor for RequestReactor { } } fn register_handler(&mut self, handler: Box>) { - self.handlers.insert(handler.handler(), handler); + self.handlers.insert(handler.handle(), handler); } fn remove_handler_by_handler(&mut self, handler: &RequestHandler) { self.handlers.remove(handler); diff --git a/src/reactor.rs b/src/reactor.rs index 4a351cc..3300a66 100644 --- a/src/reactor.rs +++ b/src/reactor.rs @@ -1,6 +1,6 @@ pub trait EventHandler { fn handle_event(&mut self, event: T, shared_data: &mut D); - fn handler(&self) -> T::Handle; + fn handle(&self) -> T::Handle; } pub trait EventDemultiplexer { diff --git a/src/test_utils/correlator.rs b/src/test_utils/correlator.rs index 2185f22..624dd3f 100644 --- a/src/test_utils/correlator.rs +++ b/src/test_utils/correlator.rs @@ -20,7 +20,7 @@ impl EventHandler>> for MessageEventHand self.responses.borrow_mut().push(event); } } - fn handler(&self) -> ResponseHandler { + fn handle(&self) -> ResponseHandler { ResponseHandler::Message } } From a9f7db7dfa256d1160975a110109c758d10d74b2 Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 12:23:30 +0100 Subject: [PATCH 04/58] Reactor: rename remove_handler_by_handler() method Signed-off-by: Tibor Benke --- src/dispatcher/reactor.rs | 2 +- src/reactor.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dispatcher/reactor.rs b/src/dispatcher/reactor.rs index 11ae87a..f9ba57d 100644 --- a/src/dispatcher/reactor.rs +++ b/src/dispatcher/reactor.rs @@ -38,7 +38,7 @@ impl Reactor for RequestReactor { fn register_handler(&mut self, handler: Box>) { self.handlers.insert(handler.handle(), handler); } - fn remove_handler_by_handler(&mut self, handler: &RequestHandler) { + fn remove_handler_by_handle(&mut self, handler: &RequestHandler) { self.handlers.remove(handler); } } diff --git a/src/reactor.rs b/src/reactor.rs index 3300a66..0bed4dc 100644 --- a/src/reactor.rs +++ b/src/reactor.rs @@ -12,7 +12,7 @@ pub trait Reactor { type Event: Event; fn handle_events(&mut self); fn register_handler(&mut self, handler: Box>); - fn remove_handler_by_handler(&mut self, + fn remove_handler_by_handle(&mut self, handler: &<>::Event as Event>::Handle); } From 76a7aac5c27fe42c69521e85b08adbfd04e1bc55 Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 12:55:52 +0100 Subject: [PATCH 05/58] Conditions: ensure first_opens doesn't cause panic If it's used without patterns... Signed-off-by: Tibor Benke --- src/conditions.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/conditions.rs b/src/conditions.rs index 4af395e..5759143 100644 --- a/src/conditions.rs +++ b/src/conditions.rs @@ -267,6 +267,20 @@ mod test { state.on_message(Rc::new(p2_msg), &context); assert_false!(state.is_open()); } + + #[test] + fn test_given_condition_when_first_opens_is_set_but_there_are_no_patterns_then_we_do_not_panic() { + let msg = MessageBuilder::new("e4f3f8b2-3135-4916-a5ea-621a754dab0d", "message") + .name(Some("p1")) + .build(); + let conditions = ConditionsBuilder::new(Duration::from_millis(100)) + .patterns(Vec::new()) + .first_opens(true) + .build(); + let context = BaseContextBuilder::new(Uuid::new_v4(), conditions).build(); + let mut state = State::new(); + state.on_message(Rc::new(msg), &context); + } } mod deser { From ab0478a3eb674550cafec38babb0e7b840772fa8 Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 12:57:24 +0100 Subject: [PATCH 06/58] Conditions: don't call unwrap() in is_opening() Signed-off-by: Tibor Benke --- src/conditions.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/conditions.rs b/src/conditions.rs index 5759143..d67cc5b 100644 --- a/src/conditions.rs +++ b/src/conditions.rs @@ -29,7 +29,7 @@ impl Conditions { pub fn is_opening(&self, message: &Message) -> bool { if self.first_opens { - message.ids().any(|x| x == self.patterns.first().unwrap()) + self.patterns.first().iter().any(|first|{ message.ids().any(|id| &id == first)}) } else { true } From e1ee3db4a46762a71bddcf50d08e9e9570104851 Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 13:14:21 +0100 Subject: [PATCH 07/58] Conditions: don't call panic when last_closes is set And there are no patterns... Signed-off-by: Tibor Benke --- src/conditions.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/conditions.rs b/src/conditions.rs index d67cc5b..02d91fa 100644 --- a/src/conditions.rs +++ b/src/conditions.rs @@ -51,8 +51,9 @@ impl Conditions { fn is_closing_message(&self, state: &State) -> bool { if self.last_closes { - let last_message = state.messages().last().unwrap(); - last_message.ids().any(|x| x == self.patterns.last().unwrap()) + state.messages().last().iter().any(|last_message| { + self.patterns.last().iter().any(|last|{ last_message.ids().any(|id| &id == last)}) + }) } else { false } From d55f8ab6d3a899a082707d2ee153e5ad6c223ed6 Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 13:14:57 +0100 Subject: [PATCH 08/58] Conditions: ensure panic is not called when last_closes is set And there are no patterns. Signed-off-by: Tibor Benke --- src/conditions.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/conditions.rs b/src/conditions.rs index 02d91fa..bb533a1 100644 --- a/src/conditions.rs +++ b/src/conditions.rs @@ -282,6 +282,20 @@ mod test { let mut state = State::new(); state.on_message(Rc::new(msg), &context); } + + #[test] + fn test_given_condition_when_last_closes_is_set_but_there_are_no_patterns_then_we_do_not_panic() { + let msg = MessageBuilder::new("e4f3f8b2-3135-4916-a5ea-621a754dab0d", "message") + .name(Some("p1")) + .build(); + let conditions = ConditionsBuilder::new(Duration::from_millis(100)) + .patterns(Vec::new()) + .last_closes(true) + .build(); + let context = BaseContextBuilder::new(Uuid::new_v4(), conditions).build(); + let mut state = State::new(); + state.on_message(Rc::new(msg), &context); + } } mod deser { From 69f31d835a2388775358d1ddd8374ff2a6bddbed Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 13:19:14 +0100 Subject: [PATCH 09/58] Condition: use Cell instead of RefCell Signed-off-by: Tibor Benke --- src/condition.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/condition.rs b/src/condition.rs index 2d50368..9279436 100644 --- a/src/condition.rs +++ b/src/condition.rs @@ -1,23 +1,23 @@ -use std::cell::RefCell; +use std::cell::Cell; use std::rc::Rc; #[derive(Clone, Debug)] -pub struct Condition(Rc>); +pub struct Condition(Rc>); impl Condition { pub fn new(state: bool) -> Condition { - Condition(Rc::new(RefCell::new(state))) + Condition(Rc::new(Cell::new(state))) } pub fn is_active(&self) -> bool { - *self.0.borrow() + self.0.get() } pub fn activate(&mut self) { - *self.0.borrow_mut() = true; + self.0.set(true); } pub fn deactivate(&mut self) { - *self.0.borrow_mut() = false; + self.0.set(false); } } From 1493ea27b335630196e12c0a81900ae15136fd62 Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 13:25:47 +0100 Subject: [PATCH 10/58] MessageBuilder: simplify name() method Signed-off-by: Tibor Benke --- src/message/builder.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/message/builder.rs b/src/message/builder.rs index c15a5c7..2fb0e3b 100644 --- a/src/message/builder.rs +++ b/src/message/builder.rs @@ -19,12 +19,8 @@ impl MessageBuilder { } } - pub fn name(&mut self, name: Option<&str>) -> &mut MessageBuilder { - if let Some(name) = name { - self.name = Some(name.to_string()); - } else { - self.name = None; - } + pub fn name>(&mut self, name: Option) -> &mut MessageBuilder { + self.name = name.map(|name| name.into()); self } From 8839188aa553ce9027779ae9d1b68ad0300a5baf Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 13:26:11 +0100 Subject: [PATCH 11/58] Conditions: fix MessageBuilder usage Signed-off-by: Tibor Benke --- src/conditions.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/conditions.rs b/src/conditions.rs index bb533a1..257ba11 100644 --- a/src/conditions.rs +++ b/src/conditions.rs @@ -241,7 +241,7 @@ mod test { .patterns(patterns) .first_opens(true) .build(); - let msg = MessageBuilder::new(&uuid, "message").name(Some(&msg_id)).build(); + let msg = MessageBuilder::new(&uuid, "message").name(Some(msg_id)).build(); assert_true!(condition.is_opening(&msg)); } @@ -260,8 +260,8 @@ mod test { .first_opens(true) .last_closes(true) .build(); - let p1_msg = MessageBuilder::new(&p1_uuid, "message").name(Some(&p1)).build(); - let p2_msg = MessageBuilder::new(&p2_uuid, "message").name(Some(&p2)).build(); + let p1_msg = MessageBuilder::new(&p1_uuid, "message").name(Some(p1)).build(); + let p2_msg = MessageBuilder::new(&p2_uuid, "message").name(Some(p2)).build(); let context = BaseContextBuilder::new(Uuid::new_v4(), conditions).build(); assert_false!(state.is_open()); state.on_message(Rc::new(p1_msg), &context); From 62c3f463e1ac13f1fc84fe08cf97444a50e796c9 Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 13:35:20 +0100 Subject: [PATCH 12/58] ResponseHandler: rename Message variant to Alert Signed-off-by: Tibor Benke --- src/dispatcher/mod.rs | 4 ++-- src/test_utils/correlator.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dispatcher/mod.rs b/src/dispatcher/mod.rs index cebab0f..3e80500 100644 --- a/src/dispatcher/mod.rs +++ b/src/dispatcher/mod.rs @@ -20,7 +20,7 @@ pub enum Response { #[derive(Debug, Eq, Hash, PartialEq)] pub enum ResponseHandler { Exit, - Message, + Alert, } impl Event for Response { @@ -28,7 +28,7 @@ impl Event for Response { fn handler(&self) -> Self::Handle { match *self { Response::Exit => ResponseHandler::Exit, - Response::Alert(_) => ResponseHandler::Message, + Response::Alert(_) => ResponseHandler::Alert, } } } diff --git a/src/test_utils/correlator.rs b/src/test_utils/correlator.rs index 624dd3f..544f6d1 100644 --- a/src/test_utils/correlator.rs +++ b/src/test_utils/correlator.rs @@ -21,6 +21,6 @@ impl EventHandler>> for MessageEventHand } } fn handle(&self) -> ResponseHandler { - ResponseHandler::Message + ResponseHandler::Alert } } From bfd1dd70e213911ec58a59a458e5b4fa364f59a0 Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 13:37:52 +0100 Subject: [PATCH 13/58] ResponseHandler: rename to ResponseHandle Signed-off-by: Tibor Benke --- src/correlator/exit_handler.rs | 6 +++--- src/correlator/mod.rs | 4 ++-- src/dispatcher/mod.rs | 8 ++++---- src/test_utils/correlator.rs | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/correlator/exit_handler.rs b/src/correlator/exit_handler.rs index cc79092..c75e7b6 100644 --- a/src/correlator/exit_handler.rs +++ b/src/correlator/exit_handler.rs @@ -4,7 +4,7 @@ use Response; use message::Message; use condition::Condition; use dispatcher::request::Request; -use dispatcher::ResponseHandler; +use dispatcher::ResponseHandle; use reactor::EventHandler; pub struct ExitHandler { @@ -34,7 +34,7 @@ impl EventHandler>> for ExitHandler { } } } - fn handle(&self) -> ResponseHandler { - ResponseHandler::Exit + fn handle(&self) -> ResponseHandle { + ResponseHandle::Exit } } diff --git a/src/correlator/mod.rs b/src/correlator/mod.rs index 3163ab2..2abbdc4 100644 --- a/src/correlator/mod.rs +++ b/src/correlator/mod.rs @@ -15,7 +15,7 @@ use context::linear::LinearContext; use context::map::MapContext; use dispatcher::request::Request; use dispatcher::reactor::RequestReactor; -use dispatcher::{ResponseSender, ResponseHandler}; +use dispatcher::{ResponseSender, ResponseHandle}; use dispatcher::response; use dispatcher::demux::Demultiplexer; use dispatcher::handlers; @@ -36,7 +36,7 @@ pub struct Correlator { dispatcher_input_channel: mpsc::Sender>, dispatcher_output_channel: mpsc::Receiver, dispatcher_thread_handle: thread::JoinHandle<()>, - handlers: HashMap>>>>, + handlers: HashMap>>>>, } fn create_context(config_context: config::Context, diff --git a/src/dispatcher/mod.rs b/src/dispatcher/mod.rs index 3e80500..fb0fd5c 100644 --- a/src/dispatcher/mod.rs +++ b/src/dispatcher/mod.rs @@ -18,17 +18,17 @@ pub enum Response { } #[derive(Debug, Eq, Hash, PartialEq)] -pub enum ResponseHandler { +pub enum ResponseHandle { Exit, Alert, } impl Event for Response { - type Handle = ResponseHandler; + type Handle = ResponseHandle; fn handler(&self) -> Self::Handle { match *self { - Response::Exit => ResponseHandler::Exit, - Response::Alert(_) => ResponseHandler::Alert, + Response::Exit => ResponseHandle::Exit, + Response::Alert(_) => ResponseHandle::Alert, } } } diff --git a/src/test_utils/correlator.rs b/src/test_utils/correlator.rs index 544f6d1..bdc4683 100644 --- a/src/test_utils/correlator.rs +++ b/src/test_utils/correlator.rs @@ -1,4 +1,4 @@ -use dispatcher::ResponseHandler; +use dispatcher::ResponseHandle; use Response; use Message; @@ -20,7 +20,7 @@ impl EventHandler>> for MessageEventHand self.responses.borrow_mut().push(event); } } - fn handle(&self) -> ResponseHandler { - ResponseHandler::Alert + fn handle(&self) -> ResponseHandle { + ResponseHandle::Alert } } From 141a84f468f3f73bf3ad552b6a4456360bd1128a Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 13:41:31 +0100 Subject: [PATCH 14/58] Event: rename handler() method to handle() Signed-off-by: Tibor Benke --- src/correlator/mod.rs | 2 +- src/dispatcher/mod.rs | 2 +- src/dispatcher/reactor.rs | 2 +- src/dispatcher/request.rs | 2 +- src/reactor.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/correlator/mod.rs b/src/correlator/mod.rs index 2abbdc4..5aa79b6 100644 --- a/src/correlator/mod.rs +++ b/src/correlator/mod.rs @@ -129,7 +129,7 @@ impl Correlator { } fn handle_event(&mut self, event: Response) { - if let Some(handler) = self.handlers.get_mut(&event.handler()) { + if let Some(handler) = self.handlers.get_mut(&event.handle()) { handler.handle_event(event, &mut self.dispatcher_input_channel); } else { trace!("no event handler found for handling a Response"); diff --git a/src/dispatcher/mod.rs b/src/dispatcher/mod.rs index fb0fd5c..a7e5406 100644 --- a/src/dispatcher/mod.rs +++ b/src/dispatcher/mod.rs @@ -25,7 +25,7 @@ pub enum ResponseHandle { impl Event for Response { type Handle = ResponseHandle; - fn handler(&self) -> Self::Handle { + fn handle(&self) -> Self::Handle { match *self { Response::Exit => ResponseHandle::Exit, Response::Alert(_) => ResponseHandle::Alert, diff --git a/src/dispatcher/reactor.rs b/src/dispatcher/reactor.rs index f9ba57d..fd2092f 100644 --- a/src/dispatcher/reactor.rs +++ b/src/dispatcher/reactor.rs @@ -28,7 +28,7 @@ impl Reactor for RequestReactor { fn handle_events(&mut self) { while let Some(request) = self.demultiplexer.select() { trace!("RequestReactor: got event"); - if let Some(handler) = self.handlers.get_mut(&request.handler()) { + if let Some(handler) = self.handlers.get_mut(&request.handle()) { handler.handle_event(request, &mut self.context_map); } else { trace!("RequestReactor: no handler found for event"); diff --git a/src/dispatcher/request.rs b/src/dispatcher/request.rs index 15dccb1..2b72c2e 100644 --- a/src/dispatcher/request.rs +++ b/src/dispatcher/request.rs @@ -23,7 +23,7 @@ pub enum RequestHandler { impl reactor::Event for Request> { type Handle = RequestHandler; - fn handler(&self) -> Self::Handle { + fn handle(&self) -> Self::Handle { match *self { Request::Message(_) => RequestHandler::Message, Request::Timer(_) => RequestHandler::Timer, diff --git a/src/reactor.rs b/src/reactor.rs index 0bed4dc..5458d2b 100644 --- a/src/reactor.rs +++ b/src/reactor.rs @@ -18,5 +18,5 @@ pub trait Reactor { pub trait Event { type Handle; - fn handler(&self) -> Self::Handle; + fn handle(&self) -> Self::Handle; } From 031ba8212bd7ed4252f5c6c49d5db8f7d9da1412 Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 16:46:38 +0100 Subject: [PATCH 15/58] RequestHandler: rename to RequestHandle Signed-off-by: Tibor Benke --- src/dispatcher/handlers/exit.rs | 6 +++--- src/dispatcher/handlers/message.rs | 6 +++--- src/dispatcher/handlers/timer.rs | 6 +++--- src/dispatcher/reactor.rs | 6 +++--- src/dispatcher/request.rs | 10 +++++----- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/dispatcher/handlers/exit.rs b/src/dispatcher/handlers/exit.rs index 7b4387d..f464777 100644 --- a/src/dispatcher/handlers/exit.rs +++ b/src/dispatcher/handlers/exit.rs @@ -3,7 +3,7 @@ use std::rc::Rc; use context::ContextMap; use dispatcher::response::ResponseSender; use dispatcher::Response; -use dispatcher::request::{Request, RequestHandler}; +use dispatcher::request::{Request, RequestHandle}; use condition::Condition; use message::Message; use reactor::EventHandler; @@ -39,7 +39,7 @@ impl EventHandler>, ContextMap> for ExitEventHandler { unreachable!("An ExitEventHandler should only receive Exit events"); } } - fn handle(&self) -> RequestHandler { - RequestHandler::Exit + fn handle(&self) -> RequestHandle { + RequestHandle::Exit } } diff --git a/src/dispatcher/handlers/message.rs b/src/dispatcher/handlers/message.rs index f03843c..795f2a9 100644 --- a/src/dispatcher/handlers/message.rs +++ b/src/dispatcher/handlers/message.rs @@ -1,4 +1,4 @@ -use dispatcher::request::{InternalRequest, Request, RequestHandler}; +use dispatcher::request::{InternalRequest, Request, RequestHandle}; use context::ContextMap; use context::context_map::StreamingIterator; use reactor; @@ -25,7 +25,7 @@ impl reactor::EventHandler for MessageEventHandler unreachable!("MessageEventHandler should only handle Message events"); } } - fn handle(&self) -> RequestHandler { - RequestHandler::Message + fn handle(&self) -> RequestHandle { + RequestHandle::Message } } diff --git a/src/dispatcher/handlers/timer.rs b/src/dispatcher/handlers/timer.rs index 8d624c6..181e2eb 100644 --- a/src/dispatcher/handlers/timer.rs +++ b/src/dispatcher/handlers/timer.rs @@ -1,4 +1,4 @@ -use dispatcher::request::{InternalRequest, RequestHandler}; +use dispatcher::request::{InternalRequest, RequestHandle}; use context::ContextMap; use reactor::EventHandler; @@ -16,7 +16,7 @@ impl EventHandler for TimerEventHandler { i.on_event(event.clone()); } } - fn handle(&self) -> RequestHandler { - RequestHandler::Timer + fn handle(&self) -> RequestHandle { + RequestHandle::Timer } } diff --git a/src/dispatcher/reactor.rs b/src/dispatcher/reactor.rs index fd2092f..fab9a09 100644 --- a/src/dispatcher/reactor.rs +++ b/src/dispatcher/reactor.rs @@ -2,11 +2,11 @@ use std::collections::BTreeMap; use context::ContextMap; use dispatcher::demux::Demultiplexer; -use dispatcher::request::{RequestHandler, InternalRequest, ExternalRequest}; +use dispatcher::request::{RequestHandle, InternalRequest, ExternalRequest}; use reactor::{Event, EventDemultiplexer, EventHandler, Reactor}; pub struct RequestReactor { - handlers: BTreeMap>>, + handlers: BTreeMap>>, demultiplexer: Demultiplexer, context_map: ContextMap, } @@ -38,7 +38,7 @@ impl Reactor for RequestReactor { fn register_handler(&mut self, handler: Box>) { self.handlers.insert(handler.handle(), handler); } - fn remove_handler_by_handle(&mut self, handler: &RequestHandler) { + fn remove_handler_by_handle(&mut self, handler: &RequestHandle) { self.handlers.remove(handler); } } diff --git a/src/dispatcher/request.rs b/src/dispatcher/request.rs index 2b72c2e..02f1a1b 100644 --- a/src/dispatcher/request.rs +++ b/src/dispatcher/request.rs @@ -15,19 +15,19 @@ pub type InternalRequest = Request>; pub type ExternalRequest = Request; #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] -pub enum RequestHandler { +pub enum RequestHandle { Message, Timer, Exit, } impl reactor::Event for Request> { - type Handle = RequestHandler; + type Handle = RequestHandle; fn handle(&self) -> Self::Handle { match *self { - Request::Message(_) => RequestHandler::Message, - Request::Timer(_) => RequestHandler::Timer, - Request::Exit => RequestHandler::Exit, + Request::Message(_) => RequestHandle::Message, + Request::Timer(_) => RequestHandle::Timer, + Request::Exit => RequestHandle::Exit, } } } From 650df2322822a3b078287d7d4743783ff1871c87 Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 17:35:28 +0100 Subject: [PATCH 16/58] Request: remove genericity The dispatcher received a Request and converted it into a Request>. The contexts don't modify the messages so an Arc is a better choice. Signed-off-by: Tibor Benke --- src/action/message/renderer_context.rs | 6 +++--- src/action/message/test.rs | 5 +++-- src/conditions.rs | 14 +++++++------- src/context/linear.rs | 8 ++++---- src/context/map/map.rs | 10 +++++----- src/context/map/test.rs | 8 ++++---- src/context/mod.rs | 4 ++-- src/context/test.rs | 10 +++++----- src/correlator/exit_handler.rs | 9 ++++----- src/correlator/mod.rs | 11 ++++++----- src/dispatcher/demux.rs | 6 +++--- src/dispatcher/handlers/exit.rs | 7 ++----- src/dispatcher/handlers/message.rs | 6 +++--- src/dispatcher/handlers/timer.rs | 6 +++--- src/dispatcher/reactor.rs | 10 +++++----- src/dispatcher/request.rs | 21 ++++----------------- src/state.rs | 12 ++++++------ src/test_utils/correlator.rs | 5 ++--- src/timer.rs | 4 ++-- 19 files changed, 73 insertions(+), 89 deletions(-) diff --git a/src/action/message/renderer_context.rs b/src/action/message/renderer_context.rs index 31b736f..929b4f2 100644 --- a/src/action/message/renderer_context.rs +++ b/src/action/message/renderer_context.rs @@ -5,12 +5,12 @@ use context::BaseContext; use uuid::Uuid; use rustc_serialize::json::{Json, ToJson}; use std::collections::BTreeMap; -use std::rc::Rc; +use std::sync::Arc; use super::{CONTEXT_LEN, CONTEXT_NAME, CONTEXT_UUID, MESSAGES}; pub struct RendererContext<'m, 'c> { - messages: &'m [Rc], + messages: &'m [Arc], context_name: Option<&'c String>, context_uuid: &'c Uuid, } @@ -39,7 +39,7 @@ impl<'m, 'c> ToJson for RendererContext<'m, 'c> { } } -fn rc_message_to_json(messages: &[Rc]) -> Json { +fn rc_message_to_json(messages: &[Arc]) -> Json { let mut array: Vec<&Message> = Vec::new(); for i in messages { array.push(i); diff --git a/src/action/message/test.rs b/src/action/message/test.rs index fe8de11..6be5619 100644 --- a/src/action/message/test.rs +++ b/src/action/message/test.rs @@ -13,6 +13,7 @@ use handlebars::Template; use std::cell::RefCell; use std::time::Duration; use std::rc::Rc; +use std::sync::Arc; use uuid::Uuid; #[derive(Clone)] @@ -83,10 +84,10 @@ fn test_given_message_action_when_it_is_executed_then_it_uses_the_messages_to_re BaseContextBuilder::new(uuid, conditions).name(name.clone()).build() }; let state = { - let messages = vec![Rc::new(MessageBuilder::new("uuid1", "message1") + let messages = vec![Arc::new(MessageBuilder::new("uuid1", "message1") .pair("key1", "value1") .build()), - Rc::new(MessageBuilder::new("uuid2", "message2") + Arc::new(MessageBuilder::new("uuid2", "message2") .pair("key2", "value2") .build())]; State::with_messages(messages) diff --git a/src/conditions.rs b/src/conditions.rs index 257ba11..813ad31 100644 --- a/src/conditions.rs +++ b/src/conditions.rs @@ -116,7 +116,7 @@ impl ConditionsBuilder { mod test { use serde_json::from_str; use super::Conditions; - use std::rc::Rc; + use std::sync::Arc; use message::MessageBuilder; use state::State; @@ -158,8 +158,8 @@ mod test { .last_closes(true) .build(); let context = BaseContextBuilder::new(Uuid::new_v4(), conditions).build(); - let msg_opening = Rc::new(MessageBuilder::new(&msg_id1, "message").build()); - let msg_closing = Rc::new(MessageBuilder::new(&msg_id2, "message").build()); + let msg_opening = Arc::new(MessageBuilder::new(&msg_id1, "message").build()); + let msg_closing = Arc::new(MessageBuilder::new(&msg_id2, "message").build()); assert_false!(state.is_open()); state.on_message(msg_opening, &context); assert_true!(state.is_open()); @@ -264,8 +264,8 @@ mod test { let p2_msg = MessageBuilder::new(&p2_uuid, "message").name(Some(p2)).build(); let context = BaseContextBuilder::new(Uuid::new_v4(), conditions).build(); assert_false!(state.is_open()); - state.on_message(Rc::new(p1_msg), &context); - state.on_message(Rc::new(p2_msg), &context); + state.on_message(Arc::new(p1_msg), &context); + state.on_message(Arc::new(p2_msg), &context); assert_false!(state.is_open()); } @@ -280,7 +280,7 @@ mod test { .build(); let context = BaseContextBuilder::new(Uuid::new_v4(), conditions).build(); let mut state = State::new(); - state.on_message(Rc::new(msg), &context); + state.on_message(Arc::new(msg), &context); } #[test] @@ -294,7 +294,7 @@ mod test { .build(); let context = BaseContextBuilder::new(Uuid::new_v4(), conditions).build(); let mut state = State::new(); - state.on_message(Rc::new(msg), &context); + state.on_message(Arc::new(msg), &context); } } diff --git a/src/context/linear.rs b/src/context/linear.rs index afcb3c5..0ec58e6 100644 --- a/src/context/linear.rs +++ b/src/context/linear.rs @@ -1,11 +1,11 @@ use uuid::Uuid; -use std::rc::Rc; +use std::sync::Arc; use conditions::Conditions; use message::Message; use state::State; use timer::TimerEvent; -use dispatcher::request::{InternalRequest, Request}; +use dispatcher::request::Request; use context::base::{BaseContext, BaseContextBuilder}; pub struct LinearContext { @@ -22,7 +22,7 @@ impl LinearContext { } } - pub fn on_event(&mut self, event: InternalRequest) { + pub fn on_event(&mut self, event: Request) { trace!("LinearContext: received event"); match event { Request::Timer(event) => self.on_timer(&event), @@ -35,7 +35,7 @@ impl LinearContext { self.state.on_timer(event, &self.base); } - pub fn on_message(&mut self, event: Rc) { + pub fn on_message(&mut self, event: Arc) { self.state.on_message(event, &self.base); } diff --git a/src/context/map/map.rs b/src/context/map/map.rs index e885ed5..5178842 100644 --- a/src/context/map/map.rs +++ b/src/context/map/map.rs @@ -1,12 +1,12 @@ use handlebars::{Handlebars, Template}; use std::collections::BTreeMap; -use std::rc::Rc; +use std::sync::Arc; use message::Message; use state::State; use timer::TimerEvent; use context::base::BaseContext; -use dispatcher::request::{Request, InternalRequest}; +use dispatcher::request::Request; const CONTEXT_ID: &'static str = ".context.id"; @@ -27,7 +27,7 @@ impl MapContext { } } - pub fn on_event(&mut self, event: InternalRequest) { + pub fn on_event(&mut self, event: Request) { trace!("MapContext: received event"); match event { Request::Timer(event) => self.on_timer(&event), @@ -62,12 +62,12 @@ impl MapContext { } } - pub fn on_message(&mut self, event: Rc) { + pub fn on_message(&mut self, event: Arc) { self.update_state(event); self.remove_closed_states(); } - fn update_state(&mut self, event: Rc) { + fn update_state(&mut self, event: Arc) { let id = self.context_id .render(CONTEXT_ID, event.values()) .ok() diff --git a/src/context/map/test.rs b/src/context/map/test.rs index 172df94..cdf6bef 100644 --- a/src/context/map/test.rs +++ b/src/context/map/test.rs @@ -5,7 +5,7 @@ use message::MessageBuilder; use handlebars::Template; use uuid::Uuid; -use std::rc::Rc; +use std::sync::Arc; use std::time::Duration; #[test] @@ -47,11 +47,11 @@ fn test_given_map_context_when_messages_have_the_same_kvpairs_then_they_go_to_th .build(); assert_false!(context.is_open()); - context.on_message(Rc::new(msg1)); + context.on_message(Arc::new(msg1)); assert_true!(context.is_open()); context.on_timer(&event); - context.on_message(Rc::new(msg2)); - context.on_message(Rc::new(msg3)); + context.on_message(Arc::new(msg2)); + context.on_message(Arc::new(msg3)); context.on_timer(&event); context.on_timer(&event); assert_true!(context.is_open()); diff --git a/src/context/mod.rs b/src/context/mod.rs index c837b2c..a40b8c6 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -1,4 +1,4 @@ -use dispatcher::request::InternalRequest; +use dispatcher::request::Request; pub use self::linear::LinearContext; pub use self::map::MapContext; @@ -18,7 +18,7 @@ pub enum Context { Map(MapContext), } impl Context { - pub fn on_event(&mut self, event: InternalRequest) { + pub fn on_event(&mut self, event: Request) { match *self { Context::Linear(ref mut context) => context.on_event(event), Context::Map(ref mut context) => context.on_event(event), diff --git a/src/context/test.rs b/src/context/test.rs index 3898600..c88c780 100644 --- a/src/context/test.rs +++ b/src/context/test.rs @@ -1,5 +1,5 @@ use uuid::Uuid; -use std::rc::Rc; +use std::sync::Arc; use std::time::Duration; use message::MessageBuilder; @@ -19,7 +19,7 @@ fn test_given_close_condition_with_timeout_when_the_timeout_expires_then_the_con .patterns(patterns) .build()); let msg1 = MessageBuilder::new(&msg_id, "message").build(); - let event = Rc::new(msg1); + let event = Arc::new(msg1); assert_false!(context.is_open()); context.on_message(event); assert_true!(context.is_open()); @@ -46,7 +46,7 @@ fn test_given_close_condition_with_max_size_when_the_max_size_reached_then_the_c .patterns(patterns) .build()); let msg1 = MessageBuilder::new(&msg_id, "message").build(); - let event = Rc::new(msg1); + let event = Arc::new(msg1); context.on_message(event.clone()); assert_true!(context.is_open()); context.on_message(event.clone()); @@ -70,7 +70,7 @@ fn test_given_close_condition_with_renew_timeout_when_the_timeout_expires_withou .patterns(patterns) .build()); let msg1 = MessageBuilder::new(&msg_id, "message").build(); - let event = Rc::new(msg1); + let event = Arc::new(msg1); context.on_message(event.clone()); assert_true!(context.is_open()); context.on_timer(&mut TimerEvent::from_millis(8)); @@ -96,7 +96,7 @@ fn test_given_close_condition_with_renew_timeout_when_the_timeout_expires_with_r .patterns(patterns) .build()); let msg1 = MessageBuilder::new(&msg_id, "message").build(); - let event = Rc::new(msg1); + let event = Arc::new(msg1); assert_false!(context.is_open()); context.on_message(event.clone()); assert_true!(context.is_open()); diff --git a/src/correlator/exit_handler.rs b/src/correlator/exit_handler.rs index c75e7b6..c1cee0c 100644 --- a/src/correlator/exit_handler.rs +++ b/src/correlator/exit_handler.rs @@ -1,20 +1,19 @@ use std::sync::mpsc; use Response; -use message::Message; use condition::Condition; use dispatcher::request::Request; use dispatcher::ResponseHandle; use reactor::EventHandler; pub struct ExitHandler { - channel: mpsc::Sender>, + channel: mpsc::Sender, exits_received: u32, condition: Condition, } impl ExitHandler { - pub fn new(condition: Condition, channel: mpsc::Sender>) -> ExitHandler { + pub fn new(condition: Condition, channel: mpsc::Sender) -> ExitHandler { ExitHandler { channel: channel, exits_received: 0, @@ -23,8 +22,8 @@ impl ExitHandler { } } -impl EventHandler>> for ExitHandler { - fn handle_event(&mut self, event: Response, _: &mut mpsc::Sender>) { +impl EventHandler> for ExitHandler { + fn handle_event(&mut self, event: Response, _: &mut mpsc::Sender) { if let Response::Exit = event { self.exits_received += 1; let _ = self.channel.send(Request::Exit); diff --git a/src/correlator/mod.rs b/src/correlator/mod.rs index 5aa79b6..8a66804 100644 --- a/src/correlator/mod.rs +++ b/src/correlator/mod.rs @@ -6,6 +6,7 @@ use std::sync::mpsc; use std::thread; use std::result::Result; use std::time::Duration; +use std::sync::Arc; use {action, config, context, Message, Response}; use condition::Condition; @@ -33,10 +34,10 @@ mod exit_handler; mod test; pub struct Correlator { - dispatcher_input_channel: mpsc::Sender>, + dispatcher_input_channel: mpsc::Sender, dispatcher_output_channel: mpsc::Receiver, dispatcher_thread_handle: thread::JoinHandle<()>, - handlers: HashMap>>>>, + handlers: HashMap>>>, } fn create_context(config_context: config::Context, @@ -117,15 +118,15 @@ impl Correlator { } pub fn register_handler(&mut self, - handler: Box>>>) { + handler: Box>>) { self.handlers.insert(handler.handle(), handler); } pub fn push_message(&mut self, message: Message) - -> Result<(), mpsc::SendError>> { + -> Result<(), mpsc::SendError> { self.handle_events(); - self.dispatcher_input_channel.send(Request::Message(message)) + self.dispatcher_input_channel.send(Request::Message(Arc::new(message))) } fn handle_event(&mut self, event: Response) { diff --git a/src/dispatcher/demux.rs b/src/dispatcher/demux.rs index 39b95db..6f97821 100644 --- a/src/dispatcher/demux.rs +++ b/src/dispatcher/demux.rs @@ -1,6 +1,6 @@ use std::sync::mpsc::Receiver; -use dispatcher::request::{InternalRequest, ExternalRequest}; +use dispatcher::request::Request; use reactor::EventDemultiplexer; use condition::Condition; @@ -18,8 +18,8 @@ impl Demultiplexer { } } -impl EventDemultiplexer for Demultiplexer { - type Event = InternalRequest; +impl EventDemultiplexer for Demultiplexer { + type Event = Request; fn select(&mut self) -> Option { if !self.condition.is_active() { let data = self.channel.recv().ok(); diff --git a/src/dispatcher/handlers/exit.rs b/src/dispatcher/handlers/exit.rs index f464777..8f12a87 100644 --- a/src/dispatcher/handlers/exit.rs +++ b/src/dispatcher/handlers/exit.rs @@ -1,11 +1,8 @@ -use std::rc::Rc; - use context::ContextMap; use dispatcher::response::ResponseSender; use dispatcher::Response; use dispatcher::request::{Request, RequestHandle}; use condition::Condition; -use message::Message; use reactor::EventHandler; pub struct ExitEventHandler { @@ -26,8 +23,8 @@ impl ExitEventHandler { } } -impl EventHandler>, ContextMap> for ExitEventHandler { - fn handle_event(&mut self, event: Request>, _: &mut ContextMap) { +impl EventHandler for ExitEventHandler { + fn handle_event(&mut self, event: Request, _: &mut ContextMap) { if let Request::Exit = event { self.stops += 1; self.response_handler.send_response(Response::Exit); diff --git a/src/dispatcher/handlers/message.rs b/src/dispatcher/handlers/message.rs index 795f2a9..a4b12b8 100644 --- a/src/dispatcher/handlers/message.rs +++ b/src/dispatcher/handlers/message.rs @@ -1,4 +1,4 @@ -use dispatcher::request::{InternalRequest, Request, RequestHandle}; +use dispatcher::request::{Request, RequestHandle}; use context::ContextMap; use context::context_map::StreamingIterator; use reactor; @@ -11,8 +11,8 @@ impl MessageEventHandler { } } -impl reactor::EventHandler for MessageEventHandler { - fn handle_event(&mut self, event: InternalRequest, data: &mut ContextMap) { +impl reactor::EventHandler for MessageEventHandler { + fn handle_event(&mut self, event: Request, data: &mut ContextMap) { trace!("MessageEventHandler: handle_event()"); if let Request::Message(event) = event { for i in event.ids() { diff --git a/src/dispatcher/handlers/timer.rs b/src/dispatcher/handlers/timer.rs index 181e2eb..cd17cb2 100644 --- a/src/dispatcher/handlers/timer.rs +++ b/src/dispatcher/handlers/timer.rs @@ -1,4 +1,4 @@ -use dispatcher::request::{InternalRequest, RequestHandle}; +use dispatcher::request::{Request, RequestHandle}; use context::ContextMap; use reactor::EventHandler; @@ -10,8 +10,8 @@ impl TimerEventHandler { } } -impl EventHandler for TimerEventHandler { - fn handle_event(&mut self, event: InternalRequest, data: &mut ContextMap) { +impl EventHandler for TimerEventHandler { + fn handle_event(&mut self, event: Request, data: &mut ContextMap) { for i in data.contexts_mut() { i.on_event(event.clone()); } diff --git a/src/dispatcher/reactor.rs b/src/dispatcher/reactor.rs index fab9a09..3b5094b 100644 --- a/src/dispatcher/reactor.rs +++ b/src/dispatcher/reactor.rs @@ -2,17 +2,17 @@ use std::collections::BTreeMap; use context::ContextMap; use dispatcher::demux::Demultiplexer; -use dispatcher::request::{RequestHandle, InternalRequest, ExternalRequest}; +use dispatcher::request::{RequestHandle, Request}; use reactor::{Event, EventDemultiplexer, EventHandler, Reactor}; pub struct RequestReactor { - handlers: BTreeMap>>, - demultiplexer: Demultiplexer, + handlers: BTreeMap>>, + demultiplexer: Demultiplexer, context_map: ContextMap, } impl RequestReactor { - pub fn new(demultiplexer: Demultiplexer, + pub fn new(demultiplexer: Demultiplexer, context_map: ContextMap) -> RequestReactor { RequestReactor { @@ -24,7 +24,7 @@ impl RequestReactor { } impl Reactor for RequestReactor { - type Event = InternalRequest; + type Event = Request; fn handle_events(&mut self) { while let Some(request) = self.demultiplexer.select() { trace!("RequestReactor: got event"); diff --git a/src/dispatcher/request.rs b/src/dispatcher/request.rs index 02f1a1b..b00bbe7 100644 --- a/src/dispatcher/request.rs +++ b/src/dispatcher/request.rs @@ -1,19 +1,16 @@ -use std::rc::Rc; +use std::sync::Arc; use message::Message; use reactor; use timer::TimerEvent; #[derive(Clone, Debug)] -pub enum Request { - Message(M), +pub enum Request { + Message(Arc), Timer(TimerEvent), Exit, } -pub type InternalRequest = Request>; -pub type ExternalRequest = Request; - #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] pub enum RequestHandle { Message, @@ -21,7 +18,7 @@ pub enum RequestHandle { Exit, } -impl reactor::Event for Request> { +impl reactor::Event for Request { type Handle = RequestHandle; fn handle(&self) -> Self::Handle { match *self { @@ -31,13 +28,3 @@ impl reactor::Event for Request> { } } } - -impl From> for Request> { - fn from(request: Request) -> Request> { - match request { - Request::Message(message) => Request::Message(Rc::new(message)), - Request::Timer(event) => Request::Timer(event), - Request::Exit => Request::Exit, - } - } -} diff --git a/src/state.rs b/src/state.rs index 06d0ca2..56efd18 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,4 +1,4 @@ -use std::rc::Rc; +use std::sync::Arc; use Message; use timer::TimerEvent; @@ -9,7 +9,7 @@ use context::BaseContext; pub struct State { elapsed_time: Duration, elapsed_time_since_last_message: Duration, - messages: Vec>, + messages: Vec>, opened: bool, } @@ -18,7 +18,7 @@ impl State { State::with_messages(Vec::new()) } - pub fn with_messages(messages: Vec>) -> State { + pub fn with_messages(messages: Vec>) -> State { State { elapsed_time: Duration::from_secs(0), elapsed_time_since_last_message: Duration::from_secs(0), @@ -55,11 +55,11 @@ impl State { self.elapsed_time_since_last_message } - pub fn messages(&self) -> &[Rc] { + pub fn messages(&self) -> &[Arc] { &self.messages } - fn add_message(&mut self, message: Rc) { + fn add_message(&mut self, message: Arc) { self.messages.push(message); self.elapsed_time_since_last_message = Duration::from_secs(0); } @@ -73,7 +73,7 @@ impl State { } } - pub fn on_message(&mut self, event: Rc, context: &BaseContext) { + pub fn on_message(&mut self, event: Arc, context: &BaseContext) { if self.is_open() { self.add_message(event); } else if context.conditions().is_opening(&event) { diff --git a/src/test_utils/correlator.rs b/src/test_utils/correlator.rs index bdc4683..4a2b2f5 100644 --- a/src/test_utils/correlator.rs +++ b/src/test_utils/correlator.rs @@ -1,6 +1,5 @@ use dispatcher::ResponseHandle; use Response; -use Message; use std::rc::Rc; use std::cell::RefCell; @@ -14,8 +13,8 @@ pub struct MessageEventHandler { pub responses: Rc>>, } -impl EventHandler>> for MessageEventHandler { - fn handle_event(&mut self, event: Response, _: &mut mpsc::Sender>) { +impl EventHandler> for MessageEventHandler { + fn handle_event(&mut self, event: Response, _: &mut mpsc::Sender) { if let Response::Alert(event) = event { self.responses.borrow_mut().push(event); } diff --git a/src/timer.rs b/src/timer.rs index d4ce60d..1c7cf5c 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -2,7 +2,7 @@ use std::sync::mpsc; use std::thread; use std::time::Duration; -use dispatcher::request::{ExternalRequest, Request}; +use dispatcher::request::Request; #[derive(Clone, Copy, Debug)] pub struct TimerEvent(pub Duration); @@ -17,7 +17,7 @@ impl TimerEvent { pub struct Timer; impl Timer { - pub fn from_chan(duration: Duration, tx: mpsc::Sender) { + pub fn from_chan(duration: Duration, tx: mpsc::Sender) { thread::spawn(move || { while let Ok(_) = tx.send(Request::Timer(TimerEvent(duration))) { thread::sleep(duration); From b0dba153f0e7b38c734e2bbce103fb414ab2a8da Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sat, 30 Jan 2016 17:49:05 +0100 Subject: [PATCH 17/58] ResponseSender: remove genericity It's used only with T=Response. Signed-off-by: Tibor Benke --- src/action/message/mod.rs | 4 ++-- src/action/message/test.rs | 4 ++-- src/action/mod.rs | 3 +-- src/correlator/mod.rs | 6 +++--- src/dispatcher/handlers/exit.rs | 4 ++-- src/dispatcher/mod.rs | 4 ++-- src/dispatcher/response.rs | 8 +++++--- 7 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/action/message/mod.rs b/src/action/message/mod.rs index ce02a78..9be79c3 100644 --- a/src/action/message/mod.rs +++ b/src/action/message/mod.rs @@ -26,7 +26,7 @@ pub const MESSAGES: &'static str = "messages"; const MESSAGE: &'static str = "MESSAGE"; pub struct MessageAction { - sender: Box>, + sender: Box, uuid: String, name: Option, values: Handlebars, @@ -35,7 +35,7 @@ pub struct MessageAction { } impl MessageAction { - pub fn new(sender: Box>, + pub fn new(sender: Box, action: config::action::MessageAction) -> MessageAction { let config::action::MessageAction { uuid, name, message, values, when, inject_mode } = diff --git a/src/action/message/test.rs b/src/action/message/test.rs index 6be5619..e8fed0f 100644 --- a/src/action/message/test.rs +++ b/src/action/message/test.rs @@ -21,12 +21,12 @@ struct DummyResponseSender { responses: Rc>>, } -impl ResponseSender for DummyResponseSender { +impl ResponseSender for DummyResponseSender { fn send_response(&self, response: Response) { self.responses.borrow_mut().push(response); } - fn boxed_clone(&self) -> Box> { + fn boxed_clone(&self) -> Box { Box::new(self.clone()) } } diff --git a/src/action/mod.rs b/src/action/mod.rs index 1ab83da..1f42ed6 100644 --- a/src/action/mod.rs +++ b/src/action/mod.rs @@ -1,6 +1,5 @@ use config::action::ActionType; use state::State; -use dispatcher::Response; use dispatcher::response::ResponseSender; use context::base::BaseContext; @@ -13,7 +12,7 @@ pub trait Action { fn on_closed(&self, state: &State, context: &BaseContext); } -pub fn from_config(config: ActionType, _sender: Box>) -> Box { +pub fn from_config(config: ActionType, _sender: Box) -> Box { match config { ActionType::Message(action) => Box::new(self::message::MessageAction::new(_sender, action)), } diff --git a/src/correlator/mod.rs b/src/correlator/mod.rs index 8a66804..7456d59 100644 --- a/src/correlator/mod.rs +++ b/src/correlator/mod.rs @@ -41,7 +41,7 @@ pub struct Correlator { } fn create_context(config_context: config::Context, - response_sender: Box>) + response_sender: Box) -> Context { let config::Context{name, uuid, conditions, context_id, actions} = config_context; let mut boxed_actions = Vec::new(); @@ -62,7 +62,7 @@ fn create_context(config_context: config::Context, } fn create_context_map(contexts: Vec, - response_sender: Box>) + response_sender: Box) -> ContextMap { let mut context_map = ContextMap::new(); for i in contexts.into_iter() { @@ -92,7 +92,7 @@ impl Correlator { let handle = thread::spawn(move || { let exit_condition = Condition::new(false); let dmux = Demultiplexer::new(rx, exit_condition.clone()); - let response_sender = Box::new(ResponseSender::new(dispatcher_output_channel_tx)) as Box>; + let response_sender = Box::new(ResponseSender::new(dispatcher_output_channel_tx)) as Box; let exit_handler = Box::new(handlers::exit::ExitEventHandler::new(exit_condition, diff --git a/src/dispatcher/handlers/exit.rs b/src/dispatcher/handlers/exit.rs index 8f12a87..631e834 100644 --- a/src/dispatcher/handlers/exit.rs +++ b/src/dispatcher/handlers/exit.rs @@ -7,13 +7,13 @@ use reactor::EventHandler; pub struct ExitEventHandler { condition: Condition, - response_handler: Box>, + response_handler: Box, stops: u32, } impl ExitEventHandler { pub fn new(condition: Condition, - response_handler: Box>) + response_handler: Box) -> ExitEventHandler { ExitEventHandler { condition: condition, diff --git a/src/dispatcher/mod.rs b/src/dispatcher/mod.rs index a7e5406..784fc39 100644 --- a/src/dispatcher/mod.rs +++ b/src/dispatcher/mod.rs @@ -44,13 +44,13 @@ impl ResponseSender { } } -impl self::response::ResponseSender for ResponseSender { +impl self::response::ResponseSender for ResponseSender { fn send_response(&self, response: Response) { let sender = self.sender.borrow_mut(); let _ = sender.send(response); } - fn boxed_clone(&self) -> Box> { + fn boxed_clone(&self) -> Box { Box::new(self.clone()) } } diff --git a/src/dispatcher/response.rs b/src/dispatcher/response.rs index c8d1f81..bf54d68 100644 --- a/src/dispatcher/response.rs +++ b/src/dispatcher/response.rs @@ -1,4 +1,6 @@ -pub trait ResponseSender { - fn send_response(&self, response: T); - fn boxed_clone(&self) -> Box>; +use super::Response; + +pub trait ResponseSender { + fn send_response(&self, response: Response); + fn boxed_clone(&self) -> Box; } From 9cdf6cae59faf6bab2a94d6e52fcda128f656a94 Mon Sep 17 00:00:00 2001 From: Tibor Benke Date: Sun, 31 Jan 2016 11:17:10 +0100 Subject: [PATCH 18/58] config/action/MessageAction: change type of values to Handlebars So there is almost no difference between this and action::MessageAction. The plan is to change action::MessageAction to contain a response sender and a MessageAction value (now, the fields are duplicated). I had to remove several derive attributes, because Handlebars doesn't implement Clone, PartialEq or Debug. That's why I modified some builders as well. Signed-off-by: Tibor Benke --- src/action/message/mod.rs | 10 +++---- src/bin/test.rs | 10 +++---- src/config/action/deser/test.rs | 2 -- src/config/action/message/builder.rs | 32 +++++++++++----------- src/config/action/message/deser.rs | 40 +++++++++++++++------------- src/config/action/message/mod.rs | 7 +++-- src/config/action/mod.rs | 1 - src/config/deser.rs | 8 +----- src/config/mod.rs | 19 +++++++------ src/correlator/test.rs | 9 +++---- 10 files changed, 62 insertions(+), 76 deletions(-) diff --git a/src/action/message/mod.rs b/src/action/message/mod.rs index 9be79c3..d5c2882 100644 --- a/src/action/message/mod.rs +++ b/src/action/message/mod.rs @@ -38,19 +38,15 @@ impl MessageAction { pub fn new(sender: Box, action: config::action::MessageAction) -> MessageAction { - let config::action::MessageAction { uuid, name, message, values, when, inject_mode } = + let config::action::MessageAction { uuid, name, message, mut values, when, inject_mode } = action; - let mut handlebars = Handlebars::new(); - for (name, template) in values.into_iter() { - handlebars.register_template(&name, template); - } - handlebars.register_template(MESSAGE, message); + values.register_template(MESSAGE, message); MessageAction { sender: sender, uuid: uuid, name: name, - values: handlebars, + values: values, when: when, inject_mode: inject_mode, } diff --git a/src/bin/test.rs b/src/bin/test.rs index 01b0349..abf53e4 100644 --- a/src/bin/test.rs +++ b/src/bin/test.rs @@ -3,7 +3,8 @@ extern crate maplit; extern crate correlation; extern crate uuid; -use correlation::{config, Correlator}; +use correlation::Correlator; +use correlation::config::ContextBuilder; use correlation::conditions::ConditionsBuilder; use correlation::message::MessageBuilder; use uuid::Uuid; @@ -25,11 +26,10 @@ fn main() { .first_opens(true) .last_closes(true) .build(); - let actions = vec![]; let contexts = vec![ - config::ContextBuilder::new(Uuid::new_v4(), condition.clone()).actions(actions.clone()).build(), - config::ContextBuilder::new(Uuid::new_v4(), condition.clone()).actions(actions.clone()).build(), - config::ContextBuilder::new(Uuid::new_v4(), condition.clone()).actions(actions.clone()).build(), + ContextBuilder::new(Uuid::new_v4(), condition.clone()).actions(Vec::new()).build(), + ContextBuilder::new(Uuid::new_v4(), condition.clone()).actions(Vec::new()).build(), + ContextBuilder::new(Uuid::new_v4(), condition.clone()).actions(Vec::new()).build(), ]; let mut correlator = Correlator::new(contexts); let _ = correlator.push_message(MessageBuilder::new(&uuid1, "message").build()); diff --git a/src/config/action/deser/test.rs b/src/config/action/deser/test.rs index 5d3c831..a066812 100644 --- a/src/config/action/deser/test.rs +++ b/src/config/action/deser/test.rs @@ -13,7 +13,6 @@ fn test_given_action_when_it_is_deserialized_then_we_get_the_right_result() { "#; let result = from_str::(text); - println!("{:?}", &result); let action = result.ok().expect("Failed to deserialize a valid ActionType"); match action { ActionType::Message(message) => { @@ -26,7 +25,6 @@ fn test_given_action_when_it_is_deserialized_then_we_get_the_right_result() { fn test_given_unknown_action_when_it_is_deserialized_then_we_get_an_error() { let text = r#"{ "unknown": {} }"#; let result = from_str::(text); - println!("{:?}", &result); let _ = result.err().expect("Successfully deserialized an unknown action"); } diff --git a/src/config/action/message/builder.rs b/src/config/action/message/builder.rs index 22594cc..5b2c333 100644 --- a/src/config/action/message/builder.rs +++ b/src/config/action/message/builder.rs @@ -1,5 +1,5 @@ use handlebars::Template; -use std::collections::BTreeMap; +use handlebars::Handlebars; use super::MessageAction; use super::InjectMode; use config::action::ExecCondition; @@ -8,7 +8,7 @@ pub struct MessageActionBuilder { uuid: String, name: Option, message: Template, - values: BTreeMap, + values: Handlebars, when: ExecCondition, inject_mode: InjectMode, } @@ -19,45 +19,45 @@ impl MessageActionBuilder { uuid: uuid.into(), name: None, message: message, - values: BTreeMap::new(), + values: Handlebars::new(), when: ExecCondition::new(), inject_mode: Default::default(), } } - pub fn name>(&mut self, name: Option) -> &mut MessageActionBuilder { + pub fn name>(mut self, name: Option) -> MessageActionBuilder { self.name = name.map(|name| name.into()); self } - pub fn when(&mut self, when: ExecCondition) -> &mut MessageActionBuilder { + pub fn when(mut self, when: ExecCondition) -> MessageActionBuilder { self.when = when; self } - pub fn values(&mut self, values: BTreeMap) -> &mut MessageActionBuilder { + pub fn values(mut self, values: Handlebars) -> MessageActionBuilder { self.values = values; self } - pub fn pair>(&mut self, key: S, value: Template) -> &mut MessageActionBuilder { - self.values.insert(key.into(), value); + pub fn pair>(mut self, key: S, value: Template) -> MessageActionBuilder { + self.values.register_template(key.as_ref(), value); self } - pub fn inject_mode(&mut self, mode: InjectMode) -> &mut MessageActionBuilder { + pub fn inject_mode(mut self, mode: InjectMode) -> MessageActionBuilder { self.inject_mode = mode; self } - pub fn build(&self) -> MessageAction { + pub fn build(self) -> MessageAction { MessageAction { - uuid: self.uuid.clone(), - name: self.name.clone(), - message: self.message.clone(), - values: self.values.clone(), - when: self.when.clone(), - inject_mode: self.inject_mode.clone(), + uuid: self.uuid, + name: self.name, + message: self.message, + values: self.values, + when: self.when, + inject_mode: self.inject_mode, } } } diff --git a/src/config/action/message/deser.rs b/src/config/action/message/deser.rs index 820039a..510bbe2 100644 --- a/src/config/action/message/deser.rs +++ b/src/config/action/message/deser.rs @@ -1,9 +1,9 @@ use super::MessageAction; -use super::MessageActionBuilder; use super::InjectMode; use config::action::ExecCondition; use handlebars::Template; +use handlebars::Handlebars; use serde::de::{Deserialize, Deserializer, Error, MapVisitor, Visitor}; use std::collections::BTreeMap; @@ -109,24 +109,26 @@ impl Visitor for MessageActionVisitor { let values = match values { Some(values) => { - let mut converted_values = BTreeMap::new(); + let mut registry = Handlebars::new(); for (key, value) in values.into_iter() { let template = try!(MessageActionVisitor::compile_template::(value, &uuid)); - converted_values.insert(key, template); + registry.register_template(&key, template); } - converted_values + registry } - None => BTreeMap::new(), + None => Handlebars::new(), }; try!(visitor.end()); - Ok(MessageActionBuilder::new(uuid, message) - .name(name) - .values(values) - .when(when) - .inject_mode(inject_mode) - .build()) + Ok(MessageAction { + uuid: uuid, + message: message, + name: name, + values: values, + when: when, + inject_mode: inject_mode + }) } } @@ -162,6 +164,12 @@ mod test { use handlebars::Template; use serde_json::from_str; + fn assert_message_action_eq(expected: &MessageAction, actual: &MessageAction) { + assert_eq!(expected.uuid(), actual.uuid()); + assert_eq!(expected.name(), actual.name()); + assert_eq!(expected.message(), actual.message()); + } + #[test] fn test_given_message_as_a_json_string_when_it_is_deserialized_then_we_get_the_expected_message () { @@ -192,9 +200,8 @@ mod test { .pair("key2", value2) .build(); let result = from_str::(text); - println!("{:?}", &result); let message = result.ok().expect("Failed to deserialize a valid MessageAction object"); - assert_eq!(expected_message, message); + assert_message_action_eq(&expected_message, &message); } #[test] @@ -212,9 +219,8 @@ mod test { .expect("Failed to compile a handlebars template"); let expected_message = MessageActionBuilder::new("UUID", message).build(); let result = from_str::(text); - println!("{:?}", &result); let message = result.ok().expect("Failed to deserialize a valid MessageAction object"); - assert_eq!(expected_message, message); + assert_message_action_eq(&expected_message, &message); } #[test] @@ -226,7 +232,6 @@ mod test { "#; let result = from_str::(text); - println!("{:?}", &result); let _ = result.err().expect("Successfully deserialized an invalid MessageAction object"); } @@ -274,8 +279,7 @@ mod test { .inject_mode(InjectMode::Forward) .build(); let result = from_str::(text); - println!("{:?}", &result); let message = result.ok().expect("Failed to deserialize a valid MessageAction object"); - assert_eq!(expected_message, message); + assert_message_action_eq(&expected_message, &message); } } diff --git a/src/config/action/message/mod.rs b/src/config/action/message/mod.rs index 420c4ee..6e15c55 100644 --- a/src/config/action/message/mod.rs +++ b/src/config/action/message/mod.rs @@ -1,5 +1,5 @@ use handlebars::Template; -use std::collections::BTreeMap; +use handlebars::Handlebars; use super::ActionType; use super::ExecCondition; @@ -9,12 +9,11 @@ mod builder; pub use self::builder::MessageActionBuilder; -#[derive(Clone, Debug, PartialEq)] pub struct MessageAction { pub uuid: String, pub name: Option, pub message: Template, - pub values: BTreeMap, + pub values: Handlebars, pub when: ExecCondition, pub inject_mode: InjectMode, } @@ -29,7 +28,7 @@ impl MessageAction { pub fn message(&self) -> &Template { &self.message } - pub fn values(&self) -> &BTreeMap { + pub fn values(&self) -> &Handlebars { &self.values } pub fn inject_mode(&self) -> &InjectMode { diff --git a/src/config/action/mod.rs b/src/config/action/mod.rs index e51a52b..749c5e5 100644 --- a/src/config/action/mod.rs +++ b/src/config/action/mod.rs @@ -3,7 +3,6 @@ pub use self::message::MessageAction; pub mod message; mod deser; -#[derive(Clone, Debug, PartialEq)] pub enum ActionType { Message(self::message::MessageAction), } diff --git a/src/config/deser.rs b/src/config/deser.rs index 9e1bf0a..b7a0c43 100644 --- a/src/config/deser.rs +++ b/src/config/deser.rs @@ -181,7 +181,6 @@ mod test { "#; let result = from_str::(text); - println!("{:?}", &result); let expected_name = "TEST_NAME".to_string(); let expected_uuid = Uuid::parse_str("86ca9f93-84fb-4813-b037-6526f7a585a3").ok().unwrap(); let expected_conditions = ConditionsBuilder::new(Duration::from_millis(100)) @@ -206,14 +205,13 @@ mod test { assert_eq!(&Some(expected_name), &context.name); assert_eq!(&expected_uuid, &context.uuid); assert_eq!(&expected_conditions, &context.conditions); - assert_eq!(&expected_actions, &context.actions); + assert_eq!(&expected_actions.len(), &context.actions.len()); } #[test] fn test_given_config_context_when_it_does_not_have_uuid_then_it_cannot_be_deserialized() { let text = r#"{ "conditions": { "timeout": 100 }}"#; let result = from_str::(text); - println!("{:?}", &result); let _ = result.err().expect("Successfully deserialized a config context without an uuid key"); } @@ -224,7 +222,6 @@ mod test { "conditions": { "timeout": 100}, "unknown": "unknown" }"#; let result = from_str::(text); - println!("{:?}", &result); let _ = result.err().expect("Successfully deserialized a config context with an unknown key"); } @@ -241,7 +238,6 @@ mod test { "#; let result = from_str::(text); - println!("{:?}", &result); let expected_uuid = Uuid::parse_str("86ca9f93-84fb-4813-b037-6526f7a585a3").ok().unwrap(); let expected_conditions = ConditionsBuilder::new(Duration::from_millis(100)).build(); let context = result.ok().expect("Failed to deserialize a valid Context"); @@ -262,7 +258,6 @@ mod test { "#; let result = from_str::(text); - println!("{:?}", &result); let _ = result.err() .expect("Successfully deserialized an invalid Context (UUID is invalid)"); } @@ -280,7 +275,6 @@ mod test { "#; let expected_context_id = "{{HOST}}{{PROGRAM}}".to_string(); let result = from_str::(text); - println!("{:?}", &result); let context = result.ok().expect("Failed to deserialize a valid Context"); assert_eq!(&expected_context_id, &context.context_id.as_ref().unwrap().to_string()); diff --git a/src/config/mod.rs b/src/config/mod.rs index d3b1696..b74678a 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -7,7 +7,6 @@ use conditions::Conditions; mod deser; pub mod action; -#[derive(Debug, Clone)] pub struct Context { pub name: Option, pub uuid: Uuid, @@ -35,28 +34,28 @@ impl ContextBuilder { } } - pub fn context_id(&mut self, context_id: Option