after/
after.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3use chrono::Utc;
4use clock_bound_c::ClockBoundClient;
5use std::env;
6use std::thread::sleep;
7use std::time::Duration;
8
9const ONE_SECOND_IN_NANOSECONDS: u64 = 1000000000;
10
11fn main() {
12    let args: Vec<String> = env::args().collect();
13    let clock_bound_d_socket = &args[1];
14
15    let client =
16        match ClockBoundClient::new_with_path(std::path::PathBuf::from(clock_bound_d_socket)) {
17            Ok(client) => client,
18            Err(e) => {
19                println!("Could not create client: {}", e);
20                return;
21            }
22        };
23
24    // Get the current time in nanoseconds since the Unix Epoch
25    let timestamp = Utc::now().timestamp_nanos() as u64;
26
27    // Test with a timestamp 1 second in the future
28    let timestamp = timestamp + ONE_SECOND_IN_NANOSECONDS;
29
30    // Checks if a point in time is after the current time's error bounds
31    let response = match client.after(timestamp) {
32        Ok(response) => response,
33        Err(e) => {
34            println!("Couldn't complete after request: {}", e);
35            return;
36        }
37    };
38
39    // A clock synchronized via NTP should generally not be a second off. One second past the
40    // current time should generally be after the latest error bound and should return true.
41    if response.after == false {
42        println!(
43            "{} nanoseconds since the Unix Epoch is not after the current time's error bounds.",
44            timestamp
45        )
46    } else if response.after == true {
47        println!(
48            "{} nanoseconds since the Unix Epoch is after the current time's error bounds.",
49            timestamp
50        )
51    }
52
53    println!("Waiting 2 seconds...");
54
55    // Checking again after the timestamp has passed should no longer be after the latest error
56    // bound and return false.
57    sleep(Duration::from_secs(2));
58
59    // Checks if a point in time is after the current time's error bounds
60    let response = match client.after(timestamp) {
61        Ok(response) => response,
62        Err(e) => {
63            println!("Couldn't complete after request: {}", e);
64            return;
65        }
66    };
67
68    if response.after == false {
69        println!(
70            "{} nanoseconds since the Unix Epoch is not after the current time's error bounds.",
71            timestamp
72        )
73    } else if response.after == true {
74        println!(
75            "{} nanoseconds since the Unix Epoch is after the current time's error bounds.",
76            timestamp
77        )
78    }
79}