//! Shared agent state: sysinfo handles and hot JSON cache. use std::sync::atomic::AtomicUsize; use std::sync::Arc; use sysinfo::{Components, Disks, Networks, System}; use tokio::sync::{Mutex, Notify, RwLock}; pub type SharedSystem = Arc>; pub type SharedComponents = Arc>; pub type SharedDisks = Arc>; pub type SharedNetworks = Arc>; #[derive(Clone)] pub struct AppState { // Persistent sysinfo handles pub sys: SharedSystem, // Last serialized JSON snapshot for fast WS responses pub last_json: Arc>, // Adaptive sampling controls pub client_count: Arc, pub wake_sampler: Arc, pub auth_token: Option, // Cached containers (enumerated once; refreshed per tick) pub components: SharedComponents, pub disks: SharedDisks, pub networks: SharedNetworks } impl AppState { #[allow(dead_code)] pub fn new() -> Self { let sys = System::new(); // targeted refreshes per tick let components = Components::new_with_refreshed_list(); // enumerate once let disks = Disks::new_with_refreshed_list(); let networks = Networks::new_with_refreshed_list(); Self { sys: Arc::new(Mutex::new(sys)), components: Arc::new(Mutex::new(components)), disks: Arc::new(Mutex::new(disks)), networks: Arc::new(Mutex::new(networks)), last_json: Arc::new(RwLock::new(String::new())), client_count: Arc::new(AtomicUsize::new(0)), wake_sampler: Arc::new(Notify::new()), auth_token: std::env::var("SOCKTOP_TOKEN").ok().filter(|s| !s.is_empty()) } } }