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 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 11 | namespace aos::message_bridge { |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 12 | |
| 13 | class 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. |
| 18 | class SimulatedMessageBridge { |
| 19 | public: |
| 20 | // Constructs the bridge. |
| 21 | SimulatedMessageBridge( |
| 22 | SimulatedEventLoopFactory *simulated_event_loop_factory); |
| 23 | ~SimulatedMessageBridge(); |
| 24 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 25 | // 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 Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 29 | 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 Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 34 | // Disables generating and sending the messages which message_gateway sends. |
| 35 | // The messages are the ClientStatistics, ServerStatistics and Timestamp |
| 36 | // messages. |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 37 | 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 Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 41 | void EnableStatistics(); |
| 42 | void EnableStatistics(const Node *node); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 43 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 44 | private: |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 45 | struct DelayersVector { |
| 46 | std::vector<std::unique_ptr<RawMessageDelayer>> v; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 47 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 48 | 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 Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 57 | State(const State &state) = delete; |
| 58 | |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 59 | void DisableStatistics(DestroySenders destroy_senders) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 60 | disable_statistics_ = true; |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 61 | destroy_senders_ = destroy_senders; |
Austin Schuh | ac6d89e | 2024-03-27 14:56:09 -0700 | [diff] [blame] | 62 | if (server_status_) { |
| 63 | server_status_->DisableStatistics(destroy_senders == |
| 64 | DestroySenders::kYes); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 65 | } |
| 66 | if (client_status) { |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 67 | client_status->DisableStatistics(destroy_senders == |
| 68 | DestroySenders::kYes); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 72 | void EnableStatistics() { |
| 73 | disable_statistics_ = false; |
Austin Schuh | ac6d89e | 2024-03-27 14:56:09 -0700 | [diff] [blame] | 74 | if (server_status_) { |
| 75 | server_status_->EnableStatistics(); |
Austin Schuh | 48205e6 | 2021-11-12 14:13:18 -0800 | [diff] [blame] | 76 | } |
| 77 | if (client_status) { |
| 78 | client_status->EnableStatistics(); |
| 79 | } |
| 80 | } |
| 81 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 82 | 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 Schuh | ac6d89e | 2024-03-27 14:56:09 -0700 | [diff] [blame] | 89 | void MakeEventLoop(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 90 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 91 | void SetEventLoop(std::unique_ptr<aos::EventLoop> loop); |
| 92 | |
Austin Schuh | ac6d89e | 2024-03-27 14:56:09 -0700 | [diff] [blame] | 93 | void SetSendData( |
| 94 | std::function<void(uint32_t, monotonic_clock::time_point)> fn); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 95 | |
| 96 | void AddDelayerWatcher(const Channel *channel, DelayersVector *v) { |
| 97 | delayer_watchers_.emplace_back(channel, v); |
| 98 | } |
| 99 | |
Austin Schuh | ac6d89e | 2024-03-27 14:56:09 -0700 | [diff] [blame] | 100 | void SetBootUUID(size_t node_index, const UUID &boot_uuid); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 101 | |
Austin Schuh | ac6d89e | 2024-03-27 14:56:09 -0700 | [diff] [blame] | 102 | void SetServerState(const Node *destination, message_bridge::State state); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 103 | |
Austin Schuh | ac6d89e | 2024-03-27 14:56:09 -0700 | [diff] [blame] | 104 | void SetClientState(const Node *source, message_bridge::State state); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 105 | |
| 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 Schuh | ac6d89e | 2024-03-27 14:56:09 -0700 | [diff] [blame] | 112 | std::function<void(uint32_t, monotonic_clock::time_point)> fn_; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 113 | |
| 114 | NodeEventLoopFactory *node_factory_; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 115 | std::unique_ptr<aos::EventLoop> event_loop; |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame] | 116 | ChannelTimestampSender timestamp_loggers; |
Austin Schuh | ac6d89e | 2024-03-27 14:56:09 -0700 | [diff] [blame] | 117 | std::unique_ptr<MessageBridgeServerStatus> server_status_; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 118 | 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 Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 127 | DestroySenders destroy_senders_ = DestroySenders::kNo; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 128 | }; |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 129 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 130 | // 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 Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 132 | std::map<const Node *, State> event_loop_map_; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 133 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 134 | // List of delayers used to resend the messages. |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 135 | std::vector<std::unique_ptr<DelayersVector>> delayers_list_; |
| 136 | }; |
| 137 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 138 | } // namespace aos::message_bridge |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 139 | |
| 140 | #endif // AOS_EVENTS_SIMULATED_NETWORK_BRIDGE_H_ |