roowhod: move config normalization to config.rs
This commit is contained in:
+6
-33
@@ -3,6 +3,7 @@ use std::{
|
|||||||
os::fd::{AsRawFd, FromRawFd, OwnedFd},
|
os::fd::{AsRawFd, FromRawFd, OwnedFd},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
@@ -21,9 +22,6 @@ use roowho2_lib::server::{
|
|||||||
varlink_api::varlink_client_server_task,
|
varlink_api::varlink_client_server_task,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Default maximum number of distinct hosts to keep rwhod status records for at the same time.
|
|
||||||
const DEFAULT_MAX_STATUS_ENTRIES: usize = 4096;
|
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(
|
#[command(
|
||||||
author = "Programvareverkstedet <projects@pvv.ntnu.no>",
|
author = "Programvareverkstedet <projects@pvv.ntnu.no>",
|
||||||
@@ -80,19 +78,9 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
let mut join_set = tokio::task::JoinSet::new();
|
let mut join_set = tokio::task::JoinSet::new();
|
||||||
|
|
||||||
let max_status_entries = match config.rwhod.max_status_entries {
|
let whod_status_store: RwhodStatusStore = Arc::new(RwLock::new(RwhodStatusRegistry::new(
|
||||||
Some(0) => {
|
config.rwhod.max_status_entries(),
|
||||||
tracing::warn!(
|
)));
|
||||||
"rwhod.max_status_entries is set to 0, remapping to default value of {}",
|
|
||||||
DEFAULT_MAX_STATUS_ENTRIES
|
|
||||||
);
|
|
||||||
DEFAULT_MAX_STATUS_ENTRIES
|
|
||||||
}
|
|
||||||
Some(max_status_entries) => max_status_entries,
|
|
||||||
None => DEFAULT_MAX_STATUS_ENTRIES,
|
|
||||||
};
|
|
||||||
let whod_status_store: RwhodStatusStore =
|
|
||||||
Arc::new(RwLock::new(RwhodStatusRegistry::new(max_status_entries)));
|
|
||||||
|
|
||||||
let client_server_token = CancellationToken::new();
|
let client_server_token = CancellationToken::new();
|
||||||
let client_server_token_ = client_server_token.clone();
|
let client_server_token_ = client_server_token.clone();
|
||||||
@@ -122,7 +110,7 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
whod_status_store.clone(),
|
whod_status_store.clone(),
|
||||||
rwhod_ignore_list.clone(),
|
rwhod_ignore_list.clone(),
|
||||||
config.rwhod.interfaces.clone(),
|
config.rwhod.interfaces.clone(),
|
||||||
config.rwhod.send_interval_seconds,
|
config.rwhod.send_interval(),
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
tracing::debug!("RWHOD server is disabled in configuration");
|
tracing::debug!("RWHOD server is disabled in configuration");
|
||||||
@@ -154,32 +142,17 @@ async fn ctrl_c_handler() -> anyhow::Result<()> {
|
|||||||
.map_err(|e| anyhow::anyhow!("Failed to listen for Ctrl-C: {}", e))
|
.map_err(|e| anyhow::anyhow!("Failed to listen for Ctrl-C: {}", e))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Default interval, in seconds, between rwhod status packet broadcasts.
|
|
||||||
const DEFAULT_SEND_INTERVAL_SECONDS: u64 = 60;
|
|
||||||
|
|
||||||
async fn rwhod_server(
|
async fn rwhod_server(
|
||||||
socket: UdpSocket,
|
socket: UdpSocket,
|
||||||
whod_status_store: RwhodStatusStore,
|
whod_status_store: RwhodStatusStore,
|
||||||
ignore_list: Option<IgnoreList>,
|
ignore_list: Option<IgnoreList>,
|
||||||
allowed_interfaces: Option<HashSet<String>>,
|
allowed_interfaces: Option<HashSet<String>>,
|
||||||
send_interval_seconds: Option<u64>,
|
send_interval: Duration,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
let socket = Arc::new(socket);
|
let socket = Arc::new(socket);
|
||||||
|
|
||||||
let interfaces =
|
let interfaces =
|
||||||
roowho2_lib::server::rwhod::determine_relevant_interfaces(allowed_interfaces.as_ref())?;
|
roowho2_lib::server::rwhod::determine_relevant_interfaces(allowed_interfaces.as_ref())?;
|
||||||
let send_interval_seconds = match send_interval_seconds {
|
|
||||||
Some(0) => {
|
|
||||||
tracing::warn!(
|
|
||||||
"rwhod.send_interval_seconds is set to 0, remapping to default value of {} seconds",
|
|
||||||
DEFAULT_SEND_INTERVAL_SECONDS
|
|
||||||
);
|
|
||||||
DEFAULT_SEND_INTERVAL_SECONDS
|
|
||||||
}
|
|
||||||
Some(seconds) => seconds,
|
|
||||||
None => DEFAULT_SEND_INTERVAL_SECONDS,
|
|
||||||
};
|
|
||||||
let send_interval = std::time::Duration::from_secs(send_interval_seconds);
|
|
||||||
let sender_task =
|
let sender_task =
|
||||||
rwhod_packet_sender_task(socket.clone(), interfaces, ignore_list, send_interval);
|
rwhod_packet_sender_task(socket.clone(), interfaces, ignore_list, send_interval);
|
||||||
let receiver_task = rwhod_packet_receiver_task(socket.clone(), whod_status_store);
|
let receiver_task = rwhod_packet_receiver_task(socket.clone(), whod_status_store);
|
||||||
|
|||||||
+41
-1
@@ -1,4 +1,4 @@
|
|||||||
use std::{collections::HashSet, path::PathBuf};
|
use std::{collections::HashSet, path::PathBuf, time::Duration};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@@ -6,6 +6,12 @@ pub const DEFAULT_CONFIG_PATH: &str = "/etc/roowho2/config.toml";
|
|||||||
|
|
||||||
pub const DEFAULT_CLIENT_SOCKET_PATH: &str = "/run/roowho2/server_client.sock";
|
pub const DEFAULT_CLIENT_SOCKET_PATH: &str = "/run/roowho2/server_client.sock";
|
||||||
|
|
||||||
|
/// Default interval between rwhod status packet broadcasts.
|
||||||
|
const DEFAULT_SEND_INTERVAL_SECONDS: u64 = 60;
|
||||||
|
|
||||||
|
/// Default maximum number of distinct hosts to keep rwhod status records for at the same time.
|
||||||
|
const DEFAULT_MAX_STATUS_ENTRIES: usize = 4096;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
/// Logging level for the daemon.
|
/// Logging level for the daemon.
|
||||||
@@ -57,6 +63,40 @@ pub struct RwhodConfig {
|
|||||||
pub max_status_entries: Option<usize>,
|
pub max_status_entries: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl RwhodConfig {
|
||||||
|
/// Resolves [`Self::send_interval_seconds`] into a concrete interval.
|
||||||
|
pub fn send_interval(&self) -> Duration {
|
||||||
|
let seconds = match self.send_interval_seconds {
|
||||||
|
Some(0) => {
|
||||||
|
tracing::warn!(
|
||||||
|
"rwhod.send_interval_seconds is set to 0, remapping to default value of {} seconds",
|
||||||
|
DEFAULT_SEND_INTERVAL_SECONDS
|
||||||
|
);
|
||||||
|
DEFAULT_SEND_INTERVAL_SECONDS
|
||||||
|
}
|
||||||
|
Some(seconds) => seconds,
|
||||||
|
None => DEFAULT_SEND_INTERVAL_SECONDS,
|
||||||
|
};
|
||||||
|
|
||||||
|
Duration::from_secs(seconds)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resolves [`Self::max_status_entries`] into a concrete limit.
|
||||||
|
pub fn max_status_entries(&self) -> usize {
|
||||||
|
match self.max_status_entries {
|
||||||
|
Some(0) => {
|
||||||
|
tracing::warn!(
|
||||||
|
"rwhod.max_status_entries is set to 0, remapping to default value of {}",
|
||||||
|
DEFAULT_MAX_STATUS_ENTRIES
|
||||||
|
);
|
||||||
|
DEFAULT_MAX_STATUS_ENTRIES
|
||||||
|
}
|
||||||
|
Some(max_status_entries) => max_status_entries,
|
||||||
|
None => DEFAULT_MAX_STATUS_ENTRIES,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub struct FingerdConfig {
|
pub struct FingerdConfig {
|
||||||
/// Enable or disable the fingerd server functionality.
|
/// Enable or disable the fingerd server functionality.
|
||||||
|
|||||||
Reference in New Issue
Block a user