use std::sync::{Arc, Mutex};
pub type EventHandler = Arc<dyn Fn(ProtocolEvent) + Send + Sync + 'static>;
pub struct EventBus {
handlers: Arc<Mutex<Vec<(EventTopic, EventHandler)>>>,
}
impl EventBus {
pub fn new(_capacity: usize) -> Self {
EventBus {
handlers: Arc::new(Mutex::new(Vec::new())),
}
}
pub fn subscribe(
&self,
topic: EventTopic,
handler: impl Fn(ProtocolEvent) + Send + Sync + 'static,
) {
self.handlers
.lock()
.unwrap()
.push((topic, Arc::new(handler)));
}
pub fn publish(&self, event: ProtocolEvent) {
let topic = event.topic();
let handlers: Vec<EventHandler> = {
let handlers = self.handlers.lock().unwrap();
handlers
.iter()
.filter(|(t, _)| *t == topic)
.map(|(_, h)| Arc::clone(h))
.collect()
};
for handler in handlers {
handler(event.clone());
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventTopic {
Connection,
Error,
Health,
}
#[derive(Debug, Clone)]
pub enum HealthState {
Healthy,
Degraded,
Unhealthy,
Unknown,
}
#[derive(Debug, Clone)]
pub enum Severity {
Info,
Warning,
Error,
Critical,
}
#[derive(Debug, Clone)]
pub enum ProtocolEvent {
Connected {
device_id: String,
protocol: String,
timestamp: std::time::Instant,
},
Disconnected {
device_id: String,
reason: String,
},
ReadCompleted {
device_id: String,
address: String,
latency_ms: u64,
},
WriteCompleted {
device_id: String,
address: String,
latency_ms: u64,
},
Error {
device_id: String,
error: String,
severity: Severity,
},
HealthChanged {
device_id: String,
from: HealthState,
to: HealthState,
},
CircuitBreakerTriggered {
device_id: String,
failures: u32,
},
}
impl ProtocolEvent {
pub fn topic(&self) -> EventTopic {
match self {
ProtocolEvent::Connected { .. }
| ProtocolEvent::Disconnected { .. }
| ProtocolEvent::ReadCompleted { .. }
| ProtocolEvent::WriteCompleted { .. } => EventTopic::Connection,
ProtocolEvent::Error { .. } | ProtocolEvent::CircuitBreakerTriggered { .. } => {
EventTopic::Error
}
ProtocolEvent::HealthChanged { .. } => EventTopic::Health,
}
}
}