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 | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 8 | |
| 9 | namespace aos { |
| 10 | namespace message_bridge { |
| 11 | |
| 12 | class RawMessageDelayer; |
| 13 | |
| 14 | // This class moves messages between nodes. It is implemented as a separate |
| 15 | // class because it would have been even harder to manage forwarding in the |
| 16 | // SimulatedEventLoopFactory. |
| 17 | class SimulatedMessageBridge { |
| 18 | public: |
| 19 | // Constructs the bridge. |
| 20 | SimulatedMessageBridge( |
| 21 | SimulatedEventLoopFactory *simulated_event_loop_factory); |
| 22 | ~SimulatedMessageBridge(); |
| 23 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 24 | // Disables forwarding for this channel. This should be used very rarely only |
| 25 | // for things like the logger. |
| 26 | void DisableForwarding(const Channel *channel); |
| 27 | |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame^] | 28 | // Disables generating and sending the messages which message_gateway sends. |
| 29 | // The messages are the ClientStatistics, ServerStatistics and Timestamp |
| 30 | // messages. |
| 31 | void DisableStatistics(); |
| 32 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 33 | private: |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame^] | 34 | struct State { |
| 35 | State(std::unique_ptr<aos::EventLoop> &&new_event_loop) |
| 36 | : event_loop(std::move(new_event_loop)), |
| 37 | server_status(event_loop.get()), |
| 38 | client_status(event_loop.get()) {} |
| 39 | |
| 40 | State(const State &state) = delete; |
| 41 | |
| 42 | std::unique_ptr<aos::EventLoop> event_loop; |
| 43 | MessageBridgeServerStatus server_status; |
| 44 | MessageBridgeClientStatus client_status; |
| 45 | }; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 46 | // Map of nodes to event loops. This is a member variable so that the |
| 47 | // lifetime of the event loops matches the lifetime of the bridge. |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame^] | 48 | std::map<const Node *, State> event_loop_map_; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 49 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 50 | // List of delayers used to resend the messages. |
| 51 | using DelayersVector = std::vector<std::unique_ptr<RawMessageDelayer>>; |
| 52 | std::vector<std::unique_ptr<DelayersVector>> delayers_list_; |
| 53 | }; |
| 54 | |
| 55 | } // namespace message_bridge |
| 56 | } // namespace aos |
| 57 | |
| 58 | #endif // AOS_EVENTS_SIMULATED_NETWORK_BRIDGE_H_ |