use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
use super::{PLUGGED, STOPPED, UNPLUGGED};
#[derive(Debug, Clone)]
pub struct Controller {
pub(super) state: Arc<AtomicUsize>,
}
impl Controller {
pub fn new() -> Self {
Controller {
state: Arc::new(AtomicUsize::new(PLUGGED)),
}
}
pub fn plug(&self) {
self.state.store(PLUGGED, Ordering::Relaxed);
}
pub fn unplug(&self) {
self.state.store(UNPLUGGED, Ordering::Relaxed);
}
pub fn is_plugged(&self) -> bool {
self.state.load(Ordering::Relaxed) == PLUGGED
}
pub fn stop(&self) {
self.state.store(STOPPED, Ordering::Relaxed);
}
pub fn is_stopped(&self) -> bool {
self.state.load(Ordering::Relaxed) == STOPPED
}
}
impl Default for Controller {
fn default() -> Self {
Self::new()
}
}