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 | |
| 89 | void ClearEventLoop() { SetEventLoop(nullptr); } |
| 90 | void SetEventLoop(std::unique_ptr<aos::EventLoop> loop); |
| 91 | |
| 92 | void SetSendData(std::function<void(const Context &)> fn) { |
| 93 | CHECK(!fn_); |
| 94 | fn_ = std::move(fn); |
| 95 | if (server_status) { |
| 96 | server_status->set_send_data(fn_); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void AddDelayerWatcher(const Channel *channel, DelayersVector *v) { |
| 101 | delayer_watchers_.emplace_back(channel, v); |
| 102 | } |
| 103 | |
| 104 | void SetBootUUID(size_t node_index, const UUID &boot_uuid) { |
| 105 | boot_uuids_[node_index] = boot_uuid; |
| 106 | const Node *node = |
| 107 | node_factory_->configuration()->nodes()->Get(node_index); |
| 108 | if (server_status) { |
| 109 | ServerConnection *connection = |
| 110 | server_status->FindServerConnection(node); |
| 111 | if (connection) { |
| 112 | connection->mutate_state(server_state_[node_index]); |
| 113 | server_status->ResetFilter(node_index); |
| 114 | server_status->SetBootUUID(node_index, boot_uuid); |
| 115 | } |
| 116 | } |
| 117 | if (client_status) { |
| 118 | const int client_index = |
| 119 | client_status->FindClientIndex(node->name()->string_view()); |
| 120 | ClientConnection *client_connection = |
| 121 | client_status->GetClientConnection(client_index); |
| 122 | if (client_connection) { |
| 123 | client_status->SampleReset(client_index); |
| 124 | client_connection->mutate_state(client_state_[node_index]); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void SetServerState(const Node *destination, message_bridge::State state) { |
| 130 | const size_t node_index = configuration::GetNodeIndex( |
| 131 | node_factory_->configuration(), destination); |
| 132 | server_state_[node_index] = state; |
| 133 | if (server_status) { |
| 134 | ServerConnection *connection = |
| 135 | server_status->FindServerConnection(destination); |
| 136 | if (connection == nullptr) return; |
| 137 | |
| 138 | connection->mutate_state(state); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void SetClientState(const Node *source, message_bridge::State state) { |
| 143 | const size_t node_index = |
| 144 | configuration::GetNodeIndex(node_factory_->configuration(), source); |
| 145 | client_state_[node_index] = state; |
| 146 | if (client_status) { |
| 147 | ClientConnection *connection = |
| 148 | client_status->GetClientConnection(source); |
| 149 | |
| 150 | if (connection == nullptr) return; |
| 151 | |
| 152 | connection->mutate_state(state); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | std::vector<UUID> boot_uuids_; |
| 157 | std::vector<message_bridge::State> client_state_; |
| 158 | std::vector<message_bridge::State> server_state_; |
| 159 | |
| 160 | std::vector<std::pair<const Channel *, DelayersVector *>> delayer_watchers_; |
| 161 | |
| 162 | std::function<void(const Context &)> fn_; |
| 163 | |
| 164 | NodeEventLoopFactory *node_factory_; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 165 | std::unique_ptr<aos::EventLoop> event_loop; |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 166 | ChannelTimestampSender timestamp_loggers; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 167 | std::unique_ptr<MessageBridgeServerStatus> server_status; |
| 168 | std::unique_ptr<MessageBridgeClientStatus> client_status; |
| 169 | |
| 170 | // List of delayers to update whenever this node starts or stops. |
| 171 | // Source delayers (which are the ones fetching). |
| 172 | std::vector<RawMessageDelayer *> source_delayers_; |
| 173 | // Destination delayers (which are the ones sending on the receiving nodes). |
| 174 | std::vector<RawMessageDelayer *> destination_delayers_; |
| 175 | |
| 176 | bool disable_statistics_ = false; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 177 | }; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 178 | // Map of nodes to event loops. This is a member variable so that the |
| 179 | // lifetime of the event loops matches the lifetime of the bridge. |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 180 | std::map<const Node *, State> event_loop_map_; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 181 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 182 | // List of delayers used to resend the messages. |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 183 | std::vector<std::unique_ptr<DelayersVector>> delayers_list_; |
| 184 | }; |
| 185 | |
| 186 | } // namespace message_bridge |
| 187 | } // namespace aos |
| 188 | |
| 189 | #endif // AOS_EVENTS_SIMULATED_NETWORK_BRIDGE_H_ |