-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple_client.rs
More file actions
79 lines (66 loc) · 2.61 KB
/
Copy pathsimple_client.rs
File metadata and controls
79 lines (66 loc) · 2.61 KB
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
//! Simple NTS client example.
//!
//! This example demonstrates the basic usage of the rkik-nts library
//! to query time from an NTS-secured NTP server.
//!
//! Run with: cargo run --example simple_client --features tracing-subscriber
use rkik_nts::{NtsClient, NtsClientConfig};
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Initialize logging
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.init();
// List of public NTS servers to try
let servers = vec!["time.cloudflare.com", "nts.ntp.se", "ntppool1.time.nl"];
println!("rkik-nts Simple Client Example\n");
println!("================================\n");
for server in servers {
println!("Querying server: {}", server);
println!("{}", "-".repeat(50));
// Create configuration for the NTS server
let config = NtsClientConfig::new(server);
// Create NTS client
let mut client = NtsClient::new(config);
// Connect and perform NTS key exchange
match client.connect().await {
Ok(_) => {
println!("✓ Successfully connected to {}", server);
println!(" NTP server: {}", client.ntp_server().unwrap());
}
Err(e) => {
println!("✗ Failed to connect to {}: {}", server, e);
println!();
continue;
}
}
// Query time
match client.get_time().await {
Ok(time) => {
println!("✓ Time query successful!\n");
println!(" Network time: {:?}", time.network_time);
println!(" System time: {:?}", time.system_time);
println!(" Offset: {:?}", time.offset);
println!(" Offset (ms): {} ms", time.offset_signed());
println!(" Round-trip: {:?}", time.round_trip_delay);
println!(" Authenticated: {}", time.authenticated);
println!(" Server: {}", time.server);
if time.is_ahead() {
println!("\n ⚠ System clock is ahead of network time");
} else if time.is_behind() {
println!("\n ⚠ System clock is behind network time");
} else {
println!("\n ✓ System clock is synchronized");
}
}
Err(e) => {
println!("✗ Failed to query time: {}", e);
}
}
println!();
// Only query the first successful server
break;
}
Ok(())
}