blob: ac5c90caa699de90f0728207f79d26204ab504fe [file] [log] [blame]
John Park398c74a2018-10-20 21:17:39 -07001#ifndef AOS_CONFIGURATION_H_
2#define AOS_CONFIGURATION_H_
Brian Silverman66f079a2013-08-26 16:24:30 -07003
Brian Silverman66f079a2013-08-26 16:24:30 -07004#include <arpa/inet.h>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07005#include <netinet/in.h>
6#include <sys/socket.h>
Brian Silverman66f079a2013-08-26 16:24:30 -07007
Tyler Chatowbf0609c2021-07-31 16:13:27 -07008#include <cstdint>
James Kuszmaul3ae42262019-11-08 12:33:41 -08009#include <string_view>
10
Austin Schuhb8075812020-10-19 09:36:49 -070011#include "aos/configuration_generated.h" // IWYU pragma: export
Austin Schuhcb108412019-10-13 16:09:54 -070012#include "aos/flatbuffers.h"
13
Brian Silverman66f079a2013-08-26 16:24:30 -070014namespace aos {
15
16// Holds global configuration data. All of the functions are safe to call
17// from wherever.
18namespace configuration {
19
Austin Schuhcb108412019-10-13 16:09:54 -070020// Reads a json configuration. This includes all imports and merges. Note:
21// duplicate imports will result in a CHECK.
Austin Schuh40485ed2019-10-26 21:51:44 -070022FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaulc0c08da2020-05-10 18:56:07 -070023 const std::string_view path,
24 const std::vector<std::string_view> &extra_import_paths = {});
Austin Schuhcb108412019-10-13 16:09:54 -070025
Alex Perrycb7da4b2019-08-28 19:35:56 -070026// Sorts and merges entries in a config.
27FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
28 const Flatbuffer<Configuration> &config);
29
30// Adds schema definitions to a sorted and merged config from the provided
31// schema list.
32FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
33 const Flatbuffer<Configuration> &config,
Austin Schuh0de30f32020-12-06 12:44:28 -080034 const std::vector<aos::FlatbufferVector<reflection::Schema>> &schemas);
Alex Perrycb7da4b2019-08-28 19:35:56 -070035
Austin Schuh8d6cea82020-02-28 12:17:16 -080036// Merges a configuration json snippet into the provided configuration and
37// returns the modified config.
38FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
39 const Configuration *config, std::string_view json);
Brian Silverman24f5aa82020-06-23 16:21:28 -070040FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
41 const Configuration *config, const Flatbuffer<Configuration> &addition);
Austin Schuh8d6cea82020-02-28 12:17:16 -080042
Austin Schuha9df9ad2021-06-16 14:49:39 -070043// Adds the list of schemas to the provide config json. This should mostly be
44// used for testing and in conjunction with MergeWithConfig.
45FlatbufferDetachedBuffer<aos::Configuration> AddSchema(
46 std::string_view json,
47 const std::vector<FlatbufferVector<reflection::Schema>> &schemas);
48
Austin Schuh681a2472020-12-31 23:55:40 -080049// Returns the resolved Channel for a name, type, and application name. Returns
Austin Schuh40485ed2019-10-26 21:51:44 -070050// nullptr if none is found.
Austin Schuhcb108412019-10-13 16:09:54 -070051//
52// If the application name is empty, it is ignored. Maps are processed in
53// reverse order, and application specific first.
James Kuszmaul71a81932020-12-15 21:08:01 -080054//
55// The config should already be fully merged and sorted (as produced by
56// MergeConfiguration() or any of the associated functions).
Austin Schuhbca6cf02019-12-22 17:28:34 -080057const Channel *GetChannel(const Configuration *config,
58 const std::string_view name,
59 const std::string_view type,
60 const std::string_view application_name,
Austin Schuh0de30f32020-12-06 12:44:28 -080061 const Node *node, bool quiet = false);
Austin Schuhbca6cf02019-12-22 17:28:34 -080062inline const Channel *GetChannel(const Flatbuffer<Configuration> &config,
63 const std::string_view name,
64 const std::string_view type,
65 const std::string_view application_name,
66 const Node *node) {
67 return GetChannel(&config.message(), name, type, application_name, node);
Austin Schuh40485ed2019-10-26 21:51:44 -070068}
Austin Schuh2f8fd752020-09-01 22:38:28 -070069template <typename T>
70inline const Channel *GetChannel(const Configuration *config,
71 const std::string_view name,
72 const std::string_view application_name,
73 const Node *node) {
74 return GetChannel(config, name, T::GetFullyQualifiedName(), application_name,
75 node);
76}
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080077// Convenience wrapper for getting a channel from a specified config if you
78// already have the name/type in a Channel object--this is useful if you Channel
79// object you have does not point to memory within config.
80inline const Channel *GetChannel(const Configuration *config,
81 const Channel *channel,
82 const std::string_view application_name,
83 const Node *node) {
84 return GetChannel(config, channel->name()->string_view(),
85 channel->type()->string_view(), application_name, node);
86}
Austin Schuhcb108412019-10-13 16:09:54 -070087
Austin Schuhc9e10ec2020-01-26 16:08:28 -080088// Returns the channel index (or dies) of channel in the provided config.
89size_t ChannelIndex(const Configuration *config, const Channel *channel);
90
Austin Schuh217a9782019-12-21 23:02:50 -080091// Returns the Node out of the config with the matching name, or nullptr if it
92// can't be found.
93const Node *GetNode(const Configuration *config, std::string_view name);
Austin Schuhc9e10ec2020-01-26 16:08:28 -080094const Node *GetNode(const Configuration *config, const Node *node);
Austin Schuh0ca1fd32020-12-18 22:53:05 -080095// Returns the node with the provided index. This works in both a single and
96// multi-node world.
97const Node *GetNode(const Configuration *config, size_t node_index);
Austin Schuhc9e10ec2020-01-26 16:08:28 -080098// Returns a matching node, or nullptr if the provided node is nullptr and we
99// are in a single node world.
100const Node *GetNodeOrDie(const Configuration *config, const Node *node);
Austin Schuh217a9782019-12-21 23:02:50 -0800101// Returns the Node out of the configuration which matches our hostname.
102// CHECKs if it can't be found.
103const Node *GetMyNode(const Configuration *config);
104const Node *GetNodeFromHostname(const Configuration *config,
105 std::string_view name);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700106
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800107// Returns a vector of the nodes in the config. (nullptr is considered the node
108// in a single node world.)
109std::vector<const Node *> GetNodes(const Configuration *config);
110
Austin Schuh65465332020-11-05 17:36:53 -0800111// Returns a vector of the nodes in the config with the provided tag. If this
112// is a single-node world, we assume that all tags match.
113std::vector<const Node *> GetNodesWithTag(const Configuration *config,
114 std::string_view tag);
115
Austin Schuh8bd96322020-02-13 21:18:22 -0800116// Returns the node index for a node. Note: will be faster if node is a pointer
117// to a node in config, but is not required.
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800118int GetNodeIndex(const Configuration *config, const Node *node);
Austin Schuh04408fc2020-02-16 21:48:54 -0800119int GetNodeIndex(const Configuration *config, std::string_view name);
Austin Schuh681a2472020-12-31 23:55:40 -0800120// Returns the number of nodes.
121size_t NodesCount(const Configuration *config);
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800122
Austin Schuhac0771c2020-01-07 18:36:30 -0800123// Returns true if we are running in a multinode configuration.
124bool MultiNode(const Configuration *config);
125
Austin Schuh217a9782019-12-21 23:02:50 -0800126// Returns true if the provided channel is sendable on the provided node.
127bool ChannelIsSendableOnNode(const Channel *channel, const Node *node);
128// Returns true if the provided channel is able to be watched or fetched on the
129// provided node.
130bool ChannelIsReadableOnNode(const Channel *channel, const Node *node);
131
Austin Schuh719946b2019-12-28 14:51:01 -0800132// Returns true if the message is supposed to be logged on this node.
133bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node);
Austin Schuh2bb80e02021-03-20 21:46:17 -0700134bool ChannelMessageIsLoggedOnNode(const Channel *channel,
135 std::string_view node_name);
Austin Schuh719946b2019-12-28 14:51:01 -0800136
Austin Schuh58646e22021-08-23 23:51:46 -0700137// Returns the number of connections.
138size_t ConnectionCount(const Channel *channel);
139
Austin Schuh719946b2019-12-28 14:51:01 -0800140const Connection *ConnectionToNode(const Channel *channel, const Node *node);
141// Returns true if the delivery timestamps are supposed to be logged on this
142// node.
143bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
144 const Node *node,
145 const Node *logger_node);
146bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
147 const Node *node);
148
Austin Schuhbca6cf02019-12-22 17:28:34 -0800149// Prints a channel to json, but without the schema.
150std::string CleanedChannelToString(const Channel *channel);
Austin Schuha81454b2020-05-12 19:58:36 -0700151// Prints out a channel to json, but only with the name and type.
152std::string StrippedChannelToString(const Channel *channel);
Austin Schuhbca6cf02019-12-22 17:28:34 -0800153
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800154// Returns the node names that this node should be forwarding to.
155std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
156 const Node *my_node);
157
158// Returns the node names that this node should be receiving messages from.
159std::vector<std::string_view> SourceNodeNames(const Configuration *config,
160 const Node *my_node);
161
Austin Schuhfc7b6a02021-07-12 21:19:07 -0700162// Returns the source node index for each channel in a config.
163std::vector<size_t> SourceNodeIndex(const Configuration *config);
164
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700165// Returns the all nodes that are logging timestamps on our node.
166std::vector<const Node *> TimestampNodes(const Configuration *config,
167 const Node *my_node);
168
Austin Schuhd2e2f6a2021-02-07 20:46:16 -0800169// Returns the application for the provided node and name if it exists, or
170// nullptr if it does not exist on this node.
171const Application *GetApplication(const Configuration *config,
172 const Node *my_node,
173 std::string_view application_name);
174
Austin Schuh217a9782019-12-21 23:02:50 -0800175// TODO(austin): GetSchema<T>(const Flatbuffer<Configuration> &config);
Brian Silverman66f079a2013-08-26 16:24:30 -0700176
Brian Silverman66f079a2013-08-26 16:24:30 -0700177} // namespace configuration
Alex Perrycb7da4b2019-08-28 19:35:56 -0700178
179// Compare and equality operators for Channel. Note: these only check the name
180// and type for equality.
181bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs,
182 const FlatbufferDetachedBuffer<Channel> &rhs);
183bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs,
184 const FlatbufferDetachedBuffer<Channel> &rhs);
Brian Silverman66f079a2013-08-26 16:24:30 -0700185} // namespace aos
186
John Park398c74a2018-10-20 21:17:39 -0700187#endif // AOS_CONFIGURATION_H_