diff options
Diffstat (limited to 'src/event/eventmgr.rs')
| -rw-r--r-- | src/event/eventmgr.rs | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/src/event/eventmgr.rs b/src/event/eventmgr.rs new file mode 100644 index 0000000..07f7547 --- /dev/null +++ b/src/event/eventmgr.rs @@ -0,0 +1,133 @@ +use std::any::{Any, TypeId}; +use std::collections::HashMap; +use std::rc::{Rc}; + +trait Event{} + +pub struct EventOnPktIn{} +impl Event for EventOnPktIn {} + +pub struct EventOnPktOut{} +impl Event for EventOnPktOut {} + +pub struct EventOnConnStart{} +impl Event for EventOnConnStart {} + +pub struct EventOnConnSetup{} +impl Event for EventOnConnSetup {} + +pub struct EventOnConnNewData{} +impl Event for EventOnConnNewData {} + +pub struct EventOnConnError{} +impl Event for EventOnConnError {} + +pub struct EventOnConnTimeout{} +impl Event for EventOnConnTimeout {} + +pub struct EventOnOrphan{} +impl Event for EventOnOrphan {} + +pub struct EventOnReTrans{} +impl Event for EventOnReTrans {} + +pub struct EventOnConnEnd{} +impl Event for EventOnConnEnd {} + +trait EventManager { + fn new() -> Self; + fn define_event<C: Event + 'static, F: FnMut(&dyn Event)->bool + 'static>(&mut self, callback: F); + fn raise_event<C: Event + 'static>(&mut self, ev: &C); +} + +trait EventCallback : FnMut(&dyn Event) -> bool {} + +struct PluginExample { +} + +impl PluginExample { + fn cb_when_pkt_in(&self, ev: &dyn Event) -> bool { + println!("cb_when_pkt_in"); + return true; + } +} + +struct EventManagerImpl { + callbacks: HashMap<TypeId, Vec<Box<dyn FnMut(&dyn Event)->bool>>>, +} + +impl EventManager for EventManagerImpl { + fn new() -> Self { + EventManagerImpl { + callbacks: HashMap::new(), + } + } + + fn define_event<C: Event + 'static, F: FnMut(&dyn Event)->bool + 'static>(&mut self, callback: F) { + let ev_type_id = TypeId::of::<C>(); + let callbacks = self.callbacks.get_mut(&ev_type_id); + if callbacks.is_none() { + self.callbacks.insert(ev_type_id, Vec::new()); + } + + let callbacks = self.callbacks.get_mut(&ev_type_id).unwrap(); + + let callback_box = Box::new(callback); + callbacks.push(callback_box); + } + + fn raise_event<C: Event + 'static>(&mut self, ev: &C) { + let ev_type_id = TypeId::of::<C>(); + + let callbacks = self.callbacks.get(&ev_type_id); + if callbacks.is_none() { + return; + } + + /* + for callback in callbacks.unwrap() { + } + */ + } +} + +/* unit test */ +#[cfg(test)] +mod tests { + use super::*; + struct TestUserStreamCtx {} + impl TestUserStreamCtx { + fn on_conn_start(&self, ev: &dyn Event) -> bool { + println!("on_conn_start"); + return true; + } + + fn on_conn_close(&self, ev: &dyn Event) -> bool { + println!("on_conn_close"); + return true; + } + } + + struct TestUserDefineEvent {} + impl Event for TestUserDefineEvent {} + + #[test] + fn test_event() { + let mut stream_ctx = Rc::new(TestUserStreamCtx{}); + let mut mgr = EventManagerImpl::new(); + + let stream_ctx_mut_ref_1 = stream_ctx.clone(); + let stream_ctx_mut_ref_2 = stream_ctx.clone(); + + let event_cb_fn = move |ev: &dyn Event| -> bool { + return stream_ctx_mut_ref_1.on_conn_start(ev); + }; + + let event_cb_fn_2 = move |ev: &dyn Event| -> bool { + return stream_ctx_mut_ref_2.on_conn_close(ev); + }; + + mgr.define_event::<TestUserDefineEvent, _>(event_cb_fn); + mgr.define_event::<TestUserDefineEvent, _>(event_cb_fn_2); + } +}
\ No newline at end of file |
