From d0835f939307e451970373b88e7c8a5a972ce770 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 10 Apr 2024 17:03:56 +0200 Subject: [PATCH] some more fixes and improvements --- src/config.rs | 1 + src/controller.rs | 52 +++++++++++++++++++++++++++++++++++++---------- src/main.rs | 11 +++++++++- src/pus/action.rs | 3 ++- src/pus/stack.rs | 10 +++++++-- 5 files changed, 62 insertions(+), 15 deletions(-) diff --git a/src/config.rs b/src/config.rs index a15a63a..24e4933 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,6 +4,7 @@ use satrs_mib::resultcode; use std::net::Ipv4Addr; pub const STOP_FILE_NAME: &str = "stop-experiment"; +pub const HOME_FOLER_EXPERIMENT: &str = "/home/exp278"; pub const OBSW_SERVER_ADDR: Ipv4Addr = Ipv4Addr::UNSPECIFIED; pub const SERVER_PORT: u16 = 7301; diff --git a/src/controller.rs b/src/controller.rs index 52cb30e..4522bfd 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -1,11 +1,14 @@ -use derive_new::new; use num_enum::TryFromPrimitive; use satrs::{ action::ActionRequest, pus::action::{ActionReplyVariant, PusActionReply}, request::{GenericMessage, MessageMetadata}, }; -use std::sync::{atomic::AtomicBool, mpsc, Arc}; +use std::{ + env::temp_dir, + path::{Path, PathBuf}, + sync::{atomic::AtomicBool, mpsc, Arc}, +}; use ops_sat_rs::config::{action_err::INVALID_ACTION_ID, STOP_FILE_NAME}; @@ -17,11 +20,34 @@ pub enum ActionId { StopExperiment = 1, } -#[derive(new)] pub struct ExperimentController { pub composite_request_rx: mpsc::Receiver>, pub action_reply_tx: mpsc::Sender>, pub stop_signal: Arc, + home_path_stop_file: PathBuf, + tmp_path_stop_file: PathBuf, +} + +impl ExperimentController { + pub fn new( + home_dir: &Path, + composite_request_rx: mpsc::Receiver>, + action_reply_tx: mpsc::Sender>, + stop_signal: Arc, + ) -> Self { + let mut home_path_stop_file = PathBuf::new(); + home_path_stop_file.push(home_dir); + home_path_stop_file.push(STOP_FILE_NAME); + let mut tmp_path_stop_file = temp_dir(); + tmp_path_stop_file.push(STOP_FILE_NAME); + Self { + composite_request_rx, + action_reply_tx, + stop_signal, + home_path_stop_file, + tmp_path_stop_file, + } + } } impl ExperimentController { @@ -78,13 +104,17 @@ impl ExperimentController { } pub fn check_stop_file(&self) { - if std::path::Path::new(STOP_FILE_NAME).exists() { - log::warn!( - "Detected stop file name at {}. Initiating experiment shutdown", - STOP_FILE_NAME - ); - self.stop_signal - .store(true, std::sync::atomic::Ordering::Relaxed); - } + let check_at_path = |path: &Path| { + if path.exists() { + log::warn!( + "Detected stop file name at {}. Initiating experiment shutdown", + STOP_FILE_NAME + ); + self.stop_signal + .store(true, std::sync::atomic::Ordering::Relaxed); + } + }; + check_at_path(self.tmp_path_stop_file.as_path()); + check_at_path(self.home_path_stop_file.as_path()); } } diff --git a/src/main.rs b/src/main.rs index db66e01..c81ce68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ use std::{ + env, net::{IpAddr, SocketAddr}, + path::Path, sync::{atomic::AtomicBool, mpsc, Arc}, thread, time::Duration, @@ -9,7 +11,7 @@ use log::info; use ops_sat_rs::config::{ components::CONTROLLER_ID, tasks::{FREQ_MS_CTRL, FREQ_MS_PUS_STACK}, - EXPERIMENT_APID, + EXPERIMENT_APID, HOME_FOLER_EXPERIMENT, }; use ops_sat_rs::config::{tasks::FREQ_MS_UDP_TMTC, OBSW_SERVER_ADDR, SERVER_PORT}; use satrs::{ @@ -159,7 +161,14 @@ fn main() { stop_signal.clone(), ); + let home_path_default = env::var("HOME").expect("HOME env variable not set"); + let home_path = Path::new(if Path::new(HOME_FOLER_EXPERIMENT).exists() { + HOME_FOLER_EXPERIMENT + } else { + &home_path_default + }); let mut controller = ExperimentController::new( + home_path, controller_composite_rx, pus_action_reply_tx, stop_signal.clone(), diff --git a/src/pus/action.rs b/src/pus/action.rs index 3900796..409f330 100644 --- a/src/pus/action.rs +++ b/src/pus/action.rs @@ -247,7 +247,8 @@ impl TargetedPusService for ActionServiceWrapper { PusPacketHandlerResult::Empty => return HandlingStatus::Empty, }, Err(error) => { - error!("PUS packet handling error: {error:?}") + error!("PUS packet handling error: {error:?}"); + return HandlingStatus::Empty; } } HandlingStatus::HandledOne diff --git a/src/pus/stack.rs b/src/pus/stack.rs index 58a5cd6..0699046 100644 --- a/src/pus/stack.rs +++ b/src/pus/stack.rs @@ -33,7 +33,8 @@ impl PusStack { loop { let mut nothing_to_do = true; let mut is_srv_finished = - |tc_handling_status: HandlingStatus, + |_srv_id: u8, + tc_handling_status: HandlingStatus, reply_handling_status: Option| { if tc_handling_status == HandlingStatus::HandledOne || (reply_handling_status.is_some() @@ -42,10 +43,15 @@ impl PusStack { nothing_to_do = false; } }; - is_srv_finished(self.test_srv.poll_and_handle_next_packet(&time_stamp), None); + is_srv_finished( + 17, + self.test_srv.poll_and_handle_next_packet(&time_stamp), + None, + ); // is_srv_finished(self.schedule_srv.poll_and_handle_next_tc(&time_stamp), None); // is_srv_finished(self.event_srv.poll_and_handle_next_tc(&time_stamp), None); is_srv_finished( + 8, self.action_srv_wrapper.poll_and_handle_next_tc(&time_stamp), Some( self.action_srv_wrapper