blob: d95eddada8a9ee413281d976c94d54aef5c56a0a [file] [log] [blame]
Austin Schuh36a2c3e2021-02-18 22:28:38 -08001#ifndef AOS_NETWORK_TIMESTAMP_CHANNEL_
2#define AOS_NETWORK_TIMESTAMP_CHANNEL_
3
Austin Schuh61e973f2021-02-21 21:43:56 -08004#include <string_view>
Austin Schuh36a2c3e2021-02-18 22:28:38 -08005#include <vector>
6
7#include "absl/container/btree_map.h"
8#include "aos/configuration.h"
9#include "aos/events/event_loop.h"
10#include "aos/network/remote_message_generated.h"
11#include "glog/logging.h"
12
13namespace aos {
14namespace message_bridge {
15
Austin Schuh61e973f2021-02-21 21:43:56 -080016// Class to find the corresponding channel where timestamps for a specified data
17// channel and connection will be logged.
Austin Schuh01f3b392022-01-25 20:03:09 -080018//
19// This abstracts (and detects) when we have combined or split remote timestamp
20// logging channels.
Austin Schuh61e973f2021-02-21 21:43:56 -080021class ChannelTimestampFinder {
22 public:
23 ChannelTimestampFinder(aos::EventLoop *event_loop)
24 : ChannelTimestampFinder(event_loop->configuration(), event_loop->name(),
25 event_loop->node()) {}
26 ChannelTimestampFinder(const Configuration *configuration,
27 const std::string_view name, const Node *node);
28
Austin Schuh01f3b392022-01-25 20:03:09 -080029 // Returns the split timestamp logging channel for the provide channel and
30 // connection if one exists, or nullptr otherwise.
31 const Channel *SplitChannelForChannel(const Channel *channel,
32 const Connection *connection);
33
Austin Schuh61e973f2021-02-21 21:43:56 -080034 // Finds the timestamp logging channel for the provided data channel and
35 // connection.
36 const Channel *ForChannel(const Channel *channel,
37 const Connection *connection);
38 std::string SplitChannelName(const Channel *channel,
39 const Connection *connection);
Austin Schuh006a9f52021-04-07 16:24:18 -070040 std::string SplitChannelName(std::string_view name, std::string type,
41 const Connection *connection);
Austin Schuh61e973f2021-02-21 21:43:56 -080042 std::string CombinedChannelName(std::string_view remote_node);
43
44 private:
45 const Configuration *configuration_;
46 const std::string_view name_;
47 const Node *node_;
48};
49
Austin Schuh36a2c3e2021-02-18 22:28:38 -080050// Class to manage lifetime, and creating senders for channels and connections.
51class ChannelTimestampSender {
52 public:
53 ChannelTimestampSender(aos::EventLoop *event_loop);
Austin Schuh58646e22021-08-23 23:51:46 -070054 ChannelTimestampSender() : event_loop_(nullptr) {}
55
56 ChannelTimestampSender(ChannelTimestampSender &&other) noexcept = default;
57 ChannelTimestampSender &operator=(ChannelTimestampSender &&other) noexcept =
58 default;
Austin Schuh36a2c3e2021-02-18 22:28:38 -080059
60 aos::Sender<RemoteMessage> *SenderForChannel(const Channel *channel,
61 const Connection *connection);
James Kuszmaul94ca5132022-07-19 09:11:08 -070062 void ClearSenderForChannel(const Channel *channel,
63 const Connection *connection);
Austin Schuh36a2c3e2021-02-18 22:28:38 -080064
65 private:
66 aos::EventLoop *event_loop_;
67
68 // We've got 3 cases to consider.
69 // 1) The old single channel per connection exists.
70 // 2) A new channel per channel exists.
71 // 3) Both exist.
72 //
73 // I want the default to be such that if no channel is found, we explode
74 // looking for the single channel per channel. This means users will add the
75 // new channel when blindly fixing errors, which is what we want.
76 //
77 // I'd prefer 3) to be an error, but don't have strong opinions. We will
78 // still be correct if it gets used, as long as everything is consistent.
79
Austin Schuh36a2c3e2021-02-18 22:28:38 -080080 // Mapping from channel and connection to logger.
81 absl::btree_map<std::pair<const Channel *, const Connection *>,
Austin Schuh61e973f2021-02-21 21:43:56 -080082 std::shared_ptr<aos::Sender<RemoteMessage>>>
Austin Schuh36a2c3e2021-02-18 22:28:38 -080083 channel_timestamp_loggers_;
Austin Schuh61e973f2021-02-21 21:43:56 -080084
85 // Mapping from channel to RemoteMessage sender. This is the channel that
86 // timestamps are published to.
87 absl::btree_map<const Channel *, std::shared_ptr<aos::Sender<RemoteMessage>>>
88 timestamp_loggers_;
Austin Schuh36a2c3e2021-02-18 22:28:38 -080089};
90
91} // namespace message_bridge
92} // namespace aos
93
94#endif // AOS_NETWORK_TIMESTAMP_CHANNEL_