blob: 88b6ed0a4c73d07117f1c34bc69d8b920dd59474 [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) {
Austin Schuh58646e22021-08-23 23:51:46 -070068 if (event_loop_) {
69 CHECK(configuration::MultiNode(event_loop_->configuration()));
70 }
Austin Schuh36a2c3e2021-02-18 22:28:38 -080071}
72
73aos::Sender<RemoteMessage> *ChannelTimestampSender::SenderForChannel(
74 const Channel *channel, const Connection *connection) {
Austin Schuh58646e22021-08-23 23:51:46 -070075 CHECK(event_loop_);
76
Austin Schuh61e973f2021-02-21 21:43:56 -080077 ChannelTimestampFinder finder(event_loop_);
Austin Schuh36a2c3e2021-02-18 22:28:38 -080078 // Look at any pre-created channel/connection pairs.
Austin Schuh61e973f2021-02-21 21:43:56 -080079 {
80 auto it =
81 channel_timestamp_loggers_.find(std::make_pair(channel, connection));
82 if (it != channel_timestamp_loggers_.end()) {
83 return it->second.get();
84 }
Austin Schuh36a2c3e2021-02-18 22:28:38 -080085 }
86
Austin Schuh61e973f2021-02-21 21:43:56 -080087 const Channel *timestamp_channel = finder.ForChannel(channel, connection);
Austin Schuh36a2c3e2021-02-18 22:28:38 -080088
Austin Schuh61e973f2021-02-21 21:43:56 -080089 {
90 auto it = timestamp_loggers_.find(timestamp_channel);
91 if (it != timestamp_loggers_.end()) {
92 CHECK(channel_timestamp_loggers_
93 .try_emplace(std::make_pair(channel, connection), it->second)
94 .second);
95 return it->second.get();
Austin Schuh36a2c3e2021-02-18 22:28:38 -080096 }
Austin Schuh36a2c3e2021-02-18 22:28:38 -080097 }
Austin Schuh61e973f2021-02-21 21:43:56 -080098
99 auto result = channel_timestamp_loggers_.try_emplace(
100 std::make_pair(channel, connection),
101 std::make_shared<aos::Sender<RemoteMessage>>(
102 event_loop_->MakeSender<RemoteMessage>(
103 timestamp_channel->name()->string_view())));
104
105 CHECK(timestamp_loggers_.try_emplace(timestamp_channel, result.first->second)
106 .second);
107 return result.first->second.get();
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800108}
109
110} // namespace message_bridge
111} // namespace aos