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 | |
| 30 | const Channel *ChannelTimestampFinder::ForChannel( |
| 31 | const Channel *channel, const Connection *connection) { |
| 32 | const std::string split_timestamp_channel_name = |
| 33 | SplitChannelName(channel, connection); |
| 34 | const Channel *split_timestamp_channel = configuration::GetChannel( |
| 35 | configuration_, split_timestamp_channel_name, |
| 36 | RemoteMessage::GetFullyQualifiedName(), name_, node_, true); |
| 37 | if (split_timestamp_channel != nullptr) { |
| 38 | return split_timestamp_channel; |
| 39 | } |
| 40 | |
| 41 | const std::string shared_timestamp_channel_name = |
| 42 | CombinedChannelName(connection->name()->string_view()); |
| 43 | const Channel *shared_timestamp_channel = configuration::GetChannel( |
| 44 | configuration_, shared_timestamp_channel_name, |
| 45 | RemoteMessage::GetFullyQualifiedName(), name_, node_, true); |
| 46 | if (shared_timestamp_channel != nullptr) { |
| 47 | LOG(WARNING) << "Failed to find timestamp channel {\"name\": \"" |
James Kuszmaul | 9c12812 | 2021-03-22 22:24:36 -0700 | [diff] [blame] | 48 | << split_timestamp_channel_name << "\", \"type\": \"" |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 49 | << RemoteMessage::GetFullyQualifiedName() |
| 50 | << "\"}, falling back to old version."; |
| 51 | return shared_timestamp_channel; |
| 52 | } |
| 53 | |
| 54 | CHECK(shared_timestamp_channel != nullptr) |
| 55 | << ": Remote timestamp channel { \"name\": \"" |
| 56 | << split_timestamp_channel_name << "\", \"type\": \"" |
| 57 | << RemoteMessage::GetFullyQualifiedName() |
| 58 | << "\" } not found in config for " << name_ |
| 59 | << (configuration::MultiNode(configuration_) |
| 60 | ? absl::StrCat(" on node ", node_->name()->string_view()) |
| 61 | : "."); |
| 62 | |
| 63 | return nullptr; |
| 64 | } |
| 65 | |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 66 | ChannelTimestampSender::ChannelTimestampSender(aos::EventLoop *event_loop) |
| 67 | : event_loop_(event_loop) { |
| 68 | CHECK(configuration::MultiNode(event_loop_->configuration())); |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | aos::Sender<RemoteMessage> *ChannelTimestampSender::SenderForChannel( |
| 72 | const Channel *channel, const Connection *connection) { |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 73 | ChannelTimestampFinder finder(event_loop_); |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 74 | // Look at any pre-created channel/connection pairs. |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 75 | { |
| 76 | auto it = |
| 77 | channel_timestamp_loggers_.find(std::make_pair(channel, connection)); |
| 78 | if (it != channel_timestamp_loggers_.end()) { |
| 79 | return it->second.get(); |
| 80 | } |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 83 | const Channel *timestamp_channel = finder.ForChannel(channel, connection); |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 84 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 85 | { |
| 86 | auto it = timestamp_loggers_.find(timestamp_channel); |
| 87 | if (it != timestamp_loggers_.end()) { |
| 88 | CHECK(channel_timestamp_loggers_ |
| 89 | .try_emplace(std::make_pair(channel, connection), it->second) |
| 90 | .second); |
| 91 | return it->second.get(); |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 92 | } |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 93 | } |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 94 | |
| 95 | auto result = channel_timestamp_loggers_.try_emplace( |
| 96 | std::make_pair(channel, connection), |
| 97 | std::make_shared<aos::Sender<RemoteMessage>>( |
| 98 | event_loop_->MakeSender<RemoteMessage>( |
| 99 | timestamp_channel->name()->string_view()))); |
| 100 | |
| 101 | CHECK(timestamp_loggers_.try_emplace(timestamp_channel, result.first->second) |
| 102 | .second); |
| 103 | return result.first->second.get(); |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // namespace message_bridge |
| 107 | } // namespace aos |