Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 1 | #include "aos/network/timestamp_channel.h" |
| 2 | |
| 3 | #include "absl/strings/str_cat.h" |
| 4 | |
| 5 | namespace aos { |
| 6 | namespace message_bridge { |
| 7 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 8 | ChannelTimestampFinder::ChannelTimestampFinder( |
| 9 | const Configuration *configuration, const std::string_view name, |
| 10 | const Node *node) |
| 11 | : configuration_(configuration), name_(name), node_(node) {} |
| 12 | |
| 13 | std::string ChannelTimestampFinder::SplitChannelName( |
| 14 | const Channel *channel, const Connection *connection) { |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 15 | return SplitChannelName(channel->name()->string_view(), channel->type()->str(), connection); |
| 16 | } |
| 17 | |
| 18 | std::string ChannelTimestampFinder::SplitChannelName( |
| 19 | std::string_view name, std::string type, const Connection *connection) { |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 20 | std::replace(type.begin(), type.end(), '.', '-'); |
| 21 | return absl::StrCat("/aos/remote_timestamps/", |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 22 | connection->name()->string_view(), name, "/", type); |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | std::string ChannelTimestampFinder::CombinedChannelName( |
| 26 | std::string_view remote_node) { |
| 27 | return absl::StrCat("/aos/remote_timestamps/", remote_node); |
| 28 | } |
| 29 | |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame^] | 30 | const Channel *ChannelTimestampFinder::SplitChannelForChannel( |
| 31 | const Channel *channel, const Connection *connection) { |
| 32 | const std::string split_timestamp_channel_name = |
| 33 | SplitChannelName(channel, connection); |
| 34 | return configuration::GetChannel(configuration_, split_timestamp_channel_name, |
| 35 | RemoteMessage::GetFullyQualifiedName(), |
| 36 | name_, node_, true); |
| 37 | } |
| 38 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 39 | const Channel *ChannelTimestampFinder::ForChannel( |
| 40 | const Channel *channel, const Connection *connection) { |
| 41 | const std::string split_timestamp_channel_name = |
| 42 | SplitChannelName(channel, connection); |
| 43 | const Channel *split_timestamp_channel = configuration::GetChannel( |
| 44 | configuration_, split_timestamp_channel_name, |
| 45 | RemoteMessage::GetFullyQualifiedName(), name_, node_, true); |
| 46 | if (split_timestamp_channel != nullptr) { |
| 47 | return split_timestamp_channel; |
| 48 | } |
| 49 | |
| 50 | const std::string shared_timestamp_channel_name = |
| 51 | CombinedChannelName(connection->name()->string_view()); |
| 52 | const Channel *shared_timestamp_channel = configuration::GetChannel( |
| 53 | configuration_, shared_timestamp_channel_name, |
| 54 | RemoteMessage::GetFullyQualifiedName(), name_, node_, true); |
| 55 | if (shared_timestamp_channel != nullptr) { |
| 56 | LOG(WARNING) << "Failed to find timestamp channel {\"name\": \"" |
James Kuszmaul | 9c12812 | 2021-03-22 22:24:36 -0700 | [diff] [blame] | 57 | << split_timestamp_channel_name << "\", \"type\": \"" |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 58 | << RemoteMessage::GetFullyQualifiedName() |
| 59 | << "\"}, falling back to old version."; |
| 60 | return shared_timestamp_channel; |
| 61 | } |
| 62 | |
| 63 | CHECK(shared_timestamp_channel != nullptr) |
| 64 | << ": Remote timestamp channel { \"name\": \"" |
| 65 | << split_timestamp_channel_name << "\", \"type\": \"" |
| 66 | << RemoteMessage::GetFullyQualifiedName() |
| 67 | << "\" } not found in config for " << name_ |
| 68 | << (configuration::MultiNode(configuration_) |
| 69 | ? absl::StrCat(" on node ", node_->name()->string_view()) |
| 70 | : "."); |
| 71 | |
| 72 | return nullptr; |
| 73 | } |
| 74 | |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 75 | ChannelTimestampSender::ChannelTimestampSender(aos::EventLoop *event_loop) |
| 76 | : event_loop_(event_loop) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 77 | if (event_loop_) { |
| 78 | CHECK(configuration::MultiNode(event_loop_->configuration())); |
| 79 | } |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | aos::Sender<RemoteMessage> *ChannelTimestampSender::SenderForChannel( |
| 83 | const Channel *channel, const Connection *connection) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 84 | CHECK(event_loop_); |
| 85 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 86 | ChannelTimestampFinder finder(event_loop_); |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 87 | // Look at any pre-created channel/connection pairs. |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 88 | { |
| 89 | auto it = |
| 90 | channel_timestamp_loggers_.find(std::make_pair(channel, connection)); |
| 91 | if (it != channel_timestamp_loggers_.end()) { |
| 92 | return it->second.get(); |
| 93 | } |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 96 | const Channel *timestamp_channel = finder.ForChannel(channel, connection); |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 97 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 98 | { |
| 99 | auto it = timestamp_loggers_.find(timestamp_channel); |
| 100 | if (it != timestamp_loggers_.end()) { |
| 101 | CHECK(channel_timestamp_loggers_ |
| 102 | .try_emplace(std::make_pair(channel, connection), it->second) |
| 103 | .second); |
| 104 | return it->second.get(); |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 105 | } |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 106 | } |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 107 | |
| 108 | auto result = channel_timestamp_loggers_.try_emplace( |
| 109 | std::make_pair(channel, connection), |
| 110 | std::make_shared<aos::Sender<RemoteMessage>>( |
| 111 | event_loop_->MakeSender<RemoteMessage>( |
| 112 | timestamp_channel->name()->string_view()))); |
| 113 | |
| 114 | CHECK(timestamp_loggers_.try_emplace(timestamp_channel, result.first->second) |
| 115 | .second); |
| 116 | return result.first->second.get(); |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | } // namespace message_bridge |
| 120 | } // namespace aos |