added new RequestId
Some checks failed
Rust/sat-rs/pipeline/pr-main There was a failure building this commit
Rust/sat-rs/pipeline/head There was a failure building this commit

This commit is contained in:
Robin Müller 2024-02-28 11:29:08 +01:00
parent dca573e8a9
commit 0647abc9c6
Signed by: muellerr
GPG Key ID: A649FB78196E3849
3 changed files with 165 additions and 94 deletions

View File

@ -6,9 +6,9 @@ use crate::{
mode::{Mode, ModeAndSubmode, ModeReply, ModeRequest, Submode}, mode::{Mode, ModeAndSubmode, ModeReply, ModeRequest, Submode},
queue::GenericTargetedMessagingError, queue::GenericTargetedMessagingError,
request::{ request::{
MessageReceiver, MessageReceiverWithId, MessageSender, MessageSenderAndReceiver, GenericMessage, MessageReceiver, MessageReceiverWithId, MessageSender,
MessageSenderMap, MessageSenderMapWithId, MessageWithSenderId, MessageSenderAndReceiver, MessageSenderMap, MessageSenderMapWithId,
RequestAndReplySenderAndReceiver, RequestAndReplySenderAndReceiver, RequestId,
}, },
ChannelId, ChannelId,
}; };
@ -44,6 +44,7 @@ pub trait ModeRequestSender {
fn local_channel_id(&self) -> ChannelId; fn local_channel_id(&self) -> ChannelId;
fn send_mode_request( fn send_mode_request(
&self, &self,
request_id: RequestId,
target_id: ChannelId, target_id: ChannelId,
request: ModeRequest, request: ModeRequest,
) -> Result<(), GenericTargetedMessagingError>; ) -> Result<(), GenericTargetedMessagingError>;
@ -54,6 +55,7 @@ pub trait ModeReplySender {
fn send_mode_reply( fn send_mode_reply(
&self, &self,
request_id: RequestId,
target_id: ChannelId, target_id: ChannelId,
reply: ModeReply, reply: ModeReply,
) -> Result<(), GenericTargetedMessagingError>; ) -> Result<(), GenericTargetedMessagingError>;
@ -62,23 +64,24 @@ pub trait ModeReplySender {
pub trait ModeRequestReceiver { pub trait ModeRequestReceiver {
fn try_recv_mode_request( fn try_recv_mode_request(
&self, &self,
) -> Result<Option<MessageWithSenderId<ModeRequest>>, GenericTargetedMessagingError>; ) -> Result<Option<GenericMessage<ModeRequest>>, GenericTargetedMessagingError>;
} }
pub trait ModeReplyReceiver { pub trait ModeReplyReceiver {
fn try_recv_mode_reply( fn try_recv_mode_reply(
&self, &self,
) -> Result<Option<MessageWithSenderId<ModeReply>>, GenericTargetedMessagingError>; ) -> Result<Option<GenericMessage<ModeReply>>, GenericTargetedMessagingError>;
} }
impl<S: MessageSender<ModeRequest>> MessageSenderMap<ModeRequest, S> { impl<S: MessageSender<ModeRequest>> MessageSenderMap<ModeRequest, S> {
pub fn send_mode_request( pub fn send_mode_request(
&self, &self,
request_id: RequestId,
local_id: ChannelId, local_id: ChannelId,
target_id: ChannelId, target_id: ChannelId,
request: ModeRequest, request: ModeRequest,
) -> Result<(), GenericTargetedMessagingError> { ) -> Result<(), GenericTargetedMessagingError> {
self.send_message(local_id, target_id, request) self.send_message(request_id, local_id, target_id, request)
} }
pub fn add_request_target(&mut self, target_id: ChannelId, request_sender: S) { pub fn add_request_target(&mut self, target_id: ChannelId, request_sender: S) {
@ -89,11 +92,12 @@ impl<S: MessageSender<ModeRequest>> MessageSenderMap<ModeRequest, S> {
impl<S: MessageSender<ModeReply>> MessageSenderMap<ModeReply, S> { impl<S: MessageSender<ModeReply>> MessageSenderMap<ModeReply, S> {
pub fn send_mode_reply( pub fn send_mode_reply(
&self, &self,
request_id: RequestId,
local_id: ChannelId, local_id: ChannelId,
target_id: ChannelId, target_id: ChannelId,
request: ModeReply, request: ModeReply,
) -> Result<(), GenericTargetedMessagingError> { ) -> Result<(), GenericTargetedMessagingError> {
self.send_message(local_id, target_id, request) self.send_message(request_id, local_id, target_id, request)
} }
pub fn add_reply_target(&mut self, target_id: ChannelId, request_sender: S) { pub fn add_reply_target(&mut self, target_id: ChannelId, request_sender: S) {
@ -104,10 +108,11 @@ impl<S: MessageSender<ModeReply>> MessageSenderMap<ModeReply, S> {
impl<S: MessageSender<ModeReply>> ModeReplySender for MessageSenderMapWithId<ModeReply, S> { impl<S: MessageSender<ModeReply>> ModeReplySender for MessageSenderMapWithId<ModeReply, S> {
fn send_mode_reply( fn send_mode_reply(
&self, &self,
request_id: RequestId,
target_channel_id: ChannelId, target_channel_id: ChannelId,
reply: ModeReply, reply: ModeReply,
) -> Result<(), GenericTargetedMessagingError> { ) -> Result<(), GenericTargetedMessagingError> {
self.send_message(target_channel_id, reply) self.send_message(request_id, target_channel_id, reply)
} }
fn local_channel_id(&self) -> ChannelId { fn local_channel_id(&self) -> ChannelId {
@ -122,17 +127,18 @@ impl<S: MessageSender<ModeRequest>> ModeRequestSender for MessageSenderMapWithId
fn send_mode_request( fn send_mode_request(
&self, &self,
request_id: RequestId,
target_id: ChannelId, target_id: ChannelId,
request: ModeRequest, request: ModeRequest,
) -> Result<(), GenericTargetedMessagingError> { ) -> Result<(), GenericTargetedMessagingError> {
self.send_message(target_id, request) self.send_message(request_id, target_id, request)
} }
} }
impl<R: MessageReceiver<ModeReply>> ModeReplyReceiver for MessageReceiverWithId<ModeReply, R> { impl<R: MessageReceiver<ModeReply>> ModeReplyReceiver for MessageReceiverWithId<ModeReply, R> {
fn try_recv_mode_reply( fn try_recv_mode_reply(
&self, &self,
) -> Result<Option<MessageWithSenderId<ModeReply>>, GenericTargetedMessagingError> { ) -> Result<Option<GenericMessage<ModeReply>>, GenericTargetedMessagingError> {
self.try_recv_message() self.try_recv_message()
} }
} }
@ -142,7 +148,7 @@ impl<R: MessageReceiver<ModeRequest>> ModeRequestReceiver
{ {
fn try_recv_mode_request( fn try_recv_mode_request(
&self, &self,
) -> Result<Option<MessageWithSenderId<ModeRequest>>, GenericTargetedMessagingError> { ) -> Result<Option<GenericMessage<ModeRequest>>, GenericTargetedMessagingError> {
self.try_recv_message() self.try_recv_message()
} }
} }
@ -156,11 +162,16 @@ impl<FROM, S: MessageSender<ModeRequest>, R: MessageReceiver<FROM>> ModeRequestS
fn send_mode_request( fn send_mode_request(
&self, &self,
request_id: RequestId,
target_id: ChannelId, target_id: ChannelId,
request: ModeRequest, request: ModeRequest,
) -> Result<(), GenericTargetedMessagingError> { ) -> Result<(), GenericTargetedMessagingError> {
self.message_sender_map self.message_sender_map.send_mode_request(
.send_mode_request(self.local_channel_id(), target_id, request) request_id,
self.local_channel_id(),
target_id,
request,
)
} }
} }
@ -173,11 +184,16 @@ impl<FROM, S: MessageSender<ModeReply>, R: MessageReceiver<FROM>> ModeReplySende
fn send_mode_reply( fn send_mode_reply(
&self, &self,
request_id: RequestId,
target_id: ChannelId, target_id: ChannelId,
request: ModeReply, request: ModeReply,
) -> Result<(), GenericTargetedMessagingError> { ) -> Result<(), GenericTargetedMessagingError> {
self.message_sender_map self.message_sender_map.send_mode_reply(
.send_mode_reply(self.local_channel_id(), target_id, request) request_id,
self.local_channel_id(),
target_id,
request,
)
} }
} }
@ -186,7 +202,7 @@ impl<TO, S: MessageSender<TO>, R: MessageReceiver<ModeReply>> ModeReplyReceiver
{ {
fn try_recv_mode_reply( fn try_recv_mode_reply(
&self, &self,
) -> Result<Option<MessageWithSenderId<ModeReply>>, GenericTargetedMessagingError> { ) -> Result<Option<GenericMessage<ModeReply>>, GenericTargetedMessagingError> {
self.message_receiver self.message_receiver
.try_recv_message(self.local_channel_id_generic()) .try_recv_message(self.local_channel_id_generic())
} }
@ -196,7 +212,7 @@ impl<TO, S: MessageSender<TO>, R: MessageReceiver<ModeRequest>> ModeRequestRecei
{ {
fn try_recv_mode_request( fn try_recv_mode_request(
&self, &self,
) -> Result<Option<MessageWithSenderId<ModeRequest>>, GenericTargetedMessagingError> { ) -> Result<Option<GenericMessage<ModeRequest>>, GenericTargetedMessagingError> {
self.message_receiver self.message_receiver
.try_recv_message(self.local_channel_id_generic()) .try_recv_message(self.local_channel_id_generic())
} }
@ -204,37 +220,37 @@ impl<TO, S: MessageSender<TO>, R: MessageReceiver<ModeRequest>> ModeRequestRecei
pub type ModeRequestHandlerConnector<S, R> = MessageSenderAndReceiver<ModeReply, ModeRequest, S, R>; pub type ModeRequestHandlerConnector<S, R> = MessageSenderAndReceiver<ModeReply, ModeRequest, S, R>;
pub type MpscModeRequestHandlerConnector = ModeRequestHandlerConnector< pub type MpscModeRequestHandlerConnector = ModeRequestHandlerConnector<
mpsc::Sender<MessageWithSenderId<ModeReply>>, mpsc::Sender<GenericMessage<ModeReply>>,
mpsc::Receiver<MessageWithSenderId<ModeRequest>>, mpsc::Receiver<GenericMessage<ModeRequest>>,
>; >;
pub type MpscBoundedModeRequestHandlerConnector = ModeRequestHandlerConnector< pub type MpscBoundedModeRequestHandlerConnector = ModeRequestHandlerConnector<
mpsc::SyncSender<MessageWithSenderId<ModeReply>>, mpsc::SyncSender<GenericMessage<ModeReply>>,
mpsc::Receiver<MessageWithSenderId<ModeRequest>>, mpsc::Receiver<GenericMessage<ModeRequest>>,
>; >;
pub type ModeRequestorConnector<S, R> = MessageSenderAndReceiver<ModeRequest, ModeReply, S, R>; pub type ModeRequestorConnector<S, R> = MessageSenderAndReceiver<ModeRequest, ModeReply, S, R>;
pub type MpscModeRequestorConnector = ModeRequestorConnector< pub type MpscModeRequestorConnector = ModeRequestorConnector<
mpsc::Sender<MessageWithSenderId<ModeRequest>>, mpsc::Sender<GenericMessage<ModeRequest>>,
mpsc::Receiver<MessageWithSenderId<ModeReply>>, mpsc::Receiver<GenericMessage<ModeReply>>,
>; >;
pub type MpscBoundedModeRequestorConnector = ModeRequestorConnector< pub type MpscBoundedModeRequestorConnector = ModeRequestorConnector<
mpsc::SyncSender<MessageWithSenderId<ModeRequest>>, mpsc::SyncSender<GenericMessage<ModeRequest>>,
mpsc::Receiver<MessageWithSenderId<ModeReply>>, mpsc::Receiver<GenericMessage<ModeReply>>,
>; >;
pub type ModeConnector<S0, R0, S1, R1> = pub type ModeConnector<S0, R0, S1, R1> =
RequestAndReplySenderAndReceiver<ModeRequest, ModeReply, S0, R0, S1, R1>; RequestAndReplySenderAndReceiver<ModeRequest, ModeReply, S0, R0, S1, R1>;
pub type MpscModeConnector = ModeConnector< pub type MpscModeConnector = ModeConnector<
mpsc::Sender<MessageWithSenderId<ModeRequest>>, mpsc::Sender<GenericMessage<ModeRequest>>,
mpsc::Receiver<MessageWithSenderId<ModeReply>>, mpsc::Receiver<GenericMessage<ModeReply>>,
mpsc::Sender<MessageWithSenderId<ModeReply>>, mpsc::Sender<GenericMessage<ModeReply>>,
mpsc::Receiver<MessageWithSenderId<ModeRequest>>, mpsc::Receiver<GenericMessage<ModeRequest>>,
>; >;
pub type MpscBoundedModeConnector = ModeConnector< pub type MpscBoundedModeConnector = ModeConnector<
mpsc::SyncSender<MessageWithSenderId<ModeRequest>>, mpsc::SyncSender<GenericMessage<ModeRequest>>,
mpsc::Receiver<MessageWithSenderId<ModeReply>>, mpsc::Receiver<GenericMessage<ModeReply>>,
mpsc::SyncSender<MessageWithSenderId<ModeReply>>, mpsc::SyncSender<GenericMessage<ModeReply>>,
mpsc::Receiver<MessageWithSenderId<ModeRequest>>, mpsc::Receiver<GenericMessage<ModeRequest>>,
>; >;
impl< impl<
@ -279,11 +295,16 @@ impl<
fn send_mode_request( fn send_mode_request(
&self, &self,
request_id: RequestId,
target_id: ChannelId, target_id: ChannelId,
request: ModeRequest, request: ModeRequest,
) -> Result<(), GenericTargetedMessagingError> { ) -> Result<(), GenericTargetedMessagingError> {
self.request_sender_map self.request_sender_map.send_mode_request(
.send_mode_request(self.local_channel_id(), target_id, request) request_id,
self.local_channel_id(),
target_id,
request,
)
} }
} }
@ -301,11 +322,16 @@ impl<
fn send_mode_reply( fn send_mode_reply(
&self, &self,
request_id: RequestId,
target_id: ChannelId, target_id: ChannelId,
request: ModeReply, request: ModeReply,
) -> Result<(), GenericTargetedMessagingError> { ) -> Result<(), GenericTargetedMessagingError> {
self.reply_sender_map self.reply_sender_map.send_mode_reply(
.send_mode_reply(self.local_channel_id(), target_id, request) request_id,
self.local_channel_id(),
target_id,
request,
)
} }
} }
@ -319,7 +345,7 @@ impl<
{ {
fn try_recv_mode_reply( fn try_recv_mode_reply(
&self, &self,
) -> Result<Option<MessageWithSenderId<ModeReply>>, GenericTargetedMessagingError> { ) -> Result<Option<GenericMessage<ModeReply>>, GenericTargetedMessagingError> {
self.reply_receiver self.reply_receiver
.try_recv_message(self.local_channel_id_generic()) .try_recv_message(self.local_channel_id_generic())
} }
@ -335,7 +361,7 @@ impl<
{ {
fn try_recv_mode_request( fn try_recv_mode_request(
&self, &self,
) -> Result<Option<MessageWithSenderId<ModeRequest>>, GenericTargetedMessagingError> { ) -> Result<Option<GenericMessage<ModeRequest>>, GenericTargetedMessagingError> {
self.request_receiver self.request_receiver
.try_recv_message(self.local_channel_id_generic()) .try_recv_message(self.local_channel_id_generic())
} }
@ -357,9 +383,14 @@ impl From<GenericTargetedMessagingError> for ModeError {
} }
pub trait ModeRequestHandler: ModeProvider { pub trait ModeRequestHandler: ModeProvider {
fn start_transition(&mut self, mode_and_submode: ModeAndSubmode) -> Result<(), ModeError>; fn start_transition(
&mut self,
request_id: RequestId,
sender_id: ChannelId,
mode_and_submode: ModeAndSubmode,
) -> Result<(), ModeError>;
fn announce_mode(&self, recursive: bool); fn announce_mode(&self, request_id: RequestId, sender_id: ChannelId, recursive: bool);
fn handle_mode_reached(&mut self) -> Result<(), GenericTargetedMessagingError>; fn handle_mode_reached(&mut self) -> Result<(), GenericTargetedMessagingError>;
} }

View File

@ -118,25 +118,32 @@ impl fmt::Display for TargetAndApidId {
} }
} }
pub struct MessageWithSenderId<MSG> { /// Generic message type which is associated with a sender using a [ChannelId] and associated
/// with a request using a [RequestId].
pub struct GenericMessage<MSG> {
pub sender_id: ChannelId, pub sender_id: ChannelId,
pub request_id: RequestId,
pub message: MSG, pub message: MSG,
} }
impl<MSG> MessageWithSenderId<MSG> { impl<MSG> GenericMessage<MSG> {
pub fn new(sender_id: ChannelId, message: MSG) -> Self { pub fn new(request_id: RequestId, sender_id: ChannelId, message: MSG) -> Self {
Self { sender_id, message } Self {
request_id,
sender_id,
message,
}
} }
} }
/// Generic trait for objects which can send targeted messages. /// Generic trait for objects which can send targeted messages.
pub trait MessageSender<MSG>: Send { pub trait MessageSender<MSG>: Send {
fn send(&self, message: MessageWithSenderId<MSG>) -> Result<(), GenericTargetedMessagingError>; fn send(&self, message: GenericMessage<MSG>) -> Result<(), GenericTargetedMessagingError>;
} }
// Generic trait for objects which can receive targeted messages. // Generic trait for objects which can receive targeted messages.
pub trait MessageReceiver<MSG> { pub trait MessageReceiver<MSG> {
fn try_recv(&self) -> Result<Option<MessageWithSenderId<MSG>>, GenericTargetedMessagingError>; fn try_recv(&self) -> Result<Option<GenericMessage<MSG>>, GenericTargetedMessagingError>;
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
@ -152,23 +159,17 @@ mod std_mod {
ChannelId, ChannelId,
}; };
use super::{MessageReceiver, MessageSender, MessageWithSenderId}; use super::{GenericMessage, MessageReceiver, MessageSender, RequestId};
impl<MSG: Send> MessageSender<MSG> for mpsc::Sender<MessageWithSenderId<MSG>> { impl<MSG: Send> MessageSender<MSG> for mpsc::Sender<GenericMessage<MSG>> {
fn send( fn send(&self, message: GenericMessage<MSG>) -> Result<(), GenericTargetedMessagingError> {
&self,
message: MessageWithSenderId<MSG>,
) -> Result<(), GenericTargetedMessagingError> {
self.send(message) self.send(message)
.map_err(|_| GenericSendError::RxDisconnected)?; .map_err(|_| GenericSendError::RxDisconnected)?;
Ok(()) Ok(())
} }
} }
impl<MSG: Send> MessageSender<MSG> for mpsc::SyncSender<MessageWithSenderId<MSG>> { impl<MSG: Send> MessageSender<MSG> for mpsc::SyncSender<GenericMessage<MSG>> {
fn send( fn send(&self, message: GenericMessage<MSG>) -> Result<(), GenericTargetedMessagingError> {
&self,
message: MessageWithSenderId<MSG>,
) -> Result<(), GenericTargetedMessagingError> {
if let Err(e) = self.try_send(message) { if let Err(e) = self.try_send(message) {
match e { match e {
mpsc::TrySendError::Full(_) => { mpsc::TrySendError::Full(_) => {
@ -202,6 +203,7 @@ mod std_mod {
pub fn send_message( pub fn send_message(
&self, &self,
request_id: RequestId,
local_channel_id: ChannelId, local_channel_id: ChannelId,
target_channel_id: ChannelId, target_channel_id: ChannelId,
message: MSG, message: MSG,
@ -210,7 +212,7 @@ mod std_mod {
self.0 self.0
.get(&target_channel_id) .get(&target_channel_id)
.unwrap() .unwrap()
.send(MessageWithSenderId::new(local_channel_id, message)) .send(GenericMessage::new(request_id, local_channel_id, message))
.map_err(|_| GenericSendError::RxDisconnected)?; .map_err(|_| GenericSendError::RxDisconnected)?;
return Ok(()); return Ok(());
} }
@ -233,11 +235,16 @@ mod std_mod {
pub fn send_message( pub fn send_message(
&self, &self,
request_id: RequestId,
target_channel_id: ChannelId, target_channel_id: ChannelId,
message: MSG, message: MSG,
) -> Result<(), GenericTargetedMessagingError> { ) -> Result<(), GenericTargetedMessagingError> {
self.message_sender_map self.message_sender_map.send_message(
.send_message(self.local_channel_id, target_channel_id, message) request_id,
self.local_channel_id,
target_channel_id,
message,
)
} }
pub fn add_message_target(&mut self, target_id: ChannelId, message_sender: S) { pub fn add_message_target(&mut self, target_id: ChannelId, message_sender: S) {
@ -246,10 +253,8 @@ mod std_mod {
} }
} }
impl<MSG> MessageReceiver<MSG> for mpsc::Receiver<MessageWithSenderId<MSG>> { impl<MSG> MessageReceiver<MSG> for mpsc::Receiver<GenericMessage<MSG>> {
fn try_recv( fn try_recv(&self) -> Result<Option<GenericMessage<MSG>>, GenericTargetedMessagingError> {
&self,
) -> Result<Option<MessageWithSenderId<MSG>>, GenericTargetedMessagingError> {
match self.try_recv() { match self.try_recv() {
Ok(msg) => Ok(Some(msg)), Ok(msg) => Ok(Some(msg)),
Err(e) => match e { Err(e) => match e {
@ -274,7 +279,7 @@ mod std_mod {
pub fn try_recv_message( pub fn try_recv_message(
&self, &self,
_local_id: ChannelId, _local_id: ChannelId,
) -> Result<Option<MessageWithSenderId<MSG>>, GenericTargetedMessagingError> { ) -> Result<Option<GenericMessage<MSG>>, GenericTargetedMessagingError> {
self.0.try_recv() self.0.try_recv()
} }
} }
@ -305,7 +310,7 @@ mod std_mod {
impl<MSG, R: MessageReceiver<MSG>> MessageReceiverWithId<MSG, R> { impl<MSG, R: MessageReceiver<MSG>> MessageReceiverWithId<MSG, R> {
pub fn try_recv_message( pub fn try_recv_message(
&self, &self,
) -> Result<Option<MessageWithSenderId<MSG>>, GenericTargetedMessagingError> { ) -> Result<Option<GenericMessage<MSG>>, GenericTargetedMessagingError> {
self.reply_receiver.0.try_recv() self.reply_receiver.0.try_recv()
} }
} }

View File

@ -1,6 +1,8 @@
use core::cell::Cell;
use std::{println, sync::mpsc}; use std::{println, sync::mpsc};
use satrs::mode_tree::ModeRequestSender; use satrs::mode_tree::ModeRequestSender;
use satrs::request::RequestId;
use satrs::{ use satrs::{
mode::{ModeAndSubmode, ModeReply, ModeRequest}, mode::{ModeAndSubmode, ModeReply, ModeRequest},
mode_tree::{ mode_tree::{
@ -9,7 +11,7 @@ use satrs::{
MpscBoundedModeRequestorConnector, MpscBoundedModeRequestorConnector,
}, },
queue::GenericTargetedMessagingError, queue::GenericTargetedMessagingError,
request::MessageWithSenderId, request::GenericMessage,
ChannelId, ChannelId,
}; };
use std::string::{String, ToString}; use std::string::{String, ToString};
@ -22,6 +24,7 @@ pub enum TestChannelId {
} }
struct PusModeService { struct PusModeService {
pub request_id_counter: Cell<u32>,
pub mode_node: MpscBoundedModeRequestorConnector, pub mode_node: MpscBoundedModeRequestorConnector,
} }
@ -29,10 +32,13 @@ impl PusModeService {
pub fn send_announce_mode_cmd_to_assy(&self) { pub fn send_announce_mode_cmd_to_assy(&self) {
self.mode_node self.mode_node
.send_mode_request( .send_mode_request(
self.request_id_counter.get(),
TestChannelId::Assembly as u32, TestChannelId::Assembly as u32,
ModeRequest::AnnounceModeRecursive, ModeRequest::AnnounceModeRecursive,
) )
.unwrap(); .unwrap();
self.request_id_counter
.replace(self.request_id_counter.get() + 1);
} }
} }
@ -40,7 +46,7 @@ struct TestDevice {
pub name: String, pub name: String,
pub mode_node: MpscBoundedModeRequestHandlerConnector, pub mode_node: MpscBoundedModeRequestHandlerConnector,
pub mode_and_submode: ModeAndSubmode, pub mode_and_submode: ModeAndSubmode,
pub mode_req_commander: Option<ChannelId>, pub mode_requestor_info: Option<(RequestId, ChannelId)>,
} }
impl TestDevice { impl TestDevice {
@ -49,21 +55,27 @@ impl TestDevice {
} }
pub fn check_mode_requests(&mut self) -> Result<(), GenericTargetedMessagingError> { pub fn check_mode_requests(&mut self) -> Result<(), GenericTargetedMessagingError> {
if let Some(request_and_id) = self.mode_node.try_recv_mode_request()? { if let Some(request) = self.mode_node.try_recv_mode_request()? {
match request_and_id.message { match request.message {
ModeRequest::SetMode(mode_and_submode) => { ModeRequest::SetMode(mode_and_submode) => {
self.start_transition(mode_and_submode).unwrap(); self.start_transition(request.request_id, request.sender_id, mode_and_submode)
self.mode_req_commander = Some(request_and_id.sender_id); .unwrap();
self.mode_requestor_info = Some((request.request_id, request.sender_id));
} }
ModeRequest::ReadMode => self ModeRequest::ReadMode => self
.mode_node .mode_node
.send_mode_reply( .send_mode_reply(
request_and_id.sender_id, request.request_id,
request.sender_id,
ModeReply::ModeReply(self.mode_and_submode), ModeReply::ModeReply(self.mode_and_submode),
) )
.unwrap(), .unwrap(),
ModeRequest::AnnounceMode => self.announce_mode(false), ModeRequest::AnnounceMode => {
ModeRequest::AnnounceModeRecursive => self.announce_mode(true), self.announce_mode(request.request_id, request.sender_id, false)
}
ModeRequest::AnnounceModeRecursive => {
self.announce_mode(request.request_id, request.sender_id, true)
}
} }
} }
Ok(()) Ok(())
@ -76,13 +88,18 @@ impl ModeProvider for TestDevice {
} }
} }
impl ModeRequestHandler for TestDevice { impl ModeRequestHandler for TestDevice {
fn start_transition(&mut self, mode_and_submode: ModeAndSubmode) -> Result<(), ModeError> { fn start_transition(
&mut self,
_request_id: RequestId,
_sender_id: ChannelId,
mode_and_submode: ModeAndSubmode,
) -> Result<(), ModeError> {
self.mode_and_submode = mode_and_submode; self.mode_and_submode = mode_and_submode;
self.handle_mode_reached()?; self.handle_mode_reached()?;
Ok(()) Ok(())
} }
fn announce_mode(&self, _recursive: bool) { fn announce_mode(&self, _request_id: RequestId, _sender_id: ChannelId, _recursive: bool) {
println!( println!(
"{}: announcing mode: {:?}", "{}: announcing mode: {:?}",
self.name, self.mode_and_submode self.name, self.mode_and_submode
@ -90,8 +107,10 @@ impl ModeRequestHandler for TestDevice {
} }
fn handle_mode_reached(&mut self) -> Result<(), GenericTargetedMessagingError> { fn handle_mode_reached(&mut self) -> Result<(), GenericTargetedMessagingError> {
let (req_id, sender_id) = self.mode_requestor_info.unwrap();
self.mode_node.send_mode_reply( self.mode_node.send_mode_reply(
self.mode_req_commander.unwrap(), req_id,
sender_id,
ModeReply::ModeReply(self.mode_and_submode), ModeReply::ModeReply(self.mode_and_submode),
)?; )?;
Ok(()) Ok(())
@ -100,7 +119,7 @@ impl ModeRequestHandler for TestDevice {
struct TestAssembly { struct TestAssembly {
pub mode_node: MpscBoundedModeConnector, pub mode_node: MpscBoundedModeConnector,
pub mode_req_commander: Option<ChannelId>, pub mode_requestor_info: Option<(RequestId, ChannelId)>,
pub mode_and_submode: ModeAndSubmode, pub mode_and_submode: ModeAndSubmode,
pub target_mode_and_submode: Option<ModeAndSubmode>, pub target_mode_and_submode: Option<ModeAndSubmode>,
} }
@ -118,21 +137,26 @@ impl TestAssembly {
} }
pub fn check_mode_requests(&mut self) -> Result<(), GenericTargetedMessagingError> { pub fn check_mode_requests(&mut self) -> Result<(), GenericTargetedMessagingError> {
if let Some(request_and_id) = self.mode_node.try_recv_mode_request()? { if let Some(request) = self.mode_node.try_recv_mode_request()? {
match request_and_id.message { match request.message {
ModeRequest::SetMode(mode_and_submode) => { ModeRequest::SetMode(mode_and_submode) => {
self.start_transition(mode_and_submode).unwrap(); self.start_transition(request.request_id, request.sender_id, mode_and_submode)
self.mode_req_commander = Some(request_and_id.sender_id); .unwrap();
} }
ModeRequest::ReadMode => self ModeRequest::ReadMode => self
.mode_node .mode_node
.send_mode_reply( .send_mode_reply(
request_and_id.sender_id, request.request_id,
request.sender_id,
ModeReply::ModeReply(self.mode_and_submode), ModeReply::ModeReply(self.mode_and_submode),
) )
.unwrap(), .unwrap(),
ModeRequest::AnnounceMode => self.announce_mode(false), ModeRequest::AnnounceMode => {
ModeRequest::AnnounceModeRecursive => self.announce_mode(true), self.announce_mode(request.request_id, request.sender_id, false)
}
ModeRequest::AnnounceModeRecursive => {
self.announce_mode(request.request_id, request.sender_id, true)
}
} }
} }
Ok(()) Ok(())
@ -162,16 +186,23 @@ impl TestAssembly {
} }
impl ModeRequestHandler for TestAssembly { impl ModeRequestHandler for TestAssembly {
fn start_transition(&mut self, mode_and_submode: ModeAndSubmode) -> Result<(), ModeError> { fn start_transition(
&mut self,
request_id: RequestId,
sender_id: ChannelId,
mode_and_submode: ModeAndSubmode,
) -> Result<(), ModeError> {
self.mode_requestor_info = Some((request_id, sender_id));
self.target_mode_and_submode = Some(mode_and_submode); self.target_mode_and_submode = Some(mode_and_submode);
Ok(()) Ok(())
} }
fn announce_mode(&self, recursive: bool) { fn announce_mode(&self, request_id: RequestId, _sender_id: ChannelId, recursive: bool) {
println!( println!(
"TestAssembly: Announcing mode (recursively: {}): {:?}", "TestAssembly: Announcing mode (recursively: {}): {:?}",
recursive, self.mode_and_submode recursive, self.mode_and_submode
); );
// self.mode_requestor_info = Some((request_id, sender_id));
let mut mode_request = ModeRequest::AnnounceMode; let mut mode_request = ModeRequest::AnnounceMode;
if recursive { if recursive {
mode_request = ModeRequest::AnnounceModeRecursive; mode_request = ModeRequest::AnnounceModeRecursive;
@ -182,7 +213,8 @@ impl ModeRequestHandler for TestAssembly {
.iter() .iter()
.for_each(|(_, sender)| { .for_each(|(_, sender)| {
sender sender
.send(MessageWithSenderId::new( .send(GenericMessage::new(
request_id,
self.mode_node.local_channel_id_generic(), self.mode_node.local_channel_id_generic(),
mode_request, mode_request,
)) ))
@ -191,8 +223,10 @@ impl ModeRequestHandler for TestAssembly {
} }
fn handle_mode_reached(&mut self) -> Result<(), GenericTargetedMessagingError> { fn handle_mode_reached(&mut self) -> Result<(), GenericTargetedMessagingError> {
let (req_id, sender_id) = self.mode_requestor_info.unwrap();
self.mode_node.send_mode_reply( self.mode_node.send_mode_reply(
self.mode_req_commander.unwrap(), req_id,
sender_id,
ModeReply::ModeReply(self.mode_and_submode), ModeReply::ModeReply(self.mode_and_submode),
)?; )?;
Ok(()) Ok(())
@ -260,22 +294,23 @@ fn main() {
let mut device1 = TestDevice { let mut device1 = TestDevice {
name: "Test Device 1".to_string(), name: "Test Device 1".to_string(),
mode_node: mode_node_dev1, mode_node: mode_node_dev1,
mode_req_commander: None, mode_requestor_info: None,
mode_and_submode: ModeAndSubmode::new(0, 0), mode_and_submode: ModeAndSubmode::new(0, 0),
}; };
let mut device2 = TestDevice { let mut device2 = TestDevice {
name: "Test Device 2".to_string(), name: "Test Device 2".to_string(),
mode_node: mode_node_dev2, mode_node: mode_node_dev2,
mode_req_commander: None, mode_requestor_info: None,
mode_and_submode: ModeAndSubmode::new(0, 0), mode_and_submode: ModeAndSubmode::new(0, 0),
}; };
let mut assy = TestAssembly { let mut assy = TestAssembly {
mode_node: mode_node_assy, mode_node: mode_node_assy,
mode_req_commander: None, mode_requestor_info: None,
mode_and_submode: ModeAndSubmode::new(0, 0), mode_and_submode: ModeAndSubmode::new(0, 0),
target_mode_and_submode: None, target_mode_and_submode: None,
}; };
let pus_service = PusModeService { let pus_service = PusModeService {
request_id_counter: Cell::new(0),
mode_node: mode_node_pus, mode_node: mode_node_pus,
}; };