blob: 5130c65c058d6694398b4a852179cd51be03be14 [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 Schuh4c3b9702020-08-30 11:34:55 -070029 // Disables generating and sending the messages which message_gateway sends.
30 // The messages are the ClientStatistics, ServerStatistics and Timestamp
31 // messages.
32 void DisableStatistics();
33
Austin Schuh898f4972020-01-11 17:21:25 -080034 private:
Austin Schuh4c3b9702020-08-30 11:34:55 -070035 struct State {
Austin Schuh2f8fd752020-09-01 22:38:28 -070036 State(std::unique_ptr<aos::EventLoop> &&new_event_loop);
Austin Schuh4c3b9702020-08-30 11:34:55 -070037
38 State(const State &state) = delete;
39
40 std::unique_ptr<aos::EventLoop> event_loop;
41 MessageBridgeServerStatus server_status;
42 MessageBridgeClientStatus client_status;
Austin Schuh2f8fd752020-09-01 22:38:28 -070043
Austin Schuh0de30f32020-12-06 12:44:28 -080044 std::vector<aos::Sender<RemoteMessage>> timestamp_loggers;
Austin Schuh4c3b9702020-08-30 11:34:55 -070045 };
Austin Schuh898f4972020-01-11 17:21:25 -080046 // 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 Schuh4c3b9702020-08-30 11:34:55 -070048 std::map<const Node *, State> event_loop_map_;
Austin Schuh898f4972020-01-11 17:21:25 -080049
Austin Schuh898f4972020-01-11 17:21:25 -080050 // 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_