diff options
| author | Lu Qiuwen <[email protected]> | 2023-08-17 10:05:30 +0800 |
|---|---|---|
| committer | Lu Qiuwen <[email protected]> | 2023-08-17 13:21:48 +0800 |
| commit | 8ef87c2ba7f35d3050c381380c4d4f2c8551d8a5 (patch) | |
| tree | 1960840e14fa139fad0b1c110fdfa3f6e3b838b8 /src | |
| parent | 85973cd021fda0d62c5f3e786e2abb8c8296a1df (diff) | |
add eventmgr trait and their tests.dev-exp-eventmgr
Diffstat (limited to 'src')
| -rw-r--r-- | src/event/eventmgr.rs | 133 | ||||
| -rw-r--r-- | src/event/mod.rs | 2 | ||||
| -rw-r--r-- | src/lib.rs | 1 |
3 files changed, 136 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 diff --git a/src/event/mod.rs b/src/event/mod.rs new file mode 100644 index 0000000..e342413 --- /dev/null +++ b/src/event/mod.rs @@ -0,0 +1,2 @@ + +pub mod eventmgr;
\ No newline at end of file @@ -1,2 +1,3 @@ pub mod packet; pub mod protocol; +pub mod event; |
