blob: 978acf9f3a529e321f8b8192090402d509048420 [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 Schuh898f4972020-01-11 17:21:25 -080039 private:
Austin Schuh4c3b9702020-08-30 11:34:55 -070040 struct State {
Austin Schuh2f8fd752020-09-01 22:38:28 -070041 State(std::unique_ptr<aos::EventLoop> &&new_event_loop);
Austin Schuh4c3b9702020-08-30 11:34:55 -070042
43 State(const State &state) = delete;
44
45 std::unique_ptr<aos::EventLoop> event_loop;
46 MessageBridgeServerStatus server_status;
47 MessageBridgeClientStatus client_status;
Austin Schuh2f8fd752020-09-01 22:38:28 -070048
Austin Schuh0de30f32020-12-06 12:44:28 -080049 std::vector<aos::Sender<RemoteMessage>> timestamp_loggers;
Austin Schuh4c3b9702020-08-30 11:34:55 -070050 };
Austin Schuh898f4972020-01-11 17:21:25 -080051 // 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 Schuh4c3b9702020-08-30 11:34:55 -070053 std::map<const Node *, State> event_loop_map_;
Austin Schuh898f4972020-01-11 17:21:25 -080054
Austin Schuh898f4972020-01-11 17:21:25 -080055 // 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_