Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame^] | 1 | #ifndef AOS_EVENTS_SIMULATED_EVENT_LOOP_H_ |
| 2 | #define AOS_EVENTS_SIMULATED_EVENT_LOOP_H_ |
| 3 | |
| 4 | #include <algorithm> |
| 5 | #include <map> |
| 6 | #include <memory> |
| 7 | #include <unordered_set> |
| 8 | #include <utility> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "absl/container/btree_map.h" |
| 12 | #include "aos/events/event_loop.h" |
| 13 | #include "aos/events/event_scheduler.h" |
| 14 | #include "aos/flatbuffer_merge.h" |
| 15 | #include "aos/flatbuffers.h" |
| 16 | #include "aos/ipc_lib/index.h" |
| 17 | #include "glog/logging.h" |
| 18 | |
| 19 | namespace aos { |
| 20 | |
| 21 | // Class for simulated fetchers. |
| 22 | class SimulatedChannel; |
| 23 | |
| 24 | struct SimpleChannel { |
| 25 | SimpleChannel(const Channel *channel); |
| 26 | std::string name; |
| 27 | std::string type; |
| 28 | |
| 29 | std::string DebugString() const { |
| 30 | return std::string("{ ") + name + ", " + type + "}"; |
| 31 | } |
| 32 | |
| 33 | bool operator==(const SimpleChannel &other) const { |
| 34 | return name == other.name && type == other.type; |
| 35 | } |
| 36 | bool operator<(const SimpleChannel &other) const { |
| 37 | int name_compare = other.name.compare(name); |
| 38 | if (name_compare == 0) { |
| 39 | return other.type < type; |
| 40 | } else if (name_compare < 0) { |
| 41 | return true; |
| 42 | } else { |
| 43 | return false; |
| 44 | } |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | class SimulatedEventLoopFactory { |
| 49 | public: |
| 50 | // Constructs a SimulatedEventLoopFactory with the provided configuration. |
| 51 | // This configuration must remain in scope for the lifetime of the factory and |
| 52 | // all sub-objects. |
| 53 | SimulatedEventLoopFactory(const Configuration *configuration); |
| 54 | ~SimulatedEventLoopFactory(); |
| 55 | |
| 56 | ::std::unique_ptr<EventLoop> MakeEventLoop(); |
| 57 | |
| 58 | // Starts executing the event loops unconditionally. |
| 59 | void Run(); |
| 60 | // Executes the event loops for a duration. |
| 61 | void RunFor(monotonic_clock::duration duration); |
| 62 | |
| 63 | // Stops executing all event loops. Meant to be called from within an event |
| 64 | // loop handler. |
| 65 | void Exit() { scheduler_.Exit(); } |
| 66 | |
| 67 | monotonic_clock::time_point monotonic_now() const { |
| 68 | return scheduler_.monotonic_now(); |
| 69 | } |
| 70 | realtime_clock::time_point realtime_now() const { |
| 71 | return scheduler_.realtime_now(); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | const Configuration *configuration_; |
| 76 | EventScheduler scheduler_; |
| 77 | // Map from name, type to queue. |
| 78 | absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> channels_; |
| 79 | // List of event loops to manage running and not running for. |
| 80 | std::vector<std::pair<EventLoop *, std::function<void(bool)>>> |
| 81 | raw_event_loops_; |
| 82 | }; |
| 83 | |
| 84 | } // namespace aos |
| 85 | |
| 86 | #endif // AOS_EVENTS_SIMULATED_EVENT_LOOP_H_ |