blob: 738ca1082e9a3a8093d87dce8d14e597bf954042 [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.
18class ChannelTimestampFinder {
19 public:
20 ChannelTimestampFinder(aos::EventLoop *event_loop)
21 : ChannelTimestampFinder(event_loop->configuration(), event_loop->name(),
22 event_loop->node()) {}
23 ChannelTimestampFinder(const Configuration *configuration,
24 const std::string_view name, const Node *node);
25
26 // Finds the timestamp logging channel for the provided data channel and
27 // connection.
28 const Channel *ForChannel(const Channel *channel,
29 const Connection *connection);
30 std::string SplitChannelName(const Channel *channel,
31 const Connection *connection);
Austin Schuh006a9f52021-04-07 16:24:18 -070032 std::string SplitChannelName(std::string_view name, std::string type,
33 const Connection *connection);
Austin Schuh61e973f2021-02-21 21:43:56 -080034 std::string CombinedChannelName(std::string_view remote_node);
35
36 private:
37 const Configuration *configuration_;
38 const std::string_view name_;
39 const Node *node_;
40};
41
Austin Schuh36a2c3e2021-02-18 22:28:38 -080042// Class to manage lifetime, and creating senders for channels and connections.
43class ChannelTimestampSender {
44 public:
45 ChannelTimestampSender(aos::EventLoop *event_loop);
46
47 aos::Sender<RemoteMessage> *SenderForChannel(const Channel *channel,
48 const Connection *connection);
49
50 private:
51 aos::EventLoop *event_loop_;
52
53 // We've got 3 cases to consider.
54 // 1) The old single channel per connection exists.
55 // 2) A new channel per channel exists.
56 // 3) Both exist.
57 //
58 // I want the default to be such that if no channel is found, we explode
59 // looking for the single channel per channel. This means users will add the
60 // new channel when blindly fixing errors, which is what we want.
61 //
62 // I'd prefer 3) to be an error, but don't have strong opinions. We will
63 // still be correct if it gets used, as long as everything is consistent.
64
Austin Schuh36a2c3e2021-02-18 22:28:38 -080065 // Mapping from channel and connection to logger.
66 absl::btree_map<std::pair<const Channel *, const Connection *>,
Austin Schuh61e973f2021-02-21 21:43:56 -080067 std::shared_ptr<aos::Sender<RemoteMessage>>>
Austin Schuh36a2c3e2021-02-18 22:28:38 -080068 channel_timestamp_loggers_;
Austin Schuh61e973f2021-02-21 21:43:56 -080069
70 // Mapping from channel to RemoteMessage sender. This is the channel that
71 // timestamps are published to.
72 absl::btree_map<const Channel *, std::shared_ptr<aos::Sender<RemoteMessage>>>
73 timestamp_loggers_;
Austin Schuh36a2c3e2021-02-18 22:28:38 -080074};
75
76} // namespace message_bridge
77} // namespace aos
78
79#endif // AOS_NETWORK_TIMESTAMP_CHANNEL_