use labstream_proto::timesync::{self, Burst};
use labstream_time::OffsetSource;
use std::net::UdpSocket;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
pub fn update_interval() -> Duration {
Duration::from_secs_f64(crate::config::get().time_update_interval)
}
pub fn probe_count() -> usize {
crate::config::get().time_probe_count as usize
}
pub fn probe_interval() -> Duration {
Duration::from_secs_f64(crate::config::get().time_probe_interval)
}
pub fn probe_max_rtt() -> Duration {
Duration::from_secs_f64(crate::config::get().time_probe_max_rtt)
}
pub fn update_min_probes() -> usize {
crate::config::get().time_update_min_probes as usize
}
#[derive(Debug)]
pub struct ClockState {
offset_bits: AtomicU64,
uncertainty_bits: AtomicU64,
have: AtomicBool,
reset: AtomicBool,
stop: AtomicBool,
updates: AtomicU64,
}
const NO_VALUE: u64 = u64::MAX;
impl Default for ClockState {
fn default() -> Self {
ClockState {
offset_bits: AtomicU64::new(NO_VALUE),
uncertainty_bits: AtomicU64::new(NO_VALUE),
have: AtomicBool::new(false),
reset: AtomicBool::new(false),
stop: AtomicBool::new(false),
updates: AtomicU64::new(0),
}
}
}
impl ClockState {
pub fn offset(&self) -> Option<f64> {
match self.offset_bits.load(Ordering::SeqCst) {
NO_VALUE => None,
b => Some(f64::from_bits(b)),
}
}
pub fn uncertainty(&self) -> Option<f64> {
match self.uncertainty_bits.load(Ordering::SeqCst) {
NO_VALUE => None,
b => Some(f64::from_bits(b)),
}
}
pub fn updates(&self) -> u64 {
self.updates.load(Ordering::SeqCst)
}
pub fn wait(&self, timeout: Duration) -> Option<f64> {
let end = Instant::now() + timeout;
while Instant::now() < end {
if let Some(v) = self.offset() {
return Some(v);
}
std::thread::sleep(Duration::from_millis(10));
}
self.offset()
}
pub fn mark_reset(&self) {
self.reset.store(true, Ordering::SeqCst);
}
pub fn stop(&self) {
self.stop.store(true, Ordering::SeqCst);
}
fn publish(&self, offset: f64, uncertainty: f64) {
self.offset_bits.store(offset.to_bits(), Ordering::SeqCst);
self.uncertainty_bits
.store(uncertainty.to_bits(), Ordering::SeqCst);
self.have.store(true, Ordering::SeqCst);
self.updates.fetch_add(1, Ordering::SeqCst);
}
}
pub fn start(target: std::net::SocketAddr, state: Arc<ClockState>, rng: Arc<Mutex<u64>>) {
std::thread::spawn(move || {
let any: std::net::IpAddr = if target.is_ipv6() {
std::net::Ipv6Addr::UNSPECIFIED.into()
} else {
std::net::Ipv4Addr::UNSPECIFIED.into()
};
let sock = match UdpSocket::bind((any, 0)) {
Ok(s) => s,
Err(_) => return,
};
let _ = sock.set_read_timeout(Some(Duration::from_millis(40)));
while !state.stop.load(Ordering::SeqCst) {
let wave_id = {
let mut r = rng.lock().unwrap();
*r ^= *r << 13;
*r ^= *r >> 7;
*r ^= *r << 17;
(*r as u32 as i32).abs()
};
let mut burst = Burst::new(wave_id);
let burst_end =
Instant::now() + probe_interval() * probe_count() as u32 + probe_max_rtt();
for i in 0..probe_count() {
if state.stop.load(Ordering::SeqCst) {
return;
}
let t0 = crate::clock();
let msg = timesync::build_probe(wave_id, t0);
let _ = sock.send_to(&msg, target);
let slot_end = Instant::now() + probe_interval();
let mut buf = [0u8; 4096];
while Instant::now() < slot_end {
match sock.recv_from(&mut buf) {
Ok((n, _)) => {
let t3 = crate::clock();
if let Some(a) = timesync::parse_answer(&buf[..n]) {
burst.accept(&a, t3);
}
}
Err(_) => break,
}
}
let _ = i;
}
let mut buf = [0u8; 4096];
while Instant::now() < burst_end {
match sock.recv_from(&mut buf) {
Ok((n, _)) => {
let t3 = crate::clock();
if let Some(a) = timesync::parse_answer(&buf[..n]) {
burst.accept(&a, t3);
}
}
Err(_) => break,
}
}
if std::env::var_os("LSL_TRACE_CLOCK").is_some() {
eprintln!(
"TRACE clock wave={wave_id} accepted={} offset={:?} rtt={:?}",
burst.accepted(),
burst.time_offset(),
burst.uncertainty()
);
}
if burst.accepted() >= update_min_probes() {
if let (Some(o), Some(u)) = (burst.time_offset(), burst.uncertainty()) {
state.publish(o, u);
}
}
let rest = update_interval()
.saturating_sub(probe_interval() * probe_count() as u32 + probe_max_rtt());
let wake = Instant::now() + rest;
while Instant::now() < wake {
if state.stop.load(Ordering::SeqCst) {
return;
}
std::thread::sleep(Duration::from_millis(20));
}
}
});
}
pub struct LiveSource {
state: Arc<ClockState>,
srate: f64,
}
impl LiveSource {
pub fn new(state: Arc<ClockState>, srate: f64) -> Self {
LiveSource { state, srate }
}
}
impl OffsetSource for LiveSource {
fn correction(&mut self) -> f64 {
self.state.offset().unwrap_or(0.0)
}
fn srate(&mut self) -> f64 {
self.srate
}
fn was_reset(&mut self) -> bool {
self.state.reset.swap(false, Ordering::SeqCst)
}
fn clock(&mut self) -> f64 {
crate::clock()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_fresh_state_holds_no_measurement() {
let s = ClockState::default();
assert_eq!(s.offset(), None);
assert_eq!(s.uncertainty(), None);
assert_eq!(s.updates(), 0);
}
#[test]
fn a_published_value_comes_back() {
let s = ClockState::default();
s.publish(-1.25, 0.004);
assert_eq!(s.offset(), Some(-1.25));
assert_eq!(s.uncertainty(), Some(0.004));
assert_eq!(s.updates(), 1);
}
#[test]
fn a_reset_reads_once() {
let s = Arc::new(ClockState::default());
let mut src = LiveSource::new(Arc::clone(&s), 100.0);
assert!(!src.was_reset());
s.mark_reset();
assert!(src.was_reset(), "the first read reports the reset");
assert!(!src.was_reset(), "a second read must not report it again");
}
#[test]
fn a_missing_measurement_leaves_a_timestamp_alone() {
let s = Arc::new(ClockState::default());
let mut src = LiveSource::new(s, 100.0);
assert_eq!(src.correction(), 0.0);
}
}