blob: de37c03e87b9f4e33c0b901a6f354bb16ef07dbe [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"
Austin Schuhe1dafe42020-01-06 21:12:03 -080015#include "aos/events/simple_channel.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070016#include "aos/flatbuffer_merge.h"
17#include "aos/flatbuffers.h"
18#include "aos/ipc_lib/index.h"
19#include "glog/logging.h"
20
21namespace aos {
22
23// Class for simulated fetchers.
24class SimulatedChannel;
25
Alex Perrycb7da4b2019-08-28 19:35:56 -070026class SimulatedEventLoopFactory {
27 public:
28 // Constructs a SimulatedEventLoopFactory with the provided configuration.
29 // This configuration must remain in scope for the lifetime of the factory and
30 // all sub-objects.
31 SimulatedEventLoopFactory(const Configuration *configuration);
Austin Schuh217a9782019-12-21 23:02:50 -080032 SimulatedEventLoopFactory(const Configuration *configuration,
33 std::string_view node_name);
Austin Schuh15649d62019-12-28 16:36:38 -080034 SimulatedEventLoopFactory(const Configuration *configuration,
35 const Node *node);
Alex Perrycb7da4b2019-08-28 19:35:56 -070036 ~SimulatedEventLoopFactory();
37
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080038 ::std::unique_ptr<EventLoop> MakeEventLoop(std::string_view name);
Alex Perrycb7da4b2019-08-28 19:35:56 -070039
40 // Starts executing the event loops unconditionally.
41 void Run();
42 // Executes the event loops for a duration.
43 void RunFor(monotonic_clock::duration duration);
44
45 // Stops executing all event loops. Meant to be called from within an event
46 // loop handler.
47 void Exit() { scheduler_.Exit(); }
48
Austin Schuh7d87b672019-12-01 20:23:49 -080049 // Sets the simulated send delay for the factory.
50 void set_send_delay(std::chrono::nanoseconds send_delay);
James Kuszmaul314f1672020-01-03 20:02:08 -080051 std::chrono::nanoseconds send_delay() const;
Austin Schuh7d87b672019-12-01 20:23:49 -080052
Austin Schuh217a9782019-12-21 23:02:50 -080053 // Returns the node that this factory is running as, or nullptr if this is a
54 // single node setup.
55 const Node *node() const { return node_; }
56
Alex Perrycb7da4b2019-08-28 19:35:56 -070057 monotonic_clock::time_point monotonic_now() const {
58 return scheduler_.monotonic_now();
59 }
60 realtime_clock::time_point realtime_now() const {
61 return scheduler_.realtime_now();
62 }
63
Austin Schuh92547522019-12-28 14:33:43 -080064 // Sets realtime clock to realtime_now for a given monotonic clock.
65 void SetRealtimeOffset(monotonic_clock::time_point monotonic_now,
66 realtime_clock::time_point realtime_now) {
67 scheduler_.SetRealtimeOffset(monotonic_now, realtime_now);
68 }
69
Alex Perrycb7da4b2019-08-28 19:35:56 -070070 private:
Austin Schuh217a9782019-12-21 23:02:50 -080071 const Configuration *const configuration_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070072 EventScheduler scheduler_;
73 // Map from name, type to queue.
74 absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> channels_;
75 // List of event loops to manage running and not running for.
76 std::vector<std::pair<EventLoop *, std::function<void(bool)>>>
77 raw_event_loops_;
Austin Schuh39788ff2019-12-01 18:22:57 -080078
Austin Schuh7d87b672019-12-01 20:23:49 -080079 std::chrono::nanoseconds send_delay_ = std::chrono::microseconds(50);
80
Austin Schuh217a9782019-12-21 23:02:50 -080081 const Node *const node_;
82
Austin Schuh39788ff2019-12-01 18:22:57 -080083 pid_t tid_ = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -070084};
85
86} // namespace aos
87
88#endif // AOS_EVENTS_SIMULATED_EVENT_LOOP_H_