use std::{fmt::Debug, hash::Hash};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use crate::{peer::Measurement, NtpClock, NtpDuration, NtpTimestamp, SystemConfig, TimeSnapshot};
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct ObservablePeerTimedata {
pub offset: NtpDuration,
pub uncertainty: NtpDuration,
pub delay: NtpDuration,
pub remote_delay: NtpDuration,
pub remote_uncertainty: NtpDuration,
pub last_update: NtpTimestamp,
}
#[derive(Debug, Clone)]
pub struct StateUpdate<PeerID: Eq + Copy + Debug> {
pub time_snapshot: Option<TimeSnapshot>,
pub used_peers: Option<Vec<PeerID>>,
pub next_update: Option<NtpTimestamp>,
}
impl<PeerID: Eq + Copy + Debug> Default for StateUpdate<PeerID> {
fn default() -> Self {
Self {
time_snapshot: None,
used_peers: None,
next_update: None,
}
}
}
pub trait TimeSyncController<C: NtpClock, PeerID: Hash + Eq + Copy + Debug> {
type AlgorithmConfig: Debug + Copy + DeserializeOwned;
fn new(clock: C, config: SystemConfig, algorithm_config: Self::AlgorithmConfig) -> Self;
fn update_config(&mut self, config: SystemConfig, algorithm_config: Self::AlgorithmConfig);
fn peer_add(&mut self, id: PeerID);
fn peer_remove(&mut self, id: PeerID);
fn peer_update(&mut self, id: PeerID, usable: bool);
fn peer_measurement(&mut self, id: PeerID, measurement: Measurement) -> StateUpdate<PeerID>;
fn time_update(&mut self) -> StateUpdate<PeerID>;
fn peer_snapshot(&self, id: PeerID) -> Option<ObservablePeerTimedata>;
}
mod kalman;
mod standard;
pub use kalman::KalmanClockController;
pub use standard::StandardClockController;
#[cfg(feature = "rfc-algorithm")]
pub type DefaultTimeSyncController<C, PeerID> = standard::StandardClockController<C, PeerID>;
#[cfg(not(feature = "rfc-algorithm"))]
pub type DefaultTimeSyncController<C, PeerID> = kalman::KalmanClockController<C, PeerID>;
#[cfg(feature = "fuzz")]
pub use standard::fuzz_find_interval;