blob: ee4e6d635c07068e55ee073a1983bce5a81085df [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#include "aos/events/simulated_event_loop.h"
2
3#include <algorithm>
4#include <deque>
Austin Schuh5f1cc5c2019-12-01 18:01:11 -08005#include <string_view>
Alex Perrycb7da4b2019-08-28 19:35:56 -07006
7#include "absl/container/btree_map.h"
Austin Schuh898f4972020-01-11 17:21:25 -08008#include "aos/events/simulated_network_bridge.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07009#include "aos/json_to_flatbuffer.h"
10#include "aos/util/phased_loop.h"
Tyler Chatow67ddb032020-01-12 14:30:04 -080011#include "aos/events/aos_logging.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012
13namespace aos {
14
15// Container for both a message, and the context for it for simulation. This
16// makes tracking the timestamps associated with the data easy.
17struct SimulatedMessage {
Alex Perrycb7da4b2019-08-28 19:35:56 -070018 // Context for the data.
19 Context context;
20
21 // The data.
Brian Silvermana1652f32020-01-29 20:41:44 -080022 char *data(size_t buffer_size) {
23 return RoundChannelData(&actual_data[0], buffer_size);
24 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070025
Brian Silvermana1652f32020-01-29 20:41:44 -080026 // Then the data, including padding on the end so we can align the buffer we
27 // actually return from data().
28 char actual_data[];
Alex Perrycb7da4b2019-08-28 19:35:56 -070029};
30
Austin Schuh7d87b672019-12-01 20:23:49 -080031class SimulatedEventLoop;
Alex Perrycb7da4b2019-08-28 19:35:56 -070032class SimulatedFetcher;
Austin Schuh39788ff2019-12-01 18:22:57 -080033class SimulatedChannel;
34
Austin Schuh7d87b672019-12-01 20:23:49 -080035class SimulatedWatcher : public WatcherState {
Austin Schuh39788ff2019-12-01 18:22:57 -080036 public:
Austin Schuh7d87b672019-12-01 20:23:49 -080037 SimulatedWatcher(
38 SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler,
39 const Channel *channel,
40 std::function<void(const Context &context, const void *message)> fn);
Austin Schuh39788ff2019-12-01 18:22:57 -080041
Austin Schuh7d87b672019-12-01 20:23:49 -080042 ~SimulatedWatcher() override;
Austin Schuh39788ff2019-12-01 18:22:57 -080043
44 void Startup(EventLoop * /*event_loop*/) override {}
45
Austin Schuh7d87b672019-12-01 20:23:49 -080046 void Schedule(std::shared_ptr<SimulatedMessage> message);
47
48 void HandleEvent();
Austin Schuh39788ff2019-12-01 18:22:57 -080049
50 void SetSimulatedChannel(SimulatedChannel *channel) {
51 simulated_channel_ = channel;
52 }
53
54 private:
Austin Schuh7d87b672019-12-01 20:23:49 -080055 void DoSchedule(monotonic_clock::time_point event_time);
56
57 ::std::deque<std::shared_ptr<SimulatedMessage>> msgs_;
58
59 SimulatedEventLoop *simulated_event_loop_;
60 EventHandler<SimulatedWatcher> event_;
61 EventScheduler *scheduler_;
62 EventScheduler::Token token_;
Austin Schuh39788ff2019-12-01 18:22:57 -080063 SimulatedChannel *simulated_channel_ = nullptr;
64};
Alex Perrycb7da4b2019-08-28 19:35:56 -070065
66class SimulatedChannel {
67 public:
68 explicit SimulatedChannel(const Channel *channel, EventScheduler *scheduler)
Austin Schuh39788ff2019-12-01 18:22:57 -080069 : channel_(channel),
Alex Perrycb7da4b2019-08-28 19:35:56 -070070 scheduler_(scheduler),
71 next_queue_index_(ipc_lib::QueueIndex::Zero(channel->max_size())) {}
72
73 ~SimulatedChannel() { CHECK_EQ(0u, fetchers_.size()); }
74
75 // Makes a connected raw sender which calls Send below.
76 ::std::unique_ptr<RawSender> MakeRawSender(EventLoop *event_loop);
77
78 // Makes a connected raw fetcher.
Austin Schuh39788ff2019-12-01 18:22:57 -080079 ::std::unique_ptr<RawFetcher> MakeRawFetcher(EventLoop *event_loop);
Alex Perrycb7da4b2019-08-28 19:35:56 -070080
81 // Registers a watcher for the queue.
Austin Schuh7d87b672019-12-01 20:23:49 -080082 void MakeRawWatcher(SimulatedWatcher *watcher);
Austin Schuh39788ff2019-12-01 18:22:57 -080083
Austin Schuh7d87b672019-12-01 20:23:49 -080084 void RemoveWatcher(SimulatedWatcher *watcher) {
Austin Schuh39788ff2019-12-01 18:22:57 -080085 watchers_.erase(std::find(watchers_.begin(), watchers_.end(), watcher));
86 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070087
Austin Schuhad154822019-12-27 15:45:13 -080088 // Sends the message to all the connected receivers and fetchers. Returns the
89 // sent queue index.
90 uint32_t Send(std::shared_ptr<SimulatedMessage> message);
Alex Perrycb7da4b2019-08-28 19:35:56 -070091
92 // Unregisters a fetcher.
93 void UnregisterFetcher(SimulatedFetcher *fetcher);
94
95 std::shared_ptr<SimulatedMessage> latest_message() { return latest_message_; }
96
Austin Schuh39788ff2019-12-01 18:22:57 -080097 size_t max_size() const { return channel()->max_size(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -070098
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080099 const std::string_view name() const {
Austin Schuh39788ff2019-12-01 18:22:57 -0800100 return channel()->name()->string_view();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700101 }
102
Austin Schuh39788ff2019-12-01 18:22:57 -0800103 const Channel *channel() const { return channel_; }
104
Austin Schuhe516ab02020-05-06 21:37:04 -0700105 void CountSenderCreated() {
106 if (sender_count_ >= channel()->num_senders()) {
107 LOG(FATAL) << "Failed to create sender on "
108 << configuration::CleanedChannelToString(channel())
109 << ", too many senders.";
110 }
111 ++sender_count_;
112 }
113 void CountSenderDestroyed() {
114 --sender_count_;
115 CHECK_GE(sender_count_, 0);
116 }
117
Alex Perrycb7da4b2019-08-28 19:35:56 -0700118 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800119 const Channel *channel_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700120
121 // List of all watchers.
Austin Schuh7d87b672019-12-01 20:23:49 -0800122 ::std::vector<SimulatedWatcher *> watchers_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700123
124 // List of all fetchers.
125 ::std::vector<SimulatedFetcher *> fetchers_;
126 std::shared_ptr<SimulatedMessage> latest_message_;
127 EventScheduler *scheduler_;
128
129 ipc_lib::QueueIndex next_queue_index_;
Austin Schuhe516ab02020-05-06 21:37:04 -0700130
131 int sender_count_ = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700132};
133
134namespace {
135
136// Creates a SimulatedMessage with size bytes of storage.
137// This is a shared_ptr so we don't have to implement refcounting or copying.
138std::shared_ptr<SimulatedMessage> MakeSimulatedMessage(size_t size) {
139 SimulatedMessage *message = reinterpret_cast<SimulatedMessage *>(
Brian Silvermana1652f32020-01-29 20:41:44 -0800140 malloc(sizeof(SimulatedMessage) + size + kChannelDataAlignment - 1));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700141 message->context.size = size;
Brian Silvermana1652f32020-01-29 20:41:44 -0800142 message->context.data = message->data(size);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700143
144 return std::shared_ptr<SimulatedMessage>(message, free);
145}
146
147class SimulatedSender : public RawSender {
148 public:
149 SimulatedSender(SimulatedChannel *simulated_channel, EventLoop *event_loop)
Austin Schuh39788ff2019-12-01 18:22:57 -0800150 : RawSender(event_loop, simulated_channel->channel()),
Austin Schuh54cf95f2019-11-29 13:14:18 -0800151 simulated_channel_(simulated_channel),
Austin Schuhe516ab02020-05-06 21:37:04 -0700152 event_loop_(event_loop) {
153 simulated_channel_->CountSenderCreated();
154 }
155 ~SimulatedSender() { simulated_channel_->CountSenderDestroyed(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700156
157 void *data() override {
158 if (!message_) {
159 message_ = MakeSimulatedMessage(simulated_channel_->max_size());
160 }
Brian Silvermana1652f32020-01-29 20:41:44 -0800161 return message_->data(simulated_channel_->max_size());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700162 }
163
164 size_t size() override { return simulated_channel_->max_size(); }
165
Austin Schuhad154822019-12-27 15:45:13 -0800166 bool DoSend(size_t length,
167 aos::monotonic_clock::time_point monotonic_remote_time,
168 aos::realtime_clock::time_point realtime_remote_time,
169 uint32_t remote_queue_index) override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700170 CHECK_LE(length, size()) << ": Attempting to send too big a message.";
Austin Schuhad154822019-12-27 15:45:13 -0800171 message_->context.monotonic_event_time = event_loop_->monotonic_now();
172 message_->context.monotonic_remote_time = monotonic_remote_time;
173 message_->context.remote_queue_index = remote_queue_index;
174 message_->context.realtime_event_time = event_loop_->realtime_now();
175 message_->context.realtime_remote_time = realtime_remote_time;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700176 CHECK_LE(length, message_->context.size);
177 message_->context.size = length;
178
179 // TODO(austin): Track sending too fast.
Austin Schuhad154822019-12-27 15:45:13 -0800180 sent_queue_index_ = simulated_channel_->Send(message_);
181 monotonic_sent_time_ = event_loop_->monotonic_now();
182 realtime_sent_time_ = event_loop_->realtime_now();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700183
184 // Drop the reference to the message so that we allocate a new message for
185 // next time. Otherwise we will continue to reuse the same memory for all
186 // messages and corrupt it.
187 message_.reset();
188 return true;
189 }
190
Austin Schuhad154822019-12-27 15:45:13 -0800191 bool DoSend(const void *msg, size_t size,
192 aos::monotonic_clock::time_point monotonic_remote_time,
193 aos::realtime_clock::time_point realtime_remote_time,
194 uint32_t remote_queue_index) override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700195 CHECK_LE(size, this->size()) << ": Attempting to send too big a message.";
196
197 // This is wasteful, but since flatbuffers fill from the back end of the
198 // queue, we need it to be full sized.
199 message_ = MakeSimulatedMessage(simulated_channel_->max_size());
200
201 // Now fill in the message. size is already populated above, and
Austin Schuhac0771c2020-01-07 18:36:30 -0800202 // queue_index will be populated in simulated_channel_. Put this at the
203 // back of the data segment.
Brian Silvermana1652f32020-01-29 20:41:44 -0800204 memcpy(message_->data(simulated_channel_->max_size()) +
205 simulated_channel_->max_size() - size,
206 msg, size);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700207
Austin Schuhac0771c2020-01-07 18:36:30 -0800208 return DoSend(size, monotonic_remote_time, realtime_remote_time,
209 remote_queue_index);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700210 }
211
Alex Perrycb7da4b2019-08-28 19:35:56 -0700212 private:
213 SimulatedChannel *simulated_channel_;
214 EventLoop *event_loop_;
215
216 std::shared_ptr<SimulatedMessage> message_;
217};
218} // namespace
219
220class SimulatedFetcher : public RawFetcher {
221 public:
Austin Schuhac0771c2020-01-07 18:36:30 -0800222 explicit SimulatedFetcher(EventLoop *event_loop,
223 SimulatedChannel *simulated_channel)
224 : RawFetcher(event_loop, simulated_channel->channel()),
225 simulated_channel_(simulated_channel) {}
226 ~SimulatedFetcher() { simulated_channel_->UnregisterFetcher(this); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700227
Austin Schuh39788ff2019-12-01 18:22:57 -0800228 std::pair<bool, monotonic_clock::time_point> DoFetchNext() override {
229 if (msgs_.size() == 0) {
230 return std::make_pair(false, monotonic_clock::min_time);
231 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700232
233 SetMsg(msgs_.front());
234 msgs_.pop_front();
Austin Schuha5e14192020-01-06 18:02:41 -0800235 return std::make_pair(true, event_loop()->monotonic_now());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700236 }
237
Austin Schuh39788ff2019-12-01 18:22:57 -0800238 std::pair<bool, monotonic_clock::time_point> DoFetch() override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700239 if (msgs_.size() == 0) {
Austin Schuh7d87b672019-12-01 20:23:49 -0800240 // TODO(austin): Can we just do this logic unconditionally? It is a lot
241 // simpler. And call clear, obviously.
Austin Schuhac0771c2020-01-07 18:36:30 -0800242 if (!msg_ && simulated_channel_->latest_message()) {
243 SetMsg(simulated_channel_->latest_message());
Austin Schuha5e14192020-01-06 18:02:41 -0800244 return std::make_pair(true, event_loop()->monotonic_now());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700245 } else {
Austin Schuh39788ff2019-12-01 18:22:57 -0800246 return std::make_pair(false, monotonic_clock::min_time);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700247 }
248 }
249
250 // We've had a message enqueued, so we don't need to go looking for the
251 // latest message from before we started.
252 SetMsg(msgs_.back());
253 msgs_.clear();
Austin Schuha5e14192020-01-06 18:02:41 -0800254 return std::make_pair(true, event_loop()->monotonic_now());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700255 }
256
257 private:
258 friend class SimulatedChannel;
259
260 // Updates the state inside RawFetcher to point to the data in msg_.
261 void SetMsg(std::shared_ptr<SimulatedMessage> msg) {
262 msg_ = msg;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700263 context_ = msg_->context;
Austin Schuhad154822019-12-27 15:45:13 -0800264 if (context_.remote_queue_index == 0xffffffffu) {
265 context_.remote_queue_index = context_.queue_index;
266 }
267 if (context_.monotonic_remote_time == aos::monotonic_clock::min_time) {
268 context_.monotonic_remote_time = context_.monotonic_event_time;
269 }
270 if (context_.realtime_remote_time == aos::realtime_clock::min_time) {
271 context_.realtime_remote_time = context_.realtime_event_time;
272 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700273 }
274
275 // Internal method for Simulation to add a message to the buffer.
276 void Enqueue(std::shared_ptr<SimulatedMessage> buffer) {
277 msgs_.emplace_back(buffer);
278 }
279
Austin Schuhac0771c2020-01-07 18:36:30 -0800280 SimulatedChannel *simulated_channel_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700281 std::shared_ptr<SimulatedMessage> msg_;
282
283 // Messages queued up but not in use.
284 ::std::deque<std::shared_ptr<SimulatedMessage>> msgs_;
285};
286
287class SimulatedTimerHandler : public TimerHandler {
288 public:
289 explicit SimulatedTimerHandler(EventScheduler *scheduler,
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800290 SimulatedEventLoop *simulated_event_loop,
Austin Schuh39788ff2019-12-01 18:22:57 -0800291 ::std::function<void()> fn);
Austin Schuh7d87b672019-12-01 20:23:49 -0800292 ~SimulatedTimerHandler() { Disable(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700293
294 void Setup(monotonic_clock::time_point base,
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800295 monotonic_clock::duration repeat_offset) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700296
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800297 void HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700298
Austin Schuh7d87b672019-12-01 20:23:49 -0800299 void Disable() override;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700300
Alex Perrycb7da4b2019-08-28 19:35:56 -0700301 private:
Austin Schuh7d87b672019-12-01 20:23:49 -0800302 SimulatedEventLoop *simulated_event_loop_;
303 EventHandler<SimulatedTimerHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700304 EventScheduler *scheduler_;
305 EventScheduler::Token token_;
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800306
Alex Perrycb7da4b2019-08-28 19:35:56 -0700307 monotonic_clock::time_point base_;
308 monotonic_clock::duration repeat_offset_;
309};
310
311class SimulatedPhasedLoopHandler : public PhasedLoopHandler {
312 public:
313 SimulatedPhasedLoopHandler(EventScheduler *scheduler,
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800314 SimulatedEventLoop *simulated_event_loop,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700315 ::std::function<void(int)> fn,
316 const monotonic_clock::duration interval,
Austin Schuh39788ff2019-12-01 18:22:57 -0800317 const monotonic_clock::duration offset);
Austin Schuh7d87b672019-12-01 20:23:49 -0800318 ~SimulatedPhasedLoopHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700319
Austin Schuh7d87b672019-12-01 20:23:49 -0800320 void HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700321
Austin Schuh7d87b672019-12-01 20:23:49 -0800322 void Schedule(monotonic_clock::time_point sleep_time) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700323
324 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800325 SimulatedEventLoop *simulated_event_loop_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800326 EventHandler<SimulatedPhasedLoopHandler> event_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700327
Austin Schuh39788ff2019-12-01 18:22:57 -0800328 EventScheduler *scheduler_;
329 EventScheduler::Token token_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700330};
331
332class SimulatedEventLoop : public EventLoop {
333 public:
334 explicit SimulatedEventLoop(
335 EventScheduler *scheduler,
Austin Schuhac0771c2020-01-07 18:36:30 -0800336 NodeEventLoopFactory *node_event_loop_factory,
Alex Perrycb7da4b2019-08-28 19:35:56 -0700337 absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>>
338 *channels,
339 const Configuration *configuration,
340 std::vector<std::pair<EventLoop *, std::function<void(bool)>>>
Austin Schuh39788ff2019-12-01 18:22:57 -0800341 *raw_event_loops,
Austin Schuh217a9782019-12-21 23:02:50 -0800342 const Node *node, pid_t tid)
Austin Schuh39788ff2019-12-01 18:22:57 -0800343 : EventLoop(CHECK_NOTNULL(configuration)),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700344 scheduler_(scheduler),
Austin Schuhac0771c2020-01-07 18:36:30 -0800345 node_event_loop_factory_(node_event_loop_factory),
Alex Perrycb7da4b2019-08-28 19:35:56 -0700346 channels_(channels),
Austin Schuh39788ff2019-12-01 18:22:57 -0800347 raw_event_loops_(raw_event_loops),
Austin Schuh217a9782019-12-21 23:02:50 -0800348 node_(node),
Austin Schuh39788ff2019-12-01 18:22:57 -0800349 tid_(tid) {
350 raw_event_loops_->push_back(std::make_pair(this, [this](bool value) {
351 if (!has_setup_) {
352 Setup();
353 has_setup_ = true;
354 }
355 set_is_running(value);
356 }));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700357 }
358 ~SimulatedEventLoop() override {
Austin Schuh39788ff2019-12-01 18:22:57 -0800359 // Trigger any remaining senders or fetchers to be cleared before destroying
360 // the event loop so the book keeping matches.
361 timing_report_sender_.reset();
362
363 // Force everything with a registered fd with epoll to be destroyed now.
364 timers_.clear();
365 phased_loops_.clear();
366 watchers_.clear();
367
Alex Perrycb7da4b2019-08-28 19:35:56 -0700368 for (auto it = raw_event_loops_->begin(); it != raw_event_loops_->end();
369 ++it) {
370 if (it->first == this) {
371 raw_event_loops_->erase(it);
372 break;
373 }
374 }
375 }
376
Austin Schuh7d87b672019-12-01 20:23:49 -0800377 std::chrono::nanoseconds send_delay() const { return send_delay_; }
378 void set_send_delay(std::chrono::nanoseconds send_delay) {
379 send_delay_ = send_delay;
380 }
381
Alex Perrycb7da4b2019-08-28 19:35:56 -0700382 ::aos::monotonic_clock::time_point monotonic_now() override {
Austin Schuhac0771c2020-01-07 18:36:30 -0800383 return node_event_loop_factory_->monotonic_now();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700384 }
385
386 ::aos::realtime_clock::time_point realtime_now() override {
Austin Schuhac0771c2020-01-07 18:36:30 -0800387 return node_event_loop_factory_->realtime_now();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700388 }
389
390 ::std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override;
391
392 ::std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override;
393
394 void MakeRawWatcher(
395 const Channel *channel,
396 ::std::function<void(const Context &context, const void *message)>
397 watcher) override;
398
399 TimerHandler *AddTimer(::std::function<void()> callback) override {
Austin Schuh39788ff2019-12-01 18:22:57 -0800400 CHECK(!is_running());
Austin Schuh8bd96322020-02-13 21:18:22 -0800401 return NewTimer(::std::unique_ptr<TimerHandler>(
402 new SimulatedTimerHandler(scheduler_, this, callback)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700403 }
404
405 PhasedLoopHandler *AddPhasedLoop(::std::function<void(int)> callback,
406 const monotonic_clock::duration interval,
407 const monotonic_clock::duration offset =
408 ::std::chrono::seconds(0)) override {
Austin Schuh8bd96322020-02-13 21:18:22 -0800409 return NewPhasedLoop(
410 ::std::unique_ptr<PhasedLoopHandler>(new SimulatedPhasedLoopHandler(
411 scheduler_, this, callback, interval, offset)));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700412 }
413
414 void OnRun(::std::function<void()> on_run) override {
Austin Schuh39788ff2019-12-01 18:22:57 -0800415 scheduler_->ScheduleOnRun(on_run);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700416 }
417
Austin Schuh217a9782019-12-21 23:02:50 -0800418 const Node *node() const override { return node_; }
419
James Kuszmaul3ae42262019-11-08 12:33:41 -0800420 void set_name(const std::string_view name) override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700421 name_ = std::string(name);
422 }
James Kuszmaul3ae42262019-11-08 12:33:41 -0800423 const std::string_view name() const override { return name_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700424
425 SimulatedChannel *GetSimulatedChannel(const Channel *channel);
426
Austin Schuh39788ff2019-12-01 18:22:57 -0800427 void SetRuntimeRealtimePriority(int priority) override {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700428 CHECK(!is_running()) << ": Cannot set realtime priority while running.";
Austin Schuh39788ff2019-12-01 18:22:57 -0800429 priority_ = priority;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700430 }
431
Austin Schuh39788ff2019-12-01 18:22:57 -0800432 int priority() const override { return priority_; }
433
Brian Silverman6a54ff32020-04-28 16:41:39 -0700434 void SetRuntimeAffinity(const cpu_set_t & /*cpuset*/) override {
435 CHECK(!is_running()) << ": Cannot set affinity while running.";
436 }
437
Tyler Chatow67ddb032020-01-12 14:30:04 -0800438 void Setup() {
439 MaybeScheduleTimingReports();
440 if (!skip_logger_) {
441 logging::ScopedLogRestorer prev_logger;
442 log_sender_.Initialize(MakeSender<logging::LogMessageFbs>("/aos"));
443 log_impl_ = logging::GetImplementation();
444 }
445 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800446
Alex Perrycb7da4b2019-08-28 19:35:56 -0700447 private:
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800448 friend class SimulatedTimerHandler;
Austin Schuh7d87b672019-12-01 20:23:49 -0800449 friend class SimulatedPhasedLoopHandler;
450 friend class SimulatedWatcher;
451
452 void HandleEvent() {
453 while (true) {
454 if (EventCount() == 0 || PeekEvent()->event_time() > monotonic_now()) {
455 break;
456 }
457
458 EventLoopEvent *event = PopEvent();
459 event->HandleEvent();
460 }
461 }
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800462
Austin Schuh39788ff2019-12-01 18:22:57 -0800463 pid_t GetTid() override { return tid_; }
464
Alex Perrycb7da4b2019-08-28 19:35:56 -0700465 EventScheduler *scheduler_;
Austin Schuhac0771c2020-01-07 18:36:30 -0800466 NodeEventLoopFactory *node_event_loop_factory_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700467 absl::btree_map<SimpleChannel, std::unique_ptr<SimulatedChannel>> *channels_;
468 std::vector<std::pair<EventLoop *, std::function<void(bool)>>>
469 *raw_event_loops_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700470
471 ::std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800472
473 int priority_ = 0;
474
475 bool has_setup_ = false;
476
Austin Schuh7d87b672019-12-01 20:23:49 -0800477 std::chrono::nanoseconds send_delay_;
478
Austin Schuh217a9782019-12-21 23:02:50 -0800479 const Node *const node_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800480 const pid_t tid_;
Tyler Chatow67ddb032020-01-12 14:30:04 -0800481
482 AosLogToFbs log_sender_;
483 logging::LogImplementation *log_impl_ = nullptr;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700484};
485
Austin Schuh7d87b672019-12-01 20:23:49 -0800486void SimulatedEventLoopFactory::set_send_delay(
487 std::chrono::nanoseconds send_delay) {
488 send_delay_ = send_delay;
489 for (std::pair<EventLoop *, std::function<void(bool)>> &loop :
490 raw_event_loops_) {
491 reinterpret_cast<SimulatedEventLoop *>(loop.first)
492 ->set_send_delay(send_delay_);
493 }
494}
495
Alex Perrycb7da4b2019-08-28 19:35:56 -0700496void SimulatedEventLoop::MakeRawWatcher(
497 const Channel *channel,
498 std::function<void(const Context &channel, const void *message)> watcher) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800499 TakeWatcher(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800500
Austin Schuh8bd96322020-02-13 21:18:22 -0800501 std::unique_ptr<SimulatedWatcher> shm_watcher(
502 new SimulatedWatcher(this, scheduler_, channel, std::move(watcher)));
Austin Schuh39788ff2019-12-01 18:22:57 -0800503
504 GetSimulatedChannel(channel)->MakeRawWatcher(shm_watcher.get());
505 NewWatcher(std::move(shm_watcher));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700506}
507
508std::unique_ptr<RawSender> SimulatedEventLoop::MakeRawSender(
509 const Channel *channel) {
Brian Silverman0fc69932020-01-24 21:54:02 -0800510 TakeSender(channel);
511
Alex Perrycb7da4b2019-08-28 19:35:56 -0700512 return GetSimulatedChannel(channel)->MakeRawSender(this);
513}
514
515std::unique_ptr<RawFetcher> SimulatedEventLoop::MakeRawFetcher(
516 const Channel *channel) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800517 ChannelIndex(channel);
Austin Schuh217a9782019-12-21 23:02:50 -0800518
Austin Schuhca4828c2019-12-28 14:21:35 -0800519 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
520 LOG(FATAL) << "Channel { \"name\": \"" << channel->name()->string_view()
521 << "\", \"type\": \"" << channel->type()->string_view()
522 << "\" } is not able to be fetched on this node. Check your "
523 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800524 }
525
Austin Schuh39788ff2019-12-01 18:22:57 -0800526 return GetSimulatedChannel(channel)->MakeRawFetcher(this);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700527}
528
529SimulatedChannel *SimulatedEventLoop::GetSimulatedChannel(
530 const Channel *channel) {
531 auto it = channels_->find(SimpleChannel(channel));
532 if (it == channels_->end()) {
533 it = channels_
534 ->emplace(SimpleChannel(channel),
535 std::unique_ptr<SimulatedChannel>(
536 new SimulatedChannel(channel, scheduler_)))
537 .first;
538 }
539 return it->second.get();
540}
541
Austin Schuh7d87b672019-12-01 20:23:49 -0800542SimulatedWatcher::SimulatedWatcher(
543 SimulatedEventLoop *simulated_event_loop, EventScheduler *scheduler,
Austin Schuh8bd96322020-02-13 21:18:22 -0800544 const Channel *channel,
Austin Schuh7d87b672019-12-01 20:23:49 -0800545 std::function<void(const Context &context, const void *message)> fn)
546 : WatcherState(simulated_event_loop, channel, std::move(fn)),
547 simulated_event_loop_(simulated_event_loop),
548 event_(this),
549 scheduler_(scheduler),
550 token_(scheduler_->InvalidToken()) {}
551
552SimulatedWatcher::~SimulatedWatcher() {
553 simulated_event_loop_->RemoveEvent(&event_);
554 if (token_ != scheduler_->InvalidToken()) {
555 scheduler_->Deschedule(token_);
556 }
557 simulated_channel_->RemoveWatcher(this);
558}
559
560void SimulatedWatcher::Schedule(std::shared_ptr<SimulatedMessage> message) {
Austin Schuha5e14192020-01-06 18:02:41 -0800561 monotonic_clock::time_point event_time =
562 simulated_event_loop_->monotonic_now();
Austin Schuh7d87b672019-12-01 20:23:49 -0800563
564 // Messages are queued in order. If we are the first, add ourselves.
565 // Otherwise, don't.
566 if (msgs_.size() == 0) {
Austin Schuhad154822019-12-27 15:45:13 -0800567 event_.set_event_time(message->context.monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800568 simulated_event_loop_->AddEvent(&event_);
569
570 DoSchedule(event_time);
571 }
572
573 msgs_.emplace_back(message);
574}
575
576void SimulatedWatcher::HandleEvent() {
577 CHECK_NE(msgs_.size(), 0u) << ": No events to handle.";
578
579 const monotonic_clock::time_point monotonic_now =
580 simulated_event_loop_->monotonic_now();
Tyler Chatow67ddb032020-01-12 14:30:04 -0800581 logging::ScopedLogRestorer prev_logger;
582 if (simulated_event_loop_->log_impl_ != nullptr) {
583 logging::SetImplementation(simulated_event_loop_->log_impl_);
584 }
Austin Schuhad154822019-12-27 15:45:13 -0800585 Context context = msgs_.front()->context;
586
587 if (context.remote_queue_index == 0xffffffffu) {
588 context.remote_queue_index = context.queue_index;
589 }
590 if (context.monotonic_remote_time == aos::monotonic_clock::min_time) {
591 context.monotonic_remote_time = context.monotonic_event_time;
592 }
593 if (context.realtime_remote_time == aos::realtime_clock::min_time) {
594 context.realtime_remote_time = context.realtime_event_time;
595 }
596
597 DoCallCallback([monotonic_now]() { return monotonic_now; }, context);
Austin Schuh7d87b672019-12-01 20:23:49 -0800598
599 msgs_.pop_front();
600 if (msgs_.size() != 0) {
Austin Schuhad154822019-12-27 15:45:13 -0800601 event_.set_event_time(msgs_.front()->context.monotonic_event_time);
Austin Schuh7d87b672019-12-01 20:23:49 -0800602 simulated_event_loop_->AddEvent(&event_);
603
604 DoSchedule(event_.event_time());
605 } else {
606 token_ = scheduler_->InvalidToken();
607 }
608}
609
610void SimulatedWatcher::DoSchedule(monotonic_clock::time_point event_time) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800611 token_ =
612 scheduler_->Schedule(event_time + simulated_event_loop_->send_delay(),
613 [this]() { simulated_event_loop_->HandleEvent(); });
Austin Schuh7d87b672019-12-01 20:23:49 -0800614}
615
616void SimulatedChannel::MakeRawWatcher(SimulatedWatcher *watcher) {
Austin Schuh39788ff2019-12-01 18:22:57 -0800617 watcher->SetSimulatedChannel(this);
618 watchers_.emplace_back(watcher);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700619}
620
621::std::unique_ptr<RawSender> SimulatedChannel::MakeRawSender(
622 EventLoop *event_loop) {
623 return ::std::unique_ptr<RawSender>(new SimulatedSender(this, event_loop));
624}
625
Austin Schuh39788ff2019-12-01 18:22:57 -0800626::std::unique_ptr<RawFetcher> SimulatedChannel::MakeRawFetcher(
627 EventLoop *event_loop) {
628 ::std::unique_ptr<SimulatedFetcher> fetcher(
629 new SimulatedFetcher(event_loop, this));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700630 fetchers_.push_back(fetcher.get());
631 return ::std::move(fetcher);
632}
633
Austin Schuhad154822019-12-27 15:45:13 -0800634uint32_t SimulatedChannel::Send(std::shared_ptr<SimulatedMessage> message) {
635 const uint32_t queue_index = next_queue_index_.index();
636 message->context.queue_index = queue_index;
Brian Silvermana1652f32020-01-29 20:41:44 -0800637 message->context.data = message->data(channel()->max_size()) +
638 channel()->max_size() - message->context.size;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700639 next_queue_index_ = next_queue_index_.Increment();
640
641 latest_message_ = message;
642 if (scheduler_->is_running()) {
Austin Schuh7d87b672019-12-01 20:23:49 -0800643 for (SimulatedWatcher *watcher : watchers_) {
644 watcher->Schedule(message);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700645 }
646 }
647 for (auto &fetcher : fetchers_) {
648 fetcher->Enqueue(message);
649 }
Austin Schuhad154822019-12-27 15:45:13 -0800650
651 return queue_index;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700652}
653
654void SimulatedChannel::UnregisterFetcher(SimulatedFetcher *fetcher) {
655 fetchers_.erase(::std::find(fetchers_.begin(), fetchers_.end(), fetcher));
656}
657
Austin Schuh39788ff2019-12-01 18:22:57 -0800658SimulatedTimerHandler::SimulatedTimerHandler(
Austin Schuh8bd96322020-02-13 21:18:22 -0800659 EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop,
660 ::std::function<void()> fn)
Austin Schuh39788ff2019-12-01 18:22:57 -0800661 : TimerHandler(simulated_event_loop, std::move(fn)),
Austin Schuh7d87b672019-12-01 20:23:49 -0800662 simulated_event_loop_(simulated_event_loop),
663 event_(this),
Austin Schuh39788ff2019-12-01 18:22:57 -0800664 scheduler_(scheduler),
665 token_(scheduler_->InvalidToken()) {}
666
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800667void SimulatedTimerHandler::Setup(monotonic_clock::time_point base,
668 monotonic_clock::duration repeat_offset) {
669 Disable();
670 const ::aos::monotonic_clock::time_point monotonic_now =
Austin Schuha5e14192020-01-06 18:02:41 -0800671 simulated_event_loop_->monotonic_now();
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800672 base_ = base;
673 repeat_offset_ = repeat_offset;
674 if (base < monotonic_now) {
Austin Schuh7d87b672019-12-01 20:23:49 -0800675 token_ = scheduler_->Schedule(
Austin Schuh8bd96322020-02-13 21:18:22 -0800676 monotonic_now, [this]() { simulated_event_loop_->HandleEvent(); });
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800677 } else {
Austin Schuh7d87b672019-12-01 20:23:49 -0800678 token_ = scheduler_->Schedule(
Austin Schuh8bd96322020-02-13 21:18:22 -0800679 base, [this]() { simulated_event_loop_->HandleEvent(); });
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800680 }
Austin Schuh7d87b672019-12-01 20:23:49 -0800681 event_.set_event_time(base_);
682 simulated_event_loop_->AddEvent(&event_);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800683}
684
685void SimulatedTimerHandler::HandleEvent() {
686 const ::aos::monotonic_clock::time_point monotonic_now =
Austin Schuha5e14192020-01-06 18:02:41 -0800687 simulated_event_loop_->monotonic_now();
Tyler Chatow67ddb032020-01-12 14:30:04 -0800688 logging::ScopedLogRestorer prev_logger;
689 if (simulated_event_loop_->log_impl_ != nullptr) {
690 logging::SetImplementation(simulated_event_loop_->log_impl_);
691 }
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800692 if (repeat_offset_ != ::aos::monotonic_clock::zero()) {
693 // Reschedule.
694 while (base_ <= monotonic_now) base_ += repeat_offset_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800695 token_ = scheduler_->Schedule(
Austin Schuh8bd96322020-02-13 21:18:22 -0800696 base_, [this]() { simulated_event_loop_->HandleEvent(); });
Austin Schuh7d87b672019-12-01 20:23:49 -0800697 event_.set_event_time(base_);
698 simulated_event_loop_->AddEvent(&event_);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800699 } else {
700 token_ = scheduler_->InvalidToken();
701 }
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800702
Austin Schuh39788ff2019-12-01 18:22:57 -0800703 Call([monotonic_now]() { return monotonic_now; }, monotonic_now);
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800704}
705
Austin Schuh7d87b672019-12-01 20:23:49 -0800706void SimulatedTimerHandler::Disable() {
707 simulated_event_loop_->RemoveEvent(&event_);
708 if (token_ != scheduler_->InvalidToken()) {
709 scheduler_->Deschedule(token_);
710 token_ = scheduler_->InvalidToken();
711 }
712}
713
Austin Schuh39788ff2019-12-01 18:22:57 -0800714SimulatedPhasedLoopHandler::SimulatedPhasedLoopHandler(
Austin Schuh8bd96322020-02-13 21:18:22 -0800715 EventScheduler *scheduler, SimulatedEventLoop *simulated_event_loop,
716 ::std::function<void(int)> fn, const monotonic_clock::duration interval,
Austin Schuh39788ff2019-12-01 18:22:57 -0800717 const monotonic_clock::duration offset)
718 : PhasedLoopHandler(simulated_event_loop, std::move(fn), interval, offset),
719 simulated_event_loop_(simulated_event_loop),
Austin Schuh7d87b672019-12-01 20:23:49 -0800720 event_(this),
Austin Schuh39788ff2019-12-01 18:22:57 -0800721 scheduler_(scheduler),
722 token_(scheduler_->InvalidToken()) {}
723
Austin Schuh7d87b672019-12-01 20:23:49 -0800724SimulatedPhasedLoopHandler::~SimulatedPhasedLoopHandler() {
725 if (token_ != scheduler_->InvalidToken()) {
726 scheduler_->Deschedule(token_);
727 token_ = scheduler_->InvalidToken();
728 }
729 simulated_event_loop_->RemoveEvent(&event_);
730}
731
732void SimulatedPhasedLoopHandler::HandleEvent() {
Austin Schuh39788ff2019-12-01 18:22:57 -0800733 monotonic_clock::time_point monotonic_now =
734 simulated_event_loop_->monotonic_now();
Tyler Chatow67ddb032020-01-12 14:30:04 -0800735 logging::ScopedLogRestorer prev_logger;
736 if (simulated_event_loop_->log_impl_ != nullptr) {
737 logging::SetImplementation(simulated_event_loop_->log_impl_);
738 }
Austin Schuh39788ff2019-12-01 18:22:57 -0800739 Call(
740 [monotonic_now]() { return monotonic_now; },
741 [this](monotonic_clock::time_point sleep_time) { Schedule(sleep_time); });
742}
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800743
Austin Schuh7d87b672019-12-01 20:23:49 -0800744void SimulatedPhasedLoopHandler::Schedule(
745 monotonic_clock::time_point sleep_time) {
746 token_ = scheduler_->Schedule(
Austin Schuh8bd96322020-02-13 21:18:22 -0800747 sleep_time, [this]() { simulated_event_loop_->HandleEvent(); });
Austin Schuh7d87b672019-12-01 20:23:49 -0800748 event_.set_event_time(sleep_time);
749 simulated_event_loop_->AddEvent(&event_);
750}
751
Austin Schuhac0771c2020-01-07 18:36:30 -0800752NodeEventLoopFactory::NodeEventLoopFactory(
Austin Schuh8bd96322020-02-13 21:18:22 -0800753 EventSchedulerScheduler *scheduler_scheduler,
754 SimulatedEventLoopFactory *factory, const Node *node,
Austin Schuhac0771c2020-01-07 18:36:30 -0800755 std::vector<std::pair<EventLoop *, std::function<void(bool)>>>
756 *raw_event_loops)
Austin Schuh8bd96322020-02-13 21:18:22 -0800757 : factory_(factory), node_(node), raw_event_loops_(raw_event_loops) {
758 scheduler_scheduler->AddEventScheduler(&scheduler_);
759}
Austin Schuhac0771c2020-01-07 18:36:30 -0800760
Alex Perrycb7da4b2019-08-28 19:35:56 -0700761SimulatedEventLoopFactory::SimulatedEventLoopFactory(
762 const Configuration *configuration)
Austin Schuh6f3babe2020-01-26 20:34:50 -0800763 : configuration_(CHECK_NOTNULL(configuration)),
764 nodes_(configuration::GetNodes(configuration_)) {
Austin Schuhac0771c2020-01-07 18:36:30 -0800765 for (const Node *node : nodes_) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800766 node_factories_.emplace_back(new NodeEventLoopFactory(
767 &scheduler_scheduler_, this, node, &raw_event_loops_));
Austin Schuh15649d62019-12-28 16:36:38 -0800768 }
Austin Schuh898f4972020-01-11 17:21:25 -0800769
770 if (configuration::MultiNode(configuration)) {
771 bridge_ = std::make_unique<message_bridge::SimulatedMessageBridge>(this);
772 }
Austin Schuh15649d62019-12-28 16:36:38 -0800773}
774
Alex Perrycb7da4b2019-08-28 19:35:56 -0700775SimulatedEventLoopFactory::~SimulatedEventLoopFactory() {}
776
Austin Schuhac0771c2020-01-07 18:36:30 -0800777NodeEventLoopFactory *SimulatedEventLoopFactory::GetNodeEventLoopFactory(
778 const Node *node) {
779 auto result = std::find_if(
780 node_factories_.begin(), node_factories_.end(),
781 [node](const std::unique_ptr<NodeEventLoopFactory> &node_factory) {
782 return node_factory->node() == node;
783 });
784
785 CHECK(result != node_factories_.end())
786 << ": Failed to find node " << FlatbufferToJson(node);
787
788 return result->get();
789}
790
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800791::std::unique_ptr<EventLoop> SimulatedEventLoopFactory::MakeEventLoop(
Austin Schuhac0771c2020-01-07 18:36:30 -0800792 std::string_view name, const Node *node) {
793 if (node == nullptr) {
794 CHECK(!configuration::MultiNode(configuration()))
795 << ": Can't make a single node event loop in a multi-node world.";
796 } else {
797 CHECK(configuration::MultiNode(configuration()))
798 << ": Can't make a multi-node event loop in a single-node world.";
799 }
800 return GetNodeEventLoopFactory(node)->MakeEventLoop(name);
801}
802
803::std::unique_ptr<EventLoop> NodeEventLoopFactory::MakeEventLoop(
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800804 std::string_view name) {
Austin Schuh8bd96322020-02-13 21:18:22 -0800805 CHECK(!scheduler_.is_running())
806 << ": Can't create an event loop while running";
807
Austin Schuh39788ff2019-12-01 18:22:57 -0800808 pid_t tid = tid_;
809 ++tid_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800810 ::std::unique_ptr<SimulatedEventLoop> result(new SimulatedEventLoop(
Austin Schuh8bd96322020-02-13 21:18:22 -0800811 &scheduler_, this, &channels_, factory_->configuration(),
812 raw_event_loops_, node_, tid));
Austin Schuh5f1cc5c2019-12-01 18:01:11 -0800813 result->set_name(name);
Austin Schuhac0771c2020-01-07 18:36:30 -0800814 result->set_send_delay(factory_->send_delay());
Austin Schuh7d87b672019-12-01 20:23:49 -0800815 return std::move(result);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700816}
817
818void SimulatedEventLoopFactory::RunFor(monotonic_clock::duration duration) {
819 for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop :
820 raw_event_loops_) {
821 event_loop.second(true);
822 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800823 scheduler_scheduler_.RunFor(duration);
Austin Schuh39788ff2019-12-01 18:22:57 -0800824 for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop :
825 raw_event_loops_) {
826 event_loop.second(false);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700827 }
828}
829
830void SimulatedEventLoopFactory::Run() {
831 for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop :
832 raw_event_loops_) {
833 event_loop.second(true);
834 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800835 scheduler_scheduler_.Run();
Austin Schuh39788ff2019-12-01 18:22:57 -0800836 for (const std::pair<EventLoop *, std::function<void(bool)>> &event_loop :
837 raw_event_loops_) {
838 event_loop.second(false);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700839 }
840}
841
Austin Schuh6f3babe2020-01-26 20:34:50 -0800842void SimulatedEventLoopFactory::DisableForwarding(const Channel *channel) {
843 bridge_->DisableForwarding(channel);
844}
845
Alex Perrycb7da4b2019-08-28 19:35:56 -0700846} // namespace aos