blob: 740a4c26b053eff935463c6e3f4de8fd86e26f52 [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#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>
Austin Schuh5f1cc5c2019-12-01 18:01:11 -08007#include <string_view>
Alex Perrycb7da4b2019-08-28 19:35:56 -07008#include <unordered_set>
9#include <utility>
10#include <vector>
11
12#include "absl/container/btree_map.h"
13#include "aos/events/event_loop.h"
14#include "aos/events/event_scheduler.h"
15#include "aos/flatbuffer_merge.h"
16#include "aos/flatbuffers.h"
17#include "aos/ipc_lib/index.h"
18#include "glog/logging.h"
19
20namespace aos {
21
22// Class for simulated fetchers.
23class SimulatedChannel;
24
25struct SimpleChannel {
26 SimpleChannel(const Channel *channel);
27 std::string name;
28 std::string type;
29
30 std::string DebugString() const {
31 return std::string("{ ") + name + ", " + type + "}";
32 }
33
34 bool operator==(const SimpleChannel &other) const {
35 return name == other.name && type == other.type;
36 }
37 bool operator<(const SimpleChannel &other) const {
38 int name_compare = other.name.compare(name);
39 if (name_compare == 0) {
40 return other.type < type;
41 } else if (name_compare < 0) {
42 return true;
43 } else {
44 return false;
45 }
46 }
47};
48
49class SimulatedEventLoopFactory {
50 public:
51 // Constructs a SimulatedEventLoopFactory with the provided configuration.
52 // This configuration must remain in scope for the lifetime of the factory and
53 // all sub-objects.
54 SimulatedEventLoopFactory(const Configuration *configuration);
Austin Schuh217a9782019-12-21 23:02:50 -080055 SimulatedEventLoopFactory(const Configuration *configuration,
56 std::string_view node_name);
Alex Perrycb7da4b2019-08-28 19:35:56 -070057 ~SimulatedEventLoopFactory();
58
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080059 ::std::unique_ptr<EventLoop> MakeEventLoop(std::string_view name);
Alex Perrycb7da4b2019-08-28 19:35:56 -070060
61 // Starts executing the event loops unconditionally.
62 void Run();
63 // Executes the event loops for a duration.
64 void RunFor(monotonic_clock::duration duration);
65
66 // Stops executing all event loops. Meant to be called from within an event
67 // loop handler.
68 void Exit() { scheduler_.Exit(); }
69
Austin Schuh7d87b672019-12-01 20:23:49 -080070 // Sets the simulated send delay for the factory.
71 void set_send_delay(std::chrono::nanoseconds send_delay);
72
Austin Schuh217a9782019-12-21 23:02:50 -080073 // Returns the node that this factory is running as, or nullptr if this is a
74 // single node setup.
75 const Node *node() const { return node_; }
76
Alex Perrycb7da4b2019-08-28 19:35:56 -070077 monotonic_clock::time_point monotonic_now() const {
78 return scheduler_.monotonic_now();
79 }
80 realtime_clock::time_point realtime_now() const {
81 return scheduler_.realtime_now();
82 }
83
84 private:
Austin Schuh217a9782019-12-21 23:02:50 -080085 const Configuration *const configuration_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070086 EventScheduler scheduler_;
87 // Map from name, type to queue.
88 absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> channels_;
89 // List of event loops to manage running and not running for.
90 std::vector<std::pair<EventLoop *, std::function<void(bool)>>>
91 raw_event_loops_;
Austin Schuh39788ff2019-12-01 18:22:57 -080092
Austin Schuh7d87b672019-12-01 20:23:49 -080093 std::chrono::nanoseconds send_delay_ = std::chrono::microseconds(50);
94
Austin Schuh217a9782019-12-21 23:02:50 -080095 const Node *const node_;
96
Austin Schuh39788ff2019-12-01 18:22:57 -080097 pid_t tid_ = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -070098};
99
100} // namespace aos
101
102#endif // AOS_EVENTS_SIMULATED_EVENT_LOOP_H_