blob: 1702632b91029447373dc9fbad6e1304cb68fb40 [file] [log] [blame]
Austin Schuh36a2c3e2021-02-18 22:28:38 -08001#ifndef AOS_NETWORK_TIMESTAMP_CHANNEL_
2#define AOS_NETWORK_TIMESTAMP_CHANNEL_
3
Austin Schuh61e973f2021-02-21 21:43:56 -08004#include <string_view>
Austin Schuh36a2c3e2021-02-18 22:28:38 -08005#include <vector>
6
7#include "absl/container/btree_map.h"
8#include "aos/configuration.h"
9#include "aos/events/event_loop.h"
10#include "aos/network/remote_message_generated.h"
11#include "glog/logging.h"
12
13namespace aos {
14namespace message_bridge {
15
Austin Schuh61e973f2021-02-21 21:43:56 -080016// Class to find the corresponding channel where timestamps for a specified data
17// channel and connection will be logged.
18class ChannelTimestampFinder {
19 public:
20 ChannelTimestampFinder(aos::EventLoop *event_loop)
21 : ChannelTimestampFinder(event_loop->configuration(), event_loop->name(),
22 event_loop->node()) {}
23 ChannelTimestampFinder(const Configuration *configuration,
24 const std::string_view name, const Node *node);
25
26 // Finds the timestamp logging channel for the provided data channel and
27 // connection.
28 const Channel *ForChannel(const Channel *channel,
29 const Connection *connection);
30 std::string SplitChannelName(const Channel *channel,
31 const Connection *connection);
32 std::string CombinedChannelName(std::string_view remote_node);
33
34 private:
35 const Configuration *configuration_;
36 const std::string_view name_;
37 const Node *node_;
38};
39
Austin Schuh36a2c3e2021-02-18 22:28:38 -080040// Class to manage lifetime, and creating senders for channels and connections.
41class ChannelTimestampSender {
42 public:
43 ChannelTimestampSender(aos::EventLoop *event_loop);
44
45 aos::Sender<RemoteMessage> *SenderForChannel(const Channel *channel,
46 const Connection *connection);
47
48 private:
49 aos::EventLoop *event_loop_;
50
51 // We've got 3 cases to consider.
52 // 1) The old single channel per connection exists.
53 // 2) A new channel per channel exists.
54 // 3) Both exist.
55 //
56 // I want the default to be such that if no channel is found, we explode
57 // looking for the single channel per channel. This means users will add the
58 // new channel when blindly fixing errors, which is what we want.
59 //
60 // I'd prefer 3) to be an error, but don't have strong opinions. We will
61 // still be correct if it gets used, as long as everything is consistent.
62
Austin Schuh36a2c3e2021-02-18 22:28:38 -080063 // Mapping from channel and connection to logger.
64 absl::btree_map<std::pair<const Channel *, const Connection *>,
Austin Schuh61e973f2021-02-21 21:43:56 -080065 std::shared_ptr<aos::Sender<RemoteMessage>>>
Austin Schuh36a2c3e2021-02-18 22:28:38 -080066 channel_timestamp_loggers_;
Austin Schuh61e973f2021-02-21 21:43:56 -080067
68 // Mapping from channel to RemoteMessage sender. This is the channel that
69 // timestamps are published to.
70 absl::btree_map<const Channel *, std::shared_ptr<aos::Sender<RemoteMessage>>>
71 timestamp_loggers_;
Austin Schuh36a2c3e2021-02-18 22:28:38 -080072};
73
74} // namespace message_bridge
75} // namespace aos
76
77#endif // AOS_NETWORK_TIMESTAMP_CHANNEL_