blob: 8b29b23fdf55aadce1f32720aafa4d09ff666b35 [file] [log] [blame]
Austin Schuh898f4972020-01-11 17:21:25 -08001#include "aos/events/simulated_network_bridge.h"
2
Austin Schuh0de30f32020-12-06 12:44:28 -08003#include "absl/strings/str_cat.h"
4#include "aos/configuration.h"
Austin Schuh898f4972020-01-11 17:21:25 -08005#include "aos/events/event_loop.h"
6#include "aos/events/simulated_event_loop.h"
Austin Schuh0de30f32020-12-06 12:44:28 -08007#include "aos/network/remote_message_generated.h"
Austin Schuh898f4972020-01-11 17:21:25 -08008
9namespace aos {
10namespace message_bridge {
11
12// This class delays messages forwarded between two factories.
13//
14// The basic design is that we need to use the distributed_clock to convert
15// monotonic times from the source to the destination node. We also use a
16// fetcher to manage the queue of data, and a timer to schedule the sends.
17class RawMessageDelayer {
18 public:
19 RawMessageDelayer(aos::NodeEventLoopFactory *fetch_node_factory,
20 aos::NodeEventLoopFactory *send_node_factory,
Austin Schuheeaa2022021-01-02 21:52:03 -080021 aos::EventLoop *fetch_event_loop,
Austin Schuh898f4972020-01-11 17:21:25 -080022 aos::EventLoop *send_event_loop,
23 std::unique_ptr<aos::RawFetcher> fetcher,
Austin Schuh4c3b9702020-08-30 11:34:55 -070024 std::unique_ptr<aos::RawSender> sender,
Austin Schuh20ac95d2020-12-05 17:24:19 -080025 MessageBridgeServerStatus *server_status,
26 size_t destination_node_index,
Austin Schuh4c3b9702020-08-30 11:34:55 -070027 ServerConnection *server_connection, int client_index,
Austin Schuh2f8fd752020-09-01 22:38:28 -070028 MessageBridgeClientStatus *client_status,
29 size_t channel_index,
Austin Schuh0de30f32020-12-06 12:44:28 -080030 aos::Sender<RemoteMessage> *timestamp_logger)
Austin Schuh898f4972020-01-11 17:21:25 -080031 : fetch_node_factory_(fetch_node_factory),
32 send_node_factory_(send_node_factory),
Austin Schuheeaa2022021-01-02 21:52:03 -080033 fetch_event_loop_(fetch_event_loop),
Austin Schuh898f4972020-01-11 17:21:25 -080034 send_event_loop_(send_event_loop),
35 fetcher_(std::move(fetcher)),
Austin Schuh4c3b9702020-08-30 11:34:55 -070036 sender_(std::move(sender)),
Austin Schuh20ac95d2020-12-05 17:24:19 -080037 server_status_(server_status),
38 destination_node_index_(destination_node_index),
Austin Schuh4c3b9702020-08-30 11:34:55 -070039 server_connection_(server_connection),
40 client_status_(client_status),
41 client_index_(client_index),
Austin Schuh2f8fd752020-09-01 22:38:28 -070042 client_connection_(client_status_->GetClientConnection(client_index)),
43 channel_index_(channel_index),
44 timestamp_logger_(timestamp_logger) {
Austin Schuh898f4972020-01-11 17:21:25 -080045 timer_ = send_event_loop_->AddTimer([this]() { Send(); });
Austin Schuheaf54272021-02-07 22:51:34 -080046 std::string timer_name =
47 absl::StrCat(send_event_loop_->node()->name()->string_view(), " ",
48 fetcher_->channel()->name()->string_view(), " ",
49 fetcher_->channel()->type()->string_view());
50 timer_->set_name(timer_name);
Austin Schuheeaa2022021-01-02 21:52:03 -080051 timestamp_timer_ =
52 fetch_event_loop_->AddTimer([this]() { SendTimestamp(); });
Austin Schuheaf54272021-02-07 22:51:34 -080053 timestamp_timer_->set_name(absl::StrCat(timer_name, " timestamps"));
Austin Schuh898f4972020-01-11 17:21:25 -080054
55 Schedule();
56 }
57
Austin Schuh6f3babe2020-01-26 20:34:50 -080058 const Channel *channel() const { return fetcher_->channel(); }
59
Austin Schuh4c570ea2020-11-19 23:13:24 -080060 uint32_t time_to_live() {
61 return configuration::ConnectionToNode(sender_->channel(),
62 send_node_factory_->node())
63 ->time_to_live();
64 }
65
Austin Schuh898f4972020-01-11 17:21:25 -080066 // Kicks us to re-fetch and schedule the timer.
67 void Schedule() {
Austin Schuh6aa77be2020-02-22 21:06:40 -080068 // Keep pulling messages out of the fetcher until we find one in the future.
69 while (true) {
70 if (fetcher_->context().data == nullptr || sent_) {
71 sent_ = !fetcher_->FetchNext();
72 }
73 if (sent_) {
74 break;
75 }
Austin Schuhc0b0f722020-12-12 18:36:06 -080076
77 if (server_connection_->state() != State::CONNECTED) {
78 sent_ = true;
79 server_connection_->mutate_dropped_packets(
80 server_connection_->dropped_packets() + 1);
81 continue;
82 }
83
Austin Schuh6aa77be2020-02-22 21:06:40 -080084 if (fetcher_->context().monotonic_event_time +
85 send_node_factory_->network_delay() +
86 send_node_factory_->send_delay() >
87 fetch_node_factory_->monotonic_now()) {
88 break;
89 }
90
91 // TODO(austin): Not cool. We want to actually forward these. This means
92 // we need a more sophisticated concept of what is running.
93 LOG(WARNING) << "Not forwarding message on "
94 << configuration::CleanedChannelToString(fetcher_->channel())
95 << " because we aren't running. Set at "
96 << fetcher_->context().monotonic_event_time << " now is "
97 << fetch_node_factory_->monotonic_now();
98 sent_ = true;
Austin Schuh4c3b9702020-08-30 11:34:55 -070099 server_connection_->mutate_dropped_packets(
100 server_connection_->dropped_packets() + 1);
Austin Schuh898f4972020-01-11 17:21:25 -0800101 }
102
103 if (fetcher_->context().data == nullptr) {
104 return;
105 }
106
107 if (sent_) {
108 return;
109 }
110
111 // Compute the time to publish this message.
112 const monotonic_clock::time_point monotonic_delivered_time =
113 DeliveredTime(fetcher_->context());
114
115 CHECK_GE(monotonic_delivered_time, send_node_factory_->monotonic_now())
Austin Schuh2febf0d2020-09-21 22:24:30 -0700116 << ": Trying to deliver message in the past on channel "
117 << configuration::StrippedChannelToString(fetcher_->channel())
118 << " to node " << send_event_loop_->node()->name()->string_view()
119 << " sent from " << fetcher_->channel()->source_node()->string_view()
120 << " at " << fetch_node_factory_->monotonic_now();
Austin Schuh898f4972020-01-11 17:21:25 -0800121
Austin Schuh4c3b9702020-08-30 11:34:55 -0700122 server_connection_->mutate_sent_packets(server_connection_->sent_packets() +
123 1);
Austin Schuh898f4972020-01-11 17:21:25 -0800124 timer_->Setup(monotonic_delivered_time);
125 }
126
127 private:
128 // Acutally sends the message, and reschedules.
129 void Send() {
Austin Schuhc0b0f722020-12-12 18:36:06 -0800130 if (server_connection_->state() != State::CONNECTED) {
131 sent_ = true;
132 Schedule();
133 return;
134 }
Austin Schuh4c3b9702020-08-30 11:34:55 -0700135 // Fill out the send times.
Austin Schuh898f4972020-01-11 17:21:25 -0800136 sender_->Send(fetcher_->context().data, fetcher_->context().size,
137 fetcher_->context().monotonic_event_time,
138 fetcher_->context().realtime_event_time,
139 fetcher_->context().queue_index);
140
Austin Schuh4c3b9702020-08-30 11:34:55 -0700141 // And simulate message_bridge's offset recovery.
142 client_status_->SampleFilter(client_index_,
143 fetcher_->context().monotonic_event_time,
144 sender_->monotonic_sent_time());
145
146 client_connection_->mutate_received_packets(
147 client_connection_->received_packets() + 1);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700148
149 if (timestamp_logger_) {
Austin Schuheeaa2022021-01-02 21:52:03 -0800150 flatbuffers::FlatBufferBuilder fbb;
151 fbb.ForceDefaults(true);
Austin Schuh20ac95d2020-12-05 17:24:19 -0800152 // Reset the filter every time the UUID changes. There's probably a more
153 // clever way to do this, but that means a better concept of rebooting.
154 if (server_status_->BootUUID(destination_node_index_) !=
155 send_node_factory_->boot_uuid().string_view()) {
156 server_status_->ResetFilter(destination_node_index_);
157 server_status_->SetBootUUID(
158 destination_node_index_,
159 send_node_factory_->boot_uuid().string_view());
160 }
161
162 flatbuffers::Offset<flatbuffers::String> boot_uuid_offset =
Austin Schuheeaa2022021-01-02 21:52:03 -0800163 fbb.CreateString(
Austin Schuh20ac95d2020-12-05 17:24:19 -0800164 send_node_factory_->boot_uuid().string_view());
165
Austin Schuheeaa2022021-01-02 21:52:03 -0800166 RemoteMessage::Builder message_header_builder(fbb);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700167
168 message_header_builder.add_channel_index(channel_index_);
169
170 // Swap the remote and sent metrics. They are from the sender's
171 // perspective, not the receiver's perspective.
172 message_header_builder.add_monotonic_remote_time(
173 fetcher_->context().monotonic_event_time.time_since_epoch().count());
174 message_header_builder.add_realtime_remote_time(
175 fetcher_->context().realtime_event_time.time_since_epoch().count());
176 message_header_builder.add_remote_queue_index(
177 fetcher_->context().queue_index);
178
179 message_header_builder.add_monotonic_sent_time(
180 sender_->monotonic_sent_time().time_since_epoch().count());
181 message_header_builder.add_realtime_sent_time(
182 sender_->realtime_sent_time().time_since_epoch().count());
183 message_header_builder.add_queue_index(sender_->sent_queue_index());
Austin Schuh20ac95d2020-12-05 17:24:19 -0800184 message_header_builder.add_boot_uuid(boot_uuid_offset);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700185
Austin Schuheeaa2022021-01-02 21:52:03 -0800186 fbb.Finish(message_header_builder.Finish());
187
188 remote_timestamps_.emplace_back(
189 FlatbufferDetachedBuffer<RemoteMessage>(fbb.Release()),
190 fetch_node_factory_->monotonic_now() +
191 send_node_factory_->network_delay());
192 ScheduleTimestamp();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700193 }
194
Austin Schuh898f4972020-01-11 17:21:25 -0800195 sent_ = true;
196 Schedule();
197 }
198
Austin Schuheeaa2022021-01-02 21:52:03 -0800199 // Schedules sending the next timestamp in remote_timestamps_ if there is one.
200 void ScheduleTimestamp() {
201 if (remote_timestamps_.empty()) {
202 timestamp_timer_->Disable();
203 return;
204 }
205
206 if (scheduled_time_ !=
207 remote_timestamps_.front().monotonic_timestamp_time) {
208 timestamp_timer_->Setup(
209 remote_timestamps_.front().monotonic_timestamp_time);
210 scheduled_time_ = remote_timestamps_.front().monotonic_timestamp_time;
211 return;
212 } else {
213 scheduled_time_ = monotonic_clock::min_time;
214 }
215 }
216
217 // Sends the next timestamp in remote_timestamps_.
218 void SendTimestamp() {
219 CHECK(!remote_timestamps_.empty());
220
221 // Send out all timestamps at the currently scheduled time.
222 while (remote_timestamps_.front().monotonic_timestamp_time ==
223 scheduled_time_) {
224 if (server_connection_->state() == State::CONNECTED) {
225 timestamp_logger_->Send(
226 std::move(remote_timestamps_.front().remote_message));
227 }
228 remote_timestamps_.pop_front();
229 if (remote_timestamps_.empty()) {
230 break;
231 }
232 }
233
234 ScheduleTimestamp();
235 }
236
Austin Schuh898f4972020-01-11 17:21:25 -0800237 // Converts from time on the sending node to time on the receiving node.
238 monotonic_clock::time_point DeliveredTime(const Context &context) const {
239 const distributed_clock::time_point distributed_sent_time =
240 fetch_node_factory_->ToDistributedClock(context.monotonic_event_time);
241
Austin Schuh2febf0d2020-09-21 22:24:30 -0700242 return send_node_factory_->FromDistributedClock(
243 distributed_sent_time + send_node_factory_->network_delay() +
244 send_node_factory_->send_delay());
Austin Schuh898f4972020-01-11 17:21:25 -0800245 }
246
247 // Factories used for time conversion.
248 aos::NodeEventLoopFactory *fetch_node_factory_;
249 aos::NodeEventLoopFactory *send_node_factory_;
250
Austin Schuheeaa2022021-01-02 21:52:03 -0800251 // Event loop which fetching and sending timestamps are scheduled on.
252 aos::EventLoop *fetch_event_loop_;
Austin Schuh898f4972020-01-11 17:21:25 -0800253 // Event loop which sending is scheduled on.
254 aos::EventLoop *send_event_loop_;
255 // Timer used to send.
256 aos::TimerHandler *timer_;
Austin Schuheeaa2022021-01-02 21:52:03 -0800257 // Timer used to send timestamps out.
258 aos::TimerHandler *timestamp_timer_;
259 // Time that the timer is scheduled for. Used to track if it needs to be
260 // rescheduled.
261 monotonic_clock::time_point scheduled_time_ = monotonic_clock::min_time;
262
Austin Schuh898f4972020-01-11 17:21:25 -0800263 // Fetcher used to receive messages.
264 std::unique_ptr<aos::RawFetcher> fetcher_;
265 // Sender to send them back out.
266 std::unique_ptr<aos::RawSender> sender_;
Austin Schuh20ac95d2020-12-05 17:24:19 -0800267
268 MessageBridgeServerStatus *server_status_;
269 const size_t destination_node_index_;
Austin Schuh898f4972020-01-11 17:21:25 -0800270 // True if we have sent the message in the fetcher.
271 bool sent_ = false;
Austin Schuh4c3b9702020-08-30 11:34:55 -0700272
273 ServerConnection *server_connection_ = nullptr;
274 MessageBridgeClientStatus *client_status_ = nullptr;
275 int client_index_;
276 ClientConnection *client_connection_ = nullptr;
Austin Schuh2f8fd752020-09-01 22:38:28 -0700277
278 size_t channel_index_;
Austin Schuh0de30f32020-12-06 12:44:28 -0800279 aos::Sender<RemoteMessage> *timestamp_logger_ = nullptr;
Austin Schuheeaa2022021-01-02 21:52:03 -0800280
281 struct Timestamp {
282 Timestamp(FlatbufferDetachedBuffer<RemoteMessage> new_remote_message,
283 monotonic_clock::time_point new_monotonic_timestamp_time)
284 : remote_message(std::move(new_remote_message)),
285 monotonic_timestamp_time(new_monotonic_timestamp_time) {}
286 FlatbufferDetachedBuffer<RemoteMessage> remote_message;
287 monotonic_clock::time_point monotonic_timestamp_time;
288 };
289
290 std::deque<Timestamp> remote_timestamps_;
Austin Schuh898f4972020-01-11 17:21:25 -0800291};
292
293SimulatedMessageBridge::SimulatedMessageBridge(
294 SimulatedEventLoopFactory *simulated_event_loop_factory) {
295 CHECK(
296 configuration::MultiNode(simulated_event_loop_factory->configuration()));
297
298 // Pre-build up event loops for every node. They are pretty cheap anyways.
299 for (const Node *node : simulated_event_loop_factory->nodes()) {
Austin Schuh4c3b9702020-08-30 11:34:55 -0700300 auto it = event_loop_map_.emplace(std::make_pair(
301 node,
302 simulated_event_loop_factory->MakeEventLoop("message_bridge", node)));
Austin Schuhcde938c2020-02-02 17:30:07 -0800303
304 CHECK(it.second);
305
Austin Schuh4c3b9702020-08-30 11:34:55 -0700306 it.first->second.event_loop->SkipTimingReport();
307 it.first->second.event_loop->SkipAosLog();
308
309 for (ServerConnection *connection :
310 it.first->second.server_status.server_connection()) {
311 if (connection == nullptr) continue;
312
313 connection->mutate_state(message_bridge::State::CONNECTED);
314 }
315
316 for (size_t i = 0;
317 i < it.first->second.client_status.mutable_client_statistics()
318 ->mutable_connections()
319 ->size();
320 ++i) {
321 ClientConnection *connection =
322 it.first->second.client_status.mutable_client_statistics()
323 ->mutable_connections()
324 ->GetMutableObject(i);
325 if (connection == nullptr) continue;
326
327 connection->mutate_state(message_bridge::State::CONNECTED);
328 }
Austin Schuh898f4972020-01-11 17:21:25 -0800329 }
330
Austin Schuh20ac95d2020-12-05 17:24:19 -0800331 for (const Node *node : simulated_event_loop_factory->nodes()) {
332 auto it = event_loop_map_.find(node);
333
334 CHECK(it != event_loop_map_.end());
335
336 size_t node_index = 0;
337 for (ServerConnection *connection :
338 it->second.server_status.server_connection()) {
339 if (connection != nullptr) {
340 const Node *client_node =
341 simulated_event_loop_factory->configuration()->nodes()->Get(
342 node_index);
343 auto client_event_loop = event_loop_map_.find(client_node);
344 it->second.server_status.ResetFilter(node_index);
345 it->second.server_status.SetBootUUID(
346 node_index,
347 client_event_loop->second.event_loop->boot_uuid().string_view());
348 }
349 ++node_index;
350 }
351 }
352
Austin Schuh898f4972020-01-11 17:21:25 -0800353 for (const Channel *channel :
354 *simulated_event_loop_factory->configuration()->channels()) {
355 if (!channel->has_destination_nodes()) {
356 continue;
357 }
358
359 // Find the sending node.
360 const Node *node =
361 configuration::GetNode(simulated_event_loop_factory->configuration(),
362 channel->source_node()->string_view());
363 auto source_event_loop = event_loop_map_.find(node);
364 CHECK(source_event_loop != event_loop_map_.end());
365
366 std::unique_ptr<DelayersVector> delayers =
367 std::make_unique<DelayersVector>();
368
369 // And then build up a RawMessageDelayer for each destination.
370 for (const Connection *connection : *channel->destination_nodes()) {
371 const Node *destination_node =
372 configuration::GetNode(simulated_event_loop_factory->configuration(),
373 connection->name()->string_view());
374 auto destination_event_loop = event_loop_map_.find(destination_node);
375 CHECK(destination_event_loop != event_loop_map_.end());
376
Austin Schuh4c3b9702020-08-30 11:34:55 -0700377 ServerConnection *server_connection =
378 source_event_loop->second.server_status.FindServerConnection(
379 connection->name()->string_view());
380
381 int client_index =
382 destination_event_loop->second.client_status.FindClientIndex(
383 channel->source_node()->string_view());
384
Austin Schuh2f8fd752020-09-01 22:38:28 -0700385 const size_t destination_node_index = configuration::GetNodeIndex(
386 simulated_event_loop_factory->configuration(), destination_node);
387
388 const bool delivery_time_is_logged =
389 configuration::ConnectionDeliveryTimeIsLoggedOnNode(
390 connection, source_event_loop->second.event_loop->node());
391
Austin Schuh898f4972020-01-11 17:21:25 -0800392 delayers->emplace_back(std::make_unique<RawMessageDelayer>(
393 simulated_event_loop_factory->GetNodeEventLoopFactory(node),
394 simulated_event_loop_factory->GetNodeEventLoopFactory(
395 destination_node),
Austin Schuheeaa2022021-01-02 21:52:03 -0800396 source_event_loop->second.event_loop.get(),
Austin Schuh4c3b9702020-08-30 11:34:55 -0700397 destination_event_loop->second.event_loop.get(),
398 source_event_loop->second.event_loop->MakeRawFetcher(channel),
399 destination_event_loop->second.event_loop->MakeRawSender(channel),
Austin Schuh20ac95d2020-12-05 17:24:19 -0800400 &source_event_loop->second.server_status, destination_node_index,
Austin Schuh4c3b9702020-08-30 11:34:55 -0700401 server_connection, client_index,
Austin Schuh2f8fd752020-09-01 22:38:28 -0700402 &destination_event_loop->second.client_status,
403 configuration::ChannelIndex(
404 source_event_loop->second.event_loop->configuration(), channel),
405 delivery_time_is_logged
Austin Schuh89c9b812021-02-20 14:42:10 -0800406 ? source_event_loop->second.timestamp_loggers.SenderForChannel(
407 channel, connection)
Austin Schuh2f8fd752020-09-01 22:38:28 -0700408 : nullptr));
Austin Schuh898f4972020-01-11 17:21:25 -0800409 }
410
Austin Schuh4c3b9702020-08-30 11:34:55 -0700411 const Channel *const timestamp_channel = configuration::GetChannel(
412 simulated_event_loop_factory->configuration(), "/aos",
413 Timestamp::GetFullyQualifiedName(),
414 source_event_loop->second.event_loop->name(), node);
415
416 if (channel == timestamp_channel) {
417 source_event_loop->second.server_status.set_send_data(
418 [captured_delayers = delayers.get()](const Context &) {
419 for (std::unique_ptr<RawMessageDelayer> &delayer :
420 *captured_delayers) {
421 delayer->Schedule();
422 }
423 });
424 } else {
425 // And register every delayer to be poked when a new message shows up.
Austin Schuh4c570ea2020-11-19 23:13:24 -0800426
427 source_event_loop->second.event_loop->OnRun([captured_delayers =
428 delayers.get()]() {
429 // Poke all the reliable delayers so they send any queued messages.
430 for (std::unique_ptr<RawMessageDelayer> &delayer : *captured_delayers) {
431 if (delayer->time_to_live() == 0) {
432 delayer->Schedule();
433 }
434 }
435 });
Austin Schuh4c3b9702020-08-30 11:34:55 -0700436 source_event_loop->second.event_loop->MakeRawNoArgWatcher(
437 channel, [captured_delayers = delayers.get()](const Context &) {
438 for (std::unique_ptr<RawMessageDelayer> &delayer :
439 *captured_delayers) {
440 delayer->Schedule();
441 }
442 });
443 }
Austin Schuh898f4972020-01-11 17:21:25 -0800444 delayers_list_.emplace_back(std::move(delayers));
445 }
446}
447
448SimulatedMessageBridge::~SimulatedMessageBridge() {}
449
Austin Schuh6f3babe2020-01-26 20:34:50 -0800450void SimulatedMessageBridge::DisableForwarding(const Channel *channel) {
451 for (std::unique_ptr<std::vector<std::unique_ptr<RawMessageDelayer>>>
452 &delayers : delayers_list_) {
453 if (delayers->size() > 0) {
454 if ((*delayers)[0]->channel() == channel) {
455 for (std::unique_ptr<RawMessageDelayer> &delayer : *delayers) {
456 CHECK(delayer->channel() == channel);
457 }
458
459 // If we clear the delayers list, nothing will be scheduled. Which is a
460 // success!
461 delayers->clear();
462 }
463 }
464 }
465}
466
Austin Schuhc0b0f722020-12-12 18:36:06 -0800467void SimulatedMessageBridge::Disconnect(const Node *source,
468 const Node *destination) {
469 SetState(source, destination, message_bridge::State::DISCONNECTED);
470}
471
472void SimulatedMessageBridge::Connect(const Node *source,
473 const Node *destination) {
474 SetState(source, destination, message_bridge::State::CONNECTED);
475}
476void SimulatedMessageBridge::SetState(const Node *source,
477 const Node *destination,
478 message_bridge::State state) {
479 auto source_state = event_loop_map_.find(source);
480 CHECK(source_state != event_loop_map_.end());
481
482 ServerConnection *server_connection =
483 source_state->second.server_status.FindServerConnection(destination);
484 if (!server_connection) {
485 return;
486 }
487 server_connection->mutate_state(state);
488
489 auto destination_state = event_loop_map_.find(destination);
490 CHECK(destination_state != event_loop_map_.end());
491 ClientConnection *client_connection =
492 destination_state->second.client_status.GetClientConnection(source);
493 if (!client_connection) {
494 return;
495 }
496 client_connection->mutate_state(state);
497}
498
Austin Schuh4c3b9702020-08-30 11:34:55 -0700499void SimulatedMessageBridge::DisableStatistics() {
500 for (std::pair<const Node *const, State> &state : event_loop_map_) {
501 state.second.server_status.DisableStatistics();
502 state.second.client_status.DisableStatistics();
503 }
504}
505
Austin Schuh2928ebe2021-02-07 22:10:27 -0800506void SimulatedMessageBridge::SkipTimingReport() {
507 for (std::pair<const Node *const, State> &state : event_loop_map_) {
508 state.second.event_loop->SkipTimingReport();
509 }
510}
511
Austin Schuh2f8fd752020-09-01 22:38:28 -0700512SimulatedMessageBridge::State::State(
513 std::unique_ptr<aos::EventLoop> &&new_event_loop)
514 : event_loop(std::move(new_event_loop)),
Austin Schuh89c9b812021-02-20 14:42:10 -0800515 timestamp_loggers(event_loop.get()),
Austin Schuh2f8fd752020-09-01 22:38:28 -0700516 server_status(event_loop.get()),
517 client_status(event_loop.get()) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700518
519 // Find all nodes which log timestamps back to us (from us).
520 for (const Channel *channel : *event_loop->configuration()->channels()) {
521 CHECK(channel->has_source_node());
522
523 // Sent by us.
524 if (configuration::ChannelIsSendableOnNode(channel, event_loop->node()) &&
525 channel->has_destination_nodes()) {
526 for (const Connection *connection : *channel->destination_nodes()) {
527 const bool delivery_time_is_logged =
528 configuration::ConnectionDeliveryTimeIsLoggedOnNode(
529 connection, event_loop->node());
530
531 // And the timestamps are then logged back by us again.
532 if (!delivery_time_is_logged) {
533 continue;
534 }
535
Austin Schuh89c9b812021-02-20 14:42:10 -0800536 timestamp_loggers.SenderForChannel(channel, connection);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700537 }
538 }
539 }
540}
541
Austin Schuh898f4972020-01-11 17:21:25 -0800542} // namespace message_bridge
543} // namespace aos