blob: a7eff21d1baa46ab81ca2d738f3f0025380baa4a [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
4#include <stdint.h>
5#include <sys/socket.h>
6#include <netinet/in.h>
7#include <arpa/inet.h>
8
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 Schuh40485ed2019-10-26 21:51:44 -070043// Returns the resolved location for a name, type, and application name. Returns
44// nullptr if none is found.
Austin Schuhcb108412019-10-13 16:09:54 -070045//
46// If the application name is empty, it is ignored. Maps are processed in
47// reverse order, and application specific first.
Austin Schuhbca6cf02019-12-22 17:28:34 -080048const Channel *GetChannel(const Configuration *config,
49 const std::string_view name,
50 const std::string_view type,
51 const std::string_view application_name,
Austin Schuh0de30f32020-12-06 12:44:28 -080052 const Node *node, bool quiet = false);
Austin Schuhbca6cf02019-12-22 17:28:34 -080053inline const Channel *GetChannel(const Flatbuffer<Configuration> &config,
54 const std::string_view name,
55 const std::string_view type,
56 const std::string_view application_name,
57 const Node *node) {
58 return GetChannel(&config.message(), name, type, application_name, node);
Austin Schuh40485ed2019-10-26 21:51:44 -070059}
Austin Schuh2f8fd752020-09-01 22:38:28 -070060template <typename T>
61inline const Channel *GetChannel(const Configuration *config,
62 const std::string_view name,
63 const std::string_view application_name,
64 const Node *node) {
65 return GetChannel(config, name, T::GetFullyQualifiedName(), application_name,
66 node);
67}
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080068// Convenience wrapper for getting a channel from a specified config if you
69// already have the name/type in a Channel object--this is useful if you Channel
70// object you have does not point to memory within config.
71inline const Channel *GetChannel(const Configuration *config,
72 const Channel *channel,
73 const std::string_view application_name,
74 const Node *node) {
75 return GetChannel(config, channel->name()->string_view(),
76 channel->type()->string_view(), application_name, node);
77}
Austin Schuhcb108412019-10-13 16:09:54 -070078
Austin Schuhc9e10ec2020-01-26 16:08:28 -080079// Returns the channel index (or dies) of channel in the provided config.
80size_t ChannelIndex(const Configuration *config, const Channel *channel);
81
Austin Schuh217a9782019-12-21 23:02:50 -080082// Returns the Node out of the config with the matching name, or nullptr if it
83// can't be found.
84const Node *GetNode(const Configuration *config, std::string_view name);
Austin Schuhc9e10ec2020-01-26 16:08:28 -080085const Node *GetNode(const Configuration *config, const Node *node);
Austin Schuh0ca1fd32020-12-18 22:53:05 -080086// Returns the node with the provided index. This works in both a single and
87// multi-node world.
88const Node *GetNode(const Configuration *config, size_t node_index);
Austin Schuhc9e10ec2020-01-26 16:08:28 -080089// Returns a matching node, or nullptr if the provided node is nullptr and we
90// are in a single node world.
91const Node *GetNodeOrDie(const Configuration *config, const Node *node);
Austin Schuh217a9782019-12-21 23:02:50 -080092// Returns the Node out of the configuration which matches our hostname.
93// CHECKs if it can't be found.
94const Node *GetMyNode(const Configuration *config);
95const Node *GetNodeFromHostname(const Configuration *config,
96 std::string_view name);
Alex Perrycb7da4b2019-08-28 19:35:56 -070097
Austin Schuhc9e10ec2020-01-26 16:08:28 -080098// Returns a vector of the nodes in the config. (nullptr is considered the node
99// in a single node world.)
100std::vector<const Node *> GetNodes(const Configuration *config);
101
Austin Schuh65465332020-11-05 17:36:53 -0800102// Returns a vector of the nodes in the config with the provided tag. If this
103// is a single-node world, we assume that all tags match.
104std::vector<const Node *> GetNodesWithTag(const Configuration *config,
105 std::string_view tag);
106
Austin Schuh8bd96322020-02-13 21:18:22 -0800107// Returns the node index for a node. Note: will be faster if node is a pointer
108// to a node in config, but is not required.
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800109int GetNodeIndex(const Configuration *config, const Node *node);
Austin Schuh04408fc2020-02-16 21:48:54 -0800110int GetNodeIndex(const Configuration *config, std::string_view name);
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800111
Austin Schuhac0771c2020-01-07 18:36:30 -0800112// Returns true if we are running in a multinode configuration.
113bool MultiNode(const Configuration *config);
114
Austin Schuh217a9782019-12-21 23:02:50 -0800115// Returns true if the provided channel is sendable on the provided node.
116bool ChannelIsSendableOnNode(const Channel *channel, const Node *node);
117// Returns true if the provided channel is able to be watched or fetched on the
118// provided node.
119bool ChannelIsReadableOnNode(const Channel *channel, const Node *node);
120
Austin Schuh719946b2019-12-28 14:51:01 -0800121// Returns true if the message is supposed to be logged on this node.
122bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node);
123
124const Connection *ConnectionToNode(const Channel *channel, const Node *node);
125// Returns true if the delivery timestamps are supposed to be logged on this
126// node.
127bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
128 const Node *node,
129 const Node *logger_node);
130bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
131 const Node *node);
132
Austin Schuhbca6cf02019-12-22 17:28:34 -0800133// Prints a channel to json, but without the schema.
134std::string CleanedChannelToString(const Channel *channel);
Austin Schuha81454b2020-05-12 19:58:36 -0700135// Prints out a channel to json, but only with the name and type.
136std::string StrippedChannelToString(const Channel *channel);
Austin Schuhbca6cf02019-12-22 17:28:34 -0800137
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800138// Returns the node names that this node should be forwarding to.
139std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
140 const Node *my_node);
141
142// Returns the node names that this node should be receiving messages from.
143std::vector<std::string_view> SourceNodeNames(const Configuration *config,
144 const Node *my_node);
145
Austin Schuh8d7e0bb2020-10-02 17:57:00 -0700146// Returns the all nodes that are logging timestamps on our node.
147std::vector<const Node *> TimestampNodes(const Configuration *config,
148 const Node *my_node);
149
Austin Schuh217a9782019-12-21 23:02:50 -0800150// TODO(austin): GetSchema<T>(const Flatbuffer<Configuration> &config);
Brian Silverman66f079a2013-08-26 16:24:30 -0700151
Brian Silverman66f079a2013-08-26 16:24:30 -0700152} // namespace configuration
Alex Perrycb7da4b2019-08-28 19:35:56 -0700153
154// Compare and equality operators for Channel. Note: these only check the name
155// and type for equality.
156bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs,
157 const FlatbufferDetachedBuffer<Channel> &rhs);
158bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs,
159 const FlatbufferDetachedBuffer<Channel> &rhs);
Brian Silverman66f079a2013-08-26 16:24:30 -0700160} // namespace aos
161
John Park398c74a2018-10-20 21:17:39 -0700162#endif // AOS_CONFIGURATION_H_