blob: d022b60038989ecfe0df0f3a1bf520e7b6550bc0 [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) {
15 std::string type(channel->type()->string_view());
16 std::replace(type.begin(), type.end(), '.', '-');
17 return absl::StrCat("/aos/remote_timestamps/",
18 connection->name()->string_view(),
19 channel->name()->string_view(), "/", type);
20}
21
22std::string ChannelTimestampFinder::CombinedChannelName(
23 std::string_view remote_node) {
24 return absl::StrCat("/aos/remote_timestamps/", remote_node);
25}
26
27const Channel *ChannelTimestampFinder::ForChannel(
28 const Channel *channel, const Connection *connection) {
29 const std::string split_timestamp_channel_name =
30 SplitChannelName(channel, connection);
31 const Channel *split_timestamp_channel = configuration::GetChannel(
32 configuration_, split_timestamp_channel_name,
33 RemoteMessage::GetFullyQualifiedName(), name_, node_, true);
34 if (split_timestamp_channel != nullptr) {
35 return split_timestamp_channel;
36 }
37
38 const std::string shared_timestamp_channel_name =
39 CombinedChannelName(connection->name()->string_view());
40 const Channel *shared_timestamp_channel = configuration::GetChannel(
41 configuration_, shared_timestamp_channel_name,
42 RemoteMessage::GetFullyQualifiedName(), name_, node_, true);
43 if (shared_timestamp_channel != nullptr) {
44 LOG(WARNING) << "Failed to find timestamp channel {\"name\": \""
45 << split_timestamp_channel << "\", \"type\": \""
46 << RemoteMessage::GetFullyQualifiedName()
47 << "\"}, falling back to old version.";
48 return shared_timestamp_channel;
49 }
50
51 CHECK(shared_timestamp_channel != nullptr)
52 << ": Remote timestamp channel { \"name\": \""
53 << split_timestamp_channel_name << "\", \"type\": \""
54 << RemoteMessage::GetFullyQualifiedName()
55 << "\" } not found in config for " << name_
56 << (configuration::MultiNode(configuration_)
57 ? absl::StrCat(" on node ", node_->name()->string_view())
58 : ".");
59
60 return nullptr;
61}
62
Austin Schuh36a2c3e2021-02-18 22:28:38 -080063ChannelTimestampSender::ChannelTimestampSender(aos::EventLoop *event_loop)
64 : event_loop_(event_loop) {
65 CHECK(configuration::MultiNode(event_loop_->configuration()));
Austin Schuh36a2c3e2021-02-18 22:28:38 -080066}
67
68aos::Sender<RemoteMessage> *ChannelTimestampSender::SenderForChannel(
69 const Channel *channel, const Connection *connection) {
Austin Schuh61e973f2021-02-21 21:43:56 -080070 ChannelTimestampFinder finder(event_loop_);
Austin Schuh36a2c3e2021-02-18 22:28:38 -080071 // Look at any pre-created channel/connection pairs.
Austin Schuh61e973f2021-02-21 21:43:56 -080072 {
73 auto it =
74 channel_timestamp_loggers_.find(std::make_pair(channel, connection));
75 if (it != channel_timestamp_loggers_.end()) {
76 return it->second.get();
77 }
Austin Schuh36a2c3e2021-02-18 22:28:38 -080078 }
79
Austin Schuh61e973f2021-02-21 21:43:56 -080080 const Channel *timestamp_channel = finder.ForChannel(channel, connection);
Austin Schuh36a2c3e2021-02-18 22:28:38 -080081
Austin Schuh61e973f2021-02-21 21:43:56 -080082 {
83 auto it = timestamp_loggers_.find(timestamp_channel);
84 if (it != timestamp_loggers_.end()) {
85 CHECK(channel_timestamp_loggers_
86 .try_emplace(std::make_pair(channel, connection), it->second)
87 .second);
88 return it->second.get();
Austin Schuh36a2c3e2021-02-18 22:28:38 -080089 }
Austin Schuh36a2c3e2021-02-18 22:28:38 -080090 }
Austin Schuh61e973f2021-02-21 21:43:56 -080091
92 auto result = channel_timestamp_loggers_.try_emplace(
93 std::make_pair(channel, connection),
94 std::make_shared<aos::Sender<RemoteMessage>>(
95 event_loop_->MakeSender<RemoteMessage>(
96 timestamp_channel->name()->string_view())));
97
98 CHECK(timestamp_loggers_.try_emplace(timestamp_channel, result.first->second)
99 .second);
100 return result.first->second.get();
Austin Schuh36a2c3e2021-02-18 22:28:38 -0800101}
102
103} // namespace message_bridge
104} // namespace aos