blob: 5ffdc0197a6b12e62d480c996cb5fccca71d3982 [file] [log] [blame]
Austin Schuha9abc032021-01-01 16:46:19 -08001#ifndef AOS_NETWORK_TESTING_TIME_CONVERTER_H_
2#define AOS_NETWORK_TESTING_TIME_CONVERTER_H_
3
4#include <deque>
5#include <optional>
6#include <tuple>
7
8#include "aos/events/event_scheduler.h"
9#include "aos/network/multinode_timestamp_filter.h"
10#include "aos/time/time.h"
11
12namespace aos {
13namespace message_bridge {
14
15// Simple class to which uses InterpolatedTimeConverter to produce an
16// interpolated timeline. Should only be used for testing.
17class TestingTimeConverter final : public InterpolatedTimeConverter {
18 public:
19 TestingTimeConverter(size_t node_count);
20
21 virtual ~TestingTimeConverter();
22
23 // Starts all nodes equal to the distributed clock.
24 void StartEqual();
25
26 // Elapses each node's clock by the provided duration, and returns the
27 // duration that the distributed clock elapsed by.
28 std::chrono::nanoseconds AddMonotonic(
29 std::vector<monotonic_clock::duration> times);
30
31 // Sets time on each node's clock to the provided times, and returns the
32 // duration that the distributed clock elapsed by. Note: time must always go
33 // forwards.
34 std::chrono::nanoseconds AddMonotonic(
Austin Schuh66168842021-08-17 19:42:21 -070035 std::vector<logger::BootTimestamp> times);
Austin Schuha9abc032021-01-01 16:46:19 -080036
37 // Adds a distributed to monotonic clock mapping to the queue.
38 void AddNextTimestamp(distributed_clock::time_point time,
Austin Schuh66168842021-08-17 19:42:21 -070039 std::vector<logger::BootTimestamp> times);
Austin Schuha9abc032021-01-01 16:46:19 -080040
41 std::optional<std::tuple<distributed_clock::time_point,
Austin Schuh66168842021-08-17 19:42:21 -070042 std::vector<logger::BootTimestamp>>>
Austin Schuha9abc032021-01-01 16:46:19 -080043 NextTimestamp() override;
44
45 private:
46 // List of timestamps.
47 std::deque<std::tuple<distributed_clock::time_point,
Austin Schuh66168842021-08-17 19:42:21 -070048 std::vector<logger::BootTimestamp>>>
Austin Schuha9abc032021-01-01 16:46:19 -080049 ts_;
50
51 // True if there is no time queued.
52 bool first_ = true;
53 // The last times returned on all clocks.
54 distributed_clock::time_point last_distributed_ = distributed_clock::epoch();
Austin Schuh66168842021-08-17 19:42:21 -070055 std::vector<logger::BootTimestamp> last_monotonic_;
Austin Schuha9abc032021-01-01 16:46:19 -080056};
57
58} // namespace message_bridge
59} // namespace aos
60
61#endif // AOS_NETWORK_TESTING_TIME_CONVERTER_H_