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 | |
Austin Schuh | 349e7ad | 2022-04-02 21:12:26 -0700 | [diff] [blame] | 5 | DEFINE_bool(combined_timestamp_channel_fallback, true, |
| 6 | "If true, fall back to using the combined timestamp channel if the " |
| 7 | "single timestamp channel doesn't exist for a timestamp."); |
| 8 | |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 9 | namespace aos { |
| 10 | namespace message_bridge { |
| 11 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 12 | ChannelTimestampFinder::ChannelTimestampFinder( |
| 13 | const Configuration *configuration, const std::string_view name, |
| 14 | const Node *node) |
| 15 | : configuration_(configuration), name_(name), node_(node) {} |
| 16 | |
| 17 | std::string ChannelTimestampFinder::SplitChannelName( |
| 18 | const Channel *channel, const Connection *connection) { |
Austin Schuh | 349e7ad | 2022-04-02 21:12:26 -0700 | [diff] [blame] | 19 | return SplitChannelName(channel->name()->string_view(), |
| 20 | channel->type()->str(), connection); |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | std::string ChannelTimestampFinder::SplitChannelName( |
| 24 | std::string_view name, std::string type, const Connection *connection) { |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 25 | std::replace(type.begin(), type.end(), '.', '-'); |
| 26 | return absl::StrCat("/aos/remote_timestamps/", |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 27 | connection->name()->string_view(), name, "/", type); |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | std::string ChannelTimestampFinder::CombinedChannelName( |
| 31 | std::string_view remote_node) { |
| 32 | return absl::StrCat("/aos/remote_timestamps/", remote_node); |
| 33 | } |
| 34 | |
Austin Schuh | 01f3b39 | 2022-01-25 20:03:09 -0800 | [diff] [blame] | 35 | const Channel *ChannelTimestampFinder::SplitChannelForChannel( |
| 36 | const Channel *channel, const Connection *connection) { |
| 37 | const std::string split_timestamp_channel_name = |
| 38 | SplitChannelName(channel, connection); |
| 39 | return configuration::GetChannel(configuration_, split_timestamp_channel_name, |
| 40 | RemoteMessage::GetFullyQualifiedName(), |
| 41 | name_, node_, true); |
| 42 | } |
| 43 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 44 | const Channel *ChannelTimestampFinder::ForChannel( |
| 45 | const Channel *channel, const Connection *connection) { |
| 46 | const std::string split_timestamp_channel_name = |
| 47 | SplitChannelName(channel, connection); |
| 48 | const Channel *split_timestamp_channel = configuration::GetChannel( |
| 49 | configuration_, split_timestamp_channel_name, |
| 50 | RemoteMessage::GetFullyQualifiedName(), name_, node_, true); |
| 51 | if (split_timestamp_channel != nullptr) { |
| 52 | return split_timestamp_channel; |
| 53 | } |
| 54 | |
Austin Schuh | 349e7ad | 2022-04-02 21:12:26 -0700 | [diff] [blame] | 55 | if (!FLAGS_combined_timestamp_channel_fallback) { |
| 56 | LOG(FATAL) << "Failed to find new timestamp channel {\"name\": \"" |
| 57 | << split_timestamp_channel_name << "\", \"type\": \"" |
| 58 | << RemoteMessage::GetFullyQualifiedName() << "\"} for " |
| 59 | << configuration::CleanedChannelToString(channel) |
| 60 | << " connection " << aos::FlatbufferToJson(connection) |
| 61 | << " and --nocombined_timestamp_channel_fallback is set"; |
| 62 | } |
| 63 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 64 | const std::string shared_timestamp_channel_name = |
| 65 | CombinedChannelName(connection->name()->string_view()); |
| 66 | const Channel *shared_timestamp_channel = configuration::GetChannel( |
| 67 | configuration_, shared_timestamp_channel_name, |
| 68 | RemoteMessage::GetFullyQualifiedName(), name_, node_, true); |
| 69 | if (shared_timestamp_channel != nullptr) { |
| 70 | LOG(WARNING) << "Failed to find timestamp channel {\"name\": \"" |
James Kuszmaul | 9c12812 | 2021-03-22 22:24:36 -0700 | [diff] [blame] | 71 | << split_timestamp_channel_name << "\", \"type\": \"" |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 72 | << RemoteMessage::GetFullyQualifiedName() |
| 73 | << "\"}, falling back to old version."; |
| 74 | return shared_timestamp_channel; |
| 75 | } |
| 76 | |
| 77 | CHECK(shared_timestamp_channel != nullptr) |
| 78 | << ": Remote timestamp channel { \"name\": \"" |
| 79 | << split_timestamp_channel_name << "\", \"type\": \"" |
| 80 | << RemoteMessage::GetFullyQualifiedName() |
| 81 | << "\" } not found in config for " << name_ |
| 82 | << (configuration::MultiNode(configuration_) |
| 83 | ? absl::StrCat(" on node ", node_->name()->string_view()) |
| 84 | : "."); |
| 85 | |
| 86 | return nullptr; |
| 87 | } |
| 88 | |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 89 | ChannelTimestampSender::ChannelTimestampSender(aos::EventLoop *event_loop) |
| 90 | : event_loop_(event_loop) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 91 | if (event_loop_) { |
| 92 | CHECK(configuration::MultiNode(event_loop_->configuration())); |
| 93 | } |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | aos::Sender<RemoteMessage> *ChannelTimestampSender::SenderForChannel( |
| 97 | const Channel *channel, const Connection *connection) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 98 | CHECK(event_loop_); |
| 99 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 100 | ChannelTimestampFinder finder(event_loop_); |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 101 | // Look at any pre-created channel/connection pairs. |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 102 | { |
| 103 | auto it = |
| 104 | channel_timestamp_loggers_.find(std::make_pair(channel, connection)); |
| 105 | if (it != channel_timestamp_loggers_.end()) { |
| 106 | return it->second.get(); |
| 107 | } |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 110 | const Channel *timestamp_channel = finder.ForChannel(channel, connection); |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 111 | |
James Kuszmaul | 839c8aa | 2023-01-10 15:27:57 -0800 | [diff] [blame] | 112 | // Sanity-check that the timestamp channel can actually support full-rate |
| 113 | // messages coming through on the source channel. |
| 114 | CHECK_GE(timestamp_channel->frequency(), channel->frequency()) |
| 115 | << ": Timestamp channel " |
| 116 | << configuration::StrippedChannelToString(timestamp_channel) |
| 117 | << "'s rate is lower than the source channel."; |
| 118 | |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 119 | { |
| 120 | auto it = timestamp_loggers_.find(timestamp_channel); |
| 121 | if (it != timestamp_loggers_.end()) { |
| 122 | CHECK(channel_timestamp_loggers_ |
| 123 | .try_emplace(std::make_pair(channel, connection), it->second) |
| 124 | .second); |
| 125 | return it->second.get(); |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 126 | } |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 127 | } |
Austin Schuh | 61e973f | 2021-02-21 21:43:56 -0800 | [diff] [blame] | 128 | |
| 129 | auto result = channel_timestamp_loggers_.try_emplace( |
| 130 | std::make_pair(channel, connection), |
| 131 | std::make_shared<aos::Sender<RemoteMessage>>( |
| 132 | event_loop_->MakeSender<RemoteMessage>( |
| 133 | timestamp_channel->name()->string_view()))); |
| 134 | |
| 135 | CHECK(timestamp_loggers_.try_emplace(timestamp_channel, result.first->second) |
| 136 | .second); |
| 137 | return result.first->second.get(); |
Austin Schuh | 36a2c3e | 2021-02-18 22:28:38 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | } // namespace message_bridge |
| 141 | } // namespace aos |