Skip to content

Commit 1ff3a1c

Browse files
committed
Remove system from ntp-proto.
1 parent 707f8b6 commit 1ff3a1c

6 files changed

Lines changed: 117 additions & 255 deletions

File tree

ntp-proto/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ mod exports {
281281
NtpSourceSnapshot, ObservableSourceState, OneWaySource, ProtocolVersion, Reach,
282282
SourceNtsData,
283283
};
284-
pub use super::system::{NtpServerInfo, NtpSnapshot, System, SystemSnapshot, TimeSnapshot};
284+
pub use super::system::{
285+
NtpManager, NtpServerInfo, NtpSnapshot, SourceType, SystemSnapshot, TimeSnapshot,
286+
};
285287

286288
#[cfg(feature = "__internal-fuzz")]
287289
pub use super::time_types::fuzz_duration_from_seconds;

ntp-proto/src/source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub struct OneWaySource<Controller: SourceController> {
111111
}
112112

113113
impl<Controller: SourceController> OneWaySource<Controller> {
114-
pub(crate) fn new(mut controller: Controller) -> OneWaySource<Controller> {
114+
pub fn new(mut controller: Controller) -> OneWaySource<Controller> {
115115
controller.set_usable(true);
116116
OneWaySource { controller }
117117
}

ntp-proto/src/system.rs

Lines changed: 5 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@ use std::sync::{Arc, Mutex, RwLock};
77
use crate::packet::v5::server_reference_id::{BloomFilter, ServerId};
88
use crate::source::SourceSnapshot;
99
use crate::{
10-
ClockId, KeySet, NtpSourceSnapshot, NtpTimestamp, OneWaySource, Server, ServerConfig,
11-
SourceController,
10+
ClockId, KeySet, NtpSourceSnapshot, NtpTimestamp, Server, ServerConfig, SourceController,
1211
};
1312
use crate::{
14-
algorithm::TimeSyncController,
15-
clock::NtpClock,
1613
config::{SourceConfig, SynchronizationConfig},
1714
identifiers::ReferenceId,
1815
packet::NtpLeapIndicator,
@@ -153,180 +150,6 @@ pub enum SourceType {
153150
Ntp,
154151
}
155152

156-
pub struct System<Controller> {
157-
system: Mutex<SystemSnapshot>,
158-
ntp_manager: NtpManager,
159-
160-
sources: Mutex<HashMap<ClockId, SourceType>>,
161-
162-
controller: Controller,
163-
}
164-
165-
impl<Controller: TimeSyncController> System<Controller> {
166-
pub fn new(
167-
clock: Controller::Clock,
168-
synchronization_config: SynchronizationConfig,
169-
algorithm_config: Controller::AlgorithmConfig,
170-
ip_list: Arc<[IpAddr]>,
171-
) -> Result<Self, <Controller::Clock as NtpClock>::Error> {
172-
// Setup system snapshot
173-
let mut system = SystemSnapshot {
174-
ntp_snapshot: NtpSnapshot {
175-
stratum: synchronization_config.local_stratum,
176-
..Default::default()
177-
},
178-
..Default::default()
179-
};
180-
181-
if synchronization_config.local_stratum == 1 {
182-
// We are a stratum 1 server so mark our selves synchronized.
183-
system.time_snapshot.leap_indicator = NtpLeapIndicator::NoWarning;
184-
// Set the reference id for the system
185-
system.ntp_snapshot.reference_id =
186-
synchronization_config.reference_id.to_reference_id();
187-
}
188-
189-
Ok(System {
190-
ntp_manager: NtpManager::new(synchronization_config, ip_list),
191-
system: Mutex::new(system),
192-
sources: Mutex::new(HashMap::new()),
193-
controller: Controller::new(clock, synchronization_config, algorithm_config)?,
194-
})
195-
}
196-
197-
pub fn new_ntp_server<C>(
198-
&self,
199-
config: ServerConfig,
200-
clock: C,
201-
keyset: Arc<KeySet>,
202-
) -> Server<C> {
203-
self.ntp_manager.new_server(config, clock, keyset)
204-
}
205-
206-
pub fn system_snapshot(&self) -> SystemSnapshot {
207-
*self.system.lock().unwrap()
208-
}
209-
210-
pub fn check_clock_access(&self) -> Result<(), <Controller::Clock as NtpClock>::Error> {
211-
self.controller.take_control()
212-
}
213-
214-
pub fn create_sock_source(
215-
&self,
216-
id: ClockId,
217-
source_config: SourceConfig,
218-
measurement_noise_estimate: f64,
219-
) -> Result<
220-
OneWaySource<Controller::OneWaySourceController>,
221-
<Controller::Clock as NtpClock>::Error,
222-
> {
223-
self.controller.take_control()?;
224-
let controller =
225-
self.controller
226-
.add_one_way_source(id, source_config, measurement_noise_estimate, None);
227-
self.sources.lock().unwrap().insert(id, SourceType::Sock);
228-
Ok(OneWaySource::new(controller))
229-
}
230-
231-
pub fn create_pps_source(
232-
&self,
233-
id: ClockId,
234-
source_config: SourceConfig,
235-
measurement_noise_estimate: f64,
236-
period: f64,
237-
) -> Result<
238-
OneWaySource<Controller::OneWaySourceController>,
239-
<Controller::Clock as NtpClock>::Error,
240-
> {
241-
self.controller.take_control()?;
242-
let controller = self.controller.add_one_way_source(
243-
id,
244-
source_config,
245-
measurement_noise_estimate,
246-
Some(period),
247-
);
248-
self.sources.lock().unwrap().insert(id, SourceType::Pps);
249-
Ok(OneWaySource::new(controller))
250-
}
251-
252-
#[expect(clippy::type_complexity)]
253-
pub fn create_ntp_source(
254-
&self,
255-
id: ClockId,
256-
source_config: SourceConfig,
257-
source_addr: SocketAddr,
258-
protocol_version: ProtocolVersion,
259-
nts: Option<Box<SourceNtsData>>,
260-
) -> Result<
261-
(
262-
NtpSource<Controller::NtpSourceController>,
263-
NtpSourceActionIterator,
264-
),
265-
<Controller::Clock as NtpClock>::Error,
266-
> {
267-
self.controller.take_control()?;
268-
let controller = self.controller.add_source(id, source_config);
269-
self.sources.lock().unwrap().insert(id, SourceType::Ntp);
270-
Ok(self.ntp_manager.new_source(
271-
source_addr,
272-
source_config,
273-
protocol_version,
274-
controller,
275-
nts,
276-
id,
277-
))
278-
}
279-
280-
pub fn handle_source_remove(
281-
&self,
282-
id: ClockId,
283-
) -> Result<(), <Controller::Clock as NtpClock>::Error> {
284-
self.sources.lock().unwrap().remove(&id);
285-
Ok(())
286-
}
287-
288-
pub fn update_ip_list(&self, ip_list: Arc<[IpAddr]>) {
289-
self.ntp_manager.update_ip_list(ip_list);
290-
}
291-
292-
pub fn run(self: Arc<Self>) -> impl Future<Output = ()> + 'static {
293-
let this = self.clone();
294-
let update_pusher = async move {
295-
loop {
296-
// Scope here is needed to keep this future sync and send.
297-
{
298-
let (time_snapshot, used_sources) = this.controller.synchronization_state();
299-
let sources = this.sources.lock().unwrap();
300-
this.ntp_manager.update_time_snapshot(time_snapshot);
301-
302-
if let Some(used_sources) = used_sources
303-
.into_iter()
304-
.map(|id| sources.get(&id).map(|&sourcetype| (id, sourcetype)))
305-
.collect::<Option<Vec<_>>>()
306-
{
307-
let ntp_snapshot = this
308-
.ntp_manager
309-
.update_used_sources(used_sources.into_iter());
310-
*this.system.lock().unwrap() = SystemSnapshot {
311-
time_snapshot,
312-
ntp_snapshot,
313-
}
314-
} else {
315-
this.system.lock().unwrap().time_snapshot = time_snapshot;
316-
}
317-
}
318-
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
319-
}
320-
};
321-
322-
let controller_run = async move { self.controller.run().await };
323-
324-
async move {
325-
tokio::join!(update_pusher, controller_run);
326-
}
327-
}
328-
}
329-
330153
#[derive(Default, Copy, Clone)]
331154
pub struct NtpServerInfo {
332155
pub time_snapshot: TimeSnapshot,
@@ -442,6 +265,10 @@ impl NtpManager {
442265
}
443266
}
444267

268+
pub fn observe(&self) -> NtpSnapshot {
269+
self.server_info.read().unwrap().ntp_snapshot
270+
}
271+
445272
pub fn update_time_snapshot(&self, time_snapshot: TimeSnapshot) {
446273
self.server_info.write().unwrap().time_snapshot = time_snapshot;
447274
}

ntpd/src/daemon/ntp_source.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,10 @@ mod tests {
408408
};
409409

410410
use ntp_proto::{
411-
AlgorithmConfig, KalmanClockController, NoCipher, NtpDuration, NtpLeapIndicator, NtpPacket,
412-
NtpServerInfo, ProtocolVersion, SourceConfig, SynchronizationConfig, TimeSnapshot,
413-
TimeSyncControllerWrapper, TwoWayKalmanSourceController, TwoWaySourceControllerWrapper,
411+
AlgorithmConfig, KalmanClockController, NoCipher, NtpDuration, NtpLeapIndicator,
412+
NtpManager, NtpPacket, NtpServerInfo, ProtocolVersion, SourceConfig, SynchronizationConfig,
413+
TimeSnapshot, TimeSyncController, TimeSyncControllerWrapper, TwoWayKalmanSourceController,
414+
TwoWaySourceControllerWrapper,
414415
};
415416
use timestamped_socket::socket::{GeneralTimestampMode, Open, open_ip};
416417
use tokio::sync::mpsc;
@@ -551,24 +552,22 @@ mod tests {
551552
let (msg_for_system_sender, msg_for_system_receiver) = mpsc::channel(1);
552553

553554
let index = ClockId::new();
554-
let system: ntp_proto::System<TimeSyncControllerWrapper<KalmanClockController<_>>> =
555-
ntp_proto::System::new(
556-
TestClock {},
557-
SynchronizationConfig::default(),
558-
AlgorithmConfig::default(),
559-
Arc::new([]),
560-
)
561-
.unwrap();
555+
let controller = TimeSyncControllerWrapper::<KalmanClockController<_>>::new(
556+
TestClock {},
557+
SynchronizationConfig::default(),
558+
AlgorithmConfig::default(),
559+
)
560+
.unwrap();
561+
let ntp_manager = NtpManager::new(SynchronizationConfig::default(), Arc::new([]));
562562

563-
let Ok((source, _)) = system.create_ntp_source(
564-
index,
565-
SourceConfig::default(),
563+
let (source, _) = ntp_manager.new_source(
566564
SocketAddr::from((Ipv4Addr::LOCALHOST, port_base)),
565+
SourceConfig::default(),
567566
ProtocolVersion::V4,
567+
controller.add_source(index, SourceConfig::default()),
568568
None,
569-
) else {
570-
panic!("Could not create test source");
571-
};
569+
index,
570+
);
572571

573572
let process = SourceTask {
574573
_wait: PhantomData,

ntpd/src/daemon/sock_source.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ mod tests {
208208

209209
use ntp_proto::{
210210
AlgorithmConfig, ClockId, KalmanClockController, NtpClock, NtpDuration, NtpLeapIndicator,
211-
NtpTimestamp, SourceConfig, SynchronizationConfig, TimeSyncControllerWrapper,
211+
NtpTimestamp, OneWaySource, SourceConfig, SynchronizationConfig, TimeSyncController,
212+
TimeSyncControllerWrapper,
212213
};
213214
use tokio::sync::mpsc;
214215

@@ -277,14 +278,12 @@ mod tests {
277278

278279
let index = ClockId::new();
279280
let clock = TestClock {};
280-
let system: ntp_proto::System<TimeSyncControllerWrapper<KalmanClockController<_>>> =
281-
ntp_proto::System::new(
282-
clock.clone(),
283-
SynchronizationConfig::default(),
284-
AlgorithmConfig::default(),
285-
Arc::new([]),
286-
)
287-
.unwrap();
281+
let controller = TimeSyncControllerWrapper::<KalmanClockController<_>>::new(
282+
clock.clone(),
283+
SynchronizationConfig::default(),
284+
AlgorithmConfig::default(),
285+
)
286+
.unwrap();
288287

289288
let socket_path = std::env::temp_dir().join(format!("ntp-test-stream-{}", alloc_port()));
290289
let _socket = create_socket(&socket_path).unwrap(); // should be overwritten by SockSource's own socket
@@ -297,9 +296,12 @@ mod tests {
297296
msg_for_system_sender,
298297
source_snapshots: Arc::new(RwLock::new(HashMap::new())),
299298
},
300-
system
301-
.create_sock_source(index, SourceConfig::default(), 0.001)
302-
.unwrap(),
299+
OneWaySource::new(controller.add_one_way_source(
300+
index,
301+
SourceConfig::default(),
302+
0.001,
303+
None,
304+
)),
303305
);
304306

305307
// Send example data to socket

0 commit comments

Comments
 (0)