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