blob: 114546199f70577fb559e1920d588b2182e1cac5 [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 Schuh898f4972020-01-11 17:21:25 -08009
10namespace aos {
11namespace message_bridge {
12
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.
37 void DisableStatistics();
38
Austin Schuh2928ebe2021-02-07 22:10:27 -080039 // Calls SkipTimingReport() on all EventLoops used in the implementation. This
40 // may improve the performance of long-simulated-duration tests.
41 void SkipTimingReport();
42
Austin Schuh898f4972020-01-11 17:21:25 -080043 private:
Austin Schuh4c3b9702020-08-30 11:34:55 -070044 struct State {
Austin Schuh2f8fd752020-09-01 22:38:28 -070045 State(std::unique_ptr<aos::EventLoop> &&new_event_loop);
Austin Schuh4c3b9702020-08-30 11:34:55 -070046
47 State(const State &state) = delete;
48
49 std::unique_ptr<aos::EventLoop> event_loop;
50 MessageBridgeServerStatus server_status;
51 MessageBridgeClientStatus client_status;
Austin Schuh2f8fd752020-09-01 22:38:28 -070052
Austin Schuh0de30f32020-12-06 12:44:28 -080053 std::vector<aos::Sender<RemoteMessage>> timestamp_loggers;
Austin Schuh4c3b9702020-08-30 11:34:55 -070054 };
Austin Schuh898f4972020-01-11 17:21:25 -080055 // 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 Schuh4c3b9702020-08-30 11:34:55 -070057 std::map<const Node *, State> event_loop_map_;
Austin Schuh898f4972020-01-11 17:21:25 -080058
Austin Schuh898f4972020-01-11 17:21:25 -080059 // 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_