blob: f8f525fddaa9ed7358ab22bbf652a333d4e72a63 [file] [log] [blame]
Austin Schuh36a2c3e2021-02-18 22:28:38 -08001#include "aos/network/timestamp_channel.h"
2
3#include "absl/strings/str_cat.h"
4
Austin Schuh349e7ad2022-04-02 21:12:26 -07005DEFINE_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 Schuh36a2c3e2021-02-18 22:28:38 -08009namespace aos {
10namespace message_bridge {
11
Austin Schuh61e973f2021-02-21 21:43:56 -080012ChannelTimestampFinder::ChannelTimestampFinder(
13 const Configuration *configuration, const std::string_view name,
14 const Node *node)
15 : configuration_(configuration), name_(name), node_(node) {}
16
17std::string ChannelTimestampFinder::SplitChannelName(
18 const Channel *channel, const Connection *connection) {
Austin Schuh349e7ad2022-04-02 21:12:26 -070019 return SplitChannelName(channel->name()->string_view(),
20 channel->type()->str(), connection);
Austin Schuh006a9f52021-04-07 16:24:18 -070021}
22
23std::string ChannelTimestampFinder::SplitChannelName(
24 std::string_view name, std::string type, const Connection *connection) {
Austin Schuh61e973f2021-02-21 21:43:56 -080025 std::replace(type.begin(), type.end(), '.', '-');
26 return absl::StrCat("/aos/remote_timestamps/",
Austin Schuh006a9f52021-04-07 16:24:18 -070027 connection->name()->string_view(), name, "/", type);
Austin Schuh61e973f2021-02-21 21:43:56 -080028}
29
30std::string ChannelTimestampFinder::CombinedChannelName(
31 std::string_view remote_node) {
32 return absl::StrCat("/aos/remote_timestamps/", remote_node);
33}
34
Austin Schuh01f3b392022-01-25 20:03:09 -080035const 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 Schuh61e973f2021-02-21 21:43:56 -080044const 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 Schuh349e7ad2022-04-02 21:12:26 -070055 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 Schuh61e973f2021-02-21 21:43:56 -080064 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 Kuszmaul9c128122021-03-22 22:24:36 -070071 << split_timestamp_channel_name << "\", \"type\": \""
Austin Schuh61e973f2021-02-21 21:43:56 -080072 << 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 Schuh36a2c3e2021-02-18 22:28:38 -080089ChannelTimestampSender::ChannelTimestampSender(aos::EventLoop *event_loop)
90 : event_loop_(event_loop) {
Austin Schuh58646e22021-08-23 23:51:46 -070091 if (event_loop_) {
92 CHECK(configuration::MultiNode(event_loop_->configuration()));
93 }
Austin Schuh36a2c3e2021-02-18 22:28:38 -080094}
95
96aos::Sender<RemoteMessage> *ChannelTimestampSender::SenderForChannel(
97 const Channel *channel, const Connection *connection) {
Austin Schuh58646e22021-08-23 23:51:46 -070098 CHECK(event_loop_);
99
Austin Schuh61e973f2021-02-21 21:43:56 -0800100 ChannelTimestampFinder finder(event_loop_);
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800101 // Look at any pre-created channel/connection pairs.
Austin Schuh61e973f2021-02-21 21:43:56 -0800102 {
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 Schuh36a2c3e2021-02-18 22:28:38 -0800108 }
109
Austin Schuh61e973f2021-02-21 21:43:56 -0800110 const Channel *timestamp_channel = finder.ForChannel(channel, connection);
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800111
James Kuszmaul839c8aa2023-01-10 15:27:57 -0800112 // 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 Schuh61e973f2021-02-21 21:43:56 -0800119 {
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 Schuh36a2c3e2021-02-18 22:28:38 -0800126 }
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800127 }
Austin Schuh61e973f2021-02-21 21:43:56 -0800128
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 Schuh36a2c3e2021-02-18 22:28:38 -0800138}
139
140} // namespace message_bridge
141} // namespace aos