diff --git a/fsrc-core/src/event_man.rs b/fsrc-core/src/event_man.rs index 93df545..3e191eb 100644 --- a/fsrc-core/src/event_man.rs +++ b/fsrc-core/src/event_man.rs @@ -46,13 +46,14 @@ use crate::params::{Params, ParamsHeapless}; use alloc::boxed::Box; use alloc::vec; use alloc::vec::Vec; +use core::slice::Iter; use hashbrown::HashMap; #[cfg(feature = "std")] pub use stdmod::*; #[derive(PartialEq, Eq, Hash, Copy, Clone)] -enum ListenerType { +pub enum ListenerKey { Single(LargestEventRaw), Group(LargestGroupIdRaw), All, @@ -66,10 +67,12 @@ pub type EventWithAuxData = (Event, Option); pub type EventU32WithAuxData = EventWithAuxData; pub type EventU16WithAuxData = EventWithAuxData; +type SenderId = u32; + pub trait SendEventProvider { type Error; - fn id(&self) -> u32; + fn id(&self) -> SenderId; fn send_no_data(&mut self, event: Provider) -> Result<(), Self::Error> { self.send(event, None) } @@ -80,8 +83,8 @@ pub trait SendEventProvider { ) -> Result<(), Self::Error>; } -struct Listener { - ltype: ListenerType, +pub struct Listener { + ltype: ListenerKey, send_provider: Box>, } @@ -98,10 +101,19 @@ pub trait EventReceiver { pub trait ListenerTable { - fn get_listeners( + fn get_listener_ids(&mut self, key: ListenerKey) -> Option>; + fn add_listener(&mut self, key: ListenerKey, sender_id: SenderId) -> bool; + + fn get_send_event_provider( &mut self, - key: ListenerType, - ) -> &[Listener]; + id: SenderId, + ) -> Option<&mut Box>>; + fn add_send_event_provider( + &mut self, + send_provider: Box< + dyn SendEventProvider, + >, + ) -> bool; } /// Generic event manager implementation. @@ -114,11 +126,66 @@ pub trait ListenerTable { - listeners: HashMap>>, + listeners: HashMap>>, //listener_table: Box>, event_receiver: Box>, } +#[cfg(feature = "std")] +#[derive(Default)] +pub struct DefaultSenderTableProvider< + SendProviderError, + Event: GenericEvent = EventU32, + AuxDataProvider = Params, +> { + listeners: HashMap>, + senders: HashMap< + SenderId, + Box>, + >, +} + +#[cfg(feature = "std")] +impl + ListenerTable + for DefaultSenderTableProvider +{ + fn get_listener_ids(&mut self, key: ListenerKey) -> Option> { + self.listeners.get(&key).map(|vec| vec.into_iter()) + } + + fn add_listener(&mut self, key: ListenerKey, sender_id: SenderId) -> bool { + if let Some(existing_list) = self.listeners.get_mut(&key) { + existing_list.push(sender_id); + } else { + let new_list = vec![sender_id]; + self.listeners.insert(key, new_list); + } + true + } + + fn get_send_event_provider( + &mut self, + id: SenderId, + ) -> Option<&mut Box>> + { + self.senders.get_mut(&id).filter(|sender| sender.id() == id) + } + + fn add_send_event_provider( + &mut self, + send_provider: Box< + dyn SendEventProvider, + >, + ) -> bool { + let id = send_provider.id(); + if self.senders.contains_key(&id) { + return false; + } + self.senders.insert(id, send_provider).is_none() + } +} + /// Safety: It is safe to implement [Send] because all fields in the [EventManager] are [Send] /// as well #[cfg(feature = "std")] @@ -144,7 +211,7 @@ impl EventManager { event: Event, dest: impl SendEventProvider + 'static, ) { - self.update_listeners(ListenerType::Single(event.raw_as_largest_type()), dest); + self.update_listeners(ListenerKey::Single(event.raw_as_largest_type()), dest); } pub fn subscribe_group( @@ -152,7 +219,7 @@ impl EventManager { group_id: LargestGroupIdRaw, dest: impl SendEventProvider + 'static, ) { - self.update_listeners(ListenerType::Group(group_id), dest); + self.update_listeners(ListenerKey::Group(group_id), dest); } /// Subscribe for all events received by the manager. @@ -163,7 +230,7 @@ impl EventManager { &mut self, send_provider: impl SendEventProvider + 'static, ) { - self.update_listeners(ListenerType::All, send_provider); + self.update_listeners(ListenerKey::All, send_provider); } /// Helper function which removes single subscriptions for which a group subscription already @@ -173,9 +240,9 @@ impl EventManager { group_id: LargestGroupIdRaw, dest: impl SendEventProvider + 'static, ) { - if self.listeners.contains_key(&ListenerType::Group(group_id)) { + if self.listeners.contains_key(&ListenerKey::Group(group_id)) { for (ltype, listeners) in &mut self.listeners { - if let ListenerType::Single(_) = ltype { + if let ListenerKey::Single(_) = ltype { listeners.retain(|f| f.send_provider.id() != dest.id()); } } @@ -188,7 +255,7 @@ impl { fn update_listeners( &mut self, - key: ListenerType, + key: ListenerKey, dest: impl SendEventProvider + 'static, ) { if !self.listeners.contains_key(&key) { @@ -230,7 +297,7 @@ impl } }; if let Some((event, aux_data)) = self.event_receiver.receive() { - let single_key = ListenerType::Single(event.raw_as_largest_type()); + let single_key = ListenerKey::Single(event.raw_as_largest_type()); if self.listeners.contains_key(&single_key) { send_handler( event, @@ -238,7 +305,7 @@ impl self.listeners.get_mut(&single_key).unwrap(), ); } - let group_key = ListenerType::Group(event.group_id_as_largest_type()); + let group_key = ListenerKey::Group(event.group_id_as_largest_type()); if self.listeners.contains_key(&group_key) { send_handler( event, @@ -246,7 +313,7 @@ impl self.listeners.get_mut(&group_key).unwrap(), ); } - if let Some(all_receivers) = self.listeners.get_mut(&ListenerType::All) { + if let Some(all_receivers) = self.listeners.get_mut(&ListenerKey::All) { send_handler(event, aux_data.clone(), all_receivers); } if let Some(err) = err_status {