blob: 88687ad0257403bf3bfc90b51bfca443a1d4576c [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 Schuhcb108412019-10-13 16:09:54 -070011#include "aos/configuration_generated.h"
12#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 Kuszmaul3ae42262019-11-08 12:33:41 -080023 const std::string_view path);
Austin Schuhcb108412019-10-13 16:09:54 -070024
Alex Perrycb7da4b2019-08-28 19:35:56 -070025// Sorts and merges entries in a config.
26FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
27 const Flatbuffer<Configuration> &config);
28
29// Adds schema definitions to a sorted and merged config from the provided
30// schema list.
31FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
32 const Flatbuffer<Configuration> &config,
33 const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas);
34
Austin Schuh40485ed2019-10-26 21:51:44 -070035// Returns the resolved location for a name, type, and application name. Returns
36// nullptr if none is found.
Austin Schuhcb108412019-10-13 16:09:54 -070037//
38// If the application name is empty, it is ignored. Maps are processed in
39// reverse order, and application specific first.
Austin Schuhbca6cf02019-12-22 17:28:34 -080040const Channel *GetChannel(const Configuration *config,
41 const std::string_view name,
42 const std::string_view type,
43 const std::string_view application_name,
44 const Node *node);
45inline const Channel *GetChannel(const Flatbuffer<Configuration> &config,
46 const std::string_view name,
47 const std::string_view type,
48 const std::string_view application_name,
49 const Node *node) {
50 return GetChannel(&config.message(), name, type, application_name, node);
Austin Schuh40485ed2019-10-26 21:51:44 -070051}
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080052// Convenience wrapper for getting a channel from a specified config if you
53// already have the name/type in a Channel object--this is useful if you Channel
54// object you have does not point to memory within config.
55inline const Channel *GetChannel(const Configuration *config,
56 const Channel *channel,
57 const std::string_view application_name,
58 const Node *node) {
59 return GetChannel(config, channel->name()->string_view(),
60 channel->type()->string_view(), application_name, node);
61}
Austin Schuhcb108412019-10-13 16:09:54 -070062
Austin Schuh217a9782019-12-21 23:02:50 -080063// Returns the Node out of the config with the matching name, or nullptr if it
64// can't be found.
65const Node *GetNode(const Configuration *config, std::string_view name);
66// Returns the Node out of the configuration which matches our hostname.
67// CHECKs if it can't be found.
68const Node *GetMyNode(const Configuration *config);
69const Node *GetNodeFromHostname(const Configuration *config,
70 std::string_view name);
Alex Perrycb7da4b2019-08-28 19:35:56 -070071
Austin Schuhac0771c2020-01-07 18:36:30 -080072// Returns true if we are running in a multinode configuration.
73bool MultiNode(const Configuration *config);
74
Austin Schuh217a9782019-12-21 23:02:50 -080075// Returns true if the provided channel is sendable on the provided node.
76bool ChannelIsSendableOnNode(const Channel *channel, const Node *node);
77// Returns true if the provided channel is able to be watched or fetched on the
78// provided node.
79bool ChannelIsReadableOnNode(const Channel *channel, const Node *node);
80
Austin Schuh719946b2019-12-28 14:51:01 -080081// Returns true if the message is supposed to be logged on this node.
82bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node);
83
84const Connection *ConnectionToNode(const Channel *channel, const Node *node);
85// Returns true if the delivery timestamps are supposed to be logged on this
86// node.
87bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
88 const Node *node,
89 const Node *logger_node);
90bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
91 const Node *node);
92
Austin Schuhbca6cf02019-12-22 17:28:34 -080093// Prints a channel to json, but without the schema.
94std::string CleanedChannelToString(const Channel *channel);
95
Austin Schuhe84c3ed2019-12-14 15:29:48 -080096// Returns the node names that this node should be forwarding to.
97std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
98 const Node *my_node);
99
100// Returns the node names that this node should be receiving messages from.
101std::vector<std::string_view> SourceNodeNames(const Configuration *config,
102 const Node *my_node);
103
Austin Schuh217a9782019-12-21 23:02:50 -0800104// TODO(austin): GetSchema<T>(const Flatbuffer<Configuration> &config);
Brian Silverman66f079a2013-08-26 16:24:30 -0700105
Brian Silverman66f079a2013-08-26 16:24:30 -0700106} // namespace configuration
Alex Perrycb7da4b2019-08-28 19:35:56 -0700107
108// Compare and equality operators for Channel. Note: these only check the name
109// and type for equality.
110bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs,
111 const FlatbufferDetachedBuffer<Channel> &rhs);
112bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs,
113 const FlatbufferDetachedBuffer<Channel> &rhs);
Brian Silverman66f079a2013-08-26 16:24:30 -0700114} // namespace aos
115
John Park398c74a2018-10-20 21:17:39 -0700116#endif // AOS_CONFIGURATION_H_