@@ -7,12 +7,9 @@ use std::sync::{Arc, Mutex, RwLock};
77use crate :: packet:: v5:: server_reference_id:: { BloomFilter , ServerId } ;
88use crate :: source:: SourceSnapshot ;
99use crate :: {
10- ClockId , KeySet , NtpSourceSnapshot , NtpTimestamp , OneWaySource , Server , ServerConfig ,
11- SourceController ,
10+ ClockId , KeySet , NtpSourceSnapshot , NtpTimestamp , Server , ServerConfig , SourceController ,
1211} ;
1312use 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 ) ]
331154pub 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 }
0 commit comments