PUS Event Manager #19

Merged
muellerr merged 41 commits from pus-event-manager into main 2022-11-19 12:26:17 +01:00
5 changed files with 278 additions and 205 deletions
Showing only changes of commit ce8babc8a1 - Show all commits

36
Cargo.lock generated
View File

@ -110,9 +110,9 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.74" version = "1.0.76"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "581f5dba903aac52ea3feb5ec4810848460ee833876f1f9b0fdeab1f19091574" checksum = "76a284da2e6fe2092f2353e51713435363112dfd60030e22add80be333fb928f"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
@ -122,9 +122,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "chrono" name = "chrono"
version = "0.4.22" version = "0.4.23"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfd4d1b31faaa3a89d7934dbded3111da0d2ef28e3ebccdb4f0179f5929d1ef1" checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f"
dependencies = [ dependencies = [
"iana-time-zone", "iana-time-zone",
"num-integer", "num-integer",
@ -214,9 +214,9 @@ dependencies = [
[[package]] [[package]]
name = "cxx" name = "cxx"
version = "1.0.80" version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b7d4e43b25d3c994662706a1d4fcfc32aaa6afd287502c111b237093bb23f3a" checksum = "d4a41a86530d0fe7f5d9ea779916b7cadd2d4f9add748b99c2c029cbbdfaf453"
dependencies = [ dependencies = [
"cc", "cc",
"cxxbridge-flags", "cxxbridge-flags",
@ -226,9 +226,9 @@ dependencies = [
[[package]] [[package]]
name = "cxx-build" name = "cxx-build"
version = "1.0.80" version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84f8829ddc213e2c1368e51a2564c552b65a8cb6a28f31e576270ac81d5e5827" checksum = "06416d667ff3e3ad2df1cd8cd8afae5da26cf9cec4d0825040f88b5ca659a2f0"
dependencies = [ dependencies = [
"cc", "cc",
"codespan-reporting", "codespan-reporting",
@ -241,15 +241,15 @@ dependencies = [
[[package]] [[package]]
name = "cxxbridge-flags" name = "cxxbridge-flags"
version = "1.0.80" version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e72537424b474af1460806647c41d4b6d35d09ef7fe031c5c2fa5766047cc56a" checksum = "820a9a2af1669deeef27cb271f476ffd196a2c4b6731336011e0ba63e2c7cf71"
[[package]] [[package]]
name = "cxxbridge-macro" name = "cxxbridge-macro"
version = "1.0.80" version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "309e4fb93eed90e1e14bea0da16b209f81813ba9fc7830c20ed151dd7bc0a4d7" checksum = "a08a6e2fcc370a089ad3b4aaf54db3b1b4cee38ddabce5896b33eb693275f470"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -504,9 +504,9 @@ dependencies = [
[[package]] [[package]]
name = "num_cpus" name = "num_cpus"
version = "1.13.1" version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" checksum = "f6058e64324c71e02bc2b150e4f3bc8286db6c83092132ffa3f6b1eab0f9def5"
dependencies = [ dependencies = [
"hermit-abi", "hermit-abi",
"libc", "libc",
@ -577,9 +577,9 @@ dependencies = [
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.6.0" version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick",
"memchr", "memchr",
@ -588,9 +588,9 @@ dependencies = [
[[package]] [[package]]
name = "regex-syntax" name = "regex-syntax"
version = "0.6.27" version = "0.6.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
[[package]] [[package]]
name = "riscv" name = "riscv"

View File

@ -52,7 +52,7 @@ use hashbrown::HashMap;
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub use stdmod::*; pub use stdmod::*;
#[derive(PartialEq, Eq, Hash, Copy, Clone)] #[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
pub enum ListenerKey { pub enum ListenerKey {
Single(LargestEventRaw), Single(LargestEventRaw),
Group(LargestGroupIdRaw), Group(LargestGroupIdRaw),
@ -67,7 +67,7 @@ pub type EventWithAuxData<Event> = (Event, Option<Params>);
pub type EventU32WithAuxData = EventWithAuxData<EventU32>; pub type EventU32WithAuxData = EventWithAuxData<EventU32>;
pub type EventU16WithAuxData = EventWithAuxData<EventU16>; pub type EventU16WithAuxData = EventWithAuxData<EventU16>;
type SenderId = u32; pub type SenderId = u32;
pub trait SendEventProvider<Provider: GenericEvent, AuxDataProvider = Params> { pub trait SendEventProvider<Provider: GenericEvent, AuxDataProvider = Params> {
type Error; type Error;
@ -83,11 +83,6 @@ pub trait SendEventProvider<Provider: GenericEvent, AuxDataProvider = Params> {
) -> Result<(), Self::Error>; ) -> Result<(), Self::Error>;
} }
pub struct Listener<E, Event: GenericEvent, AuxDataProvider = Params> {
ltype: ListenerKey,
send_provider: Box<dyn SendEventProvider<Event, AuxDataProvider, Error = E>>,
}
/// Generic abstraction for an event receiver. /// Generic abstraction for an event receiver.
pub trait EventReceiver<Event: GenericEvent, AuxDataProvider = Params> { pub trait EventReceiver<Event: GenericEvent, AuxDataProvider = Params> {
/// This function has to be provided by any event receiver. A receive call may or may not return /// This function has to be provided by any event receiver. A receive call may or may not return
@ -99,14 +94,19 @@ pub trait EventReceiver<Event: GenericEvent, AuxDataProvider = Params> {
fn receive(&mut self) -> Option<(Event, Option<AuxDataProvider>)>; fn receive(&mut self) -> Option<(Event, Option<AuxDataProvider>)>;
} }
pub trait ListenerTable<SendProviderError, Event: GenericEvent = EventU32, AuxDataProvider = Params> pub trait ListenerTable {
{ fn get_listeners(&self) -> Vec<ListenerKey>;
fn get_listener_ids(&mut self, key: ListenerKey) -> Option<Iter<SenderId>>; fn contains_listener(&self, key: &ListenerKey) -> bool;
fn get_listener_ids(&self, key: &ListenerKey) -> Option<Iter<SenderId>>;
fn add_listener(&mut self, key: ListenerKey, sender_id: SenderId) -> bool; fn add_listener(&mut self, key: ListenerKey, sender_id: SenderId) -> bool;
fn remove_duplicates(&mut self, key: &ListenerKey);
}
pub trait SenderTable<SendProviderError, Event: GenericEvent = EventU32, AuxDataProvider = Params> {
fn contains_send_event_provider(&self, id: &SenderId) -> bool;
fn get_send_event_provider( fn get_send_event_provider(
&mut self, &mut self,
id: SenderId, id: &SenderId,
) -> Option<&mut Box<dyn SendEventProvider<Event, AuxDataProvider, Error = SendProviderError>>>; ) -> Option<&mut Box<dyn SendEventProvider<Event, AuxDataProvider, Error = SendProviderError>>>;
fn add_send_event_provider( fn add_send_event_provider(
&mut self, &mut self,
@ -122,36 +122,218 @@ pub trait ListenerTable<SendProviderError, Event: GenericEvent = EventU32, AuxDa
/// ///
/// * `SendProviderError`: [SendEventProvider] error type /// * `SendProviderError`: [SendEventProvider] error type
/// * `Event`: Concrete event provider, currently either [EventU32] or [EventU16] /// * `Event`: Concrete event provider, currently either [EventU32] or [EventU16]
/// * `AuxDataProvider`: Concrete auxiliary data provder, currently either [Params] or /// * `AuxDataProvider`: Concrete auxiliary data provider, currently either [Params] or
/// [ParamsHeapless] /// [ParamsHeapless]
pub struct EventManager<SendProviderError, Event: GenericEvent = EventU32, AuxDataProvider = Params> pub struct EventManager<SendProviderError, Event: GenericEvent = EventU32, AuxDataProvider = Params>
{ {
listeners: HashMap<ListenerKey, Vec<Listener<SendProviderError, Event, AuxDataProvider>>>, listener_table: Box<dyn ListenerTable>,
//listener_table: Box<dyn ListenerTable<SendProviderError, Event, AuxDataProvider>>, sender_table: Box<dyn SenderTable<SendProviderError, Event, AuxDataProvider>>,
event_receiver: Box<dyn EventReceiver<Event, AuxDataProvider>>, event_receiver: Box<dyn EventReceiver<Event, AuxDataProvider>>,
} }
/// Safety: It is safe to implement [Send] because all fields in the [EventManager] are [Send]
/// as well
#[cfg(feature = "std")] #[cfg(feature = "std")]
unsafe impl<E, Event: GenericEvent + Send, AuxDataProvider: Send> Send
for EventManager<E, Event, AuxDataProvider>
{
}
#[cfg(feature = "std")]
pub type EventManagerWithMpscQueue<Event, AuxDataProvider> = EventManager<
std::sync::mpsc::SendError<(Event, Option<AuxDataProvider>)>,
Event,
AuxDataProvider,
>;
#[derive(Debug)]
pub enum EventRoutingResult<Event: GenericEvent, AuxDataProvider> {
/// No event was received
Empty,
/// An event was received and routed.
/// The first tuple entry will contain the number of recipients.
Handled(u32, Event, Option<AuxDataProvider>),
}
#[derive(Debug)]
pub enum EventRoutingError<E> {
SendError(E),
NoSendersForKey(ListenerKey),
NoSenderForId(SenderId),
}
#[derive(Debug)]
pub struct EventRoutingErrorsWithResult<Event: GenericEvent, AuxDataProvider, E> {
pub result: EventRoutingResult<Event, AuxDataProvider>,
pub errors: [Option<EventRoutingError<E>>; 3],
}
impl<E, Event: GenericEvent + Copy> EventManager<E, Event> {
pub fn remove_duplicates(&mut self, key: &ListenerKey) {
self.listener_table.remove_duplicates(key)
}
pub fn subscribe_single(&mut self, event: &Event, sender_id: SenderId) {
self.update_listeners(ListenerKey::Single(event.raw_as_largest_type()), sender_id);
}
pub fn subscribe_group(&mut self, group_id: LargestGroupIdRaw, sender_id: SenderId) {
self.update_listeners(ListenerKey::Group(group_id), sender_id);
}
/// Subscribe for all events received by the manager.
///
/// For example, this can be useful for a handler component which sends every event as
/// a telemetry packet.
pub fn subscribe_all(&mut self, sender_id: SenderId) {
self.update_listeners(ListenerKey::All, sender_id);
}
}
impl<E: 'static, Event: GenericEvent + Copy + 'static, AuxDataProvider: Clone + 'static>
EventManager<E, Event, AuxDataProvider>
{
pub fn new(event_receiver: Box<dyn EventReceiver<Event, AuxDataProvider>>) -> Self {
let listener_table = Box::new(DefaultListenerTableProvider::default());
let sender_table =
Box::new(DefaultSenderTableProvider::<E, Event, AuxDataProvider>::default());
Self::new_custom_tables(listener_table, sender_table, event_receiver)
}
}
impl<E, Event: GenericEvent + Copy, AuxDataProvider: Clone>
EventManager<E, Event, AuxDataProvider>
{
pub fn new_custom_tables(
listener_table: Box<dyn ListenerTable>,
sender_table: Box<dyn SenderTable<E, Event, AuxDataProvider>>,
event_receiver: Box<dyn EventReceiver<Event, AuxDataProvider>>,
) -> Self {
EventManager {
listener_table,
sender_table,
event_receiver,
}
}
pub fn add_sender(
&mut self,
send_provider: impl SendEventProvider<Event, AuxDataProvider, Error = E> + 'static,
) {
if !self
.sender_table
.contains_send_event_provider(&send_provider.id())
{
self.sender_table
.add_send_event_provider(Box::new(send_provider));
}
}
fn update_listeners(&mut self, key: ListenerKey, sender_id: SenderId) {
self.listener_table.add_listener(key, sender_id);
}
/// This function will use the cached event receiver and try to receive one event.
/// If an event was received, it will try to route that event to all subscribed event listeners.
/// If this works without any issues, the [EventRoutingResult] will contain context information
/// about the routed event.
///
/// This function will track up to 3 errors returned as part of the
/// [EventRoutingErrorsWithResult] error struct.
pub fn try_event_handling(
&mut self,
) -> Result<
EventRoutingResult<Event, AuxDataProvider>,
EventRoutingErrorsWithResult<Event, AuxDataProvider, E>,
> {
let mut err_idx = 0;
let mut err_slice = [None, None, None];
let mut num_recipients = 0;
let mut add_error = |error: EventRoutingError<E>| {
if err_idx < 3 {
err_slice[err_idx] = Some(error);
err_idx += 1;
}
};
let mut send_handler =
|key: &ListenerKey, event: Event, aux_data: &Option<AuxDataProvider>| {
if self.listener_table.contains_listener(key) {
if let Some(ids) = self.listener_table.get_listener_ids(key) {
for id in ids {
if let Some(sender) = self.sender_table.get_send_event_provider(id) {
if let Err(e) = sender.send(event, aux_data.clone()) {
add_error(EventRoutingError::SendError(e));
} else {
num_recipients += 1;
}
} else {
add_error(EventRoutingError::NoSenderForId(*id));
}
}
} else {
add_error(EventRoutingError::NoSendersForKey(*key));
}
}
};
if let Some((event, aux_data)) = self.event_receiver.receive() {
let single_key = ListenerKey::Single(event.raw_as_largest_type());
send_handler(&single_key, event, &aux_data);
let group_key = ListenerKey::Group(event.group_id_as_largest_type());
send_handler(&group_key, event, &aux_data);
send_handler(&ListenerKey::All, event, &aux_data);
if err_idx > 0 {
return Err(EventRoutingErrorsWithResult {
result: EventRoutingResult::Handled(num_recipients, event, aux_data),
errors: err_slice,
});
}
return Ok(EventRoutingResult::Handled(num_recipients, event, aux_data));
}
Ok(EventRoutingResult::Empty)
}
}
#[derive(Default)] #[derive(Default)]
pub struct DefaultListenerTableProvider {
listeners: HashMap<ListenerKey, Vec<SenderId>>,
}
pub struct DefaultSenderTableProvider< pub struct DefaultSenderTableProvider<
SendProviderError, SendProviderError,
Event: GenericEvent = EventU32, Event: GenericEvent = EventU32,
AuxDataProvider = Params, AuxDataProvider = Params,
> { > {
listeners: HashMap<ListenerKey, Vec<SenderId>>,
senders: HashMap< senders: HashMap<
SenderId, SenderId,
Box<dyn SendEventProvider<Event, AuxDataProvider, Error = SendProviderError>>, Box<dyn SendEventProvider<Event, AuxDataProvider, Error = SendProviderError>>,
>, >,
} }
#[cfg(feature = "std")] impl<SendProviderError, Event: GenericEvent, AuxDataProvider> Default
impl<SendProviderError, Event: GenericEvent, AuxDataProvider>
ListenerTable<SendProviderError, Event, AuxDataProvider>
for DefaultSenderTableProvider<SendProviderError, Event, AuxDataProvider> for DefaultSenderTableProvider<SendProviderError, Event, AuxDataProvider>
{ {
fn get_listener_ids(&mut self, key: ListenerKey) -> Option<Iter<SenderId>> { fn default() -> Self {
self.listeners.get(&key).map(|vec| vec.into_iter()) Self {
senders: HashMap::new(),
}
}
}
impl ListenerTable for DefaultListenerTableProvider {
fn get_listeners(&self) -> Vec<ListenerKey> {
let mut key_list = Vec::new();
for key in self.listeners.keys() {
key_list.push(*key);
}
key_list
}
fn contains_listener(&self, key: &ListenerKey) -> bool {
self.listeners.contains_key(key)
}
fn get_listener_ids(&self, key: &ListenerKey) -> Option<Iter<SenderId>> {
self.listeners.get(key).map(|vec| vec.iter())
} }
fn add_listener(&mut self, key: ListenerKey, sender_id: SenderId) -> bool { fn add_listener(&mut self, key: ListenerKey, sender_id: SenderId) -> bool {
@ -164,12 +346,30 @@ impl<SendProviderError, Event: GenericEvent, AuxDataProvider>
true true
} }
fn remove_duplicates(&mut self, key: &ListenerKey) {
if let Some(list) = self.listeners.get_mut(key) {
list.sort_unstable();
list.dedup();
}
}
}
impl<SendProviderError, Event: GenericEvent, AuxDataProvider>
SenderTable<SendProviderError, Event, AuxDataProvider>
for DefaultSenderTableProvider<SendProviderError, Event, AuxDataProvider>
{
fn contains_send_event_provider(&self, id: &SenderId) -> bool {
self.senders.contains_key(id)
}
fn get_send_event_provider( fn get_send_event_provider(
&mut self, &mut self,
id: SenderId, id: &SenderId,
) -> Option<&mut Box<dyn SendEventProvider<Event, AuxDataProvider, Error = SendProviderError>>> ) -> Option<&mut Box<dyn SendEventProvider<Event, AuxDataProvider, Error = SendProviderError>>>
{ {
self.senders.get_mut(&id).filter(|sender| sender.id() == id) self.senders
.get_mut(id)
.filter(|sender| sender.id() == *id)
} }
fn add_send_event_provider( fn add_send_event_provider(
@ -186,145 +386,6 @@ impl<SendProviderError, Event: GenericEvent, AuxDataProvider>
} }
} }
/// Safety: It is safe to implement [Send] because all fields in the [EventManager] are [Send]
/// as well
#[cfg(feature = "std")]
unsafe impl<E, Event: GenericEvent + Send, AuxDataProvider: Send> Send
for EventManager<E, Event, AuxDataProvider>
{
}
pub enum HandlerResult<Event: GenericEvent, AuxDataProvider> {
Empty,
Handled(u32, Event, Option<AuxDataProvider>),
}
impl<E, Event: GenericEvent + Copy> EventManager<E, Event> {
pub fn new(event_receiver: Box<dyn EventReceiver<Event>>) -> Self {
EventManager {
listeners: HashMap::new(),
event_receiver,
}
}
pub fn subscribe_single(
&mut self,
event: Event,
dest: impl SendEventProvider<Event, Error = E> + 'static,
) {
self.update_listeners(ListenerKey::Single(event.raw_as_largest_type()), dest);
}
pub fn subscribe_group(
&mut self,
group_id: LargestGroupIdRaw,
dest: impl SendEventProvider<Event, Error = E> + 'static,
) {
self.update_listeners(ListenerKey::Group(group_id), dest);
}
/// Subscribe for all events received by the manager.
///
/// For example, this can be useful for a handler component which sends every event as
/// a telemetry packet.
pub fn subscribe_all(
&mut self,
send_provider: impl SendEventProvider<Event, Error = E> + 'static,
) {
self.update_listeners(ListenerKey::All, send_provider);
}
/// Helper function which removes single subscriptions for which a group subscription already
/// exists.
pub fn remove_single_subscriptions_for_group(
&mut self,
group_id: LargestGroupIdRaw,
dest: impl SendEventProvider<Event, Error = E> + 'static,
) {
if self.listeners.contains_key(&ListenerKey::Group(group_id)) {
for (ltype, listeners) in &mut self.listeners {
if let ListenerKey::Single(_) = ltype {
listeners.retain(|f| f.send_provider.id() != dest.id());
}
}
}
}
}
impl<E, Event: GenericEvent + Copy, AuxDataProvider: Clone>
EventManager<E, Event, AuxDataProvider>
{
fn update_listeners(
&mut self,
key: ListenerKey,
dest: impl SendEventProvider<Event, AuxDataProvider, Error = E> + 'static,
) {
if !self.listeners.contains_key(&key) {
self.listeners.insert(
key,
vec![Listener {
ltype: key,
send_provider: Box::new(dest),
}],
);
} else {
let vec = self.listeners.get_mut(&key).unwrap();
// To prevent double insertions
for entry in vec.iter() {
if entry.ltype == key && entry.send_provider.id() == dest.id() {
return;
}
}
vec.push(Listener {
ltype: key,
send_provider: Box::new(dest),
});
}
}
pub fn try_event_handling(&mut self) -> Result<HandlerResult<Event, AuxDataProvider>, E> {
let mut err_status = None;
let mut num_recipients = 0;
let mut send_handler =
|event: Event,
aux_data: Option<AuxDataProvider>,
llist: &mut Vec<Listener<E, Event, AuxDataProvider>>| {
for listener in llist.iter_mut() {
if let Err(e) = listener.send_provider.send(event, aux_data.clone()) {
err_status = Some(Err(e));
} else {
num_recipients += 1;
}
}
};
if let Some((event, aux_data)) = self.event_receiver.receive() {
let single_key = ListenerKey::Single(event.raw_as_largest_type());
if self.listeners.contains_key(&single_key) {
send_handler(
event,
aux_data.clone(),
self.listeners.get_mut(&single_key).unwrap(),
);
}
let group_key = ListenerKey::Group(event.group_id_as_largest_type());
if self.listeners.contains_key(&group_key) {
send_handler(
event,
aux_data.clone(),
self.listeners.get_mut(&group_key).unwrap(),
);
}
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 {
return err;
}
return Ok(HandlerResult::Handled(num_recipients, event, aux_data));
}
Ok(HandlerResult::Empty)
}
}
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub mod stdmod { pub mod stdmod {
use super::*; use super::*;
@ -362,9 +423,6 @@ pub mod stdmod {
sender: Sender<(Event, Option<Params>)>, sender: Sender<(Event, Option<Params>)>,
} }
/// Safety: Send is safe to implement because both the ID and the MPSC sender are Send
//unsafe impl<Event: GenericEvent> Send for MpscEventSendProvider<Event> {}
impl<Event: GenericEvent + Send> MpscEventSendProvider<Event> { impl<Event: GenericEvent + Send> MpscEventSendProvider<Event> {
pub fn new(id: u32, sender: Sender<(Event, Option<Params>)>) -> Self { pub fn new(id: u32, sender: Sender<(Event, Option<Params>)>) -> Self {
Self { id, sender } Self { id, sender }
@ -431,12 +489,12 @@ mod tests {
} }
fn check_handled_event( fn check_handled_event(
res: HandlerResult<EventU32, Params>, res: EventRoutingResult<EventU32, Params>,
expected: EventU32, expected: EventU32,
expected_num_sent: u32, expected_num_sent: u32,
) { ) {
assert!(matches!(res, HandlerResult::Handled { .. })); assert!(matches!(res, EventRoutingResult::Handled { .. }));
if let HandlerResult::Handled(num_recipients, event, _aux_data) = res { if let EventRoutingResult::Handled(num_recipients, event, _aux_data) = res {
assert_eq!(event, expected); assert_eq!(event, expected);
assert_eq!(num_recipients, expected_num_sent); assert_eq!(num_recipients, expected_num_sent);
} }
@ -461,13 +519,15 @@ mod tests {
let event_grp_1_0 = EventU32::new(Severity::HIGH, 1, 0).unwrap(); let event_grp_1_0 = EventU32::new(Severity::HIGH, 1, 0).unwrap();
let (single_event_sender, single_event_receiver) = channel(); let (single_event_sender, single_event_receiver) = channel();
let single_event_listener = MpscEventSenderQueue::new(0, single_event_sender); let single_event_listener = MpscEventSenderQueue::new(0, single_event_sender);
event_man.subscribe_single(event_grp_0, single_event_listener); event_man.subscribe_single(&event_grp_0, single_event_listener.id());
event_man.add_sender(single_event_listener);
let (group_event_sender_0, group_event_receiver_0) = channel(); let (group_event_sender_0, group_event_receiver_0) = channel();
let group_event_listener = MpscEventSenderQueue { let group_event_listener = MpscEventSenderQueue {
id: 1, id: 1,
mpsc_sender: group_event_sender_0, mpsc_sender: group_event_sender_0,
}; };
event_man.subscribe_group(event_grp_1_0.group_id(), group_event_listener); event_man.subscribe_group(event_grp_1_0.group_id(), group_event_listener.id());
event_man.add_sender(group_event_listener);
// Test event with one listener // Test event with one listener
event_sender event_sender
@ -494,7 +554,8 @@ mod tests {
let event_grp_0 = EventU32::new(Severity::INFO, 0, 0).unwrap(); let event_grp_0 = EventU32::new(Severity::INFO, 0, 0).unwrap();
let (single_event_sender, single_event_receiver) = channel(); let (single_event_sender, single_event_receiver) = channel();
let single_event_listener = MpscEventSenderQueue::new(0, single_event_sender); let single_event_listener = MpscEventSenderQueue::new(0, single_event_sender);
event_man.subscribe_single(event_grp_0, single_event_listener); event_man.subscribe_single(&event_grp_0, single_event_listener.id());
event_man.add_sender(single_event_listener);
event_sender event_sender
.send((event_grp_0, Some(Params::Heapless((2_u32, 3_u32).into())))) .send((event_grp_0, Some(Params::Heapless((2_u32, 3_u32).into()))))
.expect("Sending group error failed"); .expect("Sending group error failed");
@ -519,7 +580,7 @@ mod tests {
let res = event_man.try_event_handling(); let res = event_man.try_event_handling();
assert!(res.is_ok()); assert!(res.is_ok());
let hres = res.unwrap(); let hres = res.unwrap();
assert!(matches!(hres, HandlerResult::Empty)); assert!(matches!(hres, EventRoutingResult::Empty));
let event_grp_0 = EventU32::new(Severity::INFO, 0, 0).unwrap(); let event_grp_0 = EventU32::new(Severity::INFO, 0, 0).unwrap();
let event_grp_1_0 = EventU32::new(Severity::HIGH, 1, 0).unwrap(); let event_grp_1_0 = EventU32::new(Severity::HIGH, 1, 0).unwrap();
@ -528,8 +589,9 @@ mod tests {
id: 0, id: 0,
mpsc_sender: event_grp_0_sender, mpsc_sender: event_grp_0_sender,
}; };
event_man.subscribe_group(event_grp_0.group_id(), event_grp_0_and_1_listener.clone()); event_man.subscribe_group(event_grp_0.group_id(), event_grp_0_and_1_listener.id());
event_man.subscribe_group(event_grp_1_0.group_id(), event_grp_0_and_1_listener); event_man.subscribe_group(event_grp_1_0.group_id(), event_grp_0_and_1_listener.id());
event_man.add_sender(event_grp_0_and_1_listener);
event_sender event_sender
.send((event_grp_0, None)) .send((event_grp_0, None))
@ -565,8 +627,12 @@ mod tests {
id: 1, id: 1,
mpsc_sender: event_0_tx_1, mpsc_sender: event_0_tx_1,
}; };
event_man.subscribe_single(event_0, event_listener_0.clone()); let event_listener_0_sender_id = event_listener_0.id();
event_man.subscribe_single(event_0, event_listener_1); event_man.subscribe_single(&event_0, event_listener_0_sender_id);
event_man.add_sender(event_listener_0);
let event_listener_1_sender_id = event_listener_1.id();
event_man.subscribe_single(&event_0, event_listener_1_sender_id);
event_man.add_sender(event_listener_1);
event_sender event_sender
.send((event_0, None)) .send((event_0, None))
.expect("Triggering Event 0 failed"); .expect("Triggering Event 0 failed");
@ -575,7 +641,7 @@ mod tests {
check_handled_event(res.unwrap(), event_0, 2); check_handled_event(res.unwrap(), event_0, 2);
check_next_event(event_0, &event_0_rx_0); check_next_event(event_0, &event_0_rx_0);
check_next_event(event_0, &event_0_rx_1); check_next_event(event_0, &event_0_rx_1);
event_man.subscribe_group(event_1.group_id(), event_listener_0.clone()); event_man.subscribe_group(event_1.group_id(), event_listener_0_sender_id);
event_sender event_sender
.send((event_0, None)) .send((event_0, None))
.expect("Triggering Event 0 failed"); .expect("Triggering Event 0 failed");
@ -594,8 +660,9 @@ mod tests {
check_next_event(event_0, &event_0_rx_0); check_next_event(event_0, &event_0_rx_0);
check_next_event(event_1, &event_0_rx_0); check_next_event(event_1, &event_0_rx_0);
// Double insertion should be detected, result should remain the same // Do double insertion and then remove duplicates
event_man.subscribe_group(event_1.group_id(), event_listener_0); event_man.subscribe_group(event_1.group_id(), event_listener_0_sender_id);
event_man.remove_duplicates(&ListenerKey::Group(event_1.group_id()));
event_sender event_sender
.send((event_1, None)) .send((event_1, None))
.expect("Triggering Event 1 failed"); .expect("Triggering Event 1 failed");
@ -617,7 +684,8 @@ mod tests {
id: 0, id: 0,
mpsc_sender: event_0_tx_0, mpsc_sender: event_0_tx_0,
}; };
event_man.subscribe_all(all_events_listener); event_man.subscribe_all(all_events_listener.id());
event_man.add_sender(all_events_listener);
event_sender event_sender
.send((event_0, None)) .send((event_0, None))
.expect("Triggering event 0 failed"); .expect("Triggering event 0 failed");

View File

@ -1,4 +1,6 @@
use fsrc_core::event_man::{EventManager, MpscEventReceiver, MpscEventU32SendProvider}; use fsrc_core::event_man::{
EventManagerWithMpscQueue, MpscEventU32Receiver, MpscEventU32SendProvider, SendEventProvider,
};
use fsrc_core::events::{EventU32, EventU32TypedSev, Severity, SeverityInfo}; use fsrc_core::events::{EventU32, EventU32TypedSev, Severity, SeverityInfo};
use fsrc_core::params::U32Pair; use fsrc_core::params::U32Pair;
use fsrc_core::params::{Params, ParamsHeapless, WritableToBeBytes}; use fsrc_core::params::{Params, ParamsHeapless, WritableToBeBytes};
@ -31,12 +33,13 @@ impl EcssTmSender for EventTmSender {
#[test] #[test]
fn test_threaded_usage() { fn test_threaded_usage() {
let (event_sender, event_man_receiver) = channel(); let (event_sender, event_man_receiver) = channel();
let event_receiver = MpscEventReceiver::new(event_man_receiver); let event_receiver = MpscEventU32Receiver::new(event_man_receiver);
let mut event_man = EventManager::new(Box::new(event_receiver)); let mut event_man = EventManagerWithMpscQueue::new(Box::new(event_receiver));
let (pus_event_man_tx, pus_event_man_rx) = channel(); let (pus_event_man_tx, pus_event_man_rx) = channel();
let pus_event_man_send_provider = MpscEventU32SendProvider::new(1, pus_event_man_tx); let pus_event_man_send_provider = MpscEventU32SendProvider::new(1, pus_event_man_tx);
event_man.subscribe_all(pus_event_man_send_provider); event_man.subscribe_all(pus_event_man_send_provider.id());
event_man.add_sender(pus_event_man_send_provider);
let (event_tx, event_rx) = channel(); let (event_tx, event_rx) = channel();
let reporter = EventReporter::new(0x02, 128).expect("Creating event reporter failed"); let reporter = EventReporter::new(0x02, 128).expect("Creating event reporter failed");
let backend = DefaultPusMgmtBackendProvider::<EventU32>::default(); let backend = DefaultPusMgmtBackendProvider::<EventU32>::default();

View File

@ -3,7 +3,9 @@ mod pus;
mod tmtc; mod tmtc;
use crate::tmtc::{core_tmtc_task, CoreTmtcArgs, TmStore, PUS_APID}; use crate::tmtc::{core_tmtc_task, CoreTmtcArgs, TmStore, PUS_APID};
use fsrc_core::event_man::{EventManager, MpscEventReceiver, MpscEventU32SendProvider}; use fsrc_core::event_man::{
EventManagerWithMpscQueue, MpscEventReceiver, MpscEventU32SendProvider, SendEventProvider,
};
use fsrc_core::events::EventU32; use fsrc_core::events::EventU32;
use fsrc_core::hal::host::udp_server::UdpTcServer; use fsrc_core::hal::host::udp_server::UdpTcServer;
use fsrc_core::pool::{LocalPool, PoolCfg, SharedPool, StoreAddr}; use fsrc_core::pool::{LocalPool, PoolCfg, SharedPool, StoreAddr};
@ -81,7 +83,7 @@ fn main() {
let (event_request_tx, event_request_rx) = channel::<EventRequestWithToken>(); let (event_request_tx, event_request_rx) = channel::<EventRequestWithToken>();
let (event_sender, event_man_rx) = channel(); let (event_sender, event_man_rx) = channel();
let event_recv = MpscEventReceiver::<EventU32>::new(event_man_rx); let event_recv = MpscEventReceiver::<EventU32>::new(event_man_rx);
let mut event_man = EventManager::new(Box::new(event_recv)); let mut event_man = EventManagerWithMpscQueue::new(Box::new(event_recv));
let event_reporter = EventReporter::new(PUS_APID, 128).unwrap(); let event_reporter = EventReporter::new(PUS_APID, 128).unwrap();
let pus_tm_backend = DefaultPusMgmtBackendProvider::<EventU32>::default(); let pus_tm_backend = DefaultPusMgmtBackendProvider::<EventU32>::default();
let mut pus_event_dispatcher = let mut pus_event_dispatcher =
@ -89,7 +91,7 @@ fn main() {
let (pus_event_man_tx, pus_event_man_rx) = channel(); let (pus_event_man_tx, pus_event_man_rx) = channel();
let pus_event_man_send_provider = MpscEventU32SendProvider::new(1, pus_event_man_tx); let pus_event_man_send_provider = MpscEventU32SendProvider::new(1, pus_event_man_tx);
let reporter1 = reporter_with_sender_0.clone(); let reporter1 = reporter_with_sender_0.clone();
event_man.subscribe_all(pus_event_man_send_provider); event_man.subscribe_all(pus_event_man_send_provider.id());
// Create clones here to allow move for thread 0 // Create clones here to allow move for thread 0
let core_args = CoreTmtcArgs { let core_args = CoreTmtcArgs {

@ -1 +1 @@
Subproject commit 1d6cf3a75d4082b3d962700038c21dc7dacb156f Subproject commit c750f94fba31114c5ade530ea6235acb6bb8835a