sat-rs/satrs/tests/mode_tree.rs

344 lines
11 KiB
Rust
Raw Normal View History

2024-02-28 11:29:08 +01:00
use core::cell::Cell;
use std::{println, sync::mpsc};
2024-03-21 13:06:46 +01:00
use satrs::action::ActionRequest;
2024-02-28 16:28:27 +01:00
use satrs::mode::{
ModeError, ModeProvider, ModeReplyReceiver, ModeReplySender, ModeRequestHandler,
2024-02-28 18:03:39 +01:00
ModeRequestHandlerMpscBounded, ModeRequestReceiver, ModeRequestorAndHandlerMpscBounded,
ModeRequestorBoundedMpsc,
2024-02-28 16:28:27 +01:00
};
2024-02-28 11:29:08 +01:00
use satrs::request::RequestId;
use satrs::{
mode::{ModeAndSubmode, ModeReply, ModeRequest},
queue::GenericTargetedMessagingError,
2024-02-28 11:29:08 +01:00
request::GenericMessage,
ComponentId,
};
use std::string::{String, ToString};
2024-03-21 17:16:36 +01:00
pub enum TestComponentId {
Device1 = 1,
Device2 = 2,
Assembly = 3,
PusModeService = 4,
}
struct PusModeService {
2024-02-28 11:29:08 +01:00
pub request_id_counter: Cell<u32>,
2024-02-28 17:20:51 +01:00
pub mode_node: ModeRequestorBoundedMpsc,
}
impl PusModeService {
pub fn send_announce_mode_cmd_to_assy(&self) {
self.mode_node
.send_mode_request(
2024-02-28 11:29:08 +01:00
self.request_id_counter.get(),
2024-03-21 17:16:36 +01:00
TestComponentId::Assembly as ComponentId,
ModeRequest::AnnounceModeRecursive,
)
.unwrap();
2024-02-28 11:29:08 +01:00
self.request_id_counter
.replace(self.request_id_counter.get() + 1);
}
}
struct TestDevice {
pub name: String,
2024-02-28 17:20:51 +01:00
pub mode_node: ModeRequestHandlerMpscBounded,
pub mode_and_submode: ModeAndSubmode,
pub mode_requestor_info: Option<(RequestId, ComponentId)>,
2024-03-21 17:16:36 +01:00
// pub action_queue: mpsc::Receiver<GenericMessage<ActionRequest>>,
}
2024-03-21 13:06:46 +01:00
pub struct ModeLeafDeviceHelper {}
2024-03-13 16:06:12 +01:00
impl TestDevice {
pub fn run(&mut self) {
self.check_mode_requests().expect("mode messaging error");
}
pub fn check_mode_requests(&mut self) -> Result<(), GenericTargetedMessagingError> {
2024-02-28 11:29:08 +01:00
if let Some(request) = self.mode_node.try_recv_mode_request()? {
match request.message {
ModeRequest::SetMode(mode_and_submode) => {
2024-02-28 11:29:08 +01:00
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(
2024-02-28 11:29:08 +01:00
request.request_id,
request.sender_id,
ModeReply::ModeReply(self.mode_and_submode),
)
.unwrap(),
2024-02-28 11:29:08 +01:00
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(())
}
}
impl ModeProvider for TestDevice {
fn mode_and_submode(&self) -> ModeAndSubmode {
self.mode_and_submode
}
}
impl ModeRequestHandler for TestDevice {
2024-02-28 11:29:08 +01:00
fn start_transition(
&mut self,
_request_id: RequestId,
_sender_id: ComponentId,
2024-02-28 11:29:08 +01:00
mode_and_submode: ModeAndSubmode,
) -> Result<(), ModeError> {
self.mode_and_submode = mode_and_submode;
self.handle_mode_reached()?;
Ok(())
}
fn announce_mode(&self, _request_id: RequestId, _sender_id: ComponentId, _recursive: bool) {
println!(
"{}: announcing mode: {:?}",
self.name, self.mode_and_submode
);
}
fn handle_mode_reached(&mut self) -> Result<(), GenericTargetedMessagingError> {
2024-02-28 11:29:08 +01:00
let (req_id, sender_id) = self.mode_requestor_info.unwrap();
self.mode_node.send_mode_reply(
2024-02-28 11:29:08 +01:00
req_id,
sender_id,
ModeReply::ModeReply(self.mode_and_submode),
)?;
Ok(())
}
}
struct TestAssembly {
2024-02-28 17:20:51 +01:00
pub mode_node: ModeRequestorAndHandlerMpscBounded,
pub mode_requestor_info: Option<(RequestId, ComponentId)>,
pub mode_and_submode: ModeAndSubmode,
pub target_mode_and_submode: Option<ModeAndSubmode>,
}
impl ModeProvider for TestAssembly {
fn mode_and_submode(&self) -> ModeAndSubmode {
self.mode_and_submode
}
}
impl TestAssembly {
pub fn run(&mut self) {
self.check_mode_requests().expect("mode messaging error");
self.check_mode_replies().expect("mode messaging error");
}
pub fn check_mode_requests(&mut self) -> Result<(), GenericTargetedMessagingError> {
2024-02-28 11:29:08 +01:00
if let Some(request) = self.mode_node.try_recv_mode_request()? {
match request.message {
ModeRequest::SetMode(mode_and_submode) => {
2024-02-28 11:29:08 +01:00
self.start_transition(request.request_id, request.sender_id, mode_and_submode)
.unwrap();
}
2024-02-23 13:59:15 +01:00
ModeRequest::ReadMode => self
.mode_node
.send_mode_reply(
2024-02-28 11:29:08 +01:00
request.request_id,
request.sender_id,
2024-02-23 13:59:15 +01:00
ModeReply::ModeReply(self.mode_and_submode),
)
.unwrap(),
2024-02-28 11:29:08 +01:00
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(())
}
2024-02-22 17:45:49 +01:00
pub fn check_mode_replies(&mut self) -> Result<(), GenericTargetedMessagingError> {
if let Some(reply_and_id) = self.mode_node.try_recv_mode_reply()? {
match reply_and_id.message {
ModeReply::ModeInfo(_) => todo!(),
ModeReply::ModeReply(reply) => {
println!(
"TestAssembly: Received mode reply from {:?}, reached: {:?}",
reply_and_id.sender_id, reply
);
}
ModeReply::CantReachMode(_) => todo!(),
ModeReply::WrongMode { expected, reached } => {
println!(
"TestAssembly: Wrong mode reply from {:?}, reached {:?}, expected {:?}",
reply_and_id.sender_id, reached, expected
);
}
}
}
Ok(())
}
}
impl ModeRequestHandler for TestAssembly {
2024-02-28 11:29:08 +01:00
fn start_transition(
&mut self,
request_id: RequestId,
sender_id: ComponentId,
2024-02-28 11:29:08 +01:00
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, request_id: RequestId, _sender_id: ComponentId, recursive: bool) {
println!(
"TestAssembly: Announcing mode (recursively: {}): {:?}",
recursive, self.mode_and_submode
);
2024-02-28 11:29:08 +01:00
// self.mode_requestor_info = Some((request_id, sender_id));
let mut mode_request = ModeRequest::AnnounceMode;
if recursive {
mode_request = ModeRequest::AnnounceModeRecursive;
}
self.mode_node
.request_sender_map
.0
.iter()
.for_each(|(_, sender)| {
sender
2024-02-28 11:29:08 +01:00
.send(GenericMessage::new(
request_id,
self.mode_node.local_channel_id_generic(),
mode_request,
))
.expect("sending mode request failed");
});
}
fn handle_mode_reached(&mut self) -> Result<(), GenericTargetedMessagingError> {
2024-02-28 11:29:08 +01:00
let (req_id, sender_id) = self.mode_requestor_info.unwrap();
self.mode_node.send_mode_reply(
2024-02-28 11:29:08 +01:00
req_id,
sender_id,
ModeReply::ModeReply(self.mode_and_submode),
)?;
Ok(())
}
}
fn main() {
// All request channel handles.
let (request_sender_to_dev1, request_receiver_dev1) = mpsc::sync_channel(10);
let (request_sender_to_dev2, request_receiver_dev2) = mpsc::sync_channel(10);
let (request_sender_to_assy, request_receiver_assy) = mpsc::sync_channel(10);
// All reply channel handles.
let (reply_sender_to_assy, reply_receiver_assy) = mpsc::sync_channel(10);
let (reply_sender_to_pus, reply_receiver_pus) = mpsc::sync_channel(10);
// Mode requestors and handlers.
2024-02-28 17:20:51 +01:00
let mut mode_node_assy = ModeRequestorAndHandlerMpscBounded::new(
2024-03-21 17:16:36 +01:00
TestComponentId::Assembly as ComponentId,
request_receiver_assy,
reply_receiver_assy,
);
// Mode requestors only.
2024-03-21 17:16:36 +01:00
let mut mode_node_pus = ModeRequestorBoundedMpsc::new(
TestComponentId::PusModeService as ComponentId,
reply_receiver_pus,
);
// Request handlers only.
2024-03-21 17:16:36 +01:00
let mut mode_node_dev1 = ModeRequestHandlerMpscBounded::new(
TestComponentId::Device1 as ComponentId,
request_receiver_dev1,
);
let mut mode_node_dev2 = ModeRequestHandlerMpscBounded::new(
TestComponentId::Device2 as ComponentId,
request_receiver_dev2,
);
// Set up mode request senders first.
mode_node_pus.add_message_target(
2024-03-21 17:16:36 +01:00
TestComponentId::Assembly as ComponentId,
request_sender_to_assy,
);
mode_node_pus.add_message_target(
TestComponentId::Device1 as ComponentId,
request_sender_to_dev1.clone(),
);
mode_node_pus.add_message_target(
2024-03-21 17:16:36 +01:00
TestComponentId::Device2 as ComponentId,
request_sender_to_dev2.clone(),
);
2024-03-21 17:16:36 +01:00
mode_node_assy.add_request_target(
TestComponentId::Device1 as ComponentId,
request_sender_to_dev1,
);
mode_node_assy.add_request_target(
TestComponentId::Device2 as ComponentId,
request_sender_to_dev2,
);
// Set up mode reply senders.
mode_node_dev1.add_message_target(
2024-03-21 17:16:36 +01:00
TestComponentId::Assembly as ComponentId,
reply_sender_to_assy.clone(),
);
mode_node_dev1.add_message_target(
TestComponentId::PusModeService as ComponentId,
reply_sender_to_pus.clone(),
);
mode_node_dev2.add_message_target(
2024-03-21 17:16:36 +01:00
TestComponentId::Assembly as ComponentId,
reply_sender_to_assy,
);
mode_node_dev2.add_message_target(
TestComponentId::PusModeService as ComponentId,
reply_sender_to_pus.clone(),
);
2024-03-21 17:16:36 +01:00
mode_node_assy.add_reply_target(
TestComponentId::PusModeService as ComponentId,
reply_sender_to_pus,
);
let mut device1 = TestDevice {
name: "Test Device 1".to_string(),
mode_node: mode_node_dev1,
2024-02-28 11:29:08 +01:00
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,
2024-02-28 11:29:08 +01:00
mode_requestor_info: None,
mode_and_submode: ModeAndSubmode::new(0, 0),
};
let mut assy = TestAssembly {
mode_node: mode_node_assy,
2024-02-28 11:29:08 +01:00
mode_requestor_info: None,
mode_and_submode: ModeAndSubmode::new(0, 0),
target_mode_and_submode: None,
};
let pus_service = PusModeService {
2024-02-28 11:29:08 +01:00
request_id_counter: Cell::new(0),
mode_node: mode_node_pus,
};
pus_service.send_announce_mode_cmd_to_assy();
assy.run();
device1.run();
device2.run();
assy.run();
}