-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
73 lines (64 loc) · 1.67 KB
/
Copy pathmod.rs
File metadata and controls
73 lines (64 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! Events that are dispatched by one module and listened to by another.
use std::sync::{RwLock, mpsc::Sender};
use liquidcan::{CanMessage, CanMessageId};
use socketcan::CanAnyFrame;
#[derive(Clone, Debug)]
pub enum Event {
CanMessageReceived {
id: CanMessageId,
message: CanMessage,
},
NodeFieldUpdated(crate::db::FieldLog),
Shutdown,
#[allow(unused)]
SendCanMessage {
receiver_node_id: u8,
message: CanMessage,
},
RelayCanMessage {
from_interface: String,
frame: CanAnyFrame,
},
StartSequence {
seq_name: String,
abort_seq_name: String,
},
PauseSequence,
ResumeSequence,
AbortSequence,
}
struct EventListener {
debug_name: String,
sender: Sender<Event>,
}
pub struct EventDispatcher {
listeners: RwLock<Vec<EventListener>>,
}
impl Default for EventDispatcher {
fn default() -> Self {
Self::new()
}
}
impl EventDispatcher {
pub fn new() -> Self {
Self {
listeners: RwLock::new(Vec::new()),
}
}
pub fn subscribe(&self, listener: Sender<Event>, debug_name: impl Into<String>) {
self.listeners.write().unwrap().push(EventListener {
debug_name: debug_name.into(),
sender: listener,
});
}
pub fn dispatch(&self, event: Event) {
for listener in self.listeners.read().unwrap().iter() {
if let Err(e) = listener.sender.send(event.clone()) {
eprintln!(
"Failed to send event to listener {}: {e}. Event content: {:#?}",
listener.debug_name, event
);
}
}
}
}