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 | |
| 11 | namespace aos { |
| 12 | namespace message_bridge { |
| 13 | |
| 14 | class 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. |
| 19 | class SimulatedMessageBridge { |
| 20 | public: |
| 21 | // Constructs the bridge. |
| 22 | SimulatedMessageBridge( |
| 23 | SimulatedEventLoopFactory *simulated_event_loop_factory); |
| 24 | ~SimulatedMessageBridge(); |
| 25 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 26 | // 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 Schuh | c0b0f72 | 2020-12-12 18:36:06 -0800 | [diff] [blame] | 30 | 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 Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 35 | // Disables generating and sending the messages which message_gateway sends. |
| 36 | // The messages are the ClientStatistics, ServerStatistics and Timestamp |
| 37 | // messages. |
| 38 | void DisableStatistics(); |
| 39 | |
Austin Schuh | 2928ebe | 2021-02-07 22:10:27 -0800 | [diff] [blame] | 40 | // Calls SkipTimingReport() on all EventLoops used in the implementation. This |
| 41 | // may improve the performance of long-simulated-duration tests. |
| 42 | void SkipTimingReport(); |
| 43 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 44 | private: |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 45 | struct State { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 46 | State(std::unique_ptr<aos::EventLoop> &&new_event_loop); |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 47 | |
| 48 | State(const State &state) = delete; |
| 49 | |
| 50 | std::unique_ptr<aos::EventLoop> event_loop; |
Austin Schuh | 89c9b81 | 2021-02-20 14:42:10 -0800 | [diff] [blame^] | 51 | ChannelTimestampSender timestamp_loggers; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 52 | MessageBridgeServerStatus server_status; |
| 53 | MessageBridgeClientStatus client_status; |
| 54 | }; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 55 | // Map of nodes to event loops. This is a member variable so that the |
| 56 | // lifetime of the event loops matches the lifetime of the bridge. |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 57 | std::map<const Node *, State> event_loop_map_; |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 58 | |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 59 | // List of delayers used to resend the messages. |
| 60 | using DelayersVector = std::vector<std::unique_ptr<RawMessageDelayer>>; |
| 61 | std::vector<std::unique_ptr<DelayersVector>> delayers_list_; |
| 62 | }; |
| 63 | |
| 64 | } // namespace message_bridge |
| 65 | } // namespace aos |
| 66 | |
| 67 | #endif // AOS_EVENTS_SIMULATED_NETWORK_BRIDGE_H_ |