blob: 231bba17e792cbbff0508b89c67c6146461f1592 [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
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 Schuh4c3b9702020-08-30 11:34:55 -0700165 std::unique_ptr<aos::EventLoop> event_loop;
Austin Schuh89c9b812021-02-20 14:42:10 -0800166 ChannelTimestampSender timestamp_loggers;
Austin Schuh58646e22021-08-23 23:51:46 -0700167 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 Schuh4c3b9702020-08-30 11:34:55 -0700177 };
Austin Schuh898f4972020-01-11 17:21:25 -0800178 // 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 Schuh4c3b9702020-08-30 11:34:55 -0700180 std::map<const Node *, State> event_loop_map_;
Austin Schuh898f4972020-01-11 17:21:25 -0800181
Austin Schuh898f4972020-01-11 17:21:25 -0800182 // List of delayers used to resend the messages.
Austin Schuh898f4972020-01-11 17:21:25 -0800183 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_