Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_SIMULATED_NETWORK_BRIDGE_H_ |
| 2 | #define AOS_EVENTS_SIMULATED_NETWORK_BRIDGE_H_ |
| 3 | |
| 4 | #include "aos/events/event_loop.h" |
| 5 | #include "aos/events/simulated_event_loop.h" |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 6 | #include "aos/network/message_bridge_client_status.h" |
| 7 | #include "aos/network/message_bridge_server_status.h" |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 8 | #include "aos/network/remote_message_generated.h" |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 9 | #include "aos/network/timestamp_channel.h" |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 10 | |
| 11 | namespace aos { |
| 12 | namespace message_bridge { |
| 13 | |
| 14 | class RawMessageDelayer; |
| 15 | |
| 16 | // This class moves messages between nodes. It is implemented as a separate |
| 17 | // class because it would have been even harder to manage forwarding in the |
| 18 | // SimulatedEventLoopFactory. |
| 19 | class SimulatedMessageBridge { |
| 20 | public: |
| 21 | // Constructs the bridge. |
| 22 | SimulatedMessageBridge( |
| 23 | SimulatedEventLoopFactory *simulated_event_loop_factory); |
| 24 | ~SimulatedMessageBridge(); |
| 25 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 26 | // Disables forwarding for this channel. This should be used very rarely only |
| 27 | // for things like the logger. |
| 28 | void DisableForwarding(const Channel *channel); |
| 29 | |
Austin Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 30 | void Disconnect(const Node *source, const Node *other); |
| 31 | void Connect(const Node *source, const Node *other); |
| 32 | void SetState(const Node *source, const Node *other, |
| 33 | message_bridge::State state); |
| 34 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 35 | // Disables generating and sending the messages which message_gateway sends. |
| 36 | // The messages are the ClientStatistics, ServerStatistics and Timestamp |
| 37 | // messages. |
| 38 | void DisableStatistics(); |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 39 | void DisableStatistics(const Node *node); |
| 40 | void EnableStatistics(); |
| 41 | void EnableStatistics(const Node *node); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 42 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 43 | private: |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 44 | struct DelayersVector { |
| 45 | std::vector<std::unique_ptr<RawMessageDelayer>> v; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 46 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 47 | bool disable_forwarding = false; |
| 48 | }; |
| 49 | struct State { |
| 50 | State(NodeEventLoopFactory *node_factory) : node_factory_(node_factory) { |
| 51 | const size_t num_nodes = node_factory->configuration()->nodes()->size(); |
| 52 | boot_uuids_.resize(num_nodes, UUID::Zero()); |
| 53 | client_state_.resize(num_nodes, message_bridge::State::CONNECTED); |
| 54 | server_state_.resize(num_nodes, message_bridge::State::CONNECTED); |
| 55 | } |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 56 | State(const State &state) = delete; |
| 57 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 58 | void DisableStatistics() { |
| 59 | disable_statistics_ = true; |
| 60 | if (server_status) { |
| 61 | server_status->DisableStatistics(); |
| 62 | } |
| 63 | if (client_status) { |
| 64 | client_status->DisableStatistics(); |
| 65 | } |
| 66 | } |
| 67 | |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 68 | void EnableStatistics() { |
| 69 | disable_statistics_ = false; |
| 70 | if (server_status) { |
| 71 | server_status->EnableStatistics(); |
| 72 | } |
| 73 | if (client_status) { |
| 74 | client_status->EnableStatistics(); |
| 75 | } |
| 76 | } |
| 77 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 78 | void AddSourceDelayer(RawMessageDelayer *delayer) { |
| 79 | source_delayers_.emplace_back(delayer); |
| 80 | } |
| 81 | void AddDestinationDelayer(RawMessageDelayer *delayer) { |
| 82 | destination_delayers_.emplace_back(delayer); |
| 83 | } |
| 84 | |
| 85 | void MakeEventLoop() { |
| 86 | SetEventLoop(node_factory_->MakeEventLoop("message_bridge")); |
| 87 | } |
| 88 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 89 | void SetEventLoop(std::unique_ptr<aos::EventLoop> loop); |
| 90 | |
| 91 | void SetSendData(std::function<void(const Context &)> fn) { |
| 92 | CHECK(!fn_); |
| 93 | fn_ = std::move(fn); |
| 94 | if (server_status) { |
| 95 | server_status->set_send_data(fn_); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void AddDelayerWatcher(const Channel *channel, DelayersVector *v) { |
| 100 | delayer_watchers_.emplace_back(channel, v); |
| 101 | } |
| 102 | |
| 103 | void SetBootUUID(size_t node_index, const UUID &boot_uuid) { |
| 104 | boot_uuids_[node_index] = boot_uuid; |
| 105 | const Node *node = |
| 106 | node_factory_->configuration()->nodes()->Get(node_index); |
| 107 | if (server_status) { |
| 108 | ServerConnection *connection = |
| 109 | server_status->FindServerConnection(node); |
| 110 | if (connection) { |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 111 | if (boot_uuid == UUID::Zero()) { |
| 112 | server_status->Disconnect(node_index); |
| 113 | server_status->ResetFilter(node_index); |
| 114 | } else { |
| 115 | switch (server_state_[node_index]) { |
| 116 | case message_bridge::State::DISCONNECTED: |
| 117 | server_status->Disconnect(node_index); |
| 118 | break; |
| 119 | case message_bridge::State::CONNECTED: |
| 120 | server_status->Connect(node_index, event_loop->monotonic_now()); |
| 121 | break; |
| 122 | } |
| 123 | server_status->ResetFilter(node_index); |
| 124 | server_status->SetBootUUID(node_index, boot_uuid); |
| 125 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | if (client_status) { |
| 129 | const int client_index = |
| 130 | client_status->FindClientIndex(node->name()->string_view()); |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 131 | client_status->SampleReset(client_index); |
| 132 | if (boot_uuid == UUID::Zero()) { |
| 133 | client_status->Disconnect(client_index); |
| 134 | } else { |
| 135 | switch (client_state_[node_index]) { |
| 136 | case message_bridge::State::CONNECTED: |
| 137 | client_status->Connect(client_index); |
| 138 | break; |
| 139 | case message_bridge::State::DISCONNECTED: |
| 140 | client_status->Disconnect(client_index); |
| 141 | break; |
| 142 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void SetServerState(const Node *destination, message_bridge::State state) { |
| 148 | const size_t node_index = configuration::GetNodeIndex( |
| 149 | node_factory_->configuration(), destination); |
| 150 | server_state_[node_index] = state; |
| 151 | if (server_status) { |
| 152 | ServerConnection *connection = |
| 153 | server_status->FindServerConnection(destination); |
| 154 | if (connection == nullptr) return; |
| 155 | |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 156 | if (state == connection->state()) { |
| 157 | return; |
| 158 | } |
| 159 | switch (state) { |
| 160 | case message_bridge::State::DISCONNECTED: |
| 161 | server_status->Disconnect(node_index); |
| 162 | break; |
| 163 | case message_bridge::State::CONNECTED: |
| 164 | server_status->Connect(node_index, event_loop->monotonic_now()); |
| 165 | break; |
| 166 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | void SetClientState(const Node *source, message_bridge::State state) { |
| 171 | const size_t node_index = |
| 172 | configuration::GetNodeIndex(node_factory_->configuration(), source); |
| 173 | client_state_[node_index] = state; |
| 174 | if (client_status) { |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 175 | const int client_index = |
| 176 | client_status->FindClientIndex(source->name()->string_view()); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 177 | ClientConnection *connection = |
| 178 | client_status->GetClientConnection(source); |
| 179 | |
Austin Schuh | 367a7f4 | 2021-11-23 23:04:36 -0800 | [diff] [blame] | 180 | // TODO(austin): Are there cases where we want to dedup 2 CONNECTED |
| 181 | // calls? |
| 182 | if (connection->state() != state) { |
| 183 | switch (state) { |
| 184 | case message_bridge::State::CONNECTED: |
| 185 | client_status->Connect(client_index); |
| 186 | break; |
| 187 | case message_bridge::State::DISCONNECTED: |
| 188 | client_status->Disconnect(client_index); |
| 189 | break; |
| 190 | } |
| 191 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | std::vector<UUID> boot_uuids_; |
| 196 | std::vector<message_bridge::State> client_state_; |
| 197 | std::vector<message_bridge::State> server_state_; |
| 198 | |
| 199 | std::vector<std::pair<const Channel *, DelayersVector *>> delayer_watchers_; |
| 200 | |
| 201 | std::function<void(const Context &)> fn_; |
| 202 | |
| 203 | NodeEventLoopFactory *node_factory_; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 204 | std::unique_ptr<aos::EventLoop> event_loop; |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 205 | ChannelTimestampSender timestamp_loggers; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 206 | std::unique_ptr<MessageBridgeServerStatus> server_status; |
| 207 | std::unique_ptr<MessageBridgeClientStatus> client_status; |
| 208 | |
| 209 | // List of delayers to update whenever this node starts or stops. |
| 210 | // Source delayers (which are the ones fetching). |
| 211 | std::vector<RawMessageDelayer *> source_delayers_; |
| 212 | // Destination delayers (which are the ones sending on the receiving nodes). |
| 213 | std::vector<RawMessageDelayer *> destination_delayers_; |
| 214 | |
| 215 | bool disable_statistics_ = false; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 216 | }; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 217 | // Map of nodes to event loops. This is a member variable so that the |
| 218 | // lifetime of the event loops matches the lifetime of the bridge. |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 219 | std::map<const Node *, State> event_loop_map_; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 220 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 221 | // List of delayers used to resend the messages. |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 222 | std::vector<std::unique_ptr<DelayersVector>> delayers_list_; |
| 223 | }; |
| 224 | |
| 225 | } // namespace message_bridge |
| 226 | } // namespace aos |
| 227 | |
| 228 | #endif // AOS_EVENTS_SIMULATED_NETWORK_BRIDGE_H_ |