blob: ab61051a9259ece04c575a371ab4b3bc541ac958 [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
5namespace aos {
6namespace message_bridge {
7
Austin Schuh61e973f2021-02-21 21:43:56 -08008ChannelTimestampFinder::ChannelTimestampFinder(
9 const Configuration *configuration, const std::string_view name,
10 const Node *node)
11 : configuration_(configuration), name_(name), node_(node) {}
12
13std::string ChannelTimestampFinder::SplitChannelName(
14 const Channel *channel, const Connection *connection) {
Austin Schuh006a9f52021-04-07 16:24:18 -070015 return SplitChannelName(channel->name()->string_view(), channel->type()->str(), connection);
16}
17
18std::string ChannelTimestampFinder::SplitChannelName(
19 std::string_view name, std::string type, const Connection *connection) {
Austin Schuh61e973f2021-02-21 21:43:56 -080020 std::replace(type.begin(), type.end(), '.', '-');
21 return absl::StrCat("/aos/remote_timestamps/",
Austin Schuh006a9f52021-04-07 16:24:18 -070022 connection->name()->string_view(), name, "/", type);
Austin Schuh61e973f2021-02-21 21:43:56 -080023}
24
25std::string ChannelTimestampFinder::CombinedChannelName(
26 std::string_view remote_node) {
27 return absl::StrCat("/aos/remote_timestamps/", remote_node);
28}
29
Austin Schuh01f3b392022-01-25 20:03:09 -080030const 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 Schuh61e973f2021-02-21 21:43:56 -080039const 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 Kuszmaul9c128122021-03-22 22:24:36 -070057 << split_timestamp_channel_name << "\", \"type\": \""
Austin Schuh61e973f2021-02-21 21:43:56 -080058 << 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 Schuh36a2c3e2021-02-18 22:28:38 -080075ChannelTimestampSender::ChannelTimestampSender(aos::EventLoop *event_loop)
76 : event_loop_(event_loop) {
Austin Schuh58646e22021-08-23 23:51:46 -070077 if (event_loop_) {
78 CHECK(configuration::MultiNode(event_loop_->configuration()));
79 }
Austin Schuh36a2c3e2021-02-18 22:28:38 -080080}
81
82aos::Sender<RemoteMessage> *ChannelTimestampSender::SenderForChannel(
83 const Channel *channel, const Connection *connection) {
Austin Schuh58646e22021-08-23 23:51:46 -070084 CHECK(event_loop_);
85
Austin Schuh61e973f2021-02-21 21:43:56 -080086 ChannelTimestampFinder finder(event_loop_);
Austin Schuh36a2c3e2021-02-18 22:28:38 -080087 // Look at any pre-created channel/connection pairs.
Austin Schuh61e973f2021-02-21 21:43:56 -080088 {
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 Schuh36a2c3e2021-02-18 22:28:38 -080094 }
95
Austin Schuh61e973f2021-02-21 21:43:56 -080096 const Channel *timestamp_channel = finder.ForChannel(channel, connection);
Austin Schuh36a2c3e2021-02-18 22:28:38 -080097
Austin Schuh61e973f2021-02-21 21:43:56 -080098 {
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 Schuh36a2c3e2021-02-18 22:28:38 -0800105 }
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800106 }
Austin Schuh61e973f2021-02-21 21:43:56 -0800107
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 Schuh36a2c3e2021-02-18 22:28:38 -0800117}
118
119} // namespace message_bridge
120} // namespace aos