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" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 4 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 5 | #include "aos/configuration.h" |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 6 | #include "aos/events/event_loop.h" |
| 7 | #include "aos/events/simulated_event_loop.h" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 8 | #include "aos/network/remote_message_generated.h" |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 9 | |
| 10 | namespace aos { |
| 11 | namespace message_bridge { |
| 12 | |
| 13 | // This class delays messages forwarded between two factories. |
| 14 | // |
| 15 | // The basic design is that we need to use the distributed_clock to convert |
| 16 | // monotonic times from the source to the destination node. We also use a |
| 17 | // fetcher to manage the queue of data, and a timer to schedule the sends. |
| 18 | class RawMessageDelayer { |
| 19 | public: |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 20 | RawMessageDelayer(const Channel *channel, const Connection *connection, |
| 21 | aos::NodeEventLoopFactory *fetch_node_factory, |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 22 | aos::NodeEventLoopFactory *send_node_factory, |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 23 | size_t destination_node_index, bool delivery_time_is_logged) |
| 24 | : channel_(channel), |
| 25 | connection_(connection), |
| 26 | fetch_node_factory_(fetch_node_factory), |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 27 | send_node_factory_(send_node_factory), |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 28 | destination_node_index_(destination_node_index), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 29 | channel_index_(configuration::ChannelIndex( |
| 30 | fetch_node_factory_->configuration(), channel_)), |
| 31 | delivery_time_is_logged_(delivery_time_is_logged) {} |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 32 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 33 | bool forwarding_disabled() const { return forwarding_disabled_; } |
| 34 | void set_forwarding_disabled(bool forwarding_disabled) { |
| 35 | forwarding_disabled_ = forwarding_disabled; |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 36 | if (!forwarding_disabled_) { |
| 37 | CHECK(timestamp_logger_ == nullptr); |
| 38 | CHECK(sender_ == nullptr); |
| 39 | } |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 42 | void SetFetchEventLoop(aos::EventLoop *fetch_event_loop, |
| 43 | MessageBridgeServerStatus *server_status, |
| 44 | ChannelTimestampSender *timestamp_loggers) { |
| 45 | sent_ = false; |
| 46 | fetch_event_loop_ = fetch_event_loop; |
| 47 | if (fetch_event_loop_) { |
| 48 | fetcher_ = fetch_event_loop_->MakeRawFetcher(channel_); |
| 49 | } else { |
| 50 | fetcher_ = nullptr; |
| 51 | } |
| 52 | |
| 53 | server_status_ = server_status; |
| 54 | if (server_status) { |
| 55 | server_connection_ = |
| 56 | server_status_->FindServerConnection(send_node_factory_->node()); |
| 57 | } |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 58 | if (delivery_time_is_logged_ && timestamp_loggers != nullptr && |
| 59 | !forwarding_disabled_) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 60 | timestamp_logger_ = |
| 61 | timestamp_loggers->SenderForChannel(channel_, connection_); |
| 62 | } else { |
| 63 | timestamp_logger_ = nullptr; |
| 64 | } |
| 65 | |
| 66 | if (fetch_event_loop_) { |
| 67 | timestamp_timer_ = |
| 68 | fetch_event_loop_->AddTimer([this]() { SendTimestamp(); }); |
| 69 | if (send_event_loop_) { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 70 | std::string timer_name = |
| 71 | absl::StrCat(send_event_loop_->node()->name()->string_view(), " ", |
| 72 | fetcher_->channel()->name()->string_view(), " ", |
| 73 | fetcher_->channel()->type()->string_view()); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 74 | if (timer_) { |
| 75 | timer_->set_name(timer_name); |
| 76 | } |
| 77 | timestamp_timer_->set_name(absl::StrCat(timer_name, " timestamps")); |
| 78 | } |
| 79 | } else { |
| 80 | timestamp_timer_ = nullptr; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void SetSendEventLoop(aos::EventLoop *send_event_loop, |
| 85 | MessageBridgeClientStatus *client_status) { |
| 86 | sent_ = false; |
| 87 | send_event_loop_ = send_event_loop; |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 88 | if (send_event_loop_ && !forwarding_disabled_) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 89 | sender_ = send_event_loop_->MakeRawSender(channel_); |
| 90 | } else { |
| 91 | sender_ = nullptr; |
| 92 | } |
| 93 | |
| 94 | client_status_ = client_status; |
| 95 | if (client_status_) { |
| 96 | client_index_ = client_status_->FindClientIndex( |
| 97 | channel_->source_node()->string_view()); |
| 98 | client_connection_ = client_status_->GetClientConnection(client_index_); |
| 99 | } else { |
| 100 | client_index_ = -1; |
| 101 | client_connection_ = nullptr; |
| 102 | } |
| 103 | |
| 104 | if (send_event_loop_) { |
| 105 | timer_ = send_event_loop_->AddTimer([this]() { Send(); }); |
| 106 | if (fetcher_) { |
| 107 | std::string timer_name = |
| 108 | absl::StrCat(send_event_loop_->node()->name()->string_view(), " ", |
| 109 | fetcher_->channel()->name()->string_view(), " ", |
| 110 | fetcher_->channel()->type()->string_view()); |
| 111 | timer_->set_name(timer_name); |
| 112 | if (timestamp_timer_) { |
| 113 | timestamp_timer_->set_name(absl::StrCat(timer_name, " timestamps")); |
| 114 | } |
| 115 | } |
| 116 | } else { |
| 117 | timer_ = nullptr; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | const Channel *channel() const { return channel_; } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 122 | |
Austin Schuh | 4c570ea | 2020-11-19 23:13:24 -0800 | [diff] [blame] | 123 | uint32_t time_to_live() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 124 | return configuration::ConnectionToNode(channel_, send_node_factory_->node()) |
Austin Schuh | 4c570ea | 2020-11-19 23:13:24 -0800 | [diff] [blame] | 125 | ->time_to_live(); |
| 126 | } |
| 127 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 128 | void ScheduleReliable() { |
| 129 | if (forwarding_disabled()) return; |
| 130 | |
| 131 | if (!fetcher_) { |
| 132 | return; |
| 133 | } |
| 134 | if (fetcher_->context().data == nullptr || sent_) { |
| 135 | sent_ = !fetcher_->Fetch(); |
| 136 | } |
| 137 | |
| 138 | FetchNext(); |
| 139 | if (fetcher_->context().data == nullptr || sent_) { |
| 140 | return; |
| 141 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 142 | |
| 143 | // Send at startup. It is the best we can do. |
| 144 | const monotonic_clock::time_point monotonic_delivered_time = |
| 145 | send_node_factory_->monotonic_now() + |
| 146 | send_node_factory_->network_delay(); |
| 147 | |
| 148 | CHECK_GE(monotonic_delivered_time, send_node_factory_->monotonic_now()) |
| 149 | << ": Trying to deliver message in the past on channel " |
| 150 | << configuration::StrippedChannelToString(fetcher_->channel()) |
| 151 | << " to node " << send_event_loop_->node()->name()->string_view() |
| 152 | << " sent from " << fetcher_->channel()->source_node()->string_view() |
| 153 | << " at " << fetch_node_factory_->monotonic_now(); |
| 154 | |
| 155 | if (timer_) { |
| 156 | server_connection_->mutate_sent_packets( |
| 157 | server_connection_->sent_packets() + 1); |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 158 | timer_->Schedule(monotonic_delivered_time); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 159 | timer_scheduled_ = true; |
| 160 | } else { |
| 161 | server_connection_->mutate_dropped_packets( |
| 162 | server_connection_->dropped_packets() + 1); |
| 163 | sent_ = true; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | bool timer_scheduled_ = false; |
| 168 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 169 | // Kicks us to re-fetch and schedule the timer. |
| 170 | void Schedule() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 171 | CHECK(!forwarding_disabled()); |
| 172 | if (!fetcher_) { |
| 173 | return; |
| 174 | } |
| 175 | if (timer_scheduled_) { |
| 176 | return; |
| 177 | } |
| 178 | FetchNext(); |
| 179 | if (fetcher_->context().data == nullptr || sent_) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | // Compute the time to publish this message. |
| 184 | const monotonic_clock::time_point monotonic_delivered_time = |
| 185 | DeliveredTime(fetcher_->context()); |
| 186 | |
| 187 | CHECK_GE(monotonic_delivered_time, send_node_factory_->monotonic_now()) |
| 188 | << ": Trying to deliver message in the past on channel " |
| 189 | << configuration::StrippedChannelToString(fetcher_->channel()) |
| 190 | << " to node " << send_event_loop_->node()->name()->string_view() |
| 191 | << " sent from " << fetcher_->channel()->source_node()->string_view() |
| 192 | << " at " << fetch_node_factory_->monotonic_now(); |
| 193 | |
| 194 | if (timer_) { |
| 195 | server_connection_->mutate_sent_packets( |
| 196 | server_connection_->sent_packets() + 1); |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 197 | timer_->Schedule(monotonic_delivered_time); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 198 | timer_scheduled_ = true; |
| 199 | } else { |
| 200 | server_connection_->mutate_dropped_packets( |
| 201 | server_connection_->dropped_packets() + 1); |
| 202 | sent_ = true; |
| 203 | Schedule(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | private: |
| 208 | void FetchNext() { |
| 209 | CHECK(server_connection_); |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 210 | // Keep pulling messages out of the fetcher until we find one in the future. |
| 211 | while (true) { |
| 212 | if (fetcher_->context().data == nullptr || sent_) { |
| 213 | sent_ = !fetcher_->FetchNext(); |
| 214 | } |
| 215 | if (sent_) { |
| 216 | break; |
| 217 | } |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 218 | |
| 219 | if (server_connection_->state() != State::CONNECTED) { |
| 220 | sent_ = true; |
| 221 | server_connection_->mutate_dropped_packets( |
| 222 | server_connection_->dropped_packets() + 1); |
| 223 | continue; |
| 224 | } |
| 225 | |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 226 | if (fetcher_->context().monotonic_event_time + |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 227 | send_node_factory_->network_delay() + |
| 228 | send_node_factory_->send_delay() > |
| 229 | fetch_node_factory_->monotonic_now() || |
| 230 | time_to_live() == 0) { |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 231 | break; |
| 232 | } |
| 233 | |
| 234 | // TODO(austin): Not cool. We want to actually forward these. This means |
| 235 | // we need a more sophisticated concept of what is running. |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 236 | // TODO(james): This fails if multiple messages are sent on the same |
| 237 | // channel within the same callback. |
Austin Schuh | 6aa77be | 2020-02-22 21:06:40 -0800 | [diff] [blame] | 238 | LOG(WARNING) << "Not forwarding message on " |
| 239 | << configuration::CleanedChannelToString(fetcher_->channel()) |
| 240 | << " because we aren't running. Set at " |
| 241 | << fetcher_->context().monotonic_event_time << " now is " |
| 242 | << fetch_node_factory_->monotonic_now(); |
| 243 | sent_ = true; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 244 | server_connection_->mutate_dropped_packets( |
| 245 | server_connection_->dropped_packets() + 1); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 246 | } |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 249 | // Actually sends the message, and reschedules. |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 250 | void Send() { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 251 | timer_scheduled_ = false; |
| 252 | CHECK(sender_); |
| 253 | CHECK(client_status_); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 254 | if (server_connection_->state() != State::CONNECTED) { |
| 255 | sent_ = true; |
| 256 | Schedule(); |
| 257 | return; |
| 258 | } |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 259 | // Fill out the send times. |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 260 | sender_->CheckOk(sender_->Send( |
| 261 | fetcher_->context().data, fetcher_->context().size, |
| 262 | fetcher_->context().monotonic_event_time, |
| 263 | fetcher_->context().realtime_event_time, |
| 264 | fetcher_->context().queue_index, fetcher_->context().source_boot_uuid)); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 265 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 266 | // And simulate message_bridge's offset recovery. |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 267 | client_status_->SampleFilter( |
| 268 | client_index_, fetcher_->context().monotonic_event_time, |
| 269 | sender_->monotonic_sent_time(), fetcher_->context().source_boot_uuid); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 270 | |
| 271 | client_connection_->mutate_received_packets( |
| 272 | client_connection_->received_packets() + 1); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 273 | |
| 274 | if (timestamp_logger_) { |
Austin Schuh | eeaa202 | 2021-01-02 21:52:03 -0800 | [diff] [blame] | 275 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 276 | fbb.ForceDefaults(true); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 277 | // Reset the filter every time the UUID changes. There's probably a more |
| 278 | // clever way to do this, but that means a better concept of rebooting. |
| 279 | if (server_status_->BootUUID(destination_node_index_) != |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 280 | send_node_factory_->boot_uuid()) { |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 281 | server_status_->ResetFilter(destination_node_index_); |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 282 | server_status_->SetBootUUID(destination_node_index_, |
| 283 | send_node_factory_->boot_uuid()); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Austin Schuh | cdd9027 | 2021-03-15 12:46:16 -0700 | [diff] [blame] | 286 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> boot_uuid_offset = |
| 287 | send_node_factory_->boot_uuid().PackVector(&fbb); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 288 | |
Austin Schuh | eeaa202 | 2021-01-02 21:52:03 -0800 | [diff] [blame] | 289 | RemoteMessage::Builder message_header_builder(fbb); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 290 | |
| 291 | message_header_builder.add_channel_index(channel_index_); |
| 292 | |
| 293 | // Swap the remote and sent metrics. They are from the sender's |
| 294 | // perspective, not the receiver's perspective. |
| 295 | message_header_builder.add_monotonic_remote_time( |
| 296 | fetcher_->context().monotonic_event_time.time_since_epoch().count()); |
| 297 | message_header_builder.add_realtime_remote_time( |
| 298 | fetcher_->context().realtime_event_time.time_since_epoch().count()); |
| 299 | message_header_builder.add_remote_queue_index( |
| 300 | fetcher_->context().queue_index); |
| 301 | |
| 302 | message_header_builder.add_monotonic_sent_time( |
| 303 | sender_->monotonic_sent_time().time_since_epoch().count()); |
| 304 | message_header_builder.add_realtime_sent_time( |
| 305 | sender_->realtime_sent_time().time_since_epoch().count()); |
| 306 | message_header_builder.add_queue_index(sender_->sent_queue_index()); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 307 | message_header_builder.add_boot_uuid(boot_uuid_offset); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 308 | |
Austin Schuh | eeaa202 | 2021-01-02 21:52:03 -0800 | [diff] [blame] | 309 | fbb.Finish(message_header_builder.Finish()); |
| 310 | |
| 311 | remote_timestamps_.emplace_back( |
| 312 | FlatbufferDetachedBuffer<RemoteMessage>(fbb.Release()), |
| 313 | fetch_node_factory_->monotonic_now() + |
| 314 | send_node_factory_->network_delay()); |
| 315 | ScheduleTimestamp(); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 318 | sent_ = true; |
| 319 | Schedule(); |
| 320 | } |
| 321 | |
Austin Schuh | eeaa202 | 2021-01-02 21:52:03 -0800 | [diff] [blame] | 322 | // Schedules sending the next timestamp in remote_timestamps_ if there is one. |
| 323 | void ScheduleTimestamp() { |
| 324 | if (remote_timestamps_.empty()) { |
| 325 | timestamp_timer_->Disable(); |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | if (scheduled_time_ != |
| 330 | remote_timestamps_.front().monotonic_timestamp_time) { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 331 | timestamp_timer_->Schedule( |
Austin Schuh | eeaa202 | 2021-01-02 21:52:03 -0800 | [diff] [blame] | 332 | remote_timestamps_.front().monotonic_timestamp_time); |
| 333 | scheduled_time_ = remote_timestamps_.front().monotonic_timestamp_time; |
| 334 | return; |
| 335 | } else { |
| 336 | scheduled_time_ = monotonic_clock::min_time; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // Sends the next timestamp in remote_timestamps_. |
| 341 | void SendTimestamp() { |
| 342 | CHECK(!remote_timestamps_.empty()); |
| 343 | |
| 344 | // Send out all timestamps at the currently scheduled time. |
| 345 | while (remote_timestamps_.front().monotonic_timestamp_time == |
| 346 | scheduled_time_) { |
| 347 | if (server_connection_->state() == State::CONNECTED) { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 348 | timestamp_logger_->CheckOk(timestamp_logger_->Send( |
| 349 | std::move(remote_timestamps_.front().remote_message))); |
Austin Schuh | eeaa202 | 2021-01-02 21:52:03 -0800 | [diff] [blame] | 350 | } |
| 351 | remote_timestamps_.pop_front(); |
| 352 | if (remote_timestamps_.empty()) { |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | ScheduleTimestamp(); |
| 358 | } |
| 359 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 360 | // Converts from time on the sending node to time on the receiving node. |
| 361 | monotonic_clock::time_point DeliveredTime(const Context &context) const { |
| 362 | const distributed_clock::time_point distributed_sent_time = |
| 363 | fetch_node_factory_->ToDistributedClock(context.monotonic_event_time); |
| 364 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 365 | const logger::BootTimestamp t = send_node_factory_->FromDistributedClock( |
Austin Schuh | 2febf0d | 2020-09-21 22:24:30 -0700 | [diff] [blame] | 366 | distributed_sent_time + send_node_factory_->network_delay() + |
| 367 | send_node_factory_->send_delay()); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 368 | CHECK_EQ(t.boot, send_node_factory_->boot_count()); |
| 369 | return t.time; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 372 | const Channel *channel_; |
| 373 | const Connection *connection_; |
| 374 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 375 | // Factories used for time conversion. |
| 376 | aos::NodeEventLoopFactory *fetch_node_factory_; |
| 377 | aos::NodeEventLoopFactory *send_node_factory_; |
| 378 | |
Austin Schuh | eeaa202 | 2021-01-02 21:52:03 -0800 | [diff] [blame] | 379 | // Event loop which fetching and sending timestamps are scheduled on. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 380 | aos::EventLoop *fetch_event_loop_ = nullptr; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 381 | // Event loop which sending is scheduled on. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 382 | aos::EventLoop *send_event_loop_ = nullptr; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 383 | // Timer used to send. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 384 | aos::TimerHandler *timer_ = nullptr; |
Austin Schuh | eeaa202 | 2021-01-02 21:52:03 -0800 | [diff] [blame] | 385 | // Timer used to send timestamps out. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 386 | aos::TimerHandler *timestamp_timer_ = nullptr; |
Austin Schuh | eeaa202 | 2021-01-02 21:52:03 -0800 | [diff] [blame] | 387 | // Time that the timer is scheduled for. Used to track if it needs to be |
| 388 | // rescheduled. |
| 389 | monotonic_clock::time_point scheduled_time_ = monotonic_clock::min_time; |
| 390 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 391 | // Fetcher used to receive messages. |
| 392 | std::unique_ptr<aos::RawFetcher> fetcher_; |
| 393 | // Sender to send them back out. |
| 394 | std::unique_ptr<aos::RawSender> sender_; |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 395 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 396 | MessageBridgeServerStatus *server_status_ = nullptr; |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 397 | const size_t destination_node_index_; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 398 | // True if we have sent the message in the fetcher. |
| 399 | bool sent_ = false; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 400 | |
| 401 | ServerConnection *server_connection_ = nullptr; |
| 402 | MessageBridgeClientStatus *client_status_ = nullptr; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 403 | int client_index_ = -1; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 404 | ClientConnection *client_connection_ = nullptr; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 405 | |
| 406 | size_t channel_index_; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 407 | aos::Sender<RemoteMessage> *timestamp_logger_ = nullptr; |
Austin Schuh | eeaa202 | 2021-01-02 21:52:03 -0800 | [diff] [blame] | 408 | |
| 409 | struct Timestamp { |
| 410 | Timestamp(FlatbufferDetachedBuffer<RemoteMessage> new_remote_message, |
| 411 | monotonic_clock::time_point new_monotonic_timestamp_time) |
| 412 | : remote_message(std::move(new_remote_message)), |
| 413 | monotonic_timestamp_time(new_monotonic_timestamp_time) {} |
| 414 | FlatbufferDetachedBuffer<RemoteMessage> remote_message; |
| 415 | monotonic_clock::time_point monotonic_timestamp_time; |
| 416 | }; |
| 417 | |
| 418 | std::deque<Timestamp> remote_timestamps_; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 419 | |
| 420 | bool delivery_time_is_logged_; |
| 421 | |
| 422 | bool forwarding_disabled_ = false; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 423 | }; |
| 424 | |
| 425 | SimulatedMessageBridge::SimulatedMessageBridge( |
| 426 | SimulatedEventLoopFactory *simulated_event_loop_factory) { |
| 427 | CHECK( |
| 428 | configuration::MultiNode(simulated_event_loop_factory->configuration())); |
| 429 | |
| 430 | // Pre-build up event loops for every node. They are pretty cheap anyways. |
| 431 | for (const Node *node : simulated_event_loop_factory->nodes()) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 432 | NodeEventLoopFactory *node_factory = |
| 433 | simulated_event_loop_factory->GetNodeEventLoopFactory(node); |
| 434 | auto it = event_loop_map_.emplace(node, node_factory); |
Austin Schuh | cde938c | 2020-02-02 17:30:07 -0800 | [diff] [blame] | 435 | CHECK(it.second); |
| 436 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 437 | node_factory->OnStartup( |
| 438 | [this, simulated_event_loop_factory, node_state = &it.first->second]() { |
| 439 | node_state->MakeEventLoop(); |
| 440 | const size_t my_node_index = configuration::GetNodeIndex( |
| 441 | simulated_event_loop_factory->configuration(), |
| 442 | node_state->event_loop->node()); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 443 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 444 | size_t node_index = 0; |
| 445 | for (ServerConnection *connection : |
| 446 | node_state->server_status->server_connection()) { |
| 447 | if (connection != nullptr) { |
| 448 | node_state->server_status->ResetFilter(node_index); |
| 449 | } |
| 450 | ++node_index; |
| 451 | } |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 452 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 453 | for (const ClientConnection *client_connections : |
| 454 | *node_state->client_status->mutable_client_statistics() |
| 455 | ->connections()) { |
| 456 | const Node *client_node = configuration::GetNode( |
| 457 | simulated_event_loop_factory->configuration(), |
| 458 | client_connections->node()->name()->string_view()); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 459 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 460 | auto client_event_loop = event_loop_map_.find(client_node); |
| 461 | client_event_loop->second.SetBootUUID( |
| 462 | my_node_index, node_state->event_loop->boot_uuid()); |
| 463 | } |
| 464 | }); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 465 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 466 | node_factory->OnShutdown([node_state = &it.first->second]() { |
| 467 | node_state->SetEventLoop(nullptr); |
| 468 | }); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 471 | for (const Channel *channel : |
| 472 | *simulated_event_loop_factory->configuration()->channels()) { |
| 473 | if (!channel->has_destination_nodes()) { |
| 474 | continue; |
| 475 | } |
| 476 | |
| 477 | // Find the sending node. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 478 | const Node *source_node = |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 479 | configuration::GetNode(simulated_event_loop_factory->configuration(), |
| 480 | channel->source_node()->string_view()); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 481 | auto source_event_loop = event_loop_map_.find(source_node); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 482 | CHECK(source_event_loop != event_loop_map_.end()); |
| 483 | |
| 484 | std::unique_ptr<DelayersVector> delayers = |
| 485 | std::make_unique<DelayersVector>(); |
| 486 | |
| 487 | // And then build up a RawMessageDelayer for each destination. |
| 488 | for (const Connection *connection : *channel->destination_nodes()) { |
| 489 | const Node *destination_node = |
| 490 | configuration::GetNode(simulated_event_loop_factory->configuration(), |
| 491 | connection->name()->string_view()); |
| 492 | auto destination_event_loop = event_loop_map_.find(destination_node); |
| 493 | CHECK(destination_event_loop != event_loop_map_.end()); |
| 494 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 495 | const size_t destination_node_index = configuration::GetNodeIndex( |
| 496 | simulated_event_loop_factory->configuration(), destination_node); |
| 497 | |
| 498 | const bool delivery_time_is_logged = |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 499 | configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection, |
| 500 | source_node); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 501 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 502 | delayers->v.emplace_back(std::make_unique<RawMessageDelayer>( |
| 503 | channel, connection, |
| 504 | simulated_event_loop_factory->GetNodeEventLoopFactory(source_node), |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 505 | simulated_event_loop_factory->GetNodeEventLoopFactory( |
| 506 | destination_node), |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 507 | destination_node_index, delivery_time_is_logged)); |
| 508 | |
| 509 | source_event_loop->second.AddSourceDelayer(delayers->v.back().get()); |
| 510 | destination_event_loop->second.AddDestinationDelayer( |
| 511 | delayers->v.back().get()); |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 514 | const Channel *const timestamp_channel = configuration::GetChannel( |
| 515 | simulated_event_loop_factory->configuration(), "/aos", |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 516 | Timestamp::GetFullyQualifiedName(), "message_bridge", source_node); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 517 | |
| 518 | if (channel == timestamp_channel) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 519 | source_event_loop->second.SetSendData( |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 520 | [captured_delayers = delayers.get()](const Context &) { |
| 521 | for (std::unique_ptr<RawMessageDelayer> &delayer : |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 522 | captured_delayers->v) { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 523 | delayer->Schedule(); |
| 524 | } |
| 525 | }); |
| 526 | } else { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 527 | source_event_loop->second.AddDelayerWatcher(channel, delayers.get()); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 528 | } |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 529 | delayers_list_.emplace_back(std::move(delayers)); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | SimulatedMessageBridge::~SimulatedMessageBridge() {} |
| 534 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 535 | void SimulatedMessageBridge::DisableForwarding(const Channel *channel) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 536 | for (std::unique_ptr<DelayersVector> &delayers : delayers_list_) { |
| 537 | if (delayers->v.size() > 0) { |
| 538 | if (delayers->v[0]->channel() == channel) { |
| 539 | delayers->disable_forwarding = true; |
| 540 | for (std::unique_ptr<RawMessageDelayer> &delayer : delayers->v) { |
| 541 | delayer->set_forwarding_disabled(true); |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 542 | } |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 548 | void SimulatedMessageBridge::Disconnect(const Node *source, |
| 549 | const Node *destination) { |
| 550 | SetState(source, destination, message_bridge::State::DISCONNECTED); |
| 551 | } |
| 552 | |
| 553 | void SimulatedMessageBridge::Connect(const Node *source, |
| 554 | const Node *destination) { |
| 555 | SetState(source, destination, message_bridge::State::CONNECTED); |
| 556 | } |
| 557 | void SimulatedMessageBridge::SetState(const Node *source, |
| 558 | const Node *destination, |
| 559 | message_bridge::State state) { |
| 560 | auto source_state = event_loop_map_.find(source); |
| 561 | CHECK(source_state != event_loop_map_.end()); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 562 | source_state->second.SetServerState(destination, state); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 563 | |
| 564 | auto destination_state = event_loop_map_.find(destination); |
| 565 | CHECK(destination_state != event_loop_map_.end()); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 566 | destination_state->second.SetClientState(source, state); |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 567 | } |
| 568 | |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 569 | void SimulatedMessageBridge::DisableStatistics(DestroySenders destroy_senders) { |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 570 | for (std::pair<const Node *const, State> &state : event_loop_map_) { |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 571 | state.second.DisableStatistics(destroy_senders); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 575 | void SimulatedMessageBridge::DisableStatistics(const Node *node, |
| 576 | DestroySenders destroy_senders) { |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 577 | auto it = event_loop_map_.find(node); |
| 578 | CHECK(it != event_loop_map_.end()); |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 579 | it->second.DisableStatistics(destroy_senders); |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | void SimulatedMessageBridge::EnableStatistics() { |
| 583 | for (std::pair<const Node *const, State> &state : event_loop_map_) { |
| 584 | state.second.EnableStatistics(); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | void SimulatedMessageBridge::EnableStatistics(const Node *node) { |
| 589 | auto it = event_loop_map_.find(node); |
| 590 | CHECK(it != event_loop_map_.end()); |
| 591 | it->second.EnableStatistics(); |
| 592 | } |
| 593 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 594 | void SimulatedMessageBridge::State::SetEventLoop( |
| 595 | std::unique_ptr<aos::EventLoop> loop) { |
| 596 | if (!loop) { |
| 597 | timestamp_loggers = ChannelTimestampSender(nullptr); |
| 598 | server_status.reset(); |
| 599 | client_status.reset(); |
| 600 | for (RawMessageDelayer *source_delayer : source_delayers_) { |
| 601 | source_delayer->SetFetchEventLoop(nullptr, nullptr, nullptr); |
| 602 | } |
| 603 | for (RawMessageDelayer *destination_delayer : destination_delayers_) { |
| 604 | destination_delayer->SetSendEventLoop(nullptr, nullptr); |
| 605 | } |
| 606 | event_loop = std::move(loop); |
| 607 | return; |
| 608 | } else { |
| 609 | CHECK(!event_loop); |
| 610 | } |
| 611 | event_loop = std::move(loop); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 612 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 613 | event_loop->SkipTimingReport(); |
| 614 | event_loop->SkipAosLog(); |
| 615 | |
| 616 | for (std::pair<const Channel *, DelayersVector *> &watcher : |
| 617 | delayer_watchers_) { |
| 618 | // Don't register watchers if we know we aren't forwarding. |
| 619 | if (watcher.second->disable_forwarding) continue; |
| 620 | event_loop->MakeRawNoArgWatcher( |
| 621 | watcher.first, [captured_delayers = watcher.second](const Context &) { |
| 622 | // We might get told after registering, so don't forward at that point |
| 623 | // too. |
| 624 | for (std::unique_ptr<RawMessageDelayer> &delayer : |
| 625 | captured_delayers->v) { |
| 626 | delayer->Schedule(); |
| 627 | } |
| 628 | }); |
| 629 | } |
| 630 | |
| 631 | timestamp_loggers = ChannelTimestampSender(event_loop.get()); |
| 632 | server_status = std::make_unique<MessageBridgeServerStatus>(event_loop.get()); |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 633 | if (disable_statistics_) { |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 634 | server_status->DisableStatistics(destroy_senders_ == DestroySenders::kYes); |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 635 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 636 | |
| 637 | { |
| 638 | size_t node_index = 0; |
| 639 | for (ServerConnection *connection : server_status->server_connection()) { |
| 640 | if (connection) { |
| 641 | if (boot_uuids_[node_index] != UUID::Zero()) { |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 642 | switch (server_state_[node_index]) { |
| 643 | case message_bridge::State::DISCONNECTED: |
| 644 | server_status->Disconnect(node_index); |
| 645 | break; |
| 646 | case message_bridge::State::CONNECTED: |
| 647 | server_status->Connect(node_index, event_loop->monotonic_now()); |
| 648 | break; |
| 649 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 650 | } else { |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 651 | server_status->Disconnect(node_index); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | ++node_index; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | for (size_t i = 0; i < boot_uuids_.size(); ++i) { |
| 659 | if (boot_uuids_[i] != UUID::Zero()) { |
| 660 | server_status->SetBootUUID(i, boot_uuids_[i]); |
| 661 | } |
| 662 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 663 | if (fn_) { |
| 664 | server_status->set_send_data(fn_); |
| 665 | } |
| 666 | client_status = std::make_unique<MessageBridgeClientStatus>(event_loop.get()); |
| 667 | if (disable_statistics_) { |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 668 | client_status->DisableStatistics(destroy_senders_ == DestroySenders::kYes); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | for (size_t i = 0; |
| 672 | i < client_status->mutable_client_statistics()->connections()->size(); |
| 673 | ++i) { |
| 674 | ClientConnection *client_connection = |
| 675 | client_status->mutable_client_statistics() |
| 676 | ->mutable_connections() |
| 677 | ->GetMutableObject(i); |
| 678 | const Node *client_node = configuration::GetNode( |
| 679 | node_factory_->configuration(), |
| 680 | client_connection->node()->name()->string_view()); |
| 681 | const size_t client_node_index = configuration::GetNodeIndex( |
| 682 | node_factory_->configuration(), client_node); |
| 683 | if (boot_uuids_[client_node_index] != UUID::Zero()) { |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 684 | if (client_connection->state() != client_state_[client_node_index]) { |
| 685 | switch (client_state_[client_node_index]) { |
| 686 | case message_bridge::State::DISCONNECTED: |
| 687 | client_status->Disconnect(i); |
| 688 | break; |
| 689 | case message_bridge::State::CONNECTED: |
| 690 | client_status->Connect(i); |
| 691 | break; |
| 692 | } |
| 693 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 694 | } else { |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 695 | client_status->Disconnect(i); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 699 | for (const Channel *channel : *event_loop->configuration()->channels()) { |
| 700 | CHECK(channel->has_source_node()); |
| 701 | |
| 702 | // Sent by us. |
| 703 | if (configuration::ChannelIsSendableOnNode(channel, event_loop->node()) && |
| 704 | channel->has_destination_nodes()) { |
| 705 | for (const Connection *connection : *channel->destination_nodes()) { |
| 706 | const bool delivery_time_is_logged = |
| 707 | configuration::ConnectionDeliveryTimeIsLoggedOnNode( |
| 708 | connection, event_loop->node()); |
| 709 | |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 710 | const RawMessageDelayer *delayer = nullptr; |
| 711 | for (const RawMessageDelayer *candidate : source_delayers_) { |
| 712 | if (candidate->channel() == channel) { |
| 713 | delayer = candidate; |
| 714 | } |
| 715 | } |
| 716 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 717 | // And the timestamps are then logged back by us again. |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 718 | if (!delivery_time_is_logged || |
| 719 | CHECK_NOTNULL(delayer)->forwarding_disabled()) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 720 | continue; |
| 721 | } |
| 722 | |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 723 | timestamp_loggers.SenderForChannel(channel, connection); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 727 | |
| 728 | for (RawMessageDelayer *source_delayer : source_delayers_) { |
| 729 | source_delayer->SetFetchEventLoop(event_loop.get(), server_status.get(), |
| 730 | ×tamp_loggers); |
| 731 | } |
| 732 | for (RawMessageDelayer *destination_delayer : destination_delayers_) { |
| 733 | destination_delayer->SetSendEventLoop(event_loop.get(), |
| 734 | client_status.get()); |
| 735 | } |
| 736 | event_loop->OnRun([this]() { |
| 737 | for (RawMessageDelayer *destination_delayer : destination_delayers_) { |
| 738 | if (destination_delayer->time_to_live() == 0) { |
| 739 | destination_delayer->ScheduleReliable(); |
| 740 | } |
| 741 | } |
James Kuszmaul | 86e86c3 | 2022-07-21 17:39:47 -0700 | [diff] [blame] | 742 | // Note: This exists to work around the fact that some users like to be able |
| 743 | // to send reliable messages while execution is stopped, creating a |
| 744 | // situation where the following sequencing can occur: |
| 745 | // 1) <While stopped> Send a reliable message on Node A (to be forwarded to |
| 746 | // Node B). |
| 747 | // 2) Node B starts up. |
| 748 | // 3) Anywhere from 0 to N seconds later, Node A starts up. |
| 749 | // |
| 750 | // In this case, we need the reliable message to make it to Node B, but it |
| 751 | // also shouldn't make it to Node B until Node A has started up. |
| 752 | // |
| 753 | // Ideally, if the user were to wait for the Node B OnRun callbacks to send |
| 754 | // the message, then that would trigger the watchers in the delayers. |
| 755 | // However, we so far have continued to support Sending while stopped.... |
| 756 | for (RawMessageDelayer *source_delayer : source_delayers_) { |
| 757 | if (source_delayer->time_to_live() == 0) { |
| 758 | source_delayer->ScheduleReliable(); |
| 759 | } |
| 760 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 761 | }); |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 764 | } // namespace message_bridge |
| 765 | } // namespace aos |