generic shananigans

This commit is contained in:
Robin Müller 2024-11-29 13:36:53 +01:00
parent db651ca06f
commit 5d8787b7fa
Signed by: muellerr
GPG Key ID: A649FB78196E3849
6 changed files with 454 additions and 205 deletions

View File

@ -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<Option<GenericMessage<ModeRequest>>, GenericTargetedMessagingError>;
}
impl<R: MessageReceiver<ModeRequest>> ModeRequestReceiver
impl<R: MessageReceiverProvider<ModeRequest>> ModeRequestReceiver
for MessageReceiverWithId<ModeRequest, R>
{
fn try_recv_mode_request(
@ -248,7 +250,9 @@ pub trait ModeReplyReceiver {
) -> Result<Option<GenericMessage<ModeReply>>, GenericTargetedMessagingError>;
}
impl<R: MessageReceiver<ModeReply>> ModeReplyReceiver for MessageReceiverWithId<ModeReply, R> {
impl<R: MessageReceiverProvider<ModeReply>> ModeReplyReceiver
for MessageReceiverWithId<ModeReply, R>
{
fn try_recv_mode_reply(
&self,
) -> Result<Option<GenericMessage<ModeReply>>, 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<S: MessageSender<ModeReply>> MessageSenderMap<ModeReply, S> {
impl<S: MessageSenderProvider<ModeReply>> MessageSenderMap<ModeReply, S> {
pub fn send_mode_reply(
&self,
requestor_info: MessageMetadata,
@ -290,8 +295,13 @@ pub mod alloc_mod {
}
}
impl<FROM, S: MessageSender<ModeReply>, R: MessageReceiver<FROM>> ModeReplySender
for MessageSenderAndReceiver<ModeReply, FROM, S, R>
impl<
From,
Sender: MessageSenderProvider<ModeReply>,
Receiver: MessageReceiverProvider<From>,
SenderStore: MessageSenderStoreProvider<ModeReply, Sender>,
> ModeReplySender
for MessageSenderAndReceiver<ModeReply, From, Sender, Receiver, SenderStore>
{
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<TO, S: MessageSender<TO>, R: MessageReceiver<ModeReply>> ModeReplyReceiver
for MessageSenderAndReceiver<TO, ModeReply, S, R>
impl<
To,
Sender: MessageSenderProvider<To>,
Receiver: MessageReceiverProvider<ModeReply>,
SenderStore: MessageSenderStoreProvider<To, Sender>,
> ModeReplyReceiver
for MessageSenderAndReceiver<To, ModeReply, Sender, Receiver, SenderStore>
{
fn try_recv_mode_reply(
&self,
@ -322,10 +337,10 @@ pub mod alloc_mod {
impl<
REQUEST,
S0: MessageSender<REQUEST>,
R0: MessageReceiver<ModeReply>,
S1: MessageSender<ModeReply>,
R1: MessageReceiver<REQUEST>,
S0: MessageSenderProvider<REQUEST>,
R0: MessageReceiverProvider<ModeReply>,
S1: MessageSenderProvider<ModeReply>,
R1: MessageReceiverProvider<REQUEST>,
> RequestAndReplySenderAndReceiver<REQUEST, ModeReply, S0, R0, S1, R1>
{
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<REQUEST>,
R0: MessageReceiver<ModeReply>,
S1: MessageSender<ModeReply>,
R1: MessageReceiver<REQUEST>,
S0: MessageSenderProvider<REQUEST>,
R0: MessageReceiverProvider<ModeReply>,
S1: MessageSenderProvider<ModeReply>,
R1: MessageReceiverProvider<REQUEST>,
> ModeReplySender for RequestAndReplySenderAndReceiver<REQUEST, ModeReply, S0, R0, S1, R1>
{
fn local_channel_id(&self) -> ComponentId {
@ -361,10 +376,10 @@ pub mod alloc_mod {
impl<
REQUEST,
S0: MessageSender<REQUEST>,
R0: MessageReceiver<ModeReply>,
S1: MessageSender<ModeReply>,
R1: MessageReceiver<REQUEST>,
S0: MessageSenderProvider<REQUEST>,
R0: MessageReceiverProvider<ModeReply>,
S1: MessageSenderProvider<ModeReply>,
R1: MessageReceiverProvider<REQUEST>,
> ModeReplyReceiver
for RequestAndReplySenderAndReceiver<REQUEST, ModeReply, S0, R0, S1, R1>
{
@ -376,11 +391,14 @@ pub mod alloc_mod {
}
/// Helper type definition for a mode handler which can handle mode requests.
pub type ModeRequestHandlerInterface<S, R> =
MessageSenderAndReceiver<ModeReply, ModeRequest, S, R>;
pub type ModeRequestHandlerInterface<Sender, Receiver, ReplySenderStore> =
MessageSenderAndReceiver<ModeReply, ModeRequest, Sender, Receiver, ReplySenderStore>;
impl<S: MessageSender<ModeReply>, R: MessageReceiver<ModeRequest>>
ModeRequestHandlerInterface<S, R>
impl<
Sender: MessageSenderProvider<ModeReply>,
Receiver: MessageReceiverProvider<ModeRequest>,
ReplySenderStore: MessageSenderStoreProvider<ModeReply, Sender>,
> ModeRequestHandlerInterface<Sender, Receiver, ReplySenderStore>
{
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<S, R> = MessageSenderAndReceiver<ModeRequest, ModeReply, S, R>;
pub type ModeRequestorInterface<Sender, Receiver, RequestSenderStore> =
MessageSenderAndReceiver<ModeRequest, ModeReply, Sender, Receiver, RequestSenderStore>;
impl<S: MessageSender<ModeRequest>, R: MessageReceiver<ModeReply>> ModeRequestorInterface<S, R> {
impl<
Sender: MessageSenderProvider<ModeRequest>,
Receiver: MessageReceiverProvider<ModeReply>,
RequestSenderStore: MessageSenderStoreProvider<ModeRequest, Sender>,
> ModeRequestorInterface<Sender, Receiver, RequestSenderStore>
{
pub fn try_recv_mode_reply(
&self,
) -> Result<Option<GenericMessage<ModeReply>>, GenericTargetedMessagingError> {
@ -427,7 +451,7 @@ pub mod alloc_mod {
pub type ModeInterface<S0, R0, S1, R1> =
RequestAndReplySenderAndReceiver<ModeRequest, ModeReply, S0, R0, S1, R1>;
impl<S: MessageSender<ModeRequest>> MessageSenderMap<ModeRequest, S> {
impl<S: MessageSenderProvider<ModeRequest>> MessageSenderMap<ModeRequest, S> {
pub fn send_mode_request(
&self,
requestor_info: MessageMetadata,
@ -442,25 +466,13 @@ pub mod alloc_mod {
}
}
/*
impl<S: MessageSender<ModeRequest>> ModeRequestSender for MessageSenderMapWithId<ModeRequest, S> {
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<TO, S: MessageSender<TO>, R: MessageReceiver<ModeRequest>> ModeRequestReceiver
for MessageSenderAndReceiver<TO, ModeRequest, S, R>
impl<
To,
Sender: MessageSenderProvider<To>,
Receiver: MessageReceiverProvider<ModeRequest>,
SenderStore: MessageSenderStoreProvider<To, Sender>,
> ModeRequestReceiver
for MessageSenderAndReceiver<To, ModeRequest, Sender, Receiver, SenderStore>
{
fn try_recv_mode_request(
&self,
@ -469,8 +481,13 @@ pub mod alloc_mod {
}
}
impl<FROM, S: MessageSender<ModeRequest>, R: MessageReceiver<FROM>> ModeRequestSender
for MessageSenderAndReceiver<ModeRequest, FROM, S, R>
impl<
From,
Sender: MessageSenderProvider<ModeRequest>,
Receiver: MessageReceiverProvider<From>,
SenderStore: MessageSenderStoreProvider<ModeRequest, Sender>,
> ModeRequestSender
for MessageSenderAndReceiver<ModeRequest, From, Sender, Receiver, SenderStore>
{
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<ModeRequest>,
R0: MessageReceiver<REPLY>,
S1: MessageSender<REPLY>,
R1: MessageReceiver<ModeRequest>,
S0: MessageSenderProvider<ModeRequest>,
R0: MessageReceiverProvider<REPLY>,
S1: MessageSenderProvider<REPLY>,
R1: MessageReceiverProvider<ModeRequest>,
> RequestAndReplySenderAndReceiver<ModeRequest, REPLY, S0, R0, S1, R1>
{
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<ModeRequest>,
R0: MessageReceiver<REPLY>,
S1: MessageSender<REPLY>,
R1: MessageReceiver<ModeRequest>,
S0: MessageSenderProvider<ModeRequest>,
R0: MessageReceiverProvider<REPLY>,
S1: MessageSenderProvider<REPLY>,
R1: MessageReceiverProvider<ModeRequest>,
> ModeRequestSender
for RequestAndReplySenderAndReceiver<ModeRequest, REPLY, S0, R0, S1, R1>
{
@ -533,10 +550,10 @@ pub mod alloc_mod {
impl<
REPLY,
S0: MessageSender<ModeRequest>,
R0: MessageReceiver<REPLY>,
S1: MessageSender<REPLY>,
R1: MessageReceiver<ModeRequest>,
S0: MessageSenderProvider<ModeRequest>,
R0: MessageReceiverProvider<REPLY>,
S1: MessageSenderProvider<REPLY>,
R1: MessageReceiverProvider<ModeRequest>,
> ModeRequestReceiver
for RequestAndReplySenderAndReceiver<ModeRequest, REPLY, S0, R0, S1, R1>
{
@ -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<GenericMessage<ModeReply>>,
mpsc::Receiver<GenericMessage<ModeRequest>>,
OneMessageSender<ModeReply, mpsc::Sender<GenericMessage<ModeReply>>>,
>;
pub type ModeRequestHandlerMpscBounded = ModeRequestHandlerInterface<
pub type ModeRequestHandlerOneParentMpscBounded = ModeRequestHandlerInterface<
mpsc::SyncSender<GenericMessage<ModeReply>>,
mpsc::Receiver<GenericMessage<ModeRequest>>,
OneMessageSender<ModeReply, mpsc::SyncSender<GenericMessage<ModeReply>>>,
>;
pub type ModeRequestorMpsc = ModeRequestorInterface<
pub type ModeRequestorOneChildMpsc = ModeRequestorInterface<
mpsc::Sender<GenericMessage<ModeRequest>>,
mpsc::Receiver<GenericMessage<ModeReply>>,
OneMessageSender<ModeRequest, mpsc::Sender<GenericMessage<ModeRequest>>>,
>;
pub type ModeRequestorBoundedMpsc = ModeRequestorInterface<
pub type ModeRequestorOneChildBoundedMpsc = ModeRequestorInterface<
mpsc::SyncSender<GenericMessage<ModeRequest>>,
mpsc::Receiver<GenericMessage<ModeReply>>,
OneMessageSender<
ModeRequest,
mpsc::SyncSender<GenericMessage<ModeRequest>>,
>,
>;
pub type ModeRequestorMultiChildListMpsc = ModeRequestorInterface<
mpsc::Sender<GenericMessage<ModeRequest>>,
mpsc::Receiver<GenericMessage<ModeReply>>,
MessageSenderList<ModeRequest, mpsc::Sender<GenericMessage<ModeRequest>>>,
>;
pub type ModeRequestorMultiChildListBoundedMpsc = ModeRequestorInterface<
mpsc::SyncSender<GenericMessage<ModeRequest>>,
mpsc::Receiver<GenericMessage<ModeReply>>,
MessageSenderList<
ModeRequest,
mpsc::SyncSender<GenericMessage<ModeRequest>>,
>,
>;
pub type ModeRequestorAndHandlerMpsc = ModeInterface<
pub type ModeRequestorAndHandlerOneParentMpsc = ModeInterface<
mpsc::Sender<GenericMessage<ModeRequest>>,
mpsc::Receiver<GenericMessage<ModeReply>>,
mpsc::Sender<GenericMessage<ModeReply>>,
mpsc::Receiver<GenericMessage<ModeRequest>>,
>;
pub type ModeRequestorAndHandlerMpscBounded = ModeInterface<
pub type ModeRequestorAndHandlerOneParentMpscBounded = ModeInterface<
mpsc::SyncSender<GenericMessage<ModeRequest>>,
mpsc::Receiver<GenericMessage<ModeReply>>,
mpsc::SyncSender<GenericMessage<ModeReply>>,

View File

@ -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<ModeRequest>;
type Sender: MessageSenderProvider<ModeRequest>;
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<ModeReply>;
type Sender: MessageSenderProvider<ModeReply>;
fn add_mode_parent(&mut self, id: ComponentId, reply_sender: Self::Sender);
}

View File

@ -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<S, R> =
MessageSenderAndReceiver<ActionReplyPus, ActionRequest, S, R>;
pub type ActionRequestHandlerInterface<Sender, Receiver, ReplySenderStore> =
MessageSenderAndReceiver<ActionReplyPus, ActionRequest, Sender, Receiver, ReplySenderStore>;
impl<S: MessageSender<ActionReplyPus>, R: MessageReceiver<ActionRequest>>
ActionRequestHandlerInterface<S, R>
impl<
Sender: MessageSenderProvider<ActionReplyPus>,
Receiver: MessageReceiverProvider<ActionRequest>,
ReplySender: MessageSenderStoreProvider<ActionReplyPus, Sender>,
> ActionRequestHandlerInterface<Sender, Receiver, ReplySender>
{
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<S, R> =
MessageSenderAndReceiver<ActionRequest, ActionReplyPus, S, R>;
pub type ActionRequestorInterface<Sender, Receiver, RequestSenderStore> =
MessageSenderAndReceiver<
ActionRequest,
ActionReplyPus,
Sender,
Receiver,
RequestSenderStore,
>;
impl<S: MessageSender<ActionRequest>, R: MessageReceiver<ActionReplyPus>>
ActionRequestorInterface<S, R>
impl<
Sender: MessageSenderProvider<ActionRequest>,
Receiver: MessageReceiverProvider<ActionReplyPus>,
RequestSenderStore: MessageSenderStoreProvider<ActionRequest, Sender>,
> ActionRequestorInterface<Sender, Receiver, RequestSenderStore>
{
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<ActivePusActionRequestStd>;
pub type ActionRequestHandlerMpsc = ActionRequestHandlerInterface<
pub type ActionRequestHandlerOneSenderMpsc = ActionRequestHandlerInterface<
mpsc::Sender<GenericMessage<ActionReplyPus>>,
mpsc::Receiver<GenericMessage<ActionRequest>>,
OneMessageSender<
GenericMessage<ActionReplyPus>,
mpsc::Sender<GenericMessage<ActionReplyPus>>,
>,
>;
pub type ActionRequestHandlerMpscBounded = ActionRequestHandlerInterface<
pub type ActionRequestHandlerOneSenderMpscBounded = ActionRequestHandlerInterface<
mpsc::SyncSender<GenericMessage<ActionReplyPus>>,
mpsc::Receiver<GenericMessage<ActionRequest>>,
OneMessageSender<
GenericMessage<ActionReplyPus>,
mpsc::SyncSender<GenericMessage<ActionReplyPus>>,
>,
>;
pub type ActionRequestorMpsc = ActionRequestorInterface<
pub type ActionRequestorWithSenderMapMpsc = ActionRequestorInterface<
mpsc::Sender<GenericMessage<ActionRequest>>,
mpsc::Receiver<GenericMessage<ActionReplyPus>>,
MessageSenderMap<
GenericMessage<ActionRequest>,
mpsc::Sender<GenericMessage<ActionRequest>>,
>,
>;
pub type ActionRequestorBoundedMpsc = ActionRequestorInterface<
pub type ActionRequestorWithSenderMapBoundedMpsc = ActionRequestorInterface<
mpsc::SyncSender<GenericMessage<ActionRequest>>,
mpsc::Receiver<GenericMessage<ActionReplyPus>>,
MessageSenderMap<
GenericMessage<ActionRequest>,
mpsc::SyncSender<GenericMessage<ActionRequest>>,
>,
>;
}

View File

@ -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,

View File

@ -140,24 +140,27 @@ impl<Message> GenericMessage<Message> {
}
/// Generic trait for objects which can send targeted messages.
pub trait MessageSender<MSG>: Send {
pub trait MessageSenderProvider<MSG>: Send {
fn send(&self, message: GenericMessage<MSG>) -> Result<(), GenericTargetedMessagingError>;
}
// Generic trait for objects which can receive targeted messages.
pub trait MessageReceiver<MSG> {
pub trait MessageReceiverProvider<MSG> {
fn try_recv(&self) -> Result<Option<GenericMessage<MSG>>, GenericTargetedMessagingError>;
}
pub struct MessageWithSenderIdReceiver<MSG, R: MessageReceiver<MSG>>(pub R, PhantomData<MSG>);
pub struct MessageWithSenderIdReceiver<MSG, R: MessageReceiverProvider<MSG>>(
pub R,
PhantomData<MSG>,
);
impl<MSG, R: MessageReceiver<MSG>> From<R> for MessageWithSenderIdReceiver<MSG, R> {
impl<MSG, R: MessageReceiverProvider<MSG>> From<R> for MessageWithSenderIdReceiver<MSG, R> {
fn from(receiver: R) -> Self {
MessageWithSenderIdReceiver(receiver, PhantomData)
}
}
impl<MSG, R: MessageReceiver<MSG>> MessageWithSenderIdReceiver<MSG, R> {
impl<MSG, R: MessageReceiverProvider<MSG>> MessageWithSenderIdReceiver<MSG, R> {
pub fn try_recv_message(
&self,
) -> Result<Option<GenericMessage<MSG>>, GenericTargetedMessagingError> {
@ -165,12 +168,12 @@ impl<MSG, R: MessageReceiver<MSG>> MessageWithSenderIdReceiver<MSG, R> {
}
}
pub struct MessageReceiverWithId<MSG, R: MessageReceiver<MSG>> {
pub struct MessageReceiverWithId<MSG, R: MessageReceiverProvider<MSG>> {
local_channel_id: ComponentId,
reply_receiver: MessageWithSenderIdReceiver<MSG, R>,
}
impl<MSG, R: MessageReceiver<MSG>> MessageReceiverWithId<MSG, R> {
impl<MSG, R: MessageReceiverProvider<MSG>> MessageReceiverWithId<MSG, R> {
pub fn new(local_channel_id: ComponentId, reply_receiver: R) -> Self {
Self {
local_channel_id,
@ -183,7 +186,7 @@ impl<MSG, R: MessageReceiver<MSG>> MessageReceiverWithId<MSG, R> {
}
}
impl<MSG, R: MessageReceiver<MSG>> MessageReceiverWithId<MSG, R> {
impl<MSG, R: MessageReceiverProvider<MSG>> MessageReceiverWithId<MSG, R> {
pub fn try_recv_message(
&self,
) -> Result<Option<GenericMessage<MSG>>, GenericTargetedMessagingError> {
@ -191,6 +194,17 @@ impl<MSG, R: MessageReceiver<MSG>> MessageReceiverWithId<MSG, R> {
}
}
pub trait MessageSenderStoreProvider<Message, Sender>: 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<MSG, S: MessageSender<MSG>>(
pub HashMap<ComponentId, S>,
pub struct OneMessageSender<Msg, S: MessageSenderProvider<Msg>> {
pub id_and_sender: Option<(ComponentId, S)>,
pub(crate) phantom: PhantomData<Msg>,
}
impl<Msg, S: MessageSenderProvider<Msg>> Default for OneMessageSender<Msg, S> {
fn default() -> Self {
Self {
id_and_sender: Default::default(),
phantom: Default::default(),
}
}
}
impl<Msg, S: MessageSenderProvider<Msg>> MessageSenderStoreProvider<Msg, S>
for OneMessageSender<Msg, S>
{
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<MSG, S: MessageSenderProvider<MSG>>(
pub alloc::vec::Vec<(ComponentId, S)>,
pub(crate) PhantomData<MSG>,
);
impl<MSG, S: MessageSender<MSG>> Default for MessageSenderMap<MSG, S> {
impl<MSG, S: MessageSenderProvider<MSG>> Default for MessageSenderList<MSG, S> {
fn default() -> Self {
Self(Default::default(), PhantomData)
}
}
impl<MSG, S: MessageSender<MSG>> MessageSenderMap<MSG, S> {
pub fn add_message_target(&mut self, target_id: ComponentId, message_sender: S) {
impl<MSG, S: MessageSenderProvider<MSG>> MessageSenderStoreProvider<MSG, S>
for MessageSenderList<MSG, S>
{
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<MSG, S: MessageSenderProvider<MSG>>(
pub HashMap<ComponentId, S>,
pub(crate) PhantomData<MSG>,
);
impl<MSG, S: MessageSenderProvider<MSG>> Default for MessageSenderMap<MSG, S> {
fn default() -> Self {
Self(Default::default(), PhantomData)
}
}
impl<MSG, S: MessageSenderProvider<MSG>> MessageSenderStoreProvider<MSG, S>
for MessageSenderMap<MSG, S>
{
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<TO, FROM, S: MessageSender<TO>, R: MessageReceiver<FROM>> {
pub struct MessageSenderAndReceiver<
To,
From,
Sender: MessageSenderProvider<To>,
Receiver: MessageReceiverProvider<From>,
SenderStore: MessageSenderStoreProvider<To, Sender>,
> {
pub local_channel_id: ComponentId,
pub message_sender_map: MessageSenderMap<TO, S>,
pub message_receiver: MessageWithSenderIdReceiver<FROM, R>,
pub message_sender_store: SenderStore,
pub message_receiver: MessageWithSenderIdReceiver<From, Receiver>,
pub(crate) phantom: PhantomData<(To, Sender)>,
}
impl<TO, FROM, S: MessageSender<TO>, R: MessageReceiver<FROM>>
MessageSenderAndReceiver<TO, FROM, S, R>
impl<
To,
From,
Sender: MessageSenderProvider<To>,
Receiver: MessageReceiverProvider<From>,
SenderStore: MessageSenderStoreProvider<To, Sender>,
> MessageSenderAndReceiver<To, From, Sender, Receiver, SenderStore>
{
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<Option<GenericMessage<FROM>>, GenericTargetedMessagingError> {
) -> Result<Option<GenericMessage<From>>, GenericTargetedMessagingError> {
self.message_receiver.try_recv_message()
}
}
@ -282,10 +385,10 @@ pub mod alloc_mod {
pub struct RequestAndReplySenderAndReceiver<
REQUEST,
REPLY,
S0: MessageSender<REQUEST>,
R0: MessageReceiver<REPLY>,
S1: MessageSender<REPLY>,
R1: MessageReceiver<REQUEST>,
S0: MessageSenderProvider<REQUEST>,
R0: MessageReceiverProvider<REPLY>,
S1: MessageSenderProvider<REPLY>,
R1: MessageReceiverProvider<REQUEST>,
> {
pub local_channel_id: ComponentId,
// These 2 are a functional group.
@ -299,10 +402,10 @@ pub mod alloc_mod {
impl<
REQUEST,
REPLY,
S0: MessageSender<REQUEST>,
R0: MessageReceiver<REPLY>,
S1: MessageSender<REPLY>,
R1: MessageReceiver<REQUEST>,
S0: MessageSenderProvider<REQUEST>,
R0: MessageReceiverProvider<REPLY>,
S1: MessageSenderProvider<REPLY>,
R1: MessageReceiverProvider<REQUEST>,
> RequestAndReplySenderAndReceiver<REQUEST, REPLY, S0, R0, S1, R1>
{
pub fn new(
@ -333,14 +436,14 @@ pub mod std_mod {
use crate::queue::{GenericReceiveError, GenericSendError};
impl<MSG: Send> MessageSender<MSG> for mpsc::Sender<GenericMessage<MSG>> {
impl<MSG: Send> MessageSenderProvider<MSG> for mpsc::Sender<GenericMessage<MSG>> {
fn send(&self, message: GenericMessage<MSG>) -> Result<(), GenericTargetedMessagingError> {
self.send(message)
.map_err(|_| GenericSendError::RxDisconnected)?;
Ok(())
}
}
impl<MSG: Send> MessageSender<MSG> for mpsc::SyncSender<GenericMessage<MSG>> {
impl<MSG: Send> MessageSenderProvider<MSG> for mpsc::SyncSender<GenericMessage<MSG>> {
fn send(&self, message: GenericMessage<MSG>) -> Result<(), GenericTargetedMessagingError> {
if let Err(e) = self.try_send(message) {
return match e {
@ -357,7 +460,7 @@ pub mod std_mod {
pub type MessageSenderMapMpsc<MSG> = MessageReceiverWithId<MSG, mpsc::Sender<MSG>>;
pub type MessageSenderMapBoundedMpsc<MSG> = MessageReceiverWithId<MSG, mpsc::SyncSender<MSG>>;
impl<MSG> MessageReceiver<MSG> for mpsc::Receiver<GenericMessage<MSG>> {
impl<MSG> MessageReceiverProvider<MSG> for mpsc::Receiver<GenericMessage<MSG>> {
fn try_recv(&self) -> Result<Option<GenericMessage<MSG>>, 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};

View File

@ -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<u32>,
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<MessageMetadata>,
pub mode_and_submode: ModeAndSubmode,
pub target_mode_and_submode: Option<ModeAndSubmode>,
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<MessageMetadata>,
pub mode_and_submode: ModeAndSubmode,
pub target_mode_and_submode: Option<ModeAndSubmode>,
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<VecDeque<AnnounceModeInfo>>,
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]