Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 1 | #include "aos/events/simulated_network_bridge.h" |
| 2 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 3 | #include "absl/strings/str_cat.h" |
| 4 | #include "aos/configuration.h" |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 5 | #include "aos/events/event_loop.h" |
| 6 | #include "aos/events/simulated_event_loop.h" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 7 | #include "aos/network/remote_message_generated.h" |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 8 | |
| 9 | namespace aos { |
| 10 | namespace 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. |
| 17 | class RawMessageDelayer { |
| 18 | public: |
| 19 | RawMessageDelayer(aos::NodeEventLoopFactory *fetch_node_factory, |
| 20 | aos::NodeEventLoopFactory *send_node_factory, |
| 21 | aos::EventLoop *send_event_loop, |
| 22 | std::unique_ptr<aos::RawFetcher> fetcher, |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 23 | std::unique_ptr<aos::RawSender> sender, |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 24 | MessageBridgeServerStatus *server_status, |
| 25 | size_t destination_node_index, |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 26 | ServerConnection *server_connection, int client_index, |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 27 | MessageBridgeClientStatus *client_status, |
| 28 | size_t channel_index, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 29 | aos::Sender<RemoteMessage> *timestamp_logger) |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 30 | : fetch_node_factory_(fetch_node_factory), |
| 31 | send_node_factory_(send_node_factory), |
| 32 | send_event_loop_(send_event_loop), |
| 33 | fetcher_(std::move(fetcher)), |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 34 | sender_(std::move(sender)), |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 35 | server_status_(server_status), |
| 36 | destination_node_index_(destination_node_index), |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 37 | server_connection_(server_connection), |
| 38 | client_status_(client_status), |
| 39 | client_index_(client_index), |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 40 | client_connection_(client_status_->GetClientConnection(client_index)), |
| 41 | channel_index_(channel_index), |
| 42 | timestamp_logger_(timestamp_logger) { |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 43 | timer_ = send_event_loop_->AddTimer([this]() { Send(); }); |
| 44 | |
| 45 | Schedule(); |
| 46 | } |
| 47 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 48 | const Channel *channel() const { return fetcher_->channel(); } |
| 49 | |
Austin Schuh | 4c570ea | 2020-11-19 23:13:24 -0800 | [diff] [blame] | 50 | uint32_t time_to_live() { |
| 51 | return configuration::ConnectionToNode(sender_->channel(), |
| 52 | send_node_factory_->node()) |
| 53 | ->time_to_live(); |
| 54 | } |
| 55 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 56 | // Kicks us to re-fetch and schedule the timer. |
| 57 | void Schedule() { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 58 | // Keep pulling messages out of the fetcher until we find one in the future. |
| 59 | while (true) { |
| 60 | if (fetcher_->context().data == nullptr || sent_) { |
| 61 | sent_ = !fetcher_->FetchNext(); |
| 62 | } |
| 63 | if (sent_) { |
| 64 | break; |
| 65 | } |
| 66 | if (fetcher_->context().monotonic_event_time + |
| 67 | send_node_factory_->network_delay() + |
| 68 | send_node_factory_->send_delay() > |
| 69 | fetch_node_factory_->monotonic_now()) { |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | // TODO(austin): Not cool. We want to actually forward these. This means |
| 74 | // we need a more sophisticated concept of what is running. |
| 75 | LOG(WARNING) << "Not forwarding message on " |
| 76 | << configuration::CleanedChannelToString(fetcher_->channel()) |
| 77 | << " because we aren't running. Set at " |
| 78 | << fetcher_->context().monotonic_event_time << " now is " |
| 79 | << fetch_node_factory_->monotonic_now(); |
| 80 | sent_ = true; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 81 | server_connection_->mutate_dropped_packets( |
| 82 | server_connection_->dropped_packets() + 1); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | if (fetcher_->context().data == nullptr) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | if (sent_) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | // Compute the time to publish this message. |
| 94 | const monotonic_clock::time_point monotonic_delivered_time = |
| 95 | DeliveredTime(fetcher_->context()); |
| 96 | |
| 97 | CHECK_GE(monotonic_delivered_time, send_node_factory_->monotonic_now()) |
Austin Schuh | 2febf0d | 2020-09-21 22:24:30 -0700 | [diff] [blame] | 98 | << ": Trying to deliver message in the past on channel " |
| 99 | << configuration::StrippedChannelToString(fetcher_->channel()) |
| 100 | << " to node " << send_event_loop_->node()->name()->string_view() |
| 101 | << " sent from " << fetcher_->channel()->source_node()->string_view() |
| 102 | << " at " << fetch_node_factory_->monotonic_now(); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 103 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 104 | server_connection_->mutate_sent_packets(server_connection_->sent_packets() + |
| 105 | 1); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 106 | timer_->Setup(monotonic_delivered_time); |
| 107 | } |
| 108 | |
| 109 | private: |
| 110 | // Acutally sends the message, and reschedules. |
| 111 | void Send() { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 112 | // Fill out the send times. |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 113 | sender_->Send(fetcher_->context().data, fetcher_->context().size, |
| 114 | fetcher_->context().monotonic_event_time, |
| 115 | fetcher_->context().realtime_event_time, |
| 116 | fetcher_->context().queue_index); |
| 117 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 118 | // And simulate message_bridge's offset recovery. |
| 119 | client_status_->SampleFilter(client_index_, |
| 120 | fetcher_->context().monotonic_event_time, |
| 121 | sender_->monotonic_sent_time()); |
| 122 | |
| 123 | client_connection_->mutate_received_packets( |
| 124 | client_connection_->received_packets() + 1); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 125 | |
| 126 | if (timestamp_logger_) { |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 127 | aos::Sender<RemoteMessage>::Builder builder = |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 128 | timestamp_logger_->MakeBuilder(); |
| 129 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 130 | // Reset the filter every time the UUID changes. There's probably a more |
| 131 | // clever way to do this, but that means a better concept of rebooting. |
| 132 | if (server_status_->BootUUID(destination_node_index_) != |
| 133 | send_node_factory_->boot_uuid().string_view()) { |
| 134 | server_status_->ResetFilter(destination_node_index_); |
| 135 | server_status_->SetBootUUID( |
| 136 | destination_node_index_, |
| 137 | send_node_factory_->boot_uuid().string_view()); |
| 138 | } |
| 139 | |
| 140 | flatbuffers::Offset<flatbuffers::String> boot_uuid_offset = |
| 141 | builder.fbb()->CreateString( |
| 142 | send_node_factory_->boot_uuid().string_view()); |
| 143 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 144 | RemoteMessage::Builder message_header_builder = |
| 145 | builder.MakeBuilder<RemoteMessage>(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 146 | |
| 147 | message_header_builder.add_channel_index(channel_index_); |
| 148 | |
| 149 | // Swap the remote and sent metrics. They are from the sender's |
| 150 | // perspective, not the receiver's perspective. |
| 151 | message_header_builder.add_monotonic_remote_time( |
| 152 | fetcher_->context().monotonic_event_time.time_since_epoch().count()); |
| 153 | message_header_builder.add_realtime_remote_time( |
| 154 | fetcher_->context().realtime_event_time.time_since_epoch().count()); |
| 155 | message_header_builder.add_remote_queue_index( |
| 156 | fetcher_->context().queue_index); |
| 157 | |
| 158 | message_header_builder.add_monotonic_sent_time( |
| 159 | sender_->monotonic_sent_time().time_since_epoch().count()); |
| 160 | message_header_builder.add_realtime_sent_time( |
| 161 | sender_->realtime_sent_time().time_since_epoch().count()); |
| 162 | message_header_builder.add_queue_index(sender_->sent_queue_index()); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 163 | message_header_builder.add_boot_uuid(boot_uuid_offset); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 164 | |
| 165 | builder.Send(message_header_builder.Finish()); |
| 166 | } |
| 167 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 168 | sent_ = true; |
| 169 | Schedule(); |
| 170 | } |
| 171 | |
| 172 | // Converts from time on the sending node to time on the receiving node. |
| 173 | monotonic_clock::time_point DeliveredTime(const Context &context) const { |
| 174 | const distributed_clock::time_point distributed_sent_time = |
| 175 | fetch_node_factory_->ToDistributedClock(context.monotonic_event_time); |
| 176 | |
Austin Schuh | 2febf0d | 2020-09-21 22:24:30 -0700 | [diff] [blame] | 177 | return send_node_factory_->FromDistributedClock( |
| 178 | distributed_sent_time + send_node_factory_->network_delay() + |
| 179 | send_node_factory_->send_delay()); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // Factories used for time conversion. |
| 183 | aos::NodeEventLoopFactory *fetch_node_factory_; |
| 184 | aos::NodeEventLoopFactory *send_node_factory_; |
| 185 | |
| 186 | // Event loop which sending is scheduled on. |
| 187 | aos::EventLoop *send_event_loop_; |
| 188 | // Timer used to send. |
| 189 | aos::TimerHandler *timer_; |
| 190 | // Fetcher used to receive messages. |
| 191 | std::unique_ptr<aos::RawFetcher> fetcher_; |
| 192 | // Sender to send them back out. |
| 193 | std::unique_ptr<aos::RawSender> sender_; |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 194 | |
| 195 | MessageBridgeServerStatus *server_status_; |
| 196 | const size_t destination_node_index_; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 197 | // True if we have sent the message in the fetcher. |
| 198 | bool sent_ = false; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 199 | |
| 200 | ServerConnection *server_connection_ = nullptr; |
| 201 | MessageBridgeClientStatus *client_status_ = nullptr; |
| 202 | int client_index_; |
| 203 | ClientConnection *client_connection_ = nullptr; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 204 | |
| 205 | size_t channel_index_; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 206 | aos::Sender<RemoteMessage> *timestamp_logger_ = nullptr; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | SimulatedMessageBridge::SimulatedMessageBridge( |
| 210 | SimulatedEventLoopFactory *simulated_event_loop_factory) { |
| 211 | CHECK( |
| 212 | configuration::MultiNode(simulated_event_loop_factory->configuration())); |
| 213 | |
| 214 | // Pre-build up event loops for every node. They are pretty cheap anyways. |
| 215 | for (const Node *node : simulated_event_loop_factory->nodes()) { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 216 | auto it = event_loop_map_.emplace(std::make_pair( |
| 217 | node, |
| 218 | simulated_event_loop_factory->MakeEventLoop("message_bridge", node))); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 219 | |
| 220 | CHECK(it.second); |
| 221 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 222 | it.first->second.event_loop->SkipTimingReport(); |
| 223 | it.first->second.event_loop->SkipAosLog(); |
| 224 | |
| 225 | for (ServerConnection *connection : |
| 226 | it.first->second.server_status.server_connection()) { |
| 227 | if (connection == nullptr) continue; |
| 228 | |
| 229 | connection->mutate_state(message_bridge::State::CONNECTED); |
| 230 | } |
| 231 | |
| 232 | for (size_t i = 0; |
| 233 | i < it.first->second.client_status.mutable_client_statistics() |
| 234 | ->mutable_connections() |
| 235 | ->size(); |
| 236 | ++i) { |
| 237 | ClientConnection *connection = |
| 238 | it.first->second.client_status.mutable_client_statistics() |
| 239 | ->mutable_connections() |
| 240 | ->GetMutableObject(i); |
| 241 | if (connection == nullptr) continue; |
| 242 | |
| 243 | connection->mutate_state(message_bridge::State::CONNECTED); |
| 244 | } |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 247 | for (const Node *node : simulated_event_loop_factory->nodes()) { |
| 248 | auto it = event_loop_map_.find(node); |
| 249 | |
| 250 | CHECK(it != event_loop_map_.end()); |
| 251 | |
| 252 | size_t node_index = 0; |
| 253 | for (ServerConnection *connection : |
| 254 | it->second.server_status.server_connection()) { |
| 255 | if (connection != nullptr) { |
| 256 | const Node *client_node = |
| 257 | simulated_event_loop_factory->configuration()->nodes()->Get( |
| 258 | node_index); |
| 259 | auto client_event_loop = event_loop_map_.find(client_node); |
| 260 | it->second.server_status.ResetFilter(node_index); |
| 261 | it->second.server_status.SetBootUUID( |
| 262 | node_index, |
| 263 | client_event_loop->second.event_loop->boot_uuid().string_view()); |
| 264 | } |
| 265 | ++node_index; |
| 266 | } |
| 267 | } |
| 268 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 269 | for (const Channel *channel : |
| 270 | *simulated_event_loop_factory->configuration()->channels()) { |
| 271 | if (!channel->has_destination_nodes()) { |
| 272 | continue; |
| 273 | } |
| 274 | |
| 275 | // Find the sending node. |
| 276 | const Node *node = |
| 277 | configuration::GetNode(simulated_event_loop_factory->configuration(), |
| 278 | channel->source_node()->string_view()); |
| 279 | auto source_event_loop = event_loop_map_.find(node); |
| 280 | CHECK(source_event_loop != event_loop_map_.end()); |
| 281 | |
| 282 | std::unique_ptr<DelayersVector> delayers = |
| 283 | std::make_unique<DelayersVector>(); |
| 284 | |
| 285 | // And then build up a RawMessageDelayer for each destination. |
| 286 | for (const Connection *connection : *channel->destination_nodes()) { |
| 287 | const Node *destination_node = |
| 288 | configuration::GetNode(simulated_event_loop_factory->configuration(), |
| 289 | connection->name()->string_view()); |
| 290 | auto destination_event_loop = event_loop_map_.find(destination_node); |
| 291 | CHECK(destination_event_loop != event_loop_map_.end()); |
| 292 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 293 | ServerConnection *server_connection = |
| 294 | source_event_loop->second.server_status.FindServerConnection( |
| 295 | connection->name()->string_view()); |
| 296 | |
| 297 | int client_index = |
| 298 | destination_event_loop->second.client_status.FindClientIndex( |
| 299 | channel->source_node()->string_view()); |
| 300 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 301 | const size_t destination_node_index = configuration::GetNodeIndex( |
| 302 | simulated_event_loop_factory->configuration(), destination_node); |
| 303 | |
| 304 | const bool delivery_time_is_logged = |
| 305 | configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
| 306 | connection, source_event_loop->second.event_loop->node()); |
| 307 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 308 | delayers->emplace_back(std::make_unique<RawMessageDelayer>( |
| 309 | simulated_event_loop_factory->GetNodeEventLoopFactory(node), |
| 310 | simulated_event_loop_factory->GetNodeEventLoopFactory( |
| 311 | destination_node), |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 312 | destination_event_loop->second.event_loop.get(), |
| 313 | source_event_loop->second.event_loop->MakeRawFetcher(channel), |
| 314 | destination_event_loop->second.event_loop->MakeRawSender(channel), |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame^] | 315 | &source_event_loop->second.server_status, destination_node_index, |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 316 | server_connection, client_index, |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 317 | &destination_event_loop->second.client_status, |
| 318 | configuration::ChannelIndex( |
| 319 | source_event_loop->second.event_loop->configuration(), channel), |
| 320 | delivery_time_is_logged |
| 321 | ? &source_event_loop->second |
| 322 | .timestamp_loggers[destination_node_index] |
| 323 | : nullptr)); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 326 | const Channel *const timestamp_channel = configuration::GetChannel( |
| 327 | simulated_event_loop_factory->configuration(), "/aos", |
| 328 | Timestamp::GetFullyQualifiedName(), |
| 329 | source_event_loop->second.event_loop->name(), node); |
| 330 | |
| 331 | if (channel == timestamp_channel) { |
| 332 | source_event_loop->second.server_status.set_send_data( |
| 333 | [captured_delayers = delayers.get()](const Context &) { |
| 334 | for (std::unique_ptr<RawMessageDelayer> &delayer : |
| 335 | *captured_delayers) { |
| 336 | delayer->Schedule(); |
| 337 | } |
| 338 | }); |
| 339 | } else { |
| 340 | // And register every delayer to be poked when a new message shows up. |
Austin Schuh | 4c570ea | 2020-11-19 23:13:24 -0800 | [diff] [blame] | 341 | |
| 342 | source_event_loop->second.event_loop->OnRun([captured_delayers = |
| 343 | delayers.get()]() { |
| 344 | // Poke all the reliable delayers so they send any queued messages. |
| 345 | for (std::unique_ptr<RawMessageDelayer> &delayer : *captured_delayers) { |
| 346 | if (delayer->time_to_live() == 0) { |
| 347 | delayer->Schedule(); |
| 348 | } |
| 349 | } |
| 350 | }); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 351 | source_event_loop->second.event_loop->MakeRawNoArgWatcher( |
| 352 | channel, [captured_delayers = delayers.get()](const Context &) { |
| 353 | for (std::unique_ptr<RawMessageDelayer> &delayer : |
| 354 | *captured_delayers) { |
| 355 | delayer->Schedule(); |
| 356 | } |
| 357 | }); |
| 358 | } |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 359 | delayers_list_.emplace_back(std::move(delayers)); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | SimulatedMessageBridge::~SimulatedMessageBridge() {} |
| 364 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 365 | void SimulatedMessageBridge::DisableForwarding(const Channel *channel) { |
| 366 | for (std::unique_ptr<std::vector<std::unique_ptr<RawMessageDelayer>>> |
| 367 | &delayers : delayers_list_) { |
| 368 | if (delayers->size() > 0) { |
| 369 | if ((*delayers)[0]->channel() == channel) { |
| 370 | for (std::unique_ptr<RawMessageDelayer> &delayer : *delayers) { |
| 371 | CHECK(delayer->channel() == channel); |
| 372 | } |
| 373 | |
| 374 | // If we clear the delayers list, nothing will be scheduled. Which is a |
| 375 | // success! |
| 376 | delayers->clear(); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 382 | void SimulatedMessageBridge::DisableStatistics() { |
| 383 | for (std::pair<const Node *const, State> &state : event_loop_map_) { |
| 384 | state.second.server_status.DisableStatistics(); |
| 385 | state.second.client_status.DisableStatistics(); |
| 386 | } |
| 387 | } |
| 388 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 389 | SimulatedMessageBridge::State::State( |
| 390 | std::unique_ptr<aos::EventLoop> &&new_event_loop) |
| 391 | : event_loop(std::move(new_event_loop)), |
| 392 | server_status(event_loop.get()), |
| 393 | client_status(event_loop.get()) { |
| 394 | timestamp_loggers.resize(event_loop->configuration()->nodes()->size()); |
| 395 | |
| 396 | // Find all nodes which log timestamps back to us (from us). |
| 397 | for (const Channel *channel : *event_loop->configuration()->channels()) { |
| 398 | CHECK(channel->has_source_node()); |
| 399 | |
| 400 | // Sent by us. |
| 401 | if (configuration::ChannelIsSendableOnNode(channel, event_loop->node()) && |
| 402 | channel->has_destination_nodes()) { |
| 403 | for (const Connection *connection : *channel->destination_nodes()) { |
| 404 | const bool delivery_time_is_logged = |
| 405 | configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
| 406 | connection, event_loop->node()); |
| 407 | |
| 408 | // And the timestamps are then logged back by us again. |
| 409 | if (!delivery_time_is_logged) { |
| 410 | continue; |
| 411 | } |
| 412 | |
| 413 | // (And only construct the sender if it hasn't been constructed) |
| 414 | const Node *other_node = configuration::GetNode( |
| 415 | event_loop->configuration(), connection->name()->string_view()); |
| 416 | const size_t other_node_index = configuration::GetNodeIndex( |
| 417 | event_loop->configuration(), other_node); |
| 418 | |
| 419 | if (!timestamp_loggers[other_node_index]) { |
| 420 | timestamp_loggers[other_node_index] = |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 421 | event_loop->MakeSender<RemoteMessage>( |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 422 | absl::StrCat("/aos/remote_timestamps/", |
| 423 | connection->name()->string_view())); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 430 | } // namespace message_bridge |
| 431 | } // namespace aos |