added new RequestId
This commit is contained in:
parent
dca573e8a9
commit
0647abc9c6
@ -6,9 +6,9 @@ use crate::{
|
||||
mode::{Mode, ModeAndSubmode, ModeReply, ModeRequest, Submode},
|
||||
queue::GenericTargetedMessagingError,
|
||||
request::{
|
||||
MessageReceiver, MessageReceiverWithId, MessageSender, MessageSenderAndReceiver,
|
||||
MessageSenderMap, MessageSenderMapWithId, MessageWithSenderId,
|
||||
RequestAndReplySenderAndReceiver,
|
||||
GenericMessage, MessageReceiver, MessageReceiverWithId, MessageSender,
|
||||
MessageSenderAndReceiver, MessageSenderMap, MessageSenderMapWithId,
|
||||
RequestAndReplySenderAndReceiver, RequestId,
|
||||
},
|
||||
ChannelId,
|
||||
};
|
||||
@ -44,6 +44,7 @@ pub trait ModeRequestSender {
|
||||
fn local_channel_id(&self) -> ChannelId;
|
||||
fn send_mode_request(
|
||||
&self,
|
||||
request_id: RequestId,
|
||||
target_id: ChannelId,
|
||||
request: ModeRequest,
|
||||
) -> Result<(), GenericTargetedMessagingError>;
|
||||
@ -54,6 +55,7 @@ pub trait ModeReplySender {
|
||||
|
||||
fn send_mode_reply(
|
||||
&self,
|
||||
request_id: RequestId,
|
||||
target_id: ChannelId,
|
||||
reply: ModeReply,
|
||||
) -> Result<(), GenericTargetedMessagingError>;
|
||||
@ -62,23 +64,24 @@ pub trait ModeReplySender {
|
||||
pub trait ModeRequestReceiver {
|
||||
fn try_recv_mode_request(
|
||||
&self,
|
||||
) -> Result<Option<MessageWithSenderId<ModeRequest>>, GenericTargetedMessagingError>;
|
||||
) -> Result<Option<GenericMessage<ModeRequest>>, GenericTargetedMessagingError>;
|
||||
}
|
||||
|
||||
pub trait ModeReplyReceiver {
|
||||
fn try_recv_mode_reply(
|
||||
&self,
|
||||
) -> Result<Option<MessageWithSenderId<ModeReply>>, GenericTargetedMessagingError>;
|
||||
) -> Result<Option<GenericMessage<ModeReply>>, GenericTargetedMessagingError>;
|
||||
}
|
||||
|
||||
impl<S: MessageSender<ModeRequest>> MessageSenderMap<ModeRequest, S> {
|
||||
pub fn send_mode_request(
|
||||
&self,
|
||||
request_id: RequestId,
|
||||
local_id: ChannelId,
|
||||
target_id: ChannelId,
|
||||
request: ModeRequest,
|
||||
) -> 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) {
|
||||
@ -89,11 +92,12 @@ impl<S: MessageSender<ModeRequest>> MessageSenderMap<ModeRequest, S> {
|
||||
impl<S: MessageSender<ModeReply>> MessageSenderMap<ModeReply, S> {
|
||||
pub fn send_mode_reply(
|
||||
&self,
|
||||
request_id: RequestId,
|
||||
local_id: ChannelId,
|
||||
target_id: ChannelId,
|
||||
request: ModeReply,
|
||||
) -> 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) {
|
||||
@ -104,10 +108,11 @@ impl<S: MessageSender<ModeReply>> MessageSenderMap<ModeReply, S> {
|
||||
impl<S: MessageSender<ModeReply>> ModeReplySender for MessageSenderMapWithId<ModeReply, S> {
|
||||
fn send_mode_reply(
|
||||
&self,
|
||||
request_id: RequestId,
|
||||
target_channel_id: ChannelId,
|
||||
reply: ModeReply,
|
||||
) -> Result<(), GenericTargetedMessagingError> {
|
||||
self.send_message(target_channel_id, reply)
|
||||
self.send_message(request_id, target_channel_id, reply)
|
||||
}
|
||||
|
||||
fn local_channel_id(&self) -> ChannelId {
|
||||
@ -122,17 +127,18 @@ impl<S: MessageSender<ModeRequest>> ModeRequestSender for MessageSenderMapWithId
|
||||
|
||||
fn send_mode_request(
|
||||
&self,
|
||||
request_id: RequestId,
|
||||
target_id: ChannelId,
|
||||
request: ModeRequest,
|
||||
) -> 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> {
|
||||
fn try_recv_mode_reply(
|
||||
&self,
|
||||
) -> Result<Option<MessageWithSenderId<ModeReply>>, GenericTargetedMessagingError> {
|
||||
) -> Result<Option<GenericMessage<ModeReply>>, GenericTargetedMessagingError> {
|
||||
self.try_recv_message()
|
||||
}
|
||||
}
|
||||
@ -142,7 +148,7 @@ impl<R: MessageReceiver<ModeRequest>> ModeRequestReceiver
|
||||
{
|
||||
fn try_recv_mode_request(
|
||||
&self,
|
||||
) -> Result<Option<MessageWithSenderId<ModeRequest>>, GenericTargetedMessagingError> {
|
||||
) -> Result<Option<GenericMessage<ModeRequest>>, GenericTargetedMessagingError> {
|
||||
self.try_recv_message()
|
||||
}
|
||||
}
|
||||
@ -156,11 +162,16 @@ impl<FROM, S: MessageSender<ModeRequest>, R: MessageReceiver<FROM>> ModeRequestS
|
||||
|
||||
fn send_mode_request(
|
||||
&self,
|
||||
request_id: RequestId,
|
||||
target_id: ChannelId,
|
||||
request: ModeRequest,
|
||||
) -> Result<(), GenericTargetedMessagingError> {
|
||||
self.message_sender_map
|
||||
.send_mode_request(self.local_channel_id(), target_id, request)
|
||||
self.message_sender_map.send_mode_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(
|
||||
&self,
|
||||
request_id: RequestId,
|
||||
target_id: ChannelId,
|
||||
request: ModeReply,
|
||||
) -> Result<(), GenericTargetedMessagingError> {
|
||||
self.message_sender_map
|
||||
.send_mode_reply(self.local_channel_id(), target_id, request)
|
||||
self.message_sender_map.send_mode_reply(
|
||||
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(
|
||||
&self,
|
||||
) -> Result<Option<MessageWithSenderId<ModeReply>>, GenericTargetedMessagingError> {
|
||||
) -> Result<Option<GenericMessage<ModeReply>>, GenericTargetedMessagingError> {
|
||||
self.message_receiver
|
||||
.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(
|
||||
&self,
|
||||
) -> Result<Option<MessageWithSenderId<ModeRequest>>, GenericTargetedMessagingError> {
|
||||
) -> Result<Option<GenericMessage<ModeRequest>>, GenericTargetedMessagingError> {
|
||||
self.message_receiver
|
||||
.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 MpscModeRequestHandlerConnector = ModeRequestHandlerConnector<
|
||||
mpsc::Sender<MessageWithSenderId<ModeReply>>,
|
||||
mpsc::Receiver<MessageWithSenderId<ModeRequest>>,
|
||||
mpsc::Sender<GenericMessage<ModeReply>>,
|
||||
mpsc::Receiver<GenericMessage<ModeRequest>>,
|
||||
>;
|
||||
pub type MpscBoundedModeRequestHandlerConnector = ModeRequestHandlerConnector<
|
||||
mpsc::SyncSender<MessageWithSenderId<ModeReply>>,
|
||||
mpsc::Receiver<MessageWithSenderId<ModeRequest>>,
|
||||
mpsc::SyncSender<GenericMessage<ModeReply>>,
|
||||
mpsc::Receiver<GenericMessage<ModeRequest>>,
|
||||
>;
|
||||
|
||||
pub type ModeRequestorConnector<S, R> = MessageSenderAndReceiver<ModeRequest, ModeReply, S, R>;
|
||||
pub type MpscModeRequestorConnector = ModeRequestorConnector<
|
||||
mpsc::Sender<MessageWithSenderId<ModeRequest>>,
|
||||
mpsc::Receiver<MessageWithSenderId<ModeReply>>,
|
||||
mpsc::Sender<GenericMessage<ModeRequest>>,
|
||||
mpsc::Receiver<GenericMessage<ModeReply>>,
|
||||
>;
|
||||
pub type MpscBoundedModeRequestorConnector = ModeRequestorConnector<
|
||||
mpsc::SyncSender<MessageWithSenderId<ModeRequest>>,
|
||||
mpsc::Receiver<MessageWithSenderId<ModeReply>>,
|
||||
mpsc::SyncSender<GenericMessage<ModeRequest>>,
|
||||
mpsc::Receiver<GenericMessage<ModeReply>>,
|
||||
>;
|
||||
|
||||
pub type ModeConnector<S0, R0, S1, R1> =
|
||||
RequestAndReplySenderAndReceiver<ModeRequest, ModeReply, S0, R0, S1, R1>;
|
||||
pub type MpscModeConnector = ModeConnector<
|
||||
mpsc::Sender<MessageWithSenderId<ModeRequest>>,
|
||||
mpsc::Receiver<MessageWithSenderId<ModeReply>>,
|
||||
mpsc::Sender<MessageWithSenderId<ModeReply>>,
|
||||
mpsc::Receiver<MessageWithSenderId<ModeRequest>>,
|
||||
mpsc::Sender<GenericMessage<ModeRequest>>,
|
||||
mpsc::Receiver<GenericMessage<ModeReply>>,
|
||||
mpsc::Sender<GenericMessage<ModeReply>>,
|
||||
mpsc::Receiver<GenericMessage<ModeRequest>>,
|
||||
>;
|
||||
pub type MpscBoundedModeConnector = ModeConnector<
|
||||
mpsc::SyncSender<MessageWithSenderId<ModeRequest>>,
|
||||
mpsc::Receiver<MessageWithSenderId<ModeReply>>,
|
||||
mpsc::SyncSender<MessageWithSenderId<ModeReply>>,
|
||||
mpsc::Receiver<MessageWithSenderId<ModeRequest>>,
|
||||
mpsc::SyncSender<GenericMessage<ModeRequest>>,
|
||||
mpsc::Receiver<GenericMessage<ModeReply>>,
|
||||
mpsc::SyncSender<GenericMessage<ModeReply>>,
|
||||
mpsc::Receiver<GenericMessage<ModeRequest>>,
|
||||
>;
|
||||
|
||||
impl<
|
||||
@ -279,11 +295,16 @@ impl<
|
||||
|
||||
fn send_mode_request(
|
||||
&self,
|
||||
request_id: RequestId,
|
||||
target_id: ChannelId,
|
||||
request: ModeRequest,
|
||||
) -> Result<(), GenericTargetedMessagingError> {
|
||||
self.request_sender_map
|
||||
.send_mode_request(self.local_channel_id(), target_id, request)
|
||||
self.request_sender_map.send_mode_request(
|
||||
request_id,
|
||||
self.local_channel_id(),
|
||||
target_id,
|
||||
request,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -301,11 +322,16 @@ impl<
|
||||
|
||||
fn send_mode_reply(
|
||||
&self,
|
||||
request_id: RequestId,
|
||||
target_id: ChannelId,
|
||||
request: ModeReply,
|
||||
) -> Result<(), GenericTargetedMessagingError> {
|
||||
self.reply_sender_map
|
||||
.send_mode_reply(self.local_channel_id(), target_id, request)
|
||||
self.reply_sender_map.send_mode_reply(
|
||||
request_id,
|
||||
self.local_channel_id(),
|
||||
target_id,
|
||||
request,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -319,7 +345,7 @@ impl<
|
||||
{
|
||||
fn try_recv_mode_reply(
|
||||
&self,
|
||||
) -> Result<Option<MessageWithSenderId<ModeReply>>, GenericTargetedMessagingError> {
|
||||
) -> Result<Option<GenericMessage<ModeReply>>, GenericTargetedMessagingError> {
|
||||
self.reply_receiver
|
||||
.try_recv_message(self.local_channel_id_generic())
|
||||
}
|
||||
@ -335,7 +361,7 @@ impl<
|
||||
{
|
||||
fn try_recv_mode_request(
|
||||
&self,
|
||||
) -> Result<Option<MessageWithSenderId<ModeRequest>>, GenericTargetedMessagingError> {
|
||||
) -> Result<Option<GenericMessage<ModeRequest>>, GenericTargetedMessagingError> {
|
||||
self.request_receiver
|
||||
.try_recv_message(self.local_channel_id_generic())
|
||||
}
|
||||
@ -357,9 +383,14 @@ impl From<GenericTargetedMessagingError> for ModeError {
|
||||
}
|
||||
|
||||
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>;
|
||||
}
|
||||
|
||||
|
@ -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 request_id: RequestId,
|
||||
pub message: MSG,
|
||||
}
|
||||
|
||||
impl<MSG> MessageWithSenderId<MSG> {
|
||||
pub fn new(sender_id: ChannelId, message: MSG) -> Self {
|
||||
Self { sender_id, message }
|
||||
impl<MSG> GenericMessage<MSG> {
|
||||
pub fn new(request_id: RequestId, sender_id: ChannelId, message: MSG) -> Self {
|
||||
Self {
|
||||
request_id,
|
||||
sender_id,
|
||||
message,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Generic trait for objects which can send targeted messages.
|
||||
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.
|
||||
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")]
|
||||
@ -152,23 +159,17 @@ mod std_mod {
|
||||
ChannelId,
|
||||
};
|
||||
|
||||
use super::{MessageReceiver, MessageSender, MessageWithSenderId};
|
||||
use super::{GenericMessage, MessageReceiver, MessageSender, RequestId};
|
||||
|
||||
impl<MSG: Send> MessageSender<MSG> for mpsc::Sender<MessageWithSenderId<MSG>> {
|
||||
fn send(
|
||||
&self,
|
||||
message: MessageWithSenderId<MSG>,
|
||||
) -> Result<(), GenericTargetedMessagingError> {
|
||||
impl<MSG: Send> MessageSender<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<MessageWithSenderId<MSG>> {
|
||||
fn send(
|
||||
&self,
|
||||
message: MessageWithSenderId<MSG>,
|
||||
) -> Result<(), GenericTargetedMessagingError> {
|
||||
impl<MSG: Send> MessageSender<MSG> for mpsc::SyncSender<GenericMessage<MSG>> {
|
||||
fn send(&self, message: GenericMessage<MSG>) -> Result<(), GenericTargetedMessagingError> {
|
||||
if let Err(e) = self.try_send(message) {
|
||||
match e {
|
||||
mpsc::TrySendError::Full(_) => {
|
||||
@ -202,6 +203,7 @@ mod std_mod {
|
||||
|
||||
pub fn send_message(
|
||||
&self,
|
||||
request_id: RequestId,
|
||||
local_channel_id: ChannelId,
|
||||
target_channel_id: ChannelId,
|
||||
message: MSG,
|
||||
@ -210,7 +212,7 @@ mod std_mod {
|
||||
self.0
|
||||
.get(&target_channel_id)
|
||||
.unwrap()
|
||||
.send(MessageWithSenderId::new(local_channel_id, message))
|
||||
.send(GenericMessage::new(request_id, local_channel_id, message))
|
||||
.map_err(|_| GenericSendError::RxDisconnected)?;
|
||||
return Ok(());
|
||||
}
|
||||
@ -233,11 +235,16 @@ mod std_mod {
|
||||
|
||||
pub fn send_message(
|
||||
&self,
|
||||
request_id: RequestId,
|
||||
target_channel_id: ChannelId,
|
||||
message: MSG,
|
||||
) -> Result<(), GenericTargetedMessagingError> {
|
||||
self.message_sender_map
|
||||
.send_message(self.local_channel_id, target_channel_id, message)
|
||||
self.message_sender_map.send_message(
|
||||
request_id,
|
||||
self.local_channel_id,
|
||||
target_channel_id,
|
||||
message,
|
||||
)
|
||||
}
|
||||
|
||||
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>> {
|
||||
fn try_recv(
|
||||
&self,
|
||||
) -> Result<Option<MessageWithSenderId<MSG>>, GenericTargetedMessagingError> {
|
||||
impl<MSG> MessageReceiver<MSG> for mpsc::Receiver<GenericMessage<MSG>> {
|
||||
fn try_recv(&self) -> Result<Option<GenericMessage<MSG>>, GenericTargetedMessagingError> {
|
||||
match self.try_recv() {
|
||||
Ok(msg) => Ok(Some(msg)),
|
||||
Err(e) => match e {
|
||||
@ -274,7 +279,7 @@ mod std_mod {
|
||||
pub fn try_recv_message(
|
||||
&self,
|
||||
_local_id: ChannelId,
|
||||
) -> Result<Option<MessageWithSenderId<MSG>>, GenericTargetedMessagingError> {
|
||||
) -> Result<Option<GenericMessage<MSG>>, GenericTargetedMessagingError> {
|
||||
self.0.try_recv()
|
||||
}
|
||||
}
|
||||
@ -305,7 +310,7 @@ mod std_mod {
|
||||
impl<MSG, R: MessageReceiver<MSG>> MessageReceiverWithId<MSG, R> {
|
||||
pub fn try_recv_message(
|
||||
&self,
|
||||
) -> Result<Option<MessageWithSenderId<MSG>>, GenericTargetedMessagingError> {
|
||||
) -> Result<Option<GenericMessage<MSG>>, GenericTargetedMessagingError> {
|
||||
self.reply_receiver.0.try_recv()
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
use core::cell::Cell;
|
||||
use std::{println, sync::mpsc};
|
||||
|
||||
use satrs::mode_tree::ModeRequestSender;
|
||||
use satrs::request::RequestId;
|
||||
use satrs::{
|
||||
mode::{ModeAndSubmode, ModeReply, ModeRequest},
|
||||
mode_tree::{
|
||||
@ -9,7 +11,7 @@ use satrs::{
|
||||
MpscBoundedModeRequestorConnector,
|
||||
},
|
||||
queue::GenericTargetedMessagingError,
|
||||
request::MessageWithSenderId,
|
||||
request::GenericMessage,
|
||||
ChannelId,
|
||||
};
|
||||
use std::string::{String, ToString};
|
||||
@ -22,6 +24,7 @@ pub enum TestChannelId {
|
||||
}
|
||||
|
||||
struct PusModeService {
|
||||
pub request_id_counter: Cell<u32>,
|
||||
pub mode_node: MpscBoundedModeRequestorConnector,
|
||||
}
|
||||
|
||||
@ -29,10 +32,13 @@ impl PusModeService {
|
||||
pub fn send_announce_mode_cmd_to_assy(&self) {
|
||||
self.mode_node
|
||||
.send_mode_request(
|
||||
self.request_id_counter.get(),
|
||||
TestChannelId::Assembly as u32,
|
||||
ModeRequest::AnnounceModeRecursive,
|
||||
)
|
||||
.unwrap();
|
||||
self.request_id_counter
|
||||
.replace(self.request_id_counter.get() + 1);
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,7 +46,7 @@ struct TestDevice {
|
||||
pub name: String,
|
||||
pub mode_node: MpscBoundedModeRequestHandlerConnector,
|
||||
pub mode_and_submode: ModeAndSubmode,
|
||||
pub mode_req_commander: Option<ChannelId>,
|
||||
pub mode_requestor_info: Option<(RequestId, ChannelId)>,
|
||||
}
|
||||
|
||||
impl TestDevice {
|
||||
@ -49,21 +55,27 @@ impl TestDevice {
|
||||
}
|
||||
|
||||
pub fn check_mode_requests(&mut self) -> Result<(), GenericTargetedMessagingError> {
|
||||
if let Some(request_and_id) = self.mode_node.try_recv_mode_request()? {
|
||||
match request_and_id.message {
|
||||
if let Some(request) = self.mode_node.try_recv_mode_request()? {
|
||||
match request.message {
|
||||
ModeRequest::SetMode(mode_and_submode) => {
|
||||
self.start_transition(mode_and_submode).unwrap();
|
||||
self.mode_req_commander = Some(request_and_id.sender_id);
|
||||
self.start_transition(request.request_id, request.sender_id, mode_and_submode)
|
||||
.unwrap();
|
||||
self.mode_requestor_info = Some((request.request_id, request.sender_id));
|
||||
}
|
||||
ModeRequest::ReadMode => self
|
||||
.mode_node
|
||||
.send_mode_reply(
|
||||
request_and_id.sender_id,
|
||||
request.request_id,
|
||||
request.sender_id,
|
||||
ModeReply::ModeReply(self.mode_and_submode),
|
||||
)
|
||||
.unwrap(),
|
||||
ModeRequest::AnnounceMode => self.announce_mode(false),
|
||||
ModeRequest::AnnounceModeRecursive => self.announce_mode(true),
|
||||
ModeRequest::AnnounceMode => {
|
||||
self.announce_mode(request.request_id, request.sender_id, false)
|
||||
}
|
||||
ModeRequest::AnnounceModeRecursive => {
|
||||
self.announce_mode(request.request_id, request.sender_id, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@ -76,13 +88,18 @@ impl ModeProvider 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.handle_mode_reached()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn announce_mode(&self, _recursive: bool) {
|
||||
fn announce_mode(&self, _request_id: RequestId, _sender_id: ChannelId, _recursive: bool) {
|
||||
println!(
|
||||
"{}: announcing mode: {:?}",
|
||||
self.name, self.mode_and_submode
|
||||
@ -90,8 +107,10 @@ impl ModeRequestHandler for TestDevice {
|
||||
}
|
||||
|
||||
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_req_commander.unwrap(),
|
||||
req_id,
|
||||
sender_id,
|
||||
ModeReply::ModeReply(self.mode_and_submode),
|
||||
)?;
|
||||
Ok(())
|
||||
@ -100,7 +119,7 @@ impl ModeRequestHandler for TestDevice {
|
||||
|
||||
struct TestAssembly {
|
||||
pub mode_node: MpscBoundedModeConnector,
|
||||
pub mode_req_commander: Option<ChannelId>,
|
||||
pub mode_requestor_info: Option<(RequestId, ChannelId)>,
|
||||
pub mode_and_submode: ModeAndSubmode,
|
||||
pub target_mode_and_submode: Option<ModeAndSubmode>,
|
||||
}
|
||||
@ -118,21 +137,26 @@ impl TestAssembly {
|
||||
}
|
||||
|
||||
pub fn check_mode_requests(&mut self) -> Result<(), GenericTargetedMessagingError> {
|
||||
if let Some(request_and_id) = self.mode_node.try_recv_mode_request()? {
|
||||
match request_and_id.message {
|
||||
if let Some(request) = self.mode_node.try_recv_mode_request()? {
|
||||
match request.message {
|
||||
ModeRequest::SetMode(mode_and_submode) => {
|
||||
self.start_transition(mode_and_submode).unwrap();
|
||||
self.mode_req_commander = Some(request_and_id.sender_id);
|
||||
self.start_transition(request.request_id, request.sender_id, mode_and_submode)
|
||||
.unwrap();
|
||||
}
|
||||
ModeRequest::ReadMode => self
|
||||
.mode_node
|
||||
.send_mode_reply(
|
||||
request_and_id.sender_id,
|
||||
request.request_id,
|
||||
request.sender_id,
|
||||
ModeReply::ModeReply(self.mode_and_submode),
|
||||
)
|
||||
.unwrap(),
|
||||
ModeRequest::AnnounceMode => self.announce_mode(false),
|
||||
ModeRequest::AnnounceModeRecursive => self.announce_mode(true),
|
||||
ModeRequest::AnnounceMode => {
|
||||
self.announce_mode(request.request_id, request.sender_id, false)
|
||||
}
|
||||
ModeRequest::AnnounceModeRecursive => {
|
||||
self.announce_mode(request.request_id, request.sender_id, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@ -162,16 +186,23 @@ impl 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);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn announce_mode(&self, recursive: bool) {
|
||||
fn announce_mode(&self, request_id: RequestId, _sender_id: ChannelId, recursive: bool) {
|
||||
println!(
|
||||
"TestAssembly: Announcing mode (recursively: {}): {:?}",
|
||||
recursive, self.mode_and_submode
|
||||
);
|
||||
// self.mode_requestor_info = Some((request_id, sender_id));
|
||||
let mut mode_request = ModeRequest::AnnounceMode;
|
||||
if recursive {
|
||||
mode_request = ModeRequest::AnnounceModeRecursive;
|
||||
@ -182,7 +213,8 @@ impl ModeRequestHandler for TestAssembly {
|
||||
.iter()
|
||||
.for_each(|(_, sender)| {
|
||||
sender
|
||||
.send(MessageWithSenderId::new(
|
||||
.send(GenericMessage::new(
|
||||
request_id,
|
||||
self.mode_node.local_channel_id_generic(),
|
||||
mode_request,
|
||||
))
|
||||
@ -191,8 +223,10 @@ impl ModeRequestHandler for TestAssembly {
|
||||
}
|
||||
|
||||
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_req_commander.unwrap(),
|
||||
req_id,
|
||||
sender_id,
|
||||
ModeReply::ModeReply(self.mode_and_submode),
|
||||
)?;
|
||||
Ok(())
|
||||
@ -260,22 +294,23 @@ fn main() {
|
||||
let mut device1 = TestDevice {
|
||||
name: "Test Device 1".to_string(),
|
||||
mode_node: mode_node_dev1,
|
||||
mode_req_commander: None,
|
||||
mode_requestor_info: None,
|
||||
mode_and_submode: ModeAndSubmode::new(0, 0),
|
||||
};
|
||||
let mut device2 = TestDevice {
|
||||
name: "Test Device 2".to_string(),
|
||||
mode_node: mode_node_dev2,
|
||||
mode_req_commander: None,
|
||||
mode_requestor_info: None,
|
||||
mode_and_submode: ModeAndSubmode::new(0, 0),
|
||||
};
|
||||
let mut assy = TestAssembly {
|
||||
mode_node: mode_node_assy,
|
||||
mode_req_commander: None,
|
||||
mode_requestor_info: None,
|
||||
mode_and_submode: ModeAndSubmode::new(0, 0),
|
||||
target_mode_and_submode: None,
|
||||
};
|
||||
let pus_service = PusModeService {
|
||||
request_id_counter: Cell::new(0),
|
||||
mode_node: mode_node_pus,
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user