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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use std::io;
use std::sync::atomic::{self, AtomicBool};
use std::sync::mpsc;
use std::sync::Arc;
use std::time::Duration;
pub trait DetectedDacCallback: FnMut(io::Result<DetectedDac>) {}
impl<F> DetectedDacCallback for F where F: FnMut(io::Result<DetectedDac>) {}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum Id {
EtherDream { mac_address: [u8; 6] },
}
#[derive(Clone, Debug)]
pub enum DetectedDac {
EtherDream {
broadcast: ether_dream::protocol::DacBroadcast,
source_addr: std::net::SocketAddr,
},
}
pub struct DetectDacs {
pub(crate) dac_broadcasts: ether_dream::RecvDacBroadcasts,
}
enum DetectorThreadMsg {
Close,
Tick,
}
pub struct DetectDacsAsync {
msg_tx: mpsc::Sender<DetectorThreadMsg>,
thread: Option<std::thread::JoinHandle<()>>,
}
impl DetectedDac {
pub fn max_point_hz(&self) -> u32 {
match self {
DetectedDac::EtherDream { ref broadcast, .. } => broadcast.max_point_rate as _,
}
}
pub fn buffer_capacity(&self) -> u32 {
match self {
DetectedDac::EtherDream { ref broadcast, .. } => broadcast.buffer_capacity as _,
}
}
pub fn id(&self) -> Id {
match self {
DetectedDac::EtherDream { ref broadcast, .. } => Id::EtherDream {
mac_address: broadcast.mac_address,
},
}
}
}
impl DetectDacs {
pub fn set_timeout(&self, duration: Option<std::time::Duration>) -> io::Result<()> {
self.dac_broadcasts.set_timeout(duration)
}
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
self.dac_broadcasts.set_nonblocking(nonblocking)
}
}
impl DetectDacsAsync {
pub fn close(mut self) {
self.close_inner()
}
fn close_inner(&mut self) {
if let Some(thread) = self.thread.take() {
if self.msg_tx.send(DetectorThreadMsg::Close).is_ok() {
thread.join().ok();
}
}
}
}
impl Iterator for DetectDacs {
type Item = io::Result<DetectedDac>;
fn next(&mut self) -> Option<Self::Item> {
let res = self.dac_broadcasts.next()?;
match res {
Err(err) => Some(Err(err)),
Ok((broadcast, source_addr)) => Some(Ok(DetectedDac::EtherDream {
broadcast,
source_addr,
})),
}
}
}
impl Drop for DetectDacsAsync {
fn drop(&mut self) {
self.close_inner();
}
}
pub(crate) fn detect_dacs() -> io::Result<DetectDacs> {
let dac_broadcasts = ether_dream::recv_dac_broadcasts()?;
Ok(DetectDacs { dac_broadcasts })
}
pub(crate) fn detect_dacs_async<F>(
timeout: Option<Duration>,
callback: F,
) -> io::Result<DetectDacsAsync>
where
F: 'static + DetectedDacCallback + Send,
{
detect_dacs_async_inner(timeout, Box::new(callback) as Box<_>)
}
fn detect_dacs_async_inner(
timeout: Option<Duration>,
mut callback: Box<dyn 'static + DetectedDacCallback + Send>,
) -> io::Result<DetectDacsAsync> {
let mut detect_dacs = detect_dacs()?;
detect_dacs.set_nonblocking(true)?;
let (msg_tx, msg_rx) = mpsc::channel();
let msg_tx2 = msg_tx.clone();
let thread = std::thread::Builder::new()
.name("nannou_laser-dac-detection".to_string())
.spawn(move || {
let is_closed = Arc::new(AtomicBool::new(false));
let is_closed2 = is_closed.clone();
std::thread::spawn(move || {
let tick_interval = timeout.unwrap_or(std::time::Duration::from_secs(1));
while !is_closed2.load(atomic::Ordering::Relaxed) {
std::thread::sleep(tick_interval);
if msg_tx2.send(DetectorThreadMsg::Tick).is_err() {
break;
}
}
});
'msgs: for msg in msg_rx {
if let DetectorThreadMsg::Close = msg {
is_closed.store(true, atomic::Ordering::Relaxed);
break;
}
while let Some(res) = detect_dacs.next() {
if let Err(ref e) = res {
match e.kind() {
io::ErrorKind::TimedOut | io::ErrorKind::WouldBlock => continue 'msgs,
_ => (),
}
}
callback(res);
}
}
})
.expect("failed to spawn DAC detection thread");
Ok(DetectDacsAsync {
msg_tx,
thread: Some(thread),
})
}