continue mode tree execution helper

This commit is contained in:
2024-12-09 17:28:28 +01:00
parent d4339f3ea3
commit a42aefff87
6 changed files with 169 additions and 51 deletions

View File

@ -28,6 +28,9 @@ pub struct ModeAndSubmode {
submode: Submode,
}
pub const INVALID_MODE: ModeAndSubmode = ModeAndSubmode::new(u32::MAX, 0);
pub const UNKNOWN_MODE: ModeAndSubmode = ModeAndSubmode::new(u32::MAX - 1, 0);
impl ModeAndSubmode {
pub const RAW_LEN: usize = size_of::<Mode>() + size_of::<Submode>();

View File

@ -105,7 +105,6 @@ impl TargetTableEntry {
name: &'static str,
target_id: ComponentId,
mode_submode: ModeAndSubmode,
monitor_state: bool,
allowed_submode_mask: Option<Submode>,
) -> Self {
Self {
@ -115,7 +114,7 @@ impl TargetTableEntry {
mode_submode,
allowed_submode_mask,
},
monitor_state,
monitor_state: true,
}
}
@ -123,7 +122,6 @@ impl TargetTableEntry {
name: &'static str,
target_id: ComponentId,
mode_submode: ModeAndSubmode,
monitor_state: bool,
) -> Self {
Self {
common: ModeTableEntryCommon {
@ -132,7 +130,7 @@ impl TargetTableEntry {
mode_submode,
allowed_submode_mask: None,
},
monitor_state,
monitor_state: true,
}
}
@ -214,14 +212,17 @@ pub mod alloc_mod {
pub struct TargetTablesMapValue {
/// Name for a given mode table entry.
pub name: &'static str,
/// Optional fallback mode if the target mode can not be kept.
pub fallback_mode: Option<Mode>,
/// These are the rows of the a target table.
pub entries: Vec<TargetTableEntry>,
}
impl TargetTablesMapValue {
pub fn new(name: &'static str) -> Self {
pub fn new(name: &'static str, fallback_mode: Option<Mode>) -> Self {
Self {
name,
fallback_mode,
entries: Default::default(),
}
}
@ -278,24 +279,49 @@ pub mod alloc_mod {
#[derive(Debug, Default)]
pub struct SequenceModeTables(pub HashMap<Mode, SequenceTablesMapValue>);
#[derive(Debug)]
pub struct ModeStoreVecValue {
id: ComponentId,
mode_and_submode: ModeAndSubmode,
pub awaiting_reply: bool,
}
impl ModeStoreVecValue {
pub fn new(id: ComponentId, mode_and_submode: ModeAndSubmode) -> Self {
Self {
id,
mode_and_submode,
awaiting_reply: false,
}
}
pub fn id(&self) -> ComponentId {
self.id
}
pub fn mode_and_submode(&self) -> ModeAndSubmode {
self.mode_and_submode
}
}
#[derive(Debug, Default)]
pub struct ModeStoreVec(pub alloc::vec::Vec<(ComponentId, ModeAndSubmode)>);
pub struct ModeStoreVec(pub alloc::vec::Vec<ModeStoreVecValue>);
#[derive(Debug, Default)]
pub struct ModeStoreMap(pub hashbrown::HashMap<ComponentId, ModeAndSubmode>);
impl ModeStoreProvider for ModeStoreVec {
fn add_component(&mut self, target_id: ComponentId, mode: ModeAndSubmode) {
self.0.push((target_id, mode));
self.0.push(ModeStoreVecValue::new(target_id, mode));
}
fn has_component(&self, target_id: ComponentId) -> bool {
self.0.iter().any(|(id, _)| *id == target_id)
self.0.iter().any(|val| val.id == target_id)
}
fn get_mode(&self, target_id: ComponentId) -> Option<ModeAndSubmode> {
self.0.iter().find_map(|(id, mode)| {
if *id == target_id {
return Some(*mode);
self.0.iter().find_map(|val| {
if val.id == target_id {
return Some(val.mode_and_submode);
}
None
})
@ -306,9 +332,9 @@ pub mod alloc_mod {
target_id: ComponentId,
mode_to_set: ModeAndSubmode,
) {
self.0.iter_mut().for_each(|(id, mode)| {
if *id == target_id {
*mode = mode_to_set;
self.0.iter_mut().for_each(|val| {
if val.id == target_id {
val.mode_and_submode = mode_to_set;
}
});
}

View File

@ -39,7 +39,7 @@ mod tests {
use crate::{
mode::{
ModeAndSubmode, ModeReply, ModeReplySender, ModeRequest, ModeRequestSender,
ModeRequestorAndHandlerOneParentMpsc, ModeRequestorOneChildMpsc,
ModeRequestorAndHandlerMpsc, ModeRequestorOneChildMpsc,
},
request::{GenericMessage, MessageMetadata},
};
@ -90,7 +90,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 = ModeRequestorAndHandlerOneParentMpsc::new(
let mut mode_connector = ModeRequestorAndHandlerMpsc::new(
TEST_COMPONENT_ID_0,
request_receiver_of_connector,
reply_receiver_of_connector,
@ -129,7 +129,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 = ModeRequestorAndHandlerOneParentMpsc::new(
let mut mode_connector = ModeRequestorAndHandlerMpsc::new(
TEST_COMPONENT_ID_0,
request_receiver_of_connector,
reply_receiver_of_connector,

View File

@ -1,6 +1,6 @@
use crate::{
mode::{Mode, ModeAndSubmode, ModeRequest, ModeRequestSender},
mode_tree::{SequenceModeTables, SequenceTableMapTable, SequenceTablesMapValue},
mode_tree::{ModeStoreVec, SequenceModeTables, SequenceTableMapTable, SequenceTablesMapValue},
queue::GenericTargetedMessagingError,
request::RequestId,
ComponentId,
@ -21,6 +21,12 @@ pub trait CheckSuccessProvider {
);
}
#[derive(Debug)]
pub enum TargetKeepingResult {
Ok,
Violated { fallback_mode: Option<Mode> },
}
#[derive(Debug)]
pub enum SequenceHandlerResult {
SequenceDone,
@ -92,6 +98,7 @@ impl SequenceExecutionHelper {
&mut self,
table: &SequenceModeTables,
sender: &impl ModeRequestSender,
mode_store_vec: &mut ModeStoreVec,
) -> Result<SequenceHandlerResult, GenericTargetedMessagingError> {
if self.state == SequenceExecutionHelperStates::AwaitingCheckSuccess {
return Ok(SequenceHandlerResult::AwaitingSuccessCheck);
@ -100,7 +107,12 @@ impl SequenceExecutionHelper {
Some(idx) => {
// Execute the sequence.
let seq_table_value = table.0.get(&self.target_mode).unwrap();
self.execute_sequence_and_map_to_result(seq_table_value, idx, sender)
self.execute_sequence_and_map_to_result(
seq_table_value,
idx,
sender,
mode_store_vec,
)
}
None => {
// Find the first sequence
@ -109,7 +121,12 @@ impl SequenceExecutionHelper {
Ok(SequenceHandlerResult::SequenceDone)
} else {
self.current_sequence_index = Some(0);
self.execute_sequence_and_map_to_result(seq_table_value, 0, sender)
self.execute_sequence_and_map_to_result(
seq_table_value,
0,
sender,
mode_store_vec,
)
}
}
}
@ -120,11 +137,13 @@ impl SequenceExecutionHelper {
seq_table_value: &SequenceTablesMapValue,
sequence_idx: usize,
sender: &impl ModeRequestSender,
mode_store_vec: &mut ModeStoreVec,
) -> Result<SequenceHandlerResult, GenericTargetedMessagingError> {
if Self::execute_sequence(
self.request_id,
&seq_table_value.entries[sequence_idx],
sender,
mode_store_vec,
)? {
self.state = SequenceExecutionHelperStates::AwaitingCheckSuccess;
Ok(SequenceHandlerResult::AwaitingSuccessCheck)
@ -140,6 +159,7 @@ impl SequenceExecutionHelper {
request_id: RequestId,
map_table: &SequenceTableMapTable,
sender: &impl ModeRequestSender,
mode_store_vec: &mut ModeStoreVec,
) -> Result<bool, GenericTargetedMessagingError> {
let mut some_succes_check_required = false;
for entry in &map_table.entries {
@ -148,6 +168,11 @@ impl SequenceExecutionHelper {
entry.common.target_id,
ModeRequest::SetMode(entry.common.mode_submode),
)?;
mode_store_vec.0.iter_mut().for_each(|val| {
if val.id() == entry.common.target_id {
val.awaiting_reply = true;
}
});
if entry.check_success {
some_succes_check_required = true;
}