Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 1 | #ifndef AOS_NETWORK_TIMESTAMP_CHANNEL_ |
| 2 | #define AOS_NETWORK_TIMESTAMP_CHANNEL_ |
| 3 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 4 | #include <string_view> |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 5 | #include <vector> |
| 6 | |
| 7 | #include "absl/container/btree_map.h" |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 8 | #include "absl/log/check.h" |
| 9 | #include "absl/log/log.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 10 | |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 11 | #include "aos/configuration.h" |
| 12 | #include "aos/events/event_loop.h" |
| 13 | #include "aos/network/remote_message_generated.h" |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 14 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 15 | namespace aos::message_bridge { |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 16 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 17 | // Class to find the corresponding channel where timestamps for a specified data |
| 18 | // channel and connection will be logged. |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 19 | // |
| 20 | // This abstracts (and detects) when we have combined or split remote timestamp |
| 21 | // logging channels. |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 22 | class 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 Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 30 | // 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 Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 35 | // 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 Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 41 | std::string SplitChannelName(std::string_view name, std::string type, |
| 42 | const Connection *connection); |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 43 | 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 Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 51 | // Class to manage lifetime, and creating senders for channels and connections. |
| 52 | class ChannelTimestampSender { |
| 53 | public: |
| 54 | ChannelTimestampSender(aos::EventLoop *event_loop); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 55 | ChannelTimestampSender() : event_loop_(nullptr) {} |
| 56 | |
| 57 | ChannelTimestampSender(ChannelTimestampSender &&other) noexcept = default; |
| 58 | ChannelTimestampSender &operator=(ChannelTimestampSender &&other) noexcept = |
| 59 | default; |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 60 | |
| 61 | aos::Sender<RemoteMessage> *SenderForChannel(const Channel *channel, |
| 62 | const Connection *connection); |
James Kuszmaul | 94ca513 | 2022-07-19 09:11:08 -0700 | [diff] [blame] | 63 | void ClearSenderForChannel(const Channel *channel, |
| 64 | const Connection *connection); |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 65 | |
| 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 Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 81 | // Mapping from channel and connection to logger. |
| 82 | absl::btree_map<std::pair<const Channel *, const Connection *>, |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 83 | std::shared_ptr<aos::Sender<RemoteMessage>>> |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 84 | channel_timestamp_loggers_; |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 85 | |
| 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 Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 90 | }; |
| 91 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 92 | } // namespace aos::message_bridge |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 93 | |
| 94 | #endif // AOS_NETWORK_TIMESTAMP_CHANNEL_ |