blob: e5eb4cb3bf14d1ad9348da1c1cec490da4f98823 [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;
Austin Schuh5e2bfb82021-03-13 22:46:55 -0800151 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_) !=
Austin Schuh5e2bfb82021-03-13 22:46:55 -0800155 send_node_factory_->boot_uuid()) {
Austin Schuh20ac95d2020-12-05 17:24:19 -0800156 server_status_->ResetFilter(destination_node_index_);
Austin Schuh5e2bfb82021-03-13 22:46:55 -0800157 server_status_->SetBootUUID(destination_node_index_,
158 send_node_factory_->boot_uuid());
Austin Schuh20ac95d2020-12-05 17:24:19 -0800159 }
160
161 flatbuffers::Offset<flatbuffers::String> boot_uuid_offset =
Austin Schuh5e2bfb82021-03-13 22:46:55 -0800162 send_node_factory_->boot_uuid().PackString(&fbb);
Austin Schuh20ac95d2020-12-05 17:24:19 -0800163
Austin Schuheeaa2022021-01-02 21:52:03 -0800164 RemoteMessage::Builder message_header_builder(fbb);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700165
166 message_header_builder.add_channel_index(channel_index_);
167
168 // Swap the remote and sent metrics. They are from the sender's
169 // perspective, not the receiver's perspective.
170 message_header_builder.add_monotonic_remote_time(
171 fetcher_->context().monotonic_event_time.time_since_epoch().count());
172 message_header_builder.add_realtime_remote_time(
173 fetcher_->context().realtime_event_time.time_since_epoch().count());
174 message_header_builder.add_remote_queue_index(
175 fetcher_->context().queue_index);
176
177 message_header_builder.add_monotonic_sent_time(
178 sender_->monotonic_sent_time().time_since_epoch().count());
179 message_header_builder.add_realtime_sent_time(
180 sender_->realtime_sent_time().time_since_epoch().count());
181 message_header_builder.add_queue_index(sender_->sent_queue_index());
Austin Schuh20ac95d2020-12-05 17:24:19 -0800182 message_header_builder.add_boot_uuid(boot_uuid_offset);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700183
Austin Schuheeaa2022021-01-02 21:52:03 -0800184 fbb.Finish(message_header_builder.Finish());
185
186 remote_timestamps_.emplace_back(
187 FlatbufferDetachedBuffer<RemoteMessage>(fbb.Release()),
188 fetch_node_factory_->monotonic_now() +
189 send_node_factory_->network_delay());
190 ScheduleTimestamp();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700191 }
192
Austin Schuh898f4972020-01-11 17:21:25 -0800193 sent_ = true;
194 Schedule();
195 }
196
Austin Schuheeaa2022021-01-02 21:52:03 -0800197 // Schedules sending the next timestamp in remote_timestamps_ if there is one.
198 void ScheduleTimestamp() {
199 if (remote_timestamps_.empty()) {
200 timestamp_timer_->Disable();
201 return;
202 }
203
204 if (scheduled_time_ !=
205 remote_timestamps_.front().monotonic_timestamp_time) {
206 timestamp_timer_->Setup(
207 remote_timestamps_.front().monotonic_timestamp_time);
208 scheduled_time_ = remote_timestamps_.front().monotonic_timestamp_time;
209 return;
210 } else {
211 scheduled_time_ = monotonic_clock::min_time;
212 }
213 }
214
215 // Sends the next timestamp in remote_timestamps_.
216 void SendTimestamp() {
217 CHECK(!remote_timestamps_.empty());
218
219 // Send out all timestamps at the currently scheduled time.
220 while (remote_timestamps_.front().monotonic_timestamp_time ==
221 scheduled_time_) {
222 if (server_connection_->state() == State::CONNECTED) {
223 timestamp_logger_->Send(
224 std::move(remote_timestamps_.front().remote_message));
225 }
226 remote_timestamps_.pop_front();
227 if (remote_timestamps_.empty()) {
228 break;
229 }
230 }
231
232 ScheduleTimestamp();
233 }
234
Austin Schuh898f4972020-01-11 17:21:25 -0800235 // Converts from time on the sending node to time on the receiving node.
236 monotonic_clock::time_point DeliveredTime(const Context &context) const {
237 const distributed_clock::time_point distributed_sent_time =
238 fetch_node_factory_->ToDistributedClock(context.monotonic_event_time);
239
Austin Schuh2febf0d2020-09-21 22:24:30 -0700240 return send_node_factory_->FromDistributedClock(
241 distributed_sent_time + send_node_factory_->network_delay() +
242 send_node_factory_->send_delay());
Austin Schuh898f4972020-01-11 17:21:25 -0800243 }
244
245 // Factories used for time conversion.
246 aos::NodeEventLoopFactory *fetch_node_factory_;
247 aos::NodeEventLoopFactory *send_node_factory_;
248
Austin Schuheeaa2022021-01-02 21:52:03 -0800249 // Event loop which fetching and sending timestamps are scheduled on.
250 aos::EventLoop *fetch_event_loop_;
Austin Schuh898f4972020-01-11 17:21:25 -0800251 // Event loop which sending is scheduled on.
252 aos::EventLoop *send_event_loop_;
253 // Timer used to send.
254 aos::TimerHandler *timer_;
Austin Schuheeaa2022021-01-02 21:52:03 -0800255 // Timer used to send timestamps out.
256 aos::TimerHandler *timestamp_timer_;
257 // Time that the timer is scheduled for. Used to track if it needs to be
258 // rescheduled.
259 monotonic_clock::time_point scheduled_time_ = monotonic_clock::min_time;
260
Austin Schuh898f4972020-01-11 17:21:25 -0800261 // Fetcher used to receive messages.
262 std::unique_ptr<aos::RawFetcher> fetcher_;
263 // Sender to send them back out.
264 std::unique_ptr<aos::RawSender> sender_;
Austin Schuh20ac95d2020-12-05 17:24:19 -0800265
266 MessageBridgeServerStatus *server_status_;
267 const size_t destination_node_index_;
Austin Schuh898f4972020-01-11 17:21:25 -0800268 // True if we have sent the message in the fetcher.
269 bool sent_ = false;
Austin Schuh4c3b9702020-08-30 11:34:55 -0700270
271 ServerConnection *server_connection_ = nullptr;
272 MessageBridgeClientStatus *client_status_ = nullptr;
273 int client_index_;
274 ClientConnection *client_connection_ = nullptr;
Austin Schuh2f8fd752020-09-01 22:38:28 -0700275
276 size_t channel_index_;
Austin Schuh0de30f32020-12-06 12:44:28 -0800277 aos::Sender<RemoteMessage> *timestamp_logger_ = nullptr;
Austin Schuheeaa2022021-01-02 21:52:03 -0800278
279 struct Timestamp {
280 Timestamp(FlatbufferDetachedBuffer<RemoteMessage> new_remote_message,
281 monotonic_clock::time_point new_monotonic_timestamp_time)
282 : remote_message(std::move(new_remote_message)),
283 monotonic_timestamp_time(new_monotonic_timestamp_time) {}
284 FlatbufferDetachedBuffer<RemoteMessage> remote_message;
285 monotonic_clock::time_point monotonic_timestamp_time;
286 };
287
288 std::deque<Timestamp> remote_timestamps_;
Austin Schuh898f4972020-01-11 17:21:25 -0800289};
290
291SimulatedMessageBridge::SimulatedMessageBridge(
292 SimulatedEventLoopFactory *simulated_event_loop_factory) {
293 CHECK(
294 configuration::MultiNode(simulated_event_loop_factory->configuration()));
295
296 // Pre-build up event loops for every node. They are pretty cheap anyways.
297 for (const Node *node : simulated_event_loop_factory->nodes()) {
Austin Schuh4c3b9702020-08-30 11:34:55 -0700298 auto it = event_loop_map_.emplace(std::make_pair(
299 node,
300 simulated_event_loop_factory->MakeEventLoop("message_bridge", node)));
Austin Schuhcde938c2020-02-02 17:30:07 -0800301
302 CHECK(it.second);
303
Austin Schuh4c3b9702020-08-30 11:34:55 -0700304 it.first->second.event_loop->SkipTimingReport();
305 it.first->second.event_loop->SkipAosLog();
306
307 for (ServerConnection *connection :
308 it.first->second.server_status.server_connection()) {
309 if (connection == nullptr) continue;
310
311 connection->mutate_state(message_bridge::State::CONNECTED);
312 }
313
314 for (size_t i = 0;
315 i < it.first->second.client_status.mutable_client_statistics()
316 ->mutable_connections()
317 ->size();
318 ++i) {
319 ClientConnection *connection =
320 it.first->second.client_status.mutable_client_statistics()
321 ->mutable_connections()
322 ->GetMutableObject(i);
323 if (connection == nullptr) continue;
324
325 connection->mutate_state(message_bridge::State::CONNECTED);
326 }
Austin Schuh898f4972020-01-11 17:21:25 -0800327 }
328
Austin Schuh20ac95d2020-12-05 17:24:19 -0800329 for (const Node *node : simulated_event_loop_factory->nodes()) {
330 auto it = event_loop_map_.find(node);
331
332 CHECK(it != event_loop_map_.end());
333
334 size_t node_index = 0;
335 for (ServerConnection *connection :
336 it->second.server_status.server_connection()) {
337 if (connection != nullptr) {
338 const Node *client_node =
339 simulated_event_loop_factory->configuration()->nodes()->Get(
340 node_index);
341 auto client_event_loop = event_loop_map_.find(client_node);
342 it->second.server_status.ResetFilter(node_index);
343 it->second.server_status.SetBootUUID(
Austin Schuh5e2bfb82021-03-13 22:46:55 -0800344 node_index, client_event_loop->second.event_loop->boot_uuid());
Austin Schuh20ac95d2020-12-05 17:24:19 -0800345 }
346 ++node_index;
347 }
348 }
349
Austin Schuh898f4972020-01-11 17:21:25 -0800350 for (const Channel *channel :
351 *simulated_event_loop_factory->configuration()->channels()) {
352 if (!channel->has_destination_nodes()) {
353 continue;
354 }
355
356 // Find the sending node.
357 const Node *node =
358 configuration::GetNode(simulated_event_loop_factory->configuration(),
359 channel->source_node()->string_view());
360 auto source_event_loop = event_loop_map_.find(node);
361 CHECK(source_event_loop != event_loop_map_.end());
362
363 std::unique_ptr<DelayersVector> delayers =
364 std::make_unique<DelayersVector>();
365
366 // And then build up a RawMessageDelayer for each destination.
367 for (const Connection *connection : *channel->destination_nodes()) {
368 const Node *destination_node =
369 configuration::GetNode(simulated_event_loop_factory->configuration(),
370 connection->name()->string_view());
371 auto destination_event_loop = event_loop_map_.find(destination_node);
372 CHECK(destination_event_loop != event_loop_map_.end());
373
Austin Schuh4c3b9702020-08-30 11:34:55 -0700374 ServerConnection *server_connection =
375 source_event_loop->second.server_status.FindServerConnection(
376 connection->name()->string_view());
377
378 int client_index =
379 destination_event_loop->second.client_status.FindClientIndex(
380 channel->source_node()->string_view());
381
Austin Schuh2f8fd752020-09-01 22:38:28 -0700382 const size_t destination_node_index = configuration::GetNodeIndex(
383 simulated_event_loop_factory->configuration(), destination_node);
384
385 const bool delivery_time_is_logged =
386 configuration::ConnectionDeliveryTimeIsLoggedOnNode(
387 connection, source_event_loop->second.event_loop->node());
388
Austin Schuh898f4972020-01-11 17:21:25 -0800389 delayers->emplace_back(std::make_unique<RawMessageDelayer>(
390 simulated_event_loop_factory->GetNodeEventLoopFactory(node),
391 simulated_event_loop_factory->GetNodeEventLoopFactory(
392 destination_node),
Austin Schuheeaa2022021-01-02 21:52:03 -0800393 source_event_loop->second.event_loop.get(),
Austin Schuh4c3b9702020-08-30 11:34:55 -0700394 destination_event_loop->second.event_loop.get(),
395 source_event_loop->second.event_loop->MakeRawFetcher(channel),
396 destination_event_loop->second.event_loop->MakeRawSender(channel),
Austin Schuh20ac95d2020-12-05 17:24:19 -0800397 &source_event_loop->second.server_status, destination_node_index,
Austin Schuh4c3b9702020-08-30 11:34:55 -0700398 server_connection, client_index,
Austin Schuh2f8fd752020-09-01 22:38:28 -0700399 &destination_event_loop->second.client_status,
400 configuration::ChannelIndex(
401 source_event_loop->second.event_loop->configuration(), channel),
402 delivery_time_is_logged
Austin Schuh89c9b812021-02-20 14:42:10 -0800403 ? source_event_loop->second.timestamp_loggers.SenderForChannel(
404 channel, connection)
Austin Schuh2f8fd752020-09-01 22:38:28 -0700405 : nullptr));
Austin Schuh898f4972020-01-11 17:21:25 -0800406 }
407
Austin Schuh4c3b9702020-08-30 11:34:55 -0700408 const Channel *const timestamp_channel = configuration::GetChannel(
409 simulated_event_loop_factory->configuration(), "/aos",
410 Timestamp::GetFullyQualifiedName(),
411 source_event_loop->second.event_loop->name(), node);
412
413 if (channel == timestamp_channel) {
414 source_event_loop->second.server_status.set_send_data(
415 [captured_delayers = delayers.get()](const Context &) {
416 for (std::unique_ptr<RawMessageDelayer> &delayer :
417 *captured_delayers) {
418 delayer->Schedule();
419 }
420 });
421 } else {
422 // And register every delayer to be poked when a new message shows up.
Austin Schuh4c570ea2020-11-19 23:13:24 -0800423
424 source_event_loop->second.event_loop->OnRun([captured_delayers =
425 delayers.get()]() {
426 // Poke all the reliable delayers so they send any queued messages.
427 for (std::unique_ptr<RawMessageDelayer> &delayer : *captured_delayers) {
428 if (delayer->time_to_live() == 0) {
429 delayer->Schedule();
430 }
431 }
432 });
Austin Schuh4c3b9702020-08-30 11:34:55 -0700433 source_event_loop->second.event_loop->MakeRawNoArgWatcher(
434 channel, [captured_delayers = delayers.get()](const Context &) {
435 for (std::unique_ptr<RawMessageDelayer> &delayer :
436 *captured_delayers) {
437 delayer->Schedule();
438 }
439 });
440 }
Austin Schuh898f4972020-01-11 17:21:25 -0800441 delayers_list_.emplace_back(std::move(delayers));
442 }
443}
444
445SimulatedMessageBridge::~SimulatedMessageBridge() {}
446
Austin Schuh6f3babe2020-01-26 20:34:50 -0800447void SimulatedMessageBridge::DisableForwarding(const Channel *channel) {
448 for (std::unique_ptr<std::vector<std::unique_ptr<RawMessageDelayer>>>
449 &delayers : delayers_list_) {
450 if (delayers->size() > 0) {
451 if ((*delayers)[0]->channel() == channel) {
452 for (std::unique_ptr<RawMessageDelayer> &delayer : *delayers) {
453 CHECK(delayer->channel() == channel);
454 }
455
456 // If we clear the delayers list, nothing will be scheduled. Which is a
457 // success!
458 delayers->clear();
459 }
460 }
461 }
462}
463
Austin Schuhc0b0f722020-12-12 18:36:06 -0800464void SimulatedMessageBridge::Disconnect(const Node *source,
465 const Node *destination) {
466 SetState(source, destination, message_bridge::State::DISCONNECTED);
467}
468
469void SimulatedMessageBridge::Connect(const Node *source,
470 const Node *destination) {
471 SetState(source, destination, message_bridge::State::CONNECTED);
472}
473void SimulatedMessageBridge::SetState(const Node *source,
474 const Node *destination,
475 message_bridge::State state) {
476 auto source_state = event_loop_map_.find(source);
477 CHECK(source_state != event_loop_map_.end());
478
479 ServerConnection *server_connection =
480 source_state->second.server_status.FindServerConnection(destination);
481 if (!server_connection) {
482 return;
483 }
484 server_connection->mutate_state(state);
485
486 auto destination_state = event_loop_map_.find(destination);
487 CHECK(destination_state != event_loop_map_.end());
488 ClientConnection *client_connection =
489 destination_state->second.client_status.GetClientConnection(source);
490 if (!client_connection) {
491 return;
492 }
493 client_connection->mutate_state(state);
494}
495
Austin Schuh4c3b9702020-08-30 11:34:55 -0700496void SimulatedMessageBridge::DisableStatistics() {
497 for (std::pair<const Node *const, State> &state : event_loop_map_) {
498 state.second.server_status.DisableStatistics();
499 state.second.client_status.DisableStatistics();
500 }
501}
502
Austin Schuh2928ebe2021-02-07 22:10:27 -0800503void SimulatedMessageBridge::SkipTimingReport() {
504 for (std::pair<const Node *const, State> &state : event_loop_map_) {
505 state.second.event_loop->SkipTimingReport();
506 }
507}
508
Austin Schuh2f8fd752020-09-01 22:38:28 -0700509SimulatedMessageBridge::State::State(
510 std::unique_ptr<aos::EventLoop> &&new_event_loop)
511 : event_loop(std::move(new_event_loop)),
Austin Schuh89c9b812021-02-20 14:42:10 -0800512 timestamp_loggers(event_loop.get()),
Austin Schuh2f8fd752020-09-01 22:38:28 -0700513 server_status(event_loop.get()),
514 client_status(event_loop.get()) {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700515
516 // Find all nodes which log timestamps back to us (from us).
517 for (const Channel *channel : *event_loop->configuration()->channels()) {
518 CHECK(channel->has_source_node());
519
520 // Sent by us.
521 if (configuration::ChannelIsSendableOnNode(channel, event_loop->node()) &&
522 channel->has_destination_nodes()) {
523 for (const Connection *connection : *channel->destination_nodes()) {
524 const bool delivery_time_is_logged =
525 configuration::ConnectionDeliveryTimeIsLoggedOnNode(
526 connection, event_loop->node());
527
528 // And the timestamps are then logged back by us again.
529 if (!delivery_time_is_logged) {
530 continue;
531 }
532
Austin Schuh89c9b812021-02-20 14:42:10 -0800533 timestamp_loggers.SenderForChannel(channel, connection);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700534 }
535 }
536 }
537}
538
Austin Schuh898f4972020-01-11 17:21:25 -0800539} // namespace message_bridge
540} // namespace aos