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 | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 9 | |
| 10 | namespace aos { |
| 11 | namespace message_bridge { |
| 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. |
| 37 | void DisableStatistics(); |
| 38 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 39 | private: |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 40 | struct State { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 41 | State(std::unique_ptr<aos::EventLoop> &&new_event_loop); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 42 | |
| 43 | State(const State &state) = delete; |
| 44 | |
| 45 | std::unique_ptr<aos::EventLoop> event_loop; |
| 46 | MessageBridgeServerStatus server_status; |
| 47 | MessageBridgeClientStatus client_status; |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 48 | |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 49 | std::vector<aos::Sender<RemoteMessage>> timestamp_loggers; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 50 | }; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 51 | // Map of nodes to event loops. This is a member variable so that the |
| 52 | // lifetime of the event loops matches the lifetime of the bridge. |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 53 | std::map<const Node *, State> event_loop_map_; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 54 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 55 | // List of delayers used to resend the messages. |
| 56 | using DelayersVector = std::vector<std::unique_ptr<RawMessageDelayer>>; |
| 57 | std::vector<std::unique_ptr<DelayersVector>> delayers_list_; |
| 58 | }; |
| 59 | |
| 60 | } // namespace message_bridge |
| 61 | } // namespace aos |
| 62 | |
| 63 | #endif // AOS_EVENTS_SIMULATED_NETWORK_BRIDGE_H_ |