blob: 3a7a4e898f54d8c6da1a8ac8f524ae20400ba024 [file] [log] [blame]
Austin Schuh898f4972020-01-11 17:21:25 -08001#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 Schuh4c3b9702020-08-30 11:34:55 -07006#include "aos/network/message_bridge_client_status.h"
7#include "aos/network/message_bridge_server_status.h"
Austin Schuh0de30f32020-12-06 12:44:28 -08008#include "aos/network/remote_message_generated.h"
Austin Schuh89c9b812021-02-20 14:42:10 -08009#include "aos/network/timestamp_channel.h"
Austin Schuh898f4972020-01-11 17:21:25 -080010
11namespace aos {
12namespace message_bridge {
13
14class 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.
19class SimulatedMessageBridge {
20 public:
21 // Constructs the bridge.
22 SimulatedMessageBridge(
23 SimulatedEventLoopFactory *simulated_event_loop_factory);
24 ~SimulatedMessageBridge();
25
Austin Schuh6f3babe2020-01-26 20:34:50 -080026 // 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 Schuhc0b0f722020-12-12 18:36:06 -080030 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 Schuh4c3b9702020-08-30 11:34:55 -070035 // 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 Schuh48205e62021-11-12 14:13:18 -080039 void DisableStatistics(const Node *node);
40 void EnableStatistics();
41 void EnableStatistics(const Node *node);
Austin Schuh4c3b9702020-08-30 11:34:55 -070042
Austin Schuh898f4972020-01-11 17:21:25 -080043 private:
Austin Schuh58646e22021-08-23 23:51:46 -070044 struct DelayersVector {
45 std::vector<std::unique_ptr<RawMessageDelayer>> v;
Austin Schuh4c3b9702020-08-30 11:34:55 -070046
Austin Schuh58646e22021-08-23 23:51:46 -070047 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 Schuh4c3b9702020-08-30 11:34:55 -070056 State(const State &state) = delete;
57
Austin Schuh58646e22021-08-23 23:51:46 -070058 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 Schuh48205e62021-11-12 14:13:18 -080068 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 Schuh58646e22021-08-23 23:51:46 -070078 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 Schuh58646e22021-08-23 23:51:46 -070089 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 Schuh367a7f42021-11-23 23:04:36 -0800111 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 Schuh58646e22021-08-23 23:51:46 -0700126 }
127 }
128 if (client_status) {
129 const int client_index =
130 client_status->FindClientIndex(node->name()->string_view());
Austin Schuh367a7f42021-11-23 23:04:36 -0800131 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 Schuh58646e22021-08-23 23:51:46 -0700143 }
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 Schuh367a7f42021-11-23 23:04:36 -0800156 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 Schuh58646e22021-08-23 23:51:46 -0700167 }
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 Schuh367a7f42021-11-23 23:04:36 -0800175 const int client_index =
176 client_status->FindClientIndex(source->name()->string_view());
Austin Schuh58646e22021-08-23 23:51:46 -0700177 ClientConnection *connection =
178 client_status->GetClientConnection(source);
179
Austin Schuh367a7f42021-11-23 23:04:36 -0800180 // 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 Schuh58646e22021-08-23 23:51:46 -0700192 }
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 Schuh4c3b9702020-08-30 11:34:55 -0700204 std::unique_ptr<aos::EventLoop> event_loop;
Austin Schuh89c9b812021-02-20 14:42:10 -0800205 ChannelTimestampSender timestamp_loggers;
Austin Schuh58646e22021-08-23 23:51:46 -0700206 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 Schuh4c3b9702020-08-30 11:34:55 -0700216 };
Austin Schuh898f4972020-01-11 17:21:25 -0800217 // 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 Schuh4c3b9702020-08-30 11:34:55 -0700219 std::map<const Node *, State> event_loop_map_;
Austin Schuh898f4972020-01-11 17:21:25 -0800220
Austin Schuh898f4972020-01-11 17:21:25 -0800221 // List of delayers used to resend the messages.
Austin Schuh898f4972020-01-11 17:21:25 -0800222 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_