blob: 12c67136a9ed89f116be7370ee3563635d09c243 [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
8ChannelTimestampSender::ChannelTimestampSender(aos::EventLoop *event_loop)
9 : event_loop_(event_loop) {
10 CHECK(configuration::MultiNode(event_loop_->configuration()));
11 timestamp_loggers_.resize(event_loop_->configuration()->nodes()->size());
12}
13
14aos::Sender<RemoteMessage> *ChannelTimestampSender::SenderForChannel(
15 const Channel *channel, const Connection *connection) {
16 // Look at any pre-created channel/connection pairs.
17 auto it =
18 channel_timestamp_loggers_.find(std::make_pair(channel, connection));
19 if (it != channel_timestamp_loggers_.end()) {
20 return it->second.get();
21 }
22
23 const Node *other_node = configuration::GetNode(
24 event_loop_->configuration(), connection->name()->string_view());
25 const size_t other_node_index =
26 configuration::GetNodeIndex(event_loop_->configuration(), other_node);
27
28 std::string type(channel->type()->string_view());
29 std::replace(type.begin(), type.end(), '.', '-');
30 const std::string single_timestamp_channel =
31 absl::StrCat("/aos/remote_timestamps/", connection->name()->string_view(),
32 channel->name()->string_view(), "/", type);
33 if (event_loop_->HasChannel<RemoteMessage>(single_timestamp_channel)) {
34 LOG(INFO) << "Making RemoteMessage channel " << single_timestamp_channel;
35 auto result = channel_timestamp_loggers_.try_emplace(
36 std::make_pair(channel, connection),
37 std::make_unique<aos::Sender<RemoteMessage>>(
38 event_loop_->MakeSender<RemoteMessage>(single_timestamp_channel)));
39 return result.first->second.get();
40 } else {
41 // Then look for any per-remote-node channels.
42 if (timestamp_loggers_[other_node_index]) {
43 return &timestamp_loggers_[other_node_index];
44 }
45 const std::string shared_timestamp_channel = absl::StrCat(
46 "/aos/remote_timestamps/", connection->name()->string_view());
47 LOG(INFO) << "Looking for " << shared_timestamp_channel;
48 if (event_loop_->HasChannel<RemoteMessage>(shared_timestamp_channel)) {
49 LOG(WARNING) << "Failed to find timestamp channel {\"name\": \""
50 << single_timestamp_channel << "\", \"type\": \""
51 << RemoteMessage::GetFullyQualifiedName()
52 << "\"}, falling back to old version.";
53 timestamp_loggers_[other_node_index] =
54 event_loop_->MakeSender<RemoteMessage>(shared_timestamp_channel);
55 return &timestamp_loggers_[other_node_index];
56 } else {
57 LOG(ERROR) << "Failed";
58 }
59
60 // Explode with an error about the new channel.
61 event_loop_->MakeSender<RemoteMessage>(single_timestamp_channel);
62 return nullptr;
63 }
64}
65
66} // namespace message_bridge
67} // namespace aos