Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame^] | 1 | #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 | |
| 12 | namespace aos { |
| 13 | namespace message_bridge { |
| 14 | |
| 15 | // Simple class to which uses InterpolatedTimeConverter to produce an |
| 16 | // interpolated timeline. Should only be used for testing. |
| 17 | class 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( |
| 35 | std::vector<monotonic_clock::time_point> times); |
| 36 | |
| 37 | // Adds a distributed to monotonic clock mapping to the queue. |
| 38 | void AddNextTimestamp(distributed_clock::time_point time, |
| 39 | std::vector<monotonic_clock::time_point> times); |
| 40 | |
| 41 | std::optional<std::tuple<distributed_clock::time_point, |
| 42 | std::vector<monotonic_clock::time_point>>> |
| 43 | NextTimestamp() override; |
| 44 | |
| 45 | private: |
| 46 | // List of timestamps. |
| 47 | std::deque<std::tuple<distributed_clock::time_point, |
| 48 | std::vector<monotonic_clock::time_point>>> |
| 49 | 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(); |
| 55 | std::vector<monotonic_clock::time_point> last_monotonic_; |
| 56 | }; |
| 57 | |
| 58 | } // namespace message_bridge |
| 59 | } // namespace aos |
| 60 | |
| 61 | #endif // AOS_NETWORK_TESTING_TIME_CONVERTER_H_ |