blob: e8503965986824e02a48c17f62f6d728cce1a57b [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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080011namespace aos::message_bridge {
Austin Schuh898f4972020-01-11 17:21:25 -080012
13class RawMessageDelayer;
14
15// This class moves messages between nodes. It is implemented as a separate
16// class because it would have been even harder to manage forwarding in the
17// SimulatedEventLoopFactory.
18class SimulatedMessageBridge {
19 public:
20 // Constructs the bridge.
21 SimulatedMessageBridge(
22 SimulatedEventLoopFactory *simulated_event_loop_factory);
23 ~SimulatedMessageBridge();
24
Austin Schuh6f3babe2020-01-26 20:34:50 -080025 // Disables forwarding for this channel. This should be used very rarely only
26 // for things like the logger.
27 void DisableForwarding(const Channel *channel);
28
Austin Schuhc0b0f722020-12-12 18:36:06 -080029 void Disconnect(const Node *source, const Node *other);
30 void Connect(const Node *source, const Node *other);
31 void SetState(const Node *source, const Node *other,
32 message_bridge::State state);
33
Austin Schuh4c3b9702020-08-30 11:34:55 -070034 // Disables generating and sending the messages which message_gateway sends.
35 // The messages are the ClientStatistics, ServerStatistics and Timestamp
36 // messages.
James Kuszmaul94ca5132022-07-19 09:11:08 -070037 enum class DestroySenders { kNo, kYes };
38 void DisableStatistics(DestroySenders destroy_senders = DestroySenders::kNo);
39 void DisableStatistics(const Node *node,
40 DestroySenders destroy_senders = DestroySenders::kNo);
Austin Schuh48205e62021-11-12 14:13:18 -080041 void EnableStatistics();
42 void EnableStatistics(const Node *node);
Austin Schuh4c3b9702020-08-30 11:34:55 -070043
Austin Schuh898f4972020-01-11 17:21:25 -080044 private:
Austin Schuh58646e22021-08-23 23:51:46 -070045 struct DelayersVector {
46 std::vector<std::unique_ptr<RawMessageDelayer>> v;
Austin Schuh4c3b9702020-08-30 11:34:55 -070047
Austin Schuh58646e22021-08-23 23:51:46 -070048 bool disable_forwarding = false;
49 };
50 struct State {
51 State(NodeEventLoopFactory *node_factory) : node_factory_(node_factory) {
52 const size_t num_nodes = node_factory->configuration()->nodes()->size();
53 boot_uuids_.resize(num_nodes, UUID::Zero());
54 client_state_.resize(num_nodes, message_bridge::State::CONNECTED);
55 server_state_.resize(num_nodes, message_bridge::State::CONNECTED);
56 }
Austin Schuh4c3b9702020-08-30 11:34:55 -070057 State(const State &state) = delete;
58
James Kuszmaul94ca5132022-07-19 09:11:08 -070059 void DisableStatistics(DestroySenders destroy_senders) {
Austin Schuh58646e22021-08-23 23:51:46 -070060 disable_statistics_ = true;
James Kuszmaul94ca5132022-07-19 09:11:08 -070061 destroy_senders_ = destroy_senders;
Austin Schuhac6d89e2024-03-27 14:56:09 -070062 if (server_status_) {
63 server_status_->DisableStatistics(destroy_senders ==
64 DestroySenders::kYes);
Austin Schuh58646e22021-08-23 23:51:46 -070065 }
66 if (client_status) {
James Kuszmaul94ca5132022-07-19 09:11:08 -070067 client_status->DisableStatistics(destroy_senders ==
68 DestroySenders::kYes);
Austin Schuh58646e22021-08-23 23:51:46 -070069 }
70 }
71
Austin Schuh48205e62021-11-12 14:13:18 -080072 void EnableStatistics() {
73 disable_statistics_ = false;
Austin Schuhac6d89e2024-03-27 14:56:09 -070074 if (server_status_) {
75 server_status_->EnableStatistics();
Austin Schuh48205e62021-11-12 14:13:18 -080076 }
77 if (client_status) {
78 client_status->EnableStatistics();
79 }
80 }
81
Austin Schuh58646e22021-08-23 23:51:46 -070082 void AddSourceDelayer(RawMessageDelayer *delayer) {
83 source_delayers_.emplace_back(delayer);
84 }
85 void AddDestinationDelayer(RawMessageDelayer *delayer) {
86 destination_delayers_.emplace_back(delayer);
87 }
88
Austin Schuhac6d89e2024-03-27 14:56:09 -070089 void MakeEventLoop();
Austin Schuh58646e22021-08-23 23:51:46 -070090
Austin Schuh58646e22021-08-23 23:51:46 -070091 void SetEventLoop(std::unique_ptr<aos::EventLoop> loop);
92
Austin Schuhac6d89e2024-03-27 14:56:09 -070093 void SetSendData(
94 std::function<void(uint32_t, monotonic_clock::time_point)> fn);
Austin Schuh58646e22021-08-23 23:51:46 -070095
96 void AddDelayerWatcher(const Channel *channel, DelayersVector *v) {
97 delayer_watchers_.emplace_back(channel, v);
98 }
99
Austin Schuhac6d89e2024-03-27 14:56:09 -0700100 void SetBootUUID(size_t node_index, const UUID &boot_uuid);
Austin Schuh58646e22021-08-23 23:51:46 -0700101
Austin Schuhac6d89e2024-03-27 14:56:09 -0700102 void SetServerState(const Node *destination, message_bridge::State state);
Austin Schuh58646e22021-08-23 23:51:46 -0700103
Austin Schuhac6d89e2024-03-27 14:56:09 -0700104 void SetClientState(const Node *source, message_bridge::State state);
Austin Schuh58646e22021-08-23 23:51:46 -0700105
106 std::vector<UUID> boot_uuids_;
107 std::vector<message_bridge::State> client_state_;
108 std::vector<message_bridge::State> server_state_;
109
110 std::vector<std::pair<const Channel *, DelayersVector *>> delayer_watchers_;
111
Austin Schuhac6d89e2024-03-27 14:56:09 -0700112 std::function<void(uint32_t, monotonic_clock::time_point)> fn_;
Austin Schuh58646e22021-08-23 23:51:46 -0700113
114 NodeEventLoopFactory *node_factory_;
Austin Schuh4c3b9702020-08-30 11:34:55 -0700115 std::unique_ptr<aos::EventLoop> event_loop;
Austin Schuh89c9b812021-02-20 14:42:10 -0800116 ChannelTimestampSender timestamp_loggers;
Austin Schuhac6d89e2024-03-27 14:56:09 -0700117 std::unique_ptr<MessageBridgeServerStatus> server_status_;
Austin Schuh58646e22021-08-23 23:51:46 -0700118 std::unique_ptr<MessageBridgeClientStatus> client_status;
119
120 // List of delayers to update whenever this node starts or stops.
121 // Source delayers (which are the ones fetching).
122 std::vector<RawMessageDelayer *> source_delayers_;
123 // Destination delayers (which are the ones sending on the receiving nodes).
124 std::vector<RawMessageDelayer *> destination_delayers_;
125
126 bool disable_statistics_ = false;
James Kuszmaul94ca5132022-07-19 09:11:08 -0700127 DestroySenders destroy_senders_ = DestroySenders::kNo;
Austin Schuh4c3b9702020-08-30 11:34:55 -0700128 };
James Kuszmaul94ca5132022-07-19 09:11:08 -0700129
Austin Schuh898f4972020-01-11 17:21:25 -0800130 // Map of nodes to event loops. This is a member variable so that the
131 // lifetime of the event loops matches the lifetime of the bridge.
Austin Schuh4c3b9702020-08-30 11:34:55 -0700132 std::map<const Node *, State> event_loop_map_;
Austin Schuh898f4972020-01-11 17:21:25 -0800133
Austin Schuh898f4972020-01-11 17:21:25 -0800134 // List of delayers used to resend the messages.
Austin Schuh898f4972020-01-11 17:21:25 -0800135 std::vector<std::unique_ptr<DelayersVector>> delayers_list_;
136};
137
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800138} // namespace aos::message_bridge
Austin Schuh898f4972020-01-11 17:21:25 -0800139
140#endif // AOS_EVENTS_SIMULATED_NETWORK_BRIDGE_H_