blob: 0d57c82f02a23a2856048fd76e6fac8600fceb78 [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"
Austin Schuh99f7c6a2024-06-25 22:07:44 -07008#include "absl/log/check.h"
9#include "absl/log/log.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070010
Austin Schuh36a2c3e2021-02-18 22:28:38 -080011#include "aos/configuration.h"
12#include "aos/events/event_loop.h"
13#include "aos/network/remote_message_generated.h"
Austin Schuh36a2c3e2021-02-18 22:28:38 -080014
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080015namespace aos::message_bridge {
Austin Schuh36a2c3e2021-02-18 22:28:38 -080016
Austin Schuh61e973f2021-02-21 21:43:56 -080017// Class to find the corresponding channel where timestamps for a specified data
18// channel and connection will be logged.
Austin Schuh01f3b392022-01-25 20:03:09 -080019//
20// This abstracts (and detects) when we have combined or split remote timestamp
21// logging channels.
Austin Schuh61e973f2021-02-21 21:43:56 -080022class ChannelTimestampFinder {
23 public:
24 ChannelTimestampFinder(aos::EventLoop *event_loop)
25 : ChannelTimestampFinder(event_loop->configuration(), event_loop->name(),
26 event_loop->node()) {}
27 ChannelTimestampFinder(const Configuration *configuration,
28 const std::string_view name, const Node *node);
29
Austin Schuh01f3b392022-01-25 20:03:09 -080030 // Returns the split timestamp logging channel for the provide channel and
31 // connection if one exists, or nullptr otherwise.
32 const Channel *SplitChannelForChannel(const Channel *channel,
33 const Connection *connection);
34
Austin Schuh61e973f2021-02-21 21:43:56 -080035 // Finds the timestamp logging channel for the provided data channel and
36 // connection.
37 const Channel *ForChannel(const Channel *channel,
38 const Connection *connection);
39 std::string SplitChannelName(const Channel *channel,
40 const Connection *connection);
Austin Schuh006a9f52021-04-07 16:24:18 -070041 std::string SplitChannelName(std::string_view name, std::string type,
42 const Connection *connection);
Austin Schuh61e973f2021-02-21 21:43:56 -080043 std::string CombinedChannelName(std::string_view remote_node);
44
45 private:
46 const Configuration *configuration_;
47 const std::string_view name_;
48 const Node *node_;
49};
50
Austin Schuh36a2c3e2021-02-18 22:28:38 -080051// Class to manage lifetime, and creating senders for channels and connections.
52class ChannelTimestampSender {
53 public:
54 ChannelTimestampSender(aos::EventLoop *event_loop);
Austin Schuh58646e22021-08-23 23:51:46 -070055 ChannelTimestampSender() : event_loop_(nullptr) {}
56
57 ChannelTimestampSender(ChannelTimestampSender &&other) noexcept = default;
58 ChannelTimestampSender &operator=(ChannelTimestampSender &&other) noexcept =
59 default;
Austin Schuh36a2c3e2021-02-18 22:28:38 -080060
61 aos::Sender<RemoteMessage> *SenderForChannel(const Channel *channel,
62 const Connection *connection);
James Kuszmaul94ca5132022-07-19 09:11:08 -070063 void ClearSenderForChannel(const Channel *channel,
64 const Connection *connection);
Austin Schuh36a2c3e2021-02-18 22:28:38 -080065
66 private:
67 aos::EventLoop *event_loop_;
68
69 // We've got 3 cases to consider.
70 // 1) The old single channel per connection exists.
71 // 2) A new channel per channel exists.
72 // 3) Both exist.
73 //
74 // I want the default to be such that if no channel is found, we explode
75 // looking for the single channel per channel. This means users will add the
76 // new channel when blindly fixing errors, which is what we want.
77 //
78 // I'd prefer 3) to be an error, but don't have strong opinions. We will
79 // still be correct if it gets used, as long as everything is consistent.
80
Austin Schuh36a2c3e2021-02-18 22:28:38 -080081 // Mapping from channel and connection to logger.
82 absl::btree_map<std::pair<const Channel *, const Connection *>,
Austin Schuh61e973f2021-02-21 21:43:56 -080083 std::shared_ptr<aos::Sender<RemoteMessage>>>
Austin Schuh36a2c3e2021-02-18 22:28:38 -080084 channel_timestamp_loggers_;
Austin Schuh61e973f2021-02-21 21:43:56 -080085
86 // Mapping from channel to RemoteMessage sender. This is the channel that
87 // timestamps are published to.
88 absl::btree_map<const Channel *, std::shared_ptr<aos::Sender<RemoteMessage>>>
89 timestamp_loggers_;
Austin Schuh36a2c3e2021-02-18 22:28:38 -080090};
91
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080092} // namespace aos::message_bridge
Austin Schuh36a2c3e2021-02-18 22:28:38 -080093
94#endif // AOS_NETWORK_TIMESTAMP_CHANNEL_