blob: bbd62c49cd0e8625f32b381f24b825dfe0e6b381 [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
30const 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 Kuszmaul9c128122021-03-22 22:24:36 -070048 << split_timestamp_channel_name << "\", \"type\": \""
Austin Schuh61e973f2021-02-21 21:43:56 -080049 << 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 Schuh36a2c3e2021-02-18 22:28:38 -080066ChannelTimestampSender::ChannelTimestampSender(aos::EventLoop *event_loop)
67 : event_loop_(event_loop) {
68 CHECK(configuration::MultiNode(event_loop_->configuration()));
Austin Schuh36a2c3e2021-02-18 22:28:38 -080069}
70
71aos::Sender<RemoteMessage> *ChannelTimestampSender::SenderForChannel(
72 const Channel *channel, const Connection *connection) {
Austin Schuh61e973f2021-02-21 21:43:56 -080073 ChannelTimestampFinder finder(event_loop_);
Austin Schuh36a2c3e2021-02-18 22:28:38 -080074 // Look at any pre-created channel/connection pairs.
Austin Schuh61e973f2021-02-21 21:43:56 -080075 {
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 Schuh36a2c3e2021-02-18 22:28:38 -080081 }
82
Austin Schuh61e973f2021-02-21 21:43:56 -080083 const Channel *timestamp_channel = finder.ForChannel(channel, connection);
Austin Schuh36a2c3e2021-02-18 22:28:38 -080084
Austin Schuh61e973f2021-02-21 21:43:56 -080085 {
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 Schuh36a2c3e2021-02-18 22:28:38 -080092 }
Austin Schuh36a2c3e2021-02-18 22:28:38 -080093 }
Austin Schuh61e973f2021-02-21 21:43:56 -080094
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 Schuh36a2c3e2021-02-18 22:28:38 -0800104}
105
106} // namespace message_bridge
107} // namespace aos