use std::sync::Arc;
use modem_device::Netdev;
use crate::plane::SduPlane;
#[non_exhaustive]
pub enum Plane {
Sdu(Arc<dyn SduPlane>),
Netdev(Arc<dyn Netdev>),
}
pub struct Node {
name: String,
plane: Plane,
_held: Option<Box<dyn std::any::Any + Send + Sync>>,
file_keys: std::collections::BTreeSet<String>,
writable: std::collections::BTreeSet<String>,
actions: std::collections::BTreeSet<String>,
action_intervals: std::collections::BTreeMap<String, std::time::Duration>,
}
impl Node {
#[must_use]
pub fn sdu(plane: Arc<dyn SduPlane>) -> Self {
Self {
name: plane.device().to_string(),
plane: Plane::Sdu(plane),
_held: None,
file_keys: std::collections::BTreeSet::new(),
writable: std::collections::BTreeSet::new(),
actions: std::collections::BTreeSet::new(),
action_intervals: std::collections::BTreeMap::new(),
}
}
#[must_use]
pub fn netdev(name: impl Into<String>, dev: Arc<dyn Netdev>) -> Self {
Self {
name: name.into(),
plane: Plane::Netdev(dev),
_held: None,
file_keys: std::collections::BTreeSet::new(),
writable: std::collections::BTreeSet::new(),
actions: std::collections::BTreeSet::new(),
action_intervals: std::collections::BTreeMap::new(),
}
}
#[must_use]
pub fn with_file_keys(mut self, keys: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.file_keys = keys.into_iter().map(Into::into).collect();
self
}
#[must_use]
pub fn file_keys(&self) -> &std::collections::BTreeSet<String> {
&self.file_keys
}
#[must_use]
pub fn with_writable(mut self, groups: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.writable = groups.into_iter().map(Into::into).collect();
self
}
#[must_use]
pub fn writable(&self) -> &std::collections::BTreeSet<String> {
&self.writable
}
#[must_use]
pub fn with_actions(
mut self,
allow: impl IntoIterator<Item = impl Into<String>>,
intervals: impl IntoIterator<Item = (impl Into<String>, std::time::Duration)>,
) -> Self {
self.actions = allow.into_iter().map(Into::into).collect();
self.action_intervals = intervals.into_iter().map(|(k, v)| (k.into(), v)).collect();
self
}
#[must_use]
pub fn actions(&self) -> &std::collections::BTreeSet<String> {
&self.actions
}
#[must_use]
pub fn action_intervals(&self) -> &std::collections::BTreeMap<String, std::time::Duration> {
&self.action_intervals
}
#[must_use]
pub fn holding(mut self, held: impl std::any::Any + Send + Sync) -> Self {
self._held = Some(Box::new(held));
self
}
pub fn name(&self) -> &str {
&self.name
}
pub fn plane(&self) -> &Plane {
&self.plane
}
pub fn sdu_plane(&self) -> Option<&Arc<dyn SduPlane>> {
match &self.plane {
Plane::Sdu(p) => Some(p),
Plane::Netdev(_) => None,
}
}
}
pub struct Host {
nodes: Vec<Node>,
started: std::time::Instant,
boot_id: String,
version: String,
}
impl Default for Host {
fn default() -> Self {
Self::new()
}
}
impl Host {
#[must_use]
pub fn new() -> Self {
Self {
nodes: Vec::new(),
started: std::time::Instant::now(),
boot_id: crate::clock::boot_id(),
version: env!("CARGO_PKG_VERSION").to_string(),
}
}
#[must_use]
pub fn with_version(mut self, version: impl Into<String>) -> Self {
self.version = version.into();
self
}
#[must_use]
pub fn version(&self) -> &str {
&self.version
}
#[must_use]
pub fn uptime_s(&self) -> u64 {
self.started.elapsed().as_secs()
}
#[must_use]
pub fn boot_id(&self) -> &str {
&self.boot_id
}
pub fn push(&mut self, node: Node) {
assert!(
self.node(node.name()).is_none(),
"device {:?} added twice",
node.name()
);
self.nodes.push(node);
}
#[must_use]
pub fn with(mut self, node: Node) -> Self {
self.push(node);
self
}
pub fn nodes(&self) -> &[Node] {
&self.nodes
}
pub fn node(&self, name: &str) -> Option<&Node> {
self.nodes.iter().find(|n| n.name == name)
}
pub fn sdu_planes(&self) -> impl Iterator<Item = &Arc<dyn SduPlane>> {
self.nodes.iter().filter_map(Node::sdu_plane)
}
pub async fn shutdown(&self) {
for node in &self.nodes {
match &node.plane {
Plane::Sdu(plane) => plane.shutdown().await,
Plane::Netdev(dev) => {
if let Err(e) = dev.close().await {
tracing::warn!(device = %node.name, error = %e, "the netdev did not close cleanly");
}
}
}
}
}
}