blob: 5784bb336484f705ed07d1ad02bd63cf470ab09f [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 Schuh898f4972020-01-11 17:21:25 -08008
9namespace aos {
10namespace message_bridge {
11
12class 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.
17class SimulatedMessageBridge {
18 public:
19 // Constructs the bridge.
20 SimulatedMessageBridge(
21 SimulatedEventLoopFactory *simulated_event_loop_factory);
22 ~SimulatedMessageBridge();
23
Austin Schuh6f3babe2020-01-26 20:34:50 -080024 // 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 Schuh4c3b9702020-08-30 11:34:55 -070028 // 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 Schuh898f4972020-01-11 17:21:25 -080033 private:
Austin Schuh4c3b9702020-08-30 11:34:55 -070034 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 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_