blob: 62d57a9e8737a8ceed49523e7a80bf48a51a4222 [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 Schuh0de30f32020-12-06 12:44:28 -0800152 aos::Sender<RemoteMessage>::Builder builder =
Austin Schuh2f8fd752020-09-01 22:38:28 -0700153 timestamp_logger_->MakeBuilder();
154
Austin Schuh20ac95d2020-12-05 17:24:19 -0800155 // Reset the filter every time the UUID changes. There's probably a more
156 // clever way to do this, but that means a better concept of rebooting.
157 if (server_status_->BootUUID(destination_node_index_) !=
158 send_node_factory_->boot_uuid().string_view()) {
159 server_status_->ResetFilter(destination_node_index_);
160 server_status_->SetBootUUID(
161 destination_node_index_,
162 send_node_factory_->boot_uuid().string_view());
163 }
164
165 flatbuffers::Offset<flatbuffers::String> boot_uuid_offset =
Austin Schuheeaa2022021-01-02 21:52:03 -0800166 fbb.CreateString(
Austin Schuh20ac95d2020-12-05 17:24:19 -0800167 send_node_factory_->boot_uuid().string_view());
168
Austin Schuheeaa2022021-01-02 21:52:03 -0800169 RemoteMessage::Builder message_header_builder(fbb);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700170
171 message_header_builder.add_channel_index(channel_index_);
172
173 // Swap the remote and sent metrics. They are from the sender's
174 // perspective, not the receiver's perspective.
175 message_header_builder.add_monotonic_remote_time(
176 fetcher_->context().monotonic_event_time.time_since_epoch().count());
177 message_header_builder.add_realtime_remote_time(
178 fetcher_->context().realtime_event_time.time_since_epoch().count());
179 message_header_builder.add_remote_queue_index(
180 fetcher_->context().queue_index);
181
182 message_header_builder.add_monotonic_sent_time(
183 sender_->monotonic_sent_time().time_since_epoch().count());
184 message_header_builder.add_realtime_sent_time(
185 sender_->realtime_sent_time().time_since_epoch().count());
186 message_header_builder.add_queue_index(sender_->sent_queue_index());
Austin Schuh20ac95d2020-12-05 17:24:19 -0800187 message_header_builder.add_boot_uuid(boot_uuid_offset);
Austin Schuh2f8fd752020-09-01 22:38:28 -0700188
Austin Schuheeaa2022021-01-02 21:52:03 -0800189 fbb.Finish(message_header_builder.Finish());
190
191 remote_timestamps_.emplace_back(
192 FlatbufferDetachedBuffer<RemoteMessage>(fbb.Release()),
193 fetch_node_factory_->monotonic_now() +
194 send_node_factory_->network_delay());
195 ScheduleTimestamp();
Austin Schuh2f8fd752020-09-01 22:38:28 -0700196 }
197
Austin Schuh898f4972020-01-11 17:21:25 -0800198 sent_ = true;
199 Schedule();
200 }
201
Austin Schuheeaa2022021-01-02 21:52:03 -0800202 // Schedules sending the next timestamp in remote_timestamps_ if there is one.
203 void ScheduleTimestamp() {
204 if (remote_timestamps_.empty()) {
205 timestamp_timer_->Disable();
206 return;
207 }
208
209 if (scheduled_time_ !=
210 remote_timestamps_.front().monotonic_timestamp_time) {
211 timestamp_timer_->Setup(
212 remote_timestamps_.front().monotonic_timestamp_time);
213 scheduled_time_ = remote_timestamps_.front().monotonic_timestamp_time;
214 return;
215 } else {
216 scheduled_time_ = monotonic_clock::min_time;
217 }
218 }
219
220 // Sends the next timestamp in remote_timestamps_.
221 void SendTimestamp() {
222 CHECK(!remote_timestamps_.empty());
223
224 // Send out all timestamps at the currently scheduled time.
225 while (remote_timestamps_.front().monotonic_timestamp_time ==
226 scheduled_time_) {
227 if (server_connection_->state() == State::CONNECTED) {
228 timestamp_logger_->Send(
229 std::move(remote_timestamps_.front().remote_message));
230 }
231 remote_timestamps_.pop_front();
232 if (remote_timestamps_.empty()) {
233 break;
234 }
235 }
236
237 ScheduleTimestamp();
238 }
239
Austin Schuh898f4972020-01-11 17:21:25 -0800240 // Converts from time on the sending node to time on the receiving node.
241 monotonic_clock::time_point DeliveredTime(const Context &context) const {
242 const distributed_clock::time_point distributed_sent_time =
243 fetch_node_factory_->ToDistributedClock(context.monotonic_event_time);
244
Austin Schuh2febf0d2020-09-21 22:24:30 -0700245 return send_node_factory_->FromDistributedClock(
246 distributed_sent_time + send_node_factory_->network_delay() +
247 send_node_factory_->send_delay());
Austin Schuh898f4972020-01-11 17:21:25 -0800248 }
249
250 // Factories used for time conversion.
251 aos::NodeEventLoopFactory *fetch_node_factory_;
252 aos::NodeEventLoopFactory *send_node_factory_;
253
Austin Schuheeaa2022021-01-02 21:52:03 -0800254 // Event loop which fetching and sending timestamps are scheduled on.
255 aos::EventLoop *fetch_event_loop_;
Austin Schuh898f4972020-01-11 17:21:25 -0800256 // Event loop which sending is scheduled on.
257 aos::EventLoop *send_event_loop_;
258 // Timer used to send.
259 aos::TimerHandler *timer_;
Austin Schuheeaa2022021-01-02 21:52:03 -0800260 // Timer used to send timestamps out.
261 aos::TimerHandler *timestamp_timer_;
262 // Time that the timer is scheduled for. Used to track if it needs to be
263 // rescheduled.
264 monotonic_clock::time_point scheduled_time_ = monotonic_clock::min_time;
265
Austin Schuh898f4972020-01-11 17:21:25 -0800266 // Fetcher used to receive messages.
267 std::unique_ptr<aos::RawFetcher> fetcher_;
268 // Sender to send them back out.
269 std::unique_ptr<aos::RawSender> sender_;
Austin Schuh20ac95d2020-12-05 17:24:19 -0800270
271 MessageBridgeServerStatus *server_status_;
272 const size_t destination_node_index_;
Austin Schuh898f4972020-01-11 17:21:25 -0800273 // True if we have sent the message in the fetcher.
274 bool sent_ = false;
Austin Schuh4c3b9702020-08-30 11:34:55 -0700275
276 ServerConnection *server_connection_ = nullptr;
277 MessageBridgeClientStatus *client_status_ = nullptr;
278 int client_index_;
279 ClientConnection *client_connection_ = nullptr;
Austin Schuh2f8fd752020-09-01 22:38:28 -0700280
281 size_t channel_index_;
Austin Schuh0de30f32020-12-06 12:44:28 -0800282 aos::Sender<RemoteMessage> *timestamp_logger_ = nullptr;
Austin Schuheeaa2022021-01-02 21:52:03 -0800283
284 struct Timestamp {
285 Timestamp(FlatbufferDetachedBuffer<RemoteMessage> new_remote_message,
286 monotonic_clock::time_point new_monotonic_timestamp_time)
287 : remote_message(std::move(new_remote_message)),
288 monotonic_timestamp_time(new_monotonic_timestamp_time) {}
289 FlatbufferDetachedBuffer<RemoteMessage> remote_message;
290 monotonic_clock::time_point monotonic_timestamp_time;
291 };
292
293 std::deque<Timestamp> remote_timestamps_;
Austin Schuh898f4972020-01-11 17:21:25 -0800294};
295
296SimulatedMessageBridge::SimulatedMessageBridge(
297 SimulatedEventLoopFactory *simulated_event_loop_factory) {
298 CHECK(
299 configuration::MultiNode(simulated_event_loop_factory->configuration()));
300
301 // Pre-build up event loops for every node. They are pretty cheap anyways.
302 for (const Node *node : simulated_event_loop_factory->nodes()) {
Austin Schuh4c3b9702020-08-30 11:34:55 -0700303 auto it = event_loop_map_.emplace(std::make_pair(
304 node,
305 simulated_event_loop_factory->MakeEventLoop("message_bridge", node)));
Austin Schuhcde938c2020-02-02 17:30:07 -0800306
307 CHECK(it.second);
308
Austin Schuh4c3b9702020-08-30 11:34:55 -0700309 it.first->second.event_loop->SkipTimingReport();
310 it.first->second.event_loop->SkipAosLog();
311
312 for (ServerConnection *connection :
313 it.first->second.server_status.server_connection()) {
314 if (connection == nullptr) continue;
315
316 connection->mutate_state(message_bridge::State::CONNECTED);
317 }
318
319 for (size_t i = 0;
320 i < it.first->second.client_status.mutable_client_statistics()
321 ->mutable_connections()
322 ->size();
323 ++i) {
324 ClientConnection *connection =
325 it.first->second.client_status.mutable_client_statistics()
326 ->mutable_connections()
327 ->GetMutableObject(i);
328 if (connection == nullptr) continue;
329
330 connection->mutate_state(message_bridge::State::CONNECTED);
331 }
Austin Schuh898f4972020-01-11 17:21:25 -0800332 }
333
Austin Schuh20ac95d2020-12-05 17:24:19 -0800334 for (const Node *node : simulated_event_loop_factory->nodes()) {
335 auto it = event_loop_map_.find(node);
336
337 CHECK(it != event_loop_map_.end());
338
339 size_t node_index = 0;
340 for (ServerConnection *connection :
341 it->second.server_status.server_connection()) {
342 if (connection != nullptr) {
343 const Node *client_node =
344 simulated_event_loop_factory->configuration()->nodes()->Get(
345 node_index);
346 auto client_event_loop = event_loop_map_.find(client_node);
347 it->second.server_status.ResetFilter(node_index);
348 it->second.server_status.SetBootUUID(
349 node_index,
350 client_event_loop->second.event_loop->boot_uuid().string_view());
351 }
352 ++node_index;
353 }
354 }
355
Austin Schuh898f4972020-01-11 17:21:25 -0800356 for (const Channel *channel :
357 *simulated_event_loop_factory->configuration()->channels()) {
358 if (!channel->has_destination_nodes()) {
359 continue;
360 }
361
362 // Find the sending node.
363 const Node *node =
364 configuration::GetNode(simulated_event_loop_factory->configuration(),
365 channel->source_node()->string_view());
366 auto source_event_loop = event_loop_map_.find(node);
367 CHECK(source_event_loop != event_loop_map_.end());
368
369 std::unique_ptr<DelayersVector> delayers =
370 std::make_unique<DelayersVector>();
371
372 // And then build up a RawMessageDelayer for each destination.
373 for (const Connection *connection : *channel->destination_nodes()) {
374 const Node *destination_node =
375 configuration::GetNode(simulated_event_loop_factory->configuration(),
376 connection->name()->string_view());
377 auto destination_event_loop = event_loop_map_.find(destination_node);
378 CHECK(destination_event_loop != event_loop_map_.end());
379
Austin Schuh4c3b9702020-08-30 11:34:55 -0700380 ServerConnection *server_connection =
381 source_event_loop->second.server_status.FindServerConnection(
382 connection->name()->string_view());
383
384 int client_index =
385 destination_event_loop->second.client_status.FindClientIndex(
386 channel->source_node()->string_view());
387
Austin Schuh2f8fd752020-09-01 22:38:28 -0700388 const size_t destination_node_index = configuration::GetNodeIndex(
389 simulated_event_loop_factory->configuration(), destination_node);
390
391 const bool delivery_time_is_logged =
392 configuration::ConnectionDeliveryTimeIsLoggedOnNode(
393 connection, source_event_loop->second.event_loop->node());
394
Austin Schuh898f4972020-01-11 17:21:25 -0800395 delayers->emplace_back(std::make_unique<RawMessageDelayer>(
396 simulated_event_loop_factory->GetNodeEventLoopFactory(node),
397 simulated_event_loop_factory->GetNodeEventLoopFactory(
398 destination_node),
Austin Schuheeaa2022021-01-02 21:52:03 -0800399 source_event_loop->second.event_loop.get(),
Austin Schuh4c3b9702020-08-30 11:34:55 -0700400 destination_event_loop->second.event_loop.get(),
401 source_event_loop->second.event_loop->MakeRawFetcher(channel),
402 destination_event_loop->second.event_loop->MakeRawSender(channel),
Austin Schuh20ac95d2020-12-05 17:24:19 -0800403 &source_event_loop->second.server_status, destination_node_index,
Austin Schuh4c3b9702020-08-30 11:34:55 -0700404 server_connection, client_index,
Austin Schuh2f8fd752020-09-01 22:38:28 -0700405 &destination_event_loop->second.client_status,
406 configuration::ChannelIndex(
407 source_event_loop->second.event_loop->configuration(), channel),
408 delivery_time_is_logged
409 ? &source_event_loop->second
410 .timestamp_loggers[destination_node_index]
411 : nullptr));
Austin Schuh898f4972020-01-11 17:21:25 -0800412 }
413
Austin Schuh4c3b9702020-08-30 11:34:55 -0700414 const Channel *const timestamp_channel = configuration::GetChannel(
415 simulated_event_loop_factory->configuration(), "/aos",
416 Timestamp::GetFullyQualifiedName(),
417 source_event_loop->second.event_loop->name(), node);
418
419 if (channel == timestamp_channel) {
420 source_event_loop->second.server_status.set_send_data(
421 [captured_delayers = delayers.get()](const Context &) {
422 for (std::unique_ptr<RawMessageDelayer> &delayer :
423 *captured_delayers) {
424 delayer->Schedule();
425 }
426 });
427 } else {
428 // And register every delayer to be poked when a new message shows up.
Austin Schuh4c570ea2020-11-19 23:13:24 -0800429
430 source_event_loop->second.event_loop->OnRun([captured_delayers =
431 delayers.get()]() {
432 // Poke all the reliable delayers so they send any queued messages.
433 for (std::unique_ptr<RawMessageDelayer> &delayer : *captured_delayers) {
434 if (delayer->time_to_live() == 0) {
435 delayer->Schedule();
436 }
437 }
438 });
Austin Schuh4c3b9702020-08-30 11:34:55 -0700439 source_event_loop->second.event_loop->MakeRawNoArgWatcher(
440 channel, [captured_delayers = delayers.get()](const Context &) {
441 for (std::unique_ptr<RawMessageDelayer> &delayer :
442 *captured_delayers) {
443 delayer->Schedule();
444 }
445 });
446 }
Austin Schuh898f4972020-01-11 17:21:25 -0800447 delayers_list_.emplace_back(std::move(delayers));
448 }
449}
450
451SimulatedMessageBridge::~SimulatedMessageBridge() {}
452
Austin Schuh6f3babe2020-01-26 20:34:50 -0800453void SimulatedMessageBridge::DisableForwarding(const Channel *channel) {
454 for (std::unique_ptr<std::vector<std::unique_ptr<RawMessageDelayer>>>
455 &delayers : delayers_list_) {
456 if (delayers->size() > 0) {
457 if ((*delayers)[0]->channel() == channel) {
458 for (std::unique_ptr<RawMessageDelayer> &delayer : *delayers) {
459 CHECK(delayer->channel() == channel);
460 }
461
462 // If we clear the delayers list, nothing will be scheduled. Which is a
463 // success!
464 delayers->clear();
465 }
466 }
467 }
468}
469
Austin Schuhc0b0f722020-12-12 18:36:06 -0800470void SimulatedMessageBridge::Disconnect(const Node *source,
471 const Node *destination) {
472 SetState(source, destination, message_bridge::State::DISCONNECTED);
473}
474
475void SimulatedMessageBridge::Connect(const Node *source,
476 const Node *destination) {
477 SetState(source, destination, message_bridge::State::CONNECTED);
478}
479void SimulatedMessageBridge::SetState(const Node *source,
480 const Node *destination,
481 message_bridge::State state) {
482 auto source_state = event_loop_map_.find(source);
483 CHECK(source_state != event_loop_map_.end());
484
485 ServerConnection *server_connection =
486 source_state->second.server_status.FindServerConnection(destination);
487 if (!server_connection) {
488 return;
489 }
490 server_connection->mutate_state(state);
491
492 auto destination_state = event_loop_map_.find(destination);
493 CHECK(destination_state != event_loop_map_.end());
494 ClientConnection *client_connection =
495 destination_state->second.client_status.GetClientConnection(source);
496 if (!client_connection) {
497 return;
498 }
499 client_connection->mutate_state(state);
500}
501
Austin Schuh4c3b9702020-08-30 11:34:55 -0700502void SimulatedMessageBridge::DisableStatistics() {
503 for (std::pair<const Node *const, State> &state : event_loop_map_) {
504 state.second.server_status.DisableStatistics();
505 state.second.client_status.DisableStatistics();
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)),
512 server_status(event_loop.get()),
513 client_status(event_loop.get()) {
514 timestamp_loggers.resize(event_loop->configuration()->nodes()->size());
515
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
533 // (And only construct the sender if it hasn't been constructed)
534 const Node *other_node = configuration::GetNode(
535 event_loop->configuration(), connection->name()->string_view());
536 const size_t other_node_index = configuration::GetNodeIndex(
537 event_loop->configuration(), other_node);
538
539 if (!timestamp_loggers[other_node_index]) {
540 timestamp_loggers[other_node_index] =
Austin Schuh0de30f32020-12-06 12:44:28 -0800541 event_loop->MakeSender<RemoteMessage>(
Austin Schuh2f8fd752020-09-01 22:38:28 -0700542 absl::StrCat("/aos/remote_timestamps/",
543 connection->name()->string_view()));
544 }
545 }
546 }
547 }
548}
549
Austin Schuh898f4972020-01-11 17:21:25 -0800550} // namespace message_bridge
551} // namespace aos