blob: b78c83a6b90cfd837dd8cc49b13d73dd4c5f8b67 [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);
Austin Schuh006a9f52021-04-07 16:24:18 -070032 std::string SplitChannelName(std::string_view name, std::string type,
33 const Connection *connection);
Austin Schuh61e973f2021-02-21 21:43:56 -080034 std::string CombinedChannelName(std::string_view remote_node);
35
36 private:
37 const Configuration *configuration_;
38 const std::string_view name_;
39 const Node *node_;
40};
41
Austin Schuh36a2c3e2021-02-18 22:28:38 -080042// Class to manage lifetime, and creating senders for channels and connections.
43class ChannelTimestampSender {
44 public:
45 ChannelTimestampSender(aos::EventLoop *event_loop);
Austin Schuh58646e22021-08-23 23:51:46 -070046 ChannelTimestampSender() : event_loop_(nullptr) {}
47
48 ChannelTimestampSender(ChannelTimestampSender &&other) noexcept = default;
49 ChannelTimestampSender &operator=(ChannelTimestampSender &&other) noexcept =
50 default;
Austin Schuh36a2c3e2021-02-18 22:28:38 -080051
52 aos::Sender<RemoteMessage> *SenderForChannel(const Channel *channel,
53 const Connection *connection);
54
55 private:
56 aos::EventLoop *event_loop_;
57
58 // We've got 3 cases to consider.
59 // 1) The old single channel per connection exists.
60 // 2) A new channel per channel exists.
61 // 3) Both exist.
62 //
63 // I want the default to be such that if no channel is found, we explode
64 // looking for the single channel per channel. This means users will add the
65 // new channel when blindly fixing errors, which is what we want.
66 //
67 // I'd prefer 3) to be an error, but don't have strong opinions. We will
68 // still be correct if it gets used, as long as everything is consistent.
69
Austin Schuh36a2c3e2021-02-18 22:28:38 -080070 // Mapping from channel and connection to logger.
71 absl::btree_map<std::pair<const Channel *, const Connection *>,
Austin Schuh61e973f2021-02-21 21:43:56 -080072 std::shared_ptr<aos::Sender<RemoteMessage>>>
Austin Schuh36a2c3e2021-02-18 22:28:38 -080073 channel_timestamp_loggers_;
Austin Schuh61e973f2021-02-21 21:43:56 -080074
75 // Mapping from channel to RemoteMessage sender. This is the channel that
76 // timestamps are published to.
77 absl::btree_map<const Channel *, std::shared_ptr<aos::Sender<RemoteMessage>>>
78 timestamp_loggers_;
Austin Schuh36a2c3e2021-02-18 22:28:38 -080079};
80
81} // namespace message_bridge
82} // namespace aos
83
84#endif // AOS_NETWORK_TIMESTAMP_CHANNEL_