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. |
| 18 | class 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 Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 32 | std::string SplitChannelName(std::string_view name, std::string type, |
| 33 | const Connection *connection); |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 34 | 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 Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 42 | // Class to manage lifetime, and creating senders for channels and connections. |
| 43 | class ChannelTimestampSender { |
| 44 | public: |
| 45 | ChannelTimestampSender(aos::EventLoop *event_loop); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 46 | ChannelTimestampSender() : event_loop_(nullptr) {} |
| 47 | |
| 48 | ChannelTimestampSender(ChannelTimestampSender &&other) noexcept = default; |
| 49 | ChannelTimestampSender &operator=(ChannelTimestampSender &&other) noexcept = |
| 50 | default; |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 51 | |
| 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 Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 70 | // Mapping from channel and connection to logger. |
| 71 | absl::btree_map<std::pair<const Channel *, const Connection *>, |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 72 | std::shared_ptr<aos::Sender<RemoteMessage>>> |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 73 | channel_timestamp_loggers_; |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 74 | |
| 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 Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | } // namespace message_bridge |
| 82 | } // namespace aos |
| 83 | |
| 84 | #endif // AOS_NETWORK_TIMESTAMP_CHANNEL_ |