server port is configurable now
This commit is contained in:
@ -8,6 +8,7 @@ use std::net::Ipv4Addr;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub const STOP_FILE_NAME: &str = "stop-experiment";
|
||||
pub const CONFIG_FILE_NAME: &str = "exp278.toml";
|
||||
pub const HOME_FOLER_EXPERIMENT: &str = "/home/exp278";
|
||||
pub const LOG_FOLDER: &str = "logs";
|
||||
|
||||
@ -51,6 +52,80 @@ lazy_static! {
|
||||
};
|
||||
}
|
||||
|
||||
pub mod cfg_file {
|
||||
use std::{
|
||||
fs::File,
|
||||
io::Read,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use super::{CONFIG_FILE_NAME, HOME_PATH, TCP_SPP_SERVER_PORT};
|
||||
|
||||
pub const SPP_CLIENT_PORT_CFG_KEY: &str = "tcp_spp_server_port";
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct AppCfg {
|
||||
pub tcp_spp_server_port: u16,
|
||||
}
|
||||
|
||||
impl Default for AppCfg {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
tcp_spp_server_port: TCP_SPP_SERVER_PORT,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_app_config() -> AppCfg {
|
||||
let mut cfg_path = HOME_PATH.clone();
|
||||
cfg_path.push(CONFIG_FILE_NAME);
|
||||
let cfg_path_home = cfg_path.as_path();
|
||||
let relevant_path = if Path::new(CONFIG_FILE_NAME).exists() {
|
||||
Some(PathBuf::from(Path::new(CONFIG_FILE_NAME)))
|
||||
} else if cfg_path_home.exists() {
|
||||
Some(PathBuf::from(cfg_path_home))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut app_cfg = AppCfg::default();
|
||||
if relevant_path.is_none() {
|
||||
log::warn!("No config file found, using default values");
|
||||
return app_cfg;
|
||||
}
|
||||
let relevant_path = relevant_path.unwrap();
|
||||
match File::open(relevant_path.as_path()) {
|
||||
Ok(mut file) => {
|
||||
let mut toml_str = String::new();
|
||||
match file.read_to_string(&mut toml_str) {
|
||||
Ok(_size) => match toml_str.parse::<toml::Table>() {
|
||||
Ok(table) => {
|
||||
handle_config_file_table(table, &mut app_cfg);
|
||||
}
|
||||
Err(e) => log::error!("error parsing TOML config file: {e}"),
|
||||
},
|
||||
Err(e) => log::error!("error reading TOML config file: {e}"),
|
||||
}
|
||||
}
|
||||
Err(e) => log::error!("error opening TOML config file: {e}"),
|
||||
}
|
||||
app_cfg
|
||||
}
|
||||
|
||||
#[allow(clippy::collapsible_match)]
|
||||
pub fn handle_config_file_table(table: toml::Table, app_cfg: &mut AppCfg) {
|
||||
if let Some(value) = table.get(SPP_CLIENT_PORT_CFG_KEY) {
|
||||
if let toml::Value::Integer(port) = value {
|
||||
if *port < 0 {
|
||||
log::warn!("invalid port value, is negative");
|
||||
} else {
|
||||
app_cfg.tcp_spp_server_port = *port as u16
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod tmtc_err {
|
||||
use super::*;
|
||||
use satrs::res_code::ResultU16;
|
||||
|
@ -6,7 +6,7 @@ use std::time::Duration;
|
||||
use mio::net::TcpStream;
|
||||
use mio::{Events, Interest, Poll, Token};
|
||||
use ops_sat_rs::config::tasks::STOP_CHECK_FREQUENCY;
|
||||
use ops_sat_rs::config::{SPP_CLIENT_WIRETAPPING_RX, TCP_SPP_SERVER_PORT};
|
||||
use ops_sat_rs::config::SPP_CLIENT_WIRETAPPING_RX;
|
||||
use satrs::encoding::ccsds::parse_buffer_for_ccsds_space_packets;
|
||||
use satrs::queue::GenericSendError;
|
||||
use satrs::spacepackets::PacketId;
|
||||
@ -43,11 +43,11 @@ impl TcpSppClient {
|
||||
tc_source_tx: mpsc::Sender<PacketAsVec>,
|
||||
tm_tcp_client_rx: mpsc::Receiver<PacketAsVec>,
|
||||
valid_ids: &'static [PacketId],
|
||||
port: u16,
|
||||
) -> io::Result<Self> {
|
||||
let mut poll = Poll::new()?;
|
||||
let events = Events::with_capacity(128);
|
||||
let server_addr =
|
||||
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 1, 1)), TCP_SPP_SERVER_PORT);
|
||||
let server_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port);
|
||||
let client = Self::attempt_connection(&mut poll, &server_addr);
|
||||
if client.is_err() {
|
||||
log::warn!(
|
||||
|
@ -7,6 +7,7 @@ use std::{
|
||||
|
||||
use log::info;
|
||||
use ops_sat_rs::config::{
|
||||
cfg_file::create_app_config,
|
||||
components::{CONTROLLER_ID, TCP_SERVER, TCP_SPP_CLIENT, UDP_SERVER},
|
||||
tasks::{FREQ_MS_CTRL, FREQ_MS_PUS_STACK},
|
||||
VALID_PACKET_ID_LIST,
|
||||
@ -42,6 +43,9 @@ fn main() {
|
||||
setup_logger().expect("setting up logging with fern failed");
|
||||
println!("OPS-SAT Rust Experiment OBSW");
|
||||
|
||||
let app_cfg = create_app_config();
|
||||
println!("App Configuration: {:?}", app_cfg);
|
||||
|
||||
let stop_signal = Arc::new(AtomicBool::new(false));
|
||||
|
||||
let (tc_source_tx, tc_source_rx) = mpsc::channel();
|
||||
@ -169,6 +173,7 @@ fn main() {
|
||||
tc_source_tx,
|
||||
tm_tcp_client_rx,
|
||||
VALID_PACKET_ID_LIST,
|
||||
app_cfg.tcp_spp_server_port,
|
||||
)
|
||||
.expect("creating TCP SPP client failed");
|
||||
|
||||
|
Reference in New Issue
Block a user