From 1d92084e6521145f62fac86e534477800e99fe5e Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 10 Apr 2024 17:13:29 +0200 Subject: [PATCH] more improvements --- src/config.rs | 16 ++++++++++++++++ src/controller.rs | 18 +++++++++++++----- src/main.rs | 11 +---------- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/config.rs b/src/config.rs index 24e4933..ef391a7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,10 @@ +use lazy_static::lazy_static; use num_enum::{IntoPrimitive, TryFromPrimitive}; use satrs_mib::res_code::ResultU16Info; use satrs_mib::resultcode; +use std::env; use std::net::Ipv4Addr; +use std::path::{Path, PathBuf}; pub const STOP_FILE_NAME: &str = "stop-experiment"; pub const HOME_FOLER_EXPERIMENT: &str = "/home/exp278"; @@ -26,6 +29,19 @@ pub enum GroupId { Action = 3, } +lazy_static! { + pub static ref HOME_PATH: PathBuf = { + let home_path_default = env::var("HOME").expect("HOME env variable not set"); + let mut home_path = PathBuf::new(); + home_path.push(if Path::new(HOME_FOLER_EXPERIMENT).exists() { + HOME_FOLER_EXPERIMENT + } else { + &home_path_default + }); + home_path + }; +} + pub mod tmtc_err { use super::*; use satrs::res_code::ResultU16; diff --git a/src/controller.rs b/src/controller.rs index 4522bfd..f69d76b 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -10,7 +10,7 @@ use std::{ sync::{atomic::AtomicBool, mpsc, Arc}, }; -use ops_sat_rs::config::{action_err::INVALID_ACTION_ID, STOP_FILE_NAME}; +use ops_sat_rs::config::{action_err::INVALID_ACTION_ID, HOME_PATH, STOP_FILE_NAME}; use crate::requests::CompositeRequest; @@ -30,13 +30,12 @@ pub struct ExperimentController { 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(HOME_PATH.as_path()); home_path_stop_file.push(STOP_FILE_NAME); let mut tmp_path_stop_file = temp_dir(); tmp_path_stop_file.push(STOP_FILE_NAME); @@ -107,9 +106,18 @@ impl ExperimentController { let check_at_path = |path: &Path| { if path.exists() { log::warn!( - "Detected stop file name at {}. Initiating experiment shutdown", - STOP_FILE_NAME + "Detected stop file name at {:?}. Initiating experiment shutdown", + path ); + // By default, clear the stop file. + let result = std::fs::remove_file(path); + if result.is_err() { + log::error!( + "failed to remove stop file at {:?}: {}", + path, + result.unwrap_err() + ); + } self.stop_signal .store(true, std::sync::atomic::Ordering::Relaxed); } diff --git a/src/main.rs b/src/main.rs index c81ce68..db66e01 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,5 @@ use std::{ - env, net::{IpAddr, SocketAddr}, - path::Path, sync::{atomic::AtomicBool, mpsc, Arc}, thread, time::Duration, @@ -11,7 +9,7 @@ use log::info; use ops_sat_rs::config::{ components::CONTROLLER_ID, tasks::{FREQ_MS_CTRL, FREQ_MS_PUS_STACK}, - EXPERIMENT_APID, HOME_FOLER_EXPERIMENT, + EXPERIMENT_APID, }; use ops_sat_rs::config::{tasks::FREQ_MS_UDP_TMTC, OBSW_SERVER_ADDR, SERVER_PORT}; use satrs::{ @@ -161,14 +159,7 @@ 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(),