From 5d8787b7fa7945963f5bd5bff6516c8c5cb89da5 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 29 Nov 2024 13:36:53 +0100 Subject: [PATCH] generic shananigans --- satrs/src/mode.rs | 177 +++++++++++++++++------------ satrs/src/mode_tree.rs | 6 +- satrs/src/pus/action.rs | 56 +++++++--- satrs/src/pus/mode.rs | 8 +- satrs/src/request.rs | 179 +++++++++++++++++++++++------- satrs/tests/mode_tree.rs | 233 ++++++++++++++++++++++++++------------- 6 files changed, 454 insertions(+), 205 deletions(-) diff --git a/satrs/src/mode.rs b/satrs/src/mode.rs index 01beac9..1b4d636 100644 --- a/satrs/src/mode.rs +++ b/satrs/src/mode.rs @@ -12,7 +12,9 @@ pub use std_mod::*; use crate::{ queue::GenericTargetedMessagingError, - request::{GenericMessage, MessageMetadata, MessageReceiver, MessageReceiverWithId, RequestId}, + request::{ + GenericMessage, MessageMetadata, MessageReceiverProvider, MessageReceiverWithId, RequestId, + }, ComponentId, }; @@ -156,7 +158,7 @@ pub trait ModeRequestReceiver { ) -> Result>, GenericTargetedMessagingError>; } -impl> ModeRequestReceiver +impl> ModeRequestReceiver for MessageReceiverWithId { fn try_recv_mode_request( @@ -248,7 +250,9 @@ pub trait ModeReplyReceiver { ) -> Result>, GenericTargetedMessagingError>; } -impl> ModeReplyReceiver for MessageReceiverWithId { +impl> ModeReplyReceiver + for MessageReceiverWithId +{ fn try_recv_mode_reply( &self, ) -> Result>, GenericTargetedMessagingError> { @@ -270,12 +274,13 @@ pub trait ModeReplySender { #[cfg(feature = "alloc")] pub mod alloc_mod { use crate::request::{ - MessageSender, MessageSenderAndReceiver, MessageSenderMap, RequestAndReplySenderAndReceiver, + MessageSenderAndReceiver, MessageSenderMap, MessageSenderProvider, + MessageSenderStoreProvider, RequestAndReplySenderAndReceiver, }; use super::*; - impl> MessageSenderMap { + impl> MessageSenderMap { pub fn send_mode_reply( &self, requestor_info: MessageMetadata, @@ -290,8 +295,13 @@ pub mod alloc_mod { } } - impl, R: MessageReceiver> ModeReplySender - for MessageSenderAndReceiver + impl< + From, + Sender: MessageSenderProvider, + Receiver: MessageReceiverProvider, + SenderStore: MessageSenderStoreProvider, + > ModeReplySender + for MessageSenderAndReceiver { fn local_channel_id(&self) -> ComponentId { self.local_channel_id_generic() @@ -302,7 +312,7 @@ pub mod alloc_mod { requestor_info: MessageMetadata, request: ModeReply, ) -> Result<(), GenericTargetedMessagingError> { - self.message_sender_map.send_mode_reply( + self.message_sender_store.send_message( MessageMetadata::new(requestor_info.request_id(), self.local_channel_id()), requestor_info.sender_id(), request, @@ -310,8 +320,13 @@ pub mod alloc_mod { } } - impl, R: MessageReceiver> ModeReplyReceiver - for MessageSenderAndReceiver + impl< + To, + Sender: MessageSenderProvider, + Receiver: MessageReceiverProvider, + SenderStore: MessageSenderStoreProvider, + > ModeReplyReceiver + for MessageSenderAndReceiver { fn try_recv_mode_reply( &self, @@ -322,10 +337,10 @@ pub mod alloc_mod { impl< REQUEST, - S0: MessageSender, - R0: MessageReceiver, - S1: MessageSender, - R1: MessageReceiver, + S0: MessageSenderProvider, + R0: MessageReceiverProvider, + S1: MessageSenderProvider, + R1: MessageReceiverProvider, > RequestAndReplySenderAndReceiver { pub fn add_reply_target(&mut self, target_id: ComponentId, reply_sender: S1) { @@ -336,10 +351,10 @@ pub mod alloc_mod { impl< REQUEST, - S0: MessageSender, - R0: MessageReceiver, - S1: MessageSender, - R1: MessageReceiver, + S0: MessageSenderProvider, + R0: MessageReceiverProvider, + S1: MessageSenderProvider, + R1: MessageReceiverProvider, > ModeReplySender for RequestAndReplySenderAndReceiver { fn local_channel_id(&self) -> ComponentId { @@ -361,10 +376,10 @@ pub mod alloc_mod { impl< REQUEST, - S0: MessageSender, - R0: MessageReceiver, - S1: MessageSender, - R1: MessageReceiver, + S0: MessageSenderProvider, + R0: MessageReceiverProvider, + S1: MessageSenderProvider, + R1: MessageReceiverProvider, > ModeReplyReceiver for RequestAndReplySenderAndReceiver { @@ -376,11 +391,14 @@ pub mod alloc_mod { } /// Helper type definition for a mode handler which can handle mode requests. - pub type ModeRequestHandlerInterface = - MessageSenderAndReceiver; + pub type ModeRequestHandlerInterface = + MessageSenderAndReceiver; - impl, R: MessageReceiver> - ModeRequestHandlerInterface + impl< + Sender: MessageSenderProvider, + Receiver: MessageReceiverProvider, + ReplySenderStore: MessageSenderStoreProvider, + > ModeRequestHandlerInterface { pub fn try_recv_mode_request( &self, @@ -403,9 +421,15 @@ pub mod alloc_mod { /// Helper type defintion for a mode handler object which can send mode requests and receive /// mode replies. - pub type ModeRequestorInterface = MessageSenderAndReceiver; + pub type ModeRequestorInterface = + MessageSenderAndReceiver; - impl, R: MessageReceiver> ModeRequestorInterface { + impl< + Sender: MessageSenderProvider, + Receiver: MessageReceiverProvider, + RequestSenderStore: MessageSenderStoreProvider, + > ModeRequestorInterface + { pub fn try_recv_mode_reply( &self, ) -> Result>, GenericTargetedMessagingError> { @@ -427,7 +451,7 @@ pub mod alloc_mod { pub type ModeInterface = RequestAndReplySenderAndReceiver; - impl> MessageSenderMap { + impl> MessageSenderMap { pub fn send_mode_request( &self, requestor_info: MessageMetadata, @@ -442,25 +466,13 @@ pub mod alloc_mod { } } - /* - impl> ModeRequestSender for MessageSenderMapWithId { - fn local_channel_id(&self) -> ComponentId { - self.local_channel_id - } - - fn send_mode_request( - &self, - request_id: RequestId, - target_id: ComponentId, - request: ModeRequest, - ) -> Result<(), GenericTargetedMessagingError> { - self.send_message(request_id, target_id, request) - } - } - */ - - impl, R: MessageReceiver> ModeRequestReceiver - for MessageSenderAndReceiver + impl< + To, + Sender: MessageSenderProvider, + Receiver: MessageReceiverProvider, + SenderStore: MessageSenderStoreProvider, + > ModeRequestReceiver + for MessageSenderAndReceiver { fn try_recv_mode_request( &self, @@ -469,8 +481,13 @@ pub mod alloc_mod { } } - impl, R: MessageReceiver> ModeRequestSender - for MessageSenderAndReceiver + impl< + From, + Sender: MessageSenderProvider, + Receiver: MessageReceiverProvider, + SenderStore: MessageSenderStoreProvider, + > ModeRequestSender + for MessageSenderAndReceiver { fn local_channel_id(&self) -> ComponentId { self.local_channel_id_generic() @@ -482,7 +499,7 @@ pub mod alloc_mod { target_id: ComponentId, request: ModeRequest, ) -> Result<(), GenericTargetedMessagingError> { - self.message_sender_map.send_mode_request( + self.message_sender_store.send_message( MessageMetadata::new(request_id, self.local_channel_id()), target_id, request, @@ -492,10 +509,10 @@ pub mod alloc_mod { impl< REPLY, - S0: MessageSender, - R0: MessageReceiver, - S1: MessageSender, - R1: MessageReceiver, + S0: MessageSenderProvider, + R0: MessageReceiverProvider, + S1: MessageSenderProvider, + R1: MessageReceiverProvider, > RequestAndReplySenderAndReceiver { pub fn add_request_target(&mut self, target_id: ComponentId, request_sender: S0) { @@ -506,10 +523,10 @@ pub mod alloc_mod { impl< REPLY, - S0: MessageSender, - R0: MessageReceiver, - S1: MessageSender, - R1: MessageReceiver, + S0: MessageSenderProvider, + R0: MessageReceiverProvider, + S1: MessageSenderProvider, + R1: MessageReceiverProvider, > ModeRequestSender for RequestAndReplySenderAndReceiver { @@ -533,10 +550,10 @@ pub mod alloc_mod { impl< REPLY, - S0: MessageSender, - R0: MessageReceiver, - S1: MessageSender, - R1: MessageReceiver, + S0: MessageSenderProvider, + R0: MessageReceiverProvider, + S1: MessageSenderProvider, + R1: MessageReceiverProvider, > ModeRequestReceiver for RequestAndReplySenderAndReceiver { @@ -552,33 +569,55 @@ pub mod alloc_mod { pub mod std_mod { use std::sync::mpsc; + use crate::request::{MessageSenderList, OneMessageSender}; + use super::*; - pub type ModeRequestHandlerMpsc = ModeRequestHandlerInterface< + pub type ModeRequestHandlerOneParentMpsc = ModeRequestHandlerInterface< mpsc::Sender>, mpsc::Receiver>, + OneMessageSender>>, >; - pub type ModeRequestHandlerMpscBounded = ModeRequestHandlerInterface< + pub type ModeRequestHandlerOneParentMpscBounded = ModeRequestHandlerInterface< mpsc::SyncSender>, mpsc::Receiver>, + OneMessageSender>>, >; - pub type ModeRequestorMpsc = ModeRequestorInterface< + pub type ModeRequestorOneChildMpsc = ModeRequestorInterface< mpsc::Sender>, mpsc::Receiver>, + OneMessageSender>>, >; - pub type ModeRequestorBoundedMpsc = ModeRequestorInterface< + pub type ModeRequestorOneChildBoundedMpsc = ModeRequestorInterface< mpsc::SyncSender>, mpsc::Receiver>, + OneMessageSender< + ModeRequest, + mpsc::SyncSender>, + >, + >; + pub type ModeRequestorMultiChildListMpsc = ModeRequestorInterface< + mpsc::Sender>, + mpsc::Receiver>, + MessageSenderList>>, + >; + pub type ModeRequestorMultiChildListBoundedMpsc = ModeRequestorInterface< + mpsc::SyncSender>, + mpsc::Receiver>, + MessageSenderList< + ModeRequest, + mpsc::SyncSender>, + >, >; - pub type ModeRequestorAndHandlerMpsc = ModeInterface< + pub type ModeRequestorAndHandlerOneParentMpsc = ModeInterface< mpsc::Sender>, mpsc::Receiver>, mpsc::Sender>, mpsc::Receiver>, >; - pub type ModeRequestorAndHandlerMpscBounded = ModeInterface< + pub type ModeRequestorAndHandlerOneParentMpscBounded = ModeInterface< mpsc::SyncSender>, mpsc::Receiver>, mpsc::SyncSender>, diff --git a/satrs/src/mode_tree.rs b/satrs/src/mode_tree.rs index e1063d7..50e17ff 100644 --- a/satrs/src/mode_tree.rs +++ b/satrs/src/mode_tree.rs @@ -3,7 +3,7 @@ use hashbrown::HashMap; use crate::{ mode::{Mode, ModeAndSubmode, ModeReply, ModeRequest, Submode}, - request::MessageSender, + request::MessageSenderProvider, ComponentId, }; @@ -19,7 +19,7 @@ pub trait ModeNode { /// A mode parent is capable of sending mode requests to child objects and has a unique component /// ID. pub trait ModeParent: ModeNode { - type Sender: MessageSender; + type Sender: MessageSenderProvider; fn add_mode_child(&mut self, id: ComponentId, request_sender: Self::Sender); } @@ -28,7 +28,7 @@ pub trait ModeParent: ModeNode { /// /// A child is capable of sending mode replies to parent objects and has a unique component ID. pub trait ModeChild: ModeNode { - type Sender: MessageSender; + type Sender: MessageSenderProvider; fn add_mode_parent(&mut self, id: ComponentId, reply_sender: Self::Sender); } diff --git a/satrs/src/pus/action.rs b/satrs/src/pus/action.rs index 6bcd270..0039609 100644 --- a/satrs/src/pus/action.rs +++ b/satrs/src/pus/action.rs @@ -68,7 +68,8 @@ pub mod alloc_mod { action::ActionRequest, queue::GenericTargetedMessagingError, request::{ - GenericMessage, MessageReceiver, MessageSender, MessageSenderAndReceiver, RequestId, + GenericMessage, MessageReceiverProvider, MessageSenderAndReceiver, + MessageSenderProvider, MessageSenderStoreProvider, RequestId, }, ComponentId, }; @@ -76,11 +77,14 @@ pub mod alloc_mod { use super::ActionReplyPus; /// Helper type definition for a mode handler which can handle mode requests. - pub type ActionRequestHandlerInterface = - MessageSenderAndReceiver; + pub type ActionRequestHandlerInterface = + MessageSenderAndReceiver; - impl, R: MessageReceiver> - ActionRequestHandlerInterface + impl< + Sender: MessageSenderProvider, + Receiver: MessageReceiverProvider, + ReplySender: MessageSenderStoreProvider, + > ActionRequestHandlerInterface { pub fn try_recv_action_request( &self, @@ -100,11 +104,20 @@ pub mod alloc_mod { /// Helper type defintion for a mode handler object which can send mode requests and receive /// mode replies. - pub type ActionRequestorInterface = - MessageSenderAndReceiver; + pub type ActionRequestorInterface = + MessageSenderAndReceiver< + ActionRequest, + ActionReplyPus, + Sender, + Receiver, + RequestSenderStore, + >; - impl, R: MessageReceiver> - ActionRequestorInterface + impl< + Sender: MessageSenderProvider, + Receiver: MessageReceiverProvider, + RequestSenderStore: MessageSenderStoreProvider, + > ActionRequestorInterface { pub fn try_recv_action_reply( &self, @@ -132,6 +145,7 @@ pub mod std_mod { verification::{self, TcStateToken}, ActivePusRequestStd, ActiveRequestProvider, DefaultActiveRequestMap, }, + request::{MessageSenderMap, OneMessageSender}, ComponentId, }; @@ -174,22 +188,38 @@ pub mod std_mod { } pub type DefaultActiveActionRequestMap = DefaultActiveRequestMap; - pub type ActionRequestHandlerMpsc = ActionRequestHandlerInterface< + pub type ActionRequestHandlerOneSenderMpsc = ActionRequestHandlerInterface< mpsc::Sender>, mpsc::Receiver>, + OneMessageSender< + GenericMessage, + mpsc::Sender>, + >, >; - pub type ActionRequestHandlerMpscBounded = ActionRequestHandlerInterface< + pub type ActionRequestHandlerOneSenderMpscBounded = ActionRequestHandlerInterface< mpsc::SyncSender>, mpsc::Receiver>, + OneMessageSender< + GenericMessage, + mpsc::SyncSender>, + >, >; - pub type ActionRequestorMpsc = ActionRequestorInterface< + pub type ActionRequestorWithSenderMapMpsc = ActionRequestorInterface< mpsc::Sender>, mpsc::Receiver>, + MessageSenderMap< + GenericMessage, + mpsc::Sender>, + >, >; - pub type ActionRequestorBoundedMpsc = ActionRequestorInterface< + pub type ActionRequestorWithSenderMapBoundedMpsc = ActionRequestorInterface< mpsc::SyncSender>, mpsc::Receiver>, + MessageSenderMap< + GenericMessage, + mpsc::SyncSender>, + >, >; } diff --git a/satrs/src/pus/mode.rs b/satrs/src/pus/mode.rs index 5ff78bf..75bf40c 100644 --- a/satrs/src/pus/mode.rs +++ b/satrs/src/pus/mode.rs @@ -39,7 +39,7 @@ mod tests { use crate::{ mode::{ ModeAndSubmode, ModeReply, ModeReplySender, ModeRequest, ModeRequestSender, - ModeRequestorAndHandlerMpsc, ModeRequestorMpsc, + ModeRequestorAndHandlerOneParentMpsc, ModeRequestorOneChildMpsc, }, request::{GenericMessage, MessageMetadata}, }; @@ -52,7 +52,7 @@ mod tests { fn test_simple_mode_requestor() { let (reply_sender, reply_receiver) = mpsc::channel(); let (request_sender, request_receiver) = mpsc::channel(); - let mut mode_requestor = ModeRequestorMpsc::new(TEST_COMPONENT_ID_0, reply_receiver); + let mut mode_requestor = ModeRequestorOneChildMpsc::new(TEST_COMPONENT_ID_0, reply_receiver); mode_requestor.add_message_target(TEST_COMPONENT_ID_1, request_sender); // Send a request and verify it arrives at the receiver. @@ -89,7 +89,7 @@ mod tests { let (request_sender_to_channel_1, request_receiver_channel_1) = mpsc::channel(); //let (reply_sender_to_channel_2, reply_receiver_channel_2) = mpsc::channel(); - let mut mode_connector = ModeRequestorAndHandlerMpsc::new( + let mut mode_connector = ModeRequestorAndHandlerOneParentMpsc::new( TEST_COMPONENT_ID_0, request_receiver_of_connector, reply_receiver_of_connector, @@ -128,7 +128,7 @@ mod tests { let (_request_sender_to_connector, request_receiver_of_connector) = mpsc::channel(); let (reply_sender_to_channel_2, reply_receiver_channel_2) = mpsc::channel(); - let mut mode_connector = ModeRequestorAndHandlerMpsc::new( + let mut mode_connector = ModeRequestorAndHandlerOneParentMpsc::new( TEST_COMPONENT_ID_0, request_receiver_of_connector, reply_receiver_of_connector, diff --git a/satrs/src/request.rs b/satrs/src/request.rs index f188798..ab80e90 100644 --- a/satrs/src/request.rs +++ b/satrs/src/request.rs @@ -140,24 +140,27 @@ impl GenericMessage { } /// Generic trait for objects which can send targeted messages. -pub trait MessageSender: Send { +pub trait MessageSenderProvider: Send { fn send(&self, message: GenericMessage) -> Result<(), GenericTargetedMessagingError>; } // Generic trait for objects which can receive targeted messages. -pub trait MessageReceiver { +pub trait MessageReceiverProvider { fn try_recv(&self) -> Result>, GenericTargetedMessagingError>; } -pub struct MessageWithSenderIdReceiver>(pub R, PhantomData); +pub struct MessageWithSenderIdReceiver>( + pub R, + PhantomData, +); -impl> From for MessageWithSenderIdReceiver { +impl> From for MessageWithSenderIdReceiver { fn from(receiver: R) -> Self { MessageWithSenderIdReceiver(receiver, PhantomData) } } -impl> MessageWithSenderIdReceiver { +impl> MessageWithSenderIdReceiver { pub fn try_recv_message( &self, ) -> Result>, GenericTargetedMessagingError> { @@ -165,12 +168,12 @@ impl> MessageWithSenderIdReceiver { } } -pub struct MessageReceiverWithId> { +pub struct MessageReceiverWithId> { local_channel_id: ComponentId, reply_receiver: MessageWithSenderIdReceiver, } -impl> MessageReceiverWithId { +impl> MessageReceiverWithId { pub fn new(local_channel_id: ComponentId, reply_receiver: R) -> Self { Self { local_channel_id, @@ -183,7 +186,7 @@ impl> MessageReceiverWithId { } } -impl> MessageReceiverWithId { +impl> MessageReceiverWithId { pub fn try_recv_message( &self, ) -> Result>, GenericTargetedMessagingError> { @@ -191,6 +194,17 @@ impl> MessageReceiverWithId { } } +pub trait MessageSenderStoreProvider: Default { + fn add_message_target(&mut self, target_id: ComponentId, message_sender: Sender); + + fn send_message( + &self, + requestor_info: MessageMetadata, + target_channel_id: ComponentId, + message: Message, + ) -> Result<(), GenericTargetedMessagingError>; +} + #[cfg(feature = "alloc")] pub mod alloc_mod { use crate::queue::GenericSendError; @@ -198,23 +212,99 @@ pub mod alloc_mod { use super::*; use hashbrown::HashMap; - pub struct MessageSenderMap>( - pub HashMap, + pub struct OneMessageSender> { + pub id_and_sender: Option<(ComponentId, S)>, + pub(crate) phantom: PhantomData, + } + + impl> Default for OneMessageSender { + fn default() -> Self { + Self { + id_and_sender: Default::default(), + phantom: Default::default(), + } + } + } + + impl> MessageSenderStoreProvider + for OneMessageSender + { + fn add_message_target(&mut self, target_id: ComponentId, message_sender: S) { + if self.id_and_sender.is_some() { + return; + } + self.id_and_sender = Some((target_id, message_sender)); + } + + fn send_message( + &self, + requestor_info: MessageMetadata, + target_channel_id: ComponentId, + message: Msg, + ) -> Result<(), GenericTargetedMessagingError> { + if let Some((current_id, sender)) = &self.id_and_sender { + if *current_id == target_channel_id { + sender.send(GenericMessage::new(requestor_info, message))?; + return Ok(()); + } + } + Err(GenericSendError::TargetDoesNotExist(target_channel_id).into()) + } + } + + pub struct MessageSenderList>( + pub alloc::vec::Vec<(ComponentId, S)>, pub(crate) PhantomData, ); - impl> Default for MessageSenderMap { + impl> Default for MessageSenderList { fn default() -> Self { Self(Default::default(), PhantomData) } } - impl> MessageSenderMap { - pub fn add_message_target(&mut self, target_id: ComponentId, message_sender: S) { + impl> MessageSenderStoreProvider + for MessageSenderList + { + fn add_message_target(&mut self, target_id: ComponentId, message_sender: S) { + self.0.push((target_id, message_sender)); + } + + fn send_message( + &self, + requestor_info: MessageMetadata, + target_channel_id: ComponentId, + message: MSG, + ) -> Result<(), GenericTargetedMessagingError> { + for (current_id, sender) in &self.0 { + if *current_id == target_channel_id { + sender.send(GenericMessage::new(requestor_info, message))?; + return Ok(()); + } + } + Err(GenericSendError::TargetDoesNotExist(target_channel_id).into()) + } + } + + pub struct MessageSenderMap>( + pub HashMap, + pub(crate) PhantomData, + ); + + impl> Default for MessageSenderMap { + fn default() -> Self { + Self(Default::default(), PhantomData) + } + } + + impl> MessageSenderStoreProvider + for MessageSenderMap + { + fn add_message_target(&mut self, target_id: ComponentId, message_sender: S) { self.0.insert(target_id, message_sender); } - pub fn send_message( + fn send_message( &self, requestor_info: MessageMetadata, target_channel_id: ComponentId, @@ -231,25 +321,38 @@ pub mod alloc_mod { } } - pub struct MessageSenderAndReceiver, R: MessageReceiver> { + pub struct MessageSenderAndReceiver< + To, + From, + Sender: MessageSenderProvider, + Receiver: MessageReceiverProvider, + SenderStore: MessageSenderStoreProvider, + > { pub local_channel_id: ComponentId, - pub message_sender_map: MessageSenderMap, - pub message_receiver: MessageWithSenderIdReceiver, + pub message_sender_store: SenderStore, + pub message_receiver: MessageWithSenderIdReceiver, + pub(crate) phantom: PhantomData<(To, Sender)>, } - impl, R: MessageReceiver> - MessageSenderAndReceiver + impl< + To, + From, + Sender: MessageSenderProvider, + Receiver: MessageReceiverProvider, + SenderStore: MessageSenderStoreProvider, + > MessageSenderAndReceiver { - pub fn new(local_channel_id: ComponentId, message_receiver: R) -> Self { + pub fn new(local_channel_id: ComponentId, message_receiver: Receiver) -> Self { Self { local_channel_id, - message_sender_map: Default::default(), + message_sender_store: Default::default(), message_receiver: MessageWithSenderIdReceiver::from(message_receiver), + phantom: PhantomData, } } - pub fn add_message_target(&mut self, target_id: ComponentId, message_sender: S) { - self.message_sender_map + pub fn add_message_target(&mut self, target_id: ComponentId, message_sender: Sender) { + self.message_sender_store .add_message_target(target_id, message_sender) } @@ -262,9 +365,9 @@ pub mod alloc_mod { &self, request_id: RequestId, target_id: ComponentId, - message: TO, + message: To, ) -> Result<(), GenericTargetedMessagingError> { - self.message_sender_map.send_message( + self.message_sender_store.send_message( MessageMetadata::new(request_id, self.local_channel_id_generic()), target_id, message, @@ -274,7 +377,7 @@ pub mod alloc_mod { /// Try to receive a message, which can be a reply or a request, depending on the generics. pub fn try_recv_message( &self, - ) -> Result>, GenericTargetedMessagingError> { + ) -> Result>, GenericTargetedMessagingError> { self.message_receiver.try_recv_message() } } @@ -282,10 +385,10 @@ pub mod alloc_mod { pub struct RequestAndReplySenderAndReceiver< REQUEST, REPLY, - S0: MessageSender, - R0: MessageReceiver, - S1: MessageSender, - R1: MessageReceiver, + S0: MessageSenderProvider, + R0: MessageReceiverProvider, + S1: MessageSenderProvider, + R1: MessageReceiverProvider, > { pub local_channel_id: ComponentId, // These 2 are a functional group. @@ -299,10 +402,10 @@ pub mod alloc_mod { impl< REQUEST, REPLY, - S0: MessageSender, - R0: MessageReceiver, - S1: MessageSender, - R1: MessageReceiver, + S0: MessageSenderProvider, + R0: MessageReceiverProvider, + S1: MessageSenderProvider, + R1: MessageReceiverProvider, > RequestAndReplySenderAndReceiver { pub fn new( @@ -333,14 +436,14 @@ pub mod std_mod { use crate::queue::{GenericReceiveError, GenericSendError}; - impl MessageSender for mpsc::Sender> { + impl MessageSenderProvider for mpsc::Sender> { fn send(&self, message: GenericMessage) -> Result<(), GenericTargetedMessagingError> { self.send(message) .map_err(|_| GenericSendError::RxDisconnected)?; Ok(()) } } - impl MessageSender for mpsc::SyncSender> { + impl MessageSenderProvider for mpsc::SyncSender> { fn send(&self, message: GenericMessage) -> Result<(), GenericTargetedMessagingError> { if let Err(e) = self.try_send(message) { return match e { @@ -357,7 +460,7 @@ pub mod std_mod { pub type MessageSenderMapMpsc = MessageReceiverWithId>; pub type MessageSenderMapBoundedMpsc = MessageReceiverWithId>; - impl MessageReceiver for mpsc::Receiver> { + impl MessageReceiverProvider for mpsc::Receiver> { fn try_recv(&self) -> Result>, GenericTargetedMessagingError> { match self.try_recv() { Ok(msg) => Ok(Some(msg)), @@ -386,7 +489,7 @@ mod tests { use crate::{ queue::{GenericReceiveError, GenericSendError, GenericTargetedMessagingError}, - request::{MessageMetadata, MessageSenderMap}, + request::{MessageMetadata, MessageSenderMap, MessageSenderStoreProvider}, }; use super::{GenericMessage, MessageReceiverWithId, UniqueApidTargetId}; diff --git a/satrs/tests/mode_tree.rs b/satrs/tests/mode_tree.rs index eb4235c..93d3213 100644 --- a/satrs/tests/mode_tree.rs +++ b/satrs/tests/mode_tree.rs @@ -1,9 +1,5 @@ use core::cell::Cell; -use satrs::mode::{ - Mode, ModeError, ModeProvider, ModeReplyReceiver, ModeReplySender, ModeRequestHandler, - ModeRequestHandlerMpscBounded, ModeRequestReceiver, ModeRequestorAndHandlerMpscBounded, - ModeRequestorBoundedMpsc, -}; +use satrs::mode::{Mode, ModeError, ModeProvider, ModeReplyReceiver, ModeReplySender, ModeRequestHandler, ModeRequestHandlerOneParentMpscBounded, ModeRequestReceiver, ModeRequestSender, ModeRequestorAndHandlerOneParentMpscBounded, ModeRequestorOneChildBoundedMpsc}; use satrs::mode_tree::{ connect_mode_nodes, ModeChild, ModeNode, ModeParent, ModeStoreProvider, SequenceTableMapTable, TargetTableEntry, @@ -159,7 +155,7 @@ impl ModeRequestHandler for ModeRequestHandlerMock { struct PusModeService { pub request_id_counter: Cell, - pub mode_node: ModeRequestorBoundedMpsc, + pub mode_node: ModeRequestorOneChildBoundedMpsc, } impl PusModeService { @@ -191,16 +187,17 @@ impl ModeParent for PusModeService { } struct AcsSubsystem { - pub mode_node: ModeRequestorAndHandlerMpscBounded, + pub mode_node: ModeRequestorAndHandlerOneParentMpscBounded, pub mode_requestor_info: Option, pub mode_and_submode: ModeAndSubmode, pub target_mode_and_submode: Option, pub subsystem_helper: SubsystemHelper, pub mode_req_handler_mock: ModeRequestHandlerMock, + pub mode_msgs_recvd: u32, } impl AcsSubsystem { - pub fn new(mode_node: ModeRequestorAndHandlerMpscBounded) -> Self { + pub fn new(mode_node: ModeRequestorAndHandlerOneParentMpscBounded) -> Self { Self { mode_node, mode_requestor_info: None, @@ -208,13 +205,25 @@ impl AcsSubsystem { target_mode_and_submode: None, subsystem_helper: Default::default(), mode_req_handler_mock: Default::default(), + mode_msgs_recvd: 0, } } + + pub fn get_and_clear_num_mode_msgs(&mut self) -> u32 { + let tmp = self.mode_msgs_recvd; + self.mode_msgs_recvd = 0; + tmp + } + pub fn run(&mut self) { if let Some(request) = self.mode_node.try_recv_mode_request().unwrap() { + self.mode_msgs_recvd += 1; self.handle_mode_request(request) .expect("mode messaging error"); } + if let Some(_reply) = self.mode_node.try_recv_mode_reply().unwrap(){ + // TODO: Implementation. + } } pub fn add_target_and_sequence_table( &mut self, @@ -345,20 +354,34 @@ impl ModeRequestHandler for AcsSubsystem { } struct MgmAssembly { - pub mode_node: ModeRequestorAndHandlerMpscBounded, + pub mode_node: ModeRequestorAndHandlerOneParentMpscBounded, pub mode_requestor_info: Option, pub mode_and_submode: ModeAndSubmode, pub target_mode_and_submode: Option, pub mode_req_mock: ModeRequestHandlerMock, + pub mode_msgs_recvd: u32, } impl MgmAssembly { + pub fn new(mode_node: ModeRequestorAndHandlerOneParentMpscBounded) -> Self { + Self { + mode_node, + mode_requestor_info: None, + mode_and_submode: UNKNOWN_MODE, + target_mode_and_submode: None, + mode_req_mock: Default::default(), + mode_msgs_recvd: 0, + } + } + pub fn run(&mut self) { self.check_mode_requests().expect("mode messaging error"); self.check_mode_replies().expect("mode messaging error"); } - fn id(&self) -> ComponentId { - TestComponentId::MagnetometerAssembly as u64 + pub fn get_and_clear_num_mode_msgs(&mut self) -> u32 { + let tmp = self.mode_msgs_recvd; + self.mode_msgs_recvd = 0; + tmp } pub fn check_mode_requests(&mut self) -> Result<(), GenericTargetedMessagingError> { @@ -395,7 +418,7 @@ impl MgmAssembly { impl ModeNode for MgmAssembly { fn id(&self) -> ComponentId { - self.id() + TestComponentId::MagnetometerAssembly as u64 } } impl ModeParent for MgmAssembly { @@ -473,18 +496,6 @@ impl ModeRequestHandler for MgmAssembly { Ok(()) } - fn send_mode_reply( - &self, - requestor: MessageMetadata, - reply: ModeReply, - ) -> Result<(), Self::Error> { - self.mode_node.send_mode_reply(requestor, reply)?; - self.mode_req_mock - .send_mode_reply(requestor, reply) - .unwrap(); - Ok(()) - } - fn handle_mode_info( &mut self, requestor_info: MessageMetadata, @@ -496,21 +507,34 @@ impl ModeRequestHandler for MgmAssembly { // TODO: A proper assembly must reach to mode changes of its children.. Ok(()) } + + fn send_mode_reply( + &self, + requestor: MessageMetadata, + reply: ModeReply, + ) -> Result<(), Self::Error> { + self.mode_node.send_mode_reply(requestor, reply)?; + self.mode_req_mock + .send_mode_reply(requestor, reply) + .unwrap(); + Ok(()) + } } struct DeviceManager { name: &'static str, pub id: ComponentId, - pub mode_node: ModeRequestorAndHandlerMpscBounded, + pub mode_node: ModeRequestorAndHandlerOneParentMpscBounded, pub mode_and_submode: ModeAndSubmode, pub mode_req_mock: ModeRequestHandlerMock, + pub mode_msgs_recvd: u32, } impl DeviceManager { pub fn new( name: &'static str, id: ComponentId, - mode_node: ModeRequestorAndHandlerMpscBounded, + mode_node: ModeRequestorAndHandlerOneParentMpscBounded, ) -> Self { Self { name, @@ -518,8 +542,26 @@ impl DeviceManager { mode_node, mode_and_submode: UNKNOWN_MODE, mode_req_mock: Default::default(), + mode_msgs_recvd: 0, } } + + pub fn get_and_clear_num_mode_msgs(&mut self) -> u32 { + let tmp = self.mode_msgs_recvd; + self.mode_msgs_recvd = 0; + tmp + } + + pub fn run(&mut self) { + self.check_mode_requests().expect("mode messaging error"); + } + + pub fn check_mode_requests(&mut self) -> Result<(), ModeError> { + if let Some(request) = self.mode_node.try_recv_mode_request()? { + self.handle_mode_request(request)? + } + Ok(()) + } } impl ModeNode for DeviceManager { @@ -571,6 +613,23 @@ impl ModeRequestHandler for DeviceManager { "{}: announcing mode: {:?}", self.name, self.mode_and_submode ); + let mut mode_request = ModeRequest::AnnounceMode; + if recursive { + mode_request = ModeRequest::AnnounceModeRecursive; + } + let request_id = requestor_info.map_or(0, |info| info.request_id()); + self.mode_node + .request_sender_map + .0 + .iter() + .for_each(|(_, sender)| { + sender + .send(GenericMessage::new( + MessageMetadata::new(request_id, self.mode_node.local_channel_id_generic()), + mode_request, + )) + .expect("sending mode request failed"); + }); self.mode_req_mock.announce_mode(requestor_info, recursive); } @@ -581,18 +640,6 @@ impl ModeRequestHandler for DeviceManager { self.mode_req_mock.handle_mode_reached(requestor).unwrap(); Ok(()) } - fn send_mode_reply( - &self, - requestor_info: MessageMetadata, - reply: ModeReply, - ) -> Result<(), ModeError> { - self.mode_node.send_mode_reply(requestor_info, reply)?; - self.mode_req_mock - .send_mode_reply(requestor_info, reply) - .unwrap(); - Ok(()) - } - fn handle_mode_info( &mut self, requestor_info: MessageMetadata, @@ -610,21 +657,34 @@ impl ModeRequestHandler for DeviceManager { .unwrap(); Ok(()) } + + fn send_mode_reply( + &self, + requestor_info: MessageMetadata, + reply: ModeReply, + ) -> Result<(), ModeError> { + self.mode_node.send_mode_reply(requestor_info, reply)?; + self.mode_req_mock + .send_mode_reply(requestor_info, reply) + .unwrap(); + Ok(()) + } } struct CommonDevice { name: &'static str, pub id: ComponentId, - pub mode_node: ModeRequestHandlerMpscBounded, + pub mode_node: ModeRequestHandlerOneParentMpscBounded, pub mode_and_submode: ModeAndSubmode, pub mode_req_mock: ModeRequestHandlerMock, + pub num_mode_msgs_recvd: u32, } impl CommonDevice { pub fn new( name: &'static str, id: ComponentId, - mode_node: ModeRequestHandlerMpscBounded, + mode_node: ModeRequestHandlerOneParentMpscBounded, ) -> Self { Self { name, @@ -632,6 +692,7 @@ impl CommonDevice { mode_node, mode_and_submode: UNKNOWN_MODE, mode_req_mock: Default::default(), + num_mode_msgs_recvd: 0, } } @@ -641,10 +702,17 @@ impl CommonDevice { pub fn check_mode_requests(&mut self) -> Result<(), ModeError> { if let Some(request) = self.mode_node.try_recv_mode_request()? { + self.num_mode_msgs_recvd += 1; self.handle_mode_request(request)? } Ok(()) } + + pub fn get_and_clear_num_mode_msgs(&mut self) -> u32 { + let tmp = self.num_mode_msgs_recvd; + self.num_mode_msgs_recvd = 0; + tmp + } } impl ModeNode for CommonDevice { @@ -698,18 +766,6 @@ impl ModeRequestHandler for CommonDevice { self.mode_req_mock.handle_mode_reached(requestor).unwrap(); Ok(()) } - fn send_mode_reply( - &self, - requestor_info: MessageMetadata, - reply: ModeReply, - ) -> Result<(), ModeError> { - self.mode_node.send_mode_reply(requestor_info, reply)?; - self.mode_req_mock - .send_mode_reply(requestor_info, reply) - .unwrap(); - Ok(()) - } - fn handle_mode_info( &mut self, requestor_info: MessageMetadata, @@ -727,6 +783,18 @@ impl ModeRequestHandler for CommonDevice { .unwrap(); Ok(()) } + + fn send_mode_reply( + &self, + requestor_info: MessageMetadata, + reply: ModeReply, + ) -> Result<(), ModeError> { + self.mode_node.send_mode_reply(requestor_info, reply)?; + self.mode_req_mock + .send_mode_reply(requestor_info, reply) + .unwrap(); + Ok(()) + } } #[derive(Debug, Default)] @@ -736,7 +804,7 @@ pub struct AnnounceModeInfo { } pub struct AcsController { - pub mode_node: ModeRequestHandlerMpscBounded, + pub mode_node: ModeRequestHandlerOneParentMpscBounded, pub mode_and_submode: ModeAndSubmode, pub announce_mode_queue: RefCell>, pub mode_req_mock: ModeRequestHandlerMock, @@ -879,42 +947,42 @@ impl TreeTestbench { let (reply_sender_to_mgt_man, reply_receiver_mgt_man) = mpsc::sync_channel(10); // Mode requestors only. - let mode_node_pus = ModeRequestorBoundedMpsc::new( + let mode_node_pus = ModeRequestorOneChildBoundedMpsc::new( TestComponentId::PusModeService as ComponentId, reply_receiver_pus, ); // Mode requestors and handlers. - let mut mgm_assy_node = ModeRequestorAndHandlerMpscBounded::new( + let mgm_assy_node = ModeRequestorAndHandlerOneParentMpscBounded::new( TestComponentId::MagnetometerAssembly as ComponentId, request_receiver_mgm_assy, reply_receiver_mgm_assy, ); - let mut mgt_dev_mgmt_node = ModeRequestorAndHandlerMpscBounded::new( + let mgt_dev_mgmt_node = ModeRequestorAndHandlerOneParentMpscBounded::new( TestComponentId::MgtDevManager as ComponentId, request_receiver_mgt_man, reply_receiver_mgt_man, ); - let acs_subsystem_node = ModeRequestorAndHandlerMpscBounded::new( + let acs_subsystem_node = ModeRequestorAndHandlerOneParentMpscBounded::new( TestComponentId::AcsSubsystem as ComponentId, request_receiver_acs_subsystem, reply_receiver_acs_subsystem, ); // Request handlers only. - let mut mgm_dev_node_0 = ModeRequestHandlerMpscBounded::new( + let mgm_dev_node_0 = ModeRequestHandlerOneParentMpscBounded::new( TestComponentId::MagnetometerDevice0 as ComponentId, request_receiver_mgm_dev_0, ); - let mut mgm_dev_node_1 = ModeRequestHandlerMpscBounded::new( + let mgm_dev_node_1 = ModeRequestHandlerOneParentMpscBounded::new( TestComponentId::MagnetometerDevice1 as ComponentId, request_receiver_mgm_dev_1, ); - let mut mgt_dev_node = ModeRequestHandlerMpscBounded::new( + let mgt_dev_node = ModeRequestHandlerOneParentMpscBounded::new( TestComponentId::MagnetorquerDevice as ComponentId, request_receiver_mgt_dev, ); - let acs_ctrl_node = ModeRequestHandlerMpscBounded::new( + let acs_ctrl_node = ModeRequestHandlerOneParentMpscBounded::new( TestComponentId::AcsController as ComponentId, request_receiver_acs_ctrl, ); @@ -926,7 +994,7 @@ impl TreeTestbench { ); let mut mgm_dev_1 = CommonDevice::new( "MGM_1", - TestComponentId::MagnetometerDevice0 as u64, + TestComponentId::MagnetometerDevice1 as u64, mgm_dev_node_1, ); let mut mgt_dev = CommonDevice::new( @@ -939,13 +1007,7 @@ impl TreeTestbench { TestComponentId::MgtDevManager as u64, mgt_dev_mgmt_node, ); - let mut mgm_assy = MgmAssembly { - mode_node: mgm_assy_node, - mode_requestor_info: None, - mode_and_submode: UNKNOWN_MODE, - target_mode_and_submode: None, - mode_req_mock: ModeRequestHandlerMock::default(), - }; + let mut mgm_assy = MgmAssembly::new(mgm_assy_node); let mut acs_subsystem = AcsSubsystem::new(acs_subsystem_node); let mut acs_ctrl = AcsController { mode_node: acs_ctrl_node, @@ -1042,8 +1104,8 @@ impl TreeTestbench { ); connect_mode_nodes( &mut acs_subsystem, - request_sender_to_mgt_dev.clone(), - &mut mgt_dev, + request_sender_to_mgt_man.clone(), + &mut mgt_manager, reply_sender_to_acs_subsystem.clone(), ); @@ -1059,6 +1121,7 @@ impl TreeTestbench { &mut mgm_dev_1, reply_sender_to_mgm_assy, ); + connect_mode_nodes( &mut mgt_manager, request_sender_to_mgt_dev, @@ -1081,12 +1144,16 @@ impl TreeTestbench { fn announce_recursively() { let mut tb = TreeTestbench::new(); tb.pus.announce_modes_recursively(); - tb.subsystem.run(); - tb.ctrl.run(); - tb.mgm_assy.run(); - tb.mgm_devs[0].run(); - tb.mgm_devs[1].run(); - tb.mgt_dev.run(); + // Run everything twice so the order does not matter. + for _ in 0..2 { + tb.subsystem.run(); + tb.ctrl.run(); + tb.mgt_manager.run(); + tb.mgm_assy.run(); + tb.mgm_devs[0].run(); + tb.mgm_devs[1].run(); + tb.mgt_dev.run(); + } let mut announces = tb .subsystem .mode_req_handler_mock @@ -1101,17 +1168,27 @@ fn announce_recursively() { assert_eq!(tb.mgm_assy.mode_req_mock.start_transition_calls.len(), 0); assert_eq!(tb.mgm_assy.mode_and_submode(), UNKNOWN_MODE); assert_eq!(announces.len(), 1); - for mgm_dev in &tb.mgm_devs { + for mgm_dev in &mut tb.mgm_devs { + assert_eq!(mgm_dev.get_and_clear_num_mode_msgs(), 1); announces = mgm_dev.mode_req_mock.announce_mode_calls.borrow_mut(); assert_eq!(mgm_dev.mode_req_mock.start_transition_calls.len(), 0); assert_eq!(mgm_dev.mode_and_submode(), UNKNOWN_MODE); assert_eq!(announces.len(), 1); } assert_eq!(announces.len(), 1); + assert_eq!(tb.mgt_dev.get_and_clear_num_mode_msgs(), 1); announces = tb.mgt_dev.mode_req_mock.announce_mode_calls.borrow_mut(); assert_eq!(tb.mgt_dev.mode_req_mock.start_transition_calls.len(), 0); assert_eq!(tb.mgt_dev.mode_and_submode(), UNKNOWN_MODE); assert_eq!(announces.len(), 1); + announces = tb + .mgt_manager + .mode_req_mock + .announce_mode_calls + .borrow_mut(); + assert_eq!(tb.mgt_manager.mode_req_mock.start_transition_calls.len(), 0); + assert_eq!(tb.mgt_manager.mode_and_submode(), UNKNOWN_MODE); + assert_eq!(announces.len(), 1); } #[test]