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" |
| 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 | |
| 13 | namespace aos { |
| 14 | namespace message_bridge { |
| 15 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 16 | // Class to find the corresponding channel where timestamps for a specified data |
| 17 | // channel and connection will be logged. |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 18 | // |
| 19 | // This abstracts (and detects) when we have combined or split remote timestamp |
| 20 | // logging channels. |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 21 | class ChannelTimestampFinder { |
| 22 | public: |
| 23 | ChannelTimestampFinder(aos::EventLoop *event_loop) |
| 24 | : ChannelTimestampFinder(event_loop->configuration(), event_loop->name(), |
| 25 | event_loop->node()) {} |
| 26 | ChannelTimestampFinder(const Configuration *configuration, |
| 27 | const std::string_view name, const Node *node); |
| 28 | |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 29 | // Returns the split timestamp logging channel for the provide channel and |
| 30 | // connection if one exists, or nullptr otherwise. |
| 31 | const Channel *SplitChannelForChannel(const Channel *channel, |
| 32 | const Connection *connection); |
| 33 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 34 | // Finds the timestamp logging channel for the provided data channel and |
| 35 | // connection. |
| 36 | const Channel *ForChannel(const Channel *channel, |
| 37 | const Connection *connection); |
| 38 | std::string SplitChannelName(const Channel *channel, |
| 39 | const Connection *connection); |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 40 | std::string SplitChannelName(std::string_view name, std::string type, |
| 41 | const Connection *connection); |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 42 | std::string CombinedChannelName(std::string_view remote_node); |
| 43 | |
| 44 | private: |
| 45 | const Configuration *configuration_; |
| 46 | const std::string_view name_; |
| 47 | const Node *node_; |
| 48 | }; |
| 49 | |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 50 | // Class to manage lifetime, and creating senders for channels and connections. |
| 51 | class ChannelTimestampSender { |
| 52 | public: |
| 53 | ChannelTimestampSender(aos::EventLoop *event_loop); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 54 | ChannelTimestampSender() : event_loop_(nullptr) {} |
| 55 | |
| 56 | ChannelTimestampSender(ChannelTimestampSender &&other) noexcept = default; |
| 57 | ChannelTimestampSender &operator=(ChannelTimestampSender &&other) noexcept = |
| 58 | default; |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 59 | |
| 60 | aos::Sender<RemoteMessage> *SenderForChannel(const Channel *channel, |
| 61 | const Connection *connection); |
| 62 | |
| 63 | private: |
| 64 | aos::EventLoop *event_loop_; |
| 65 | |
| 66 | // We've got 3 cases to consider. |
| 67 | // 1) The old single channel per connection exists. |
| 68 | // 2) A new channel per channel exists. |
| 69 | // 3) Both exist. |
| 70 | // |
| 71 | // I want the default to be such that if no channel is found, we explode |
| 72 | // looking for the single channel per channel. This means users will add the |
| 73 | // new channel when blindly fixing errors, which is what we want. |
| 74 | // |
| 75 | // I'd prefer 3) to be an error, but don't have strong opinions. We will |
| 76 | // still be correct if it gets used, as long as everything is consistent. |
| 77 | |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 78 | // Mapping from channel and connection to logger. |
| 79 | absl::btree_map<std::pair<const Channel *, const Connection *>, |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 80 | std::shared_ptr<aos::Sender<RemoteMessage>>> |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 81 | channel_timestamp_loggers_; |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 82 | |
| 83 | // Mapping from channel to RemoteMessage sender. This is the channel that |
| 84 | // timestamps are published to. |
| 85 | absl::btree_map<const Channel *, std::shared_ptr<aos::Sender<RemoteMessage>>> |
| 86 | timestamp_loggers_; |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // namespace message_bridge |
| 90 | } // namespace aos |
| 91 | |
| 92 | #endif // AOS_NETWORK_TIMESTAMP_CHANNEL_ |