John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 1 | #ifndef AOS_CONFIGURATION_H_ |
| 2 | #define AOS_CONFIGURATION_H_ |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 3 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 4 | #include <arpa/inet.h> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 5 | #include <netinet/in.h> |
| 6 | #include <sys/socket.h> |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 7 | |
Austin Schuh | fb37c61 | 2022-08-11 15:24:51 -0700 | [diff] [blame] | 8 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 9 | #include <cstdint> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 10 | #include <string_view> |
| 11 | |
Austin Schuh | b807581 | 2020-10-19 09:36:49 -0700 | [diff] [blame] | 12 | #include "aos/configuration_generated.h" // IWYU pragma: export |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 13 | #include "aos/flatbuffers.h" |
| 14 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 15 | namespace aos { |
| 16 | |
| 17 | // Holds global configuration data. All of the functions are safe to call |
| 18 | // from wherever. |
| 19 | namespace configuration { |
| 20 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 21 | // Reads a json configuration. This includes all imports and merges. Note: |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 22 | // duplicate imports or invalid paths will result in a FATAL. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 23 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 24 | const std::string_view path, |
| 25 | const std::vector<std::string_view> &extra_import_paths = {}); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 26 | |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 27 | // Reads a json configuration. This includes all imports and merges. Returns |
| 28 | // std::nullopt on duplicate imports or invalid paths. |
| 29 | std::optional<FlatbufferDetachedBuffer<Configuration>> MaybeReadConfig( |
| 30 | const std::string_view path, |
| 31 | const std::vector<std::string_view> &extra_import_paths = {}); |
| 32 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 33 | // Sorts and merges entries in a config. |
| 34 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 35 | const Flatbuffer<Configuration> &config); |
| 36 | |
| 37 | // Adds schema definitions to a sorted and merged config from the provided |
| 38 | // schema list. |
| 39 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 40 | const Flatbuffer<Configuration> &config, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 41 | const std::vector<aos::FlatbufferVector<reflection::Schema>> &schemas); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 42 | |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 43 | // Merges a configuration json snippet into the provided configuration and |
| 44 | // returns the modified config. |
| 45 | FlatbufferDetachedBuffer<Configuration> MergeWithConfig( |
| 46 | const Configuration *config, std::string_view json); |
Brian Silverman | 24f5aa8 | 2020-06-23 16:21:28 -0700 | [diff] [blame] | 47 | FlatbufferDetachedBuffer<Configuration> MergeWithConfig( |
| 48 | const Configuration *config, const Flatbuffer<Configuration> &addition); |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 49 | |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 50 | // Adds the list of schemas to the provide aos_config.json. This should mostly |
| 51 | // be used for testing and in conjunction with MergeWithConfig. |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 52 | FlatbufferDetachedBuffer<aos::Configuration> AddSchema( |
| 53 | std::string_view json, |
| 54 | const std::vector<FlatbufferVector<reflection::Schema>> &schemas); |
| 55 | |
Austin Schuh | 681a247 | 2020-12-31 23:55:40 -0800 | [diff] [blame] | 56 | // Returns the resolved Channel for a name, type, and application name. Returns |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 57 | // nullptr if none is found. |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 58 | // |
| 59 | // If the application name is empty, it is ignored. Maps are processed in |
| 60 | // reverse order, and application specific first. |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 61 | // |
| 62 | // The config should already be fully merged and sorted (as produced by |
| 63 | // MergeConfiguration() or any of the associated functions). |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 64 | const Channel *GetChannel(const Configuration *config, |
| 65 | const std::string_view name, |
| 66 | const std::string_view type, |
| 67 | const std::string_view application_name, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 68 | const Node *node, bool quiet = false); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 69 | inline const Channel *GetChannel(const Flatbuffer<Configuration> &config, |
| 70 | const std::string_view name, |
| 71 | const std::string_view type, |
| 72 | const std::string_view application_name, |
| 73 | const Node *node) { |
| 74 | return GetChannel(&config.message(), name, type, application_name, node); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 75 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 76 | template <typename T> |
| 77 | inline const Channel *GetChannel(const Configuration *config, |
| 78 | const std::string_view name, |
| 79 | const std::string_view application_name, |
| 80 | const Node *node) { |
| 81 | return GetChannel(config, name, T::GetFullyQualifiedName(), application_name, |
| 82 | node); |
| 83 | } |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 84 | // Convenience wrapper for getting a channel from a specified config if you |
| 85 | // already have the name/type in a Channel object--this is useful if you Channel |
| 86 | // object you have does not point to memory within config. |
| 87 | inline const Channel *GetChannel(const Configuration *config, |
| 88 | const Channel *channel, |
| 89 | const std::string_view application_name, |
| 90 | const Node *node) { |
| 91 | return GetChannel(config, channel->name()->string_view(), |
| 92 | channel->type()->string_view(), application_name, node); |
| 93 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 94 | |
James Kuszmaul | c8503f3 | 2022-06-25 16:17:12 -0700 | [diff] [blame] | 95 | // Returns a list of all the channel names that can be used to refer to the |
| 96 | // specified channel on the given node/application. this allows a reverse-lookup |
| 97 | // of any renames that happen. |
| 98 | // Does not perform forwards-lookup first. |
| 99 | std::set<std::string> GetChannelAliases(const Configuration *config, |
| 100 | std::string_view name, |
| 101 | std::string_view type, |
| 102 | const std::string_view application_name, |
| 103 | const Node *node); |
| 104 | inline std::set<std::string> GetChannelAliases( |
| 105 | const Configuration *config, const Channel *channel, |
| 106 | const std::string_view application_name, const Node *node) { |
| 107 | return GetChannelAliases(config, channel->name()->string_view(), |
| 108 | channel->type()->string_view(), application_name, |
| 109 | node); |
| 110 | } |
| 111 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 112 | // Returns the channel index (or dies) of channel in the provided config. |
| 113 | size_t ChannelIndex(const Configuration *config, const Channel *channel); |
| 114 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 115 | // Returns the Node out of the config with the matching name, or nullptr if it |
| 116 | // can't be found. |
| 117 | const Node *GetNode(const Configuration *config, std::string_view name); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 118 | const Node *GetNode(const Configuration *config, const Node *node); |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 119 | // Returns the node with the provided index. This works in both a single and |
| 120 | // multi-node world. |
| 121 | const Node *GetNode(const Configuration *config, size_t node_index); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 122 | // Returns a matching node, or nullptr if the provided node is nullptr and we |
| 123 | // are in a single node world. |
| 124 | const Node *GetNodeOrDie(const Configuration *config, const Node *node); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 125 | // Returns the Node out of the configuration which matches our hostname. |
| 126 | // CHECKs if it can't be found. |
| 127 | const Node *GetMyNode(const Configuration *config); |
| 128 | const Node *GetNodeFromHostname(const Configuration *config, |
| 129 | std::string_view name); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 130 | |
Austin Schuh | 6309726 | 2023-08-16 17:04:29 -0700 | [diff] [blame] | 131 | // Returns a printable name for the node. (singlenode) if we are on a single |
| 132 | // node system, and the name otherwise. |
| 133 | std::string_view NodeName(const Configuration *config, size_t node_index); |
| 134 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 135 | // Returns a vector of the nodes in the config. (nullptr is considered the node |
| 136 | // in a single node world.) |
| 137 | std::vector<const Node *> GetNodes(const Configuration *config); |
| 138 | |
Austin Schuh | 6546533 | 2020-11-05 17:36:53 -0800 | [diff] [blame] | 139 | // Returns a vector of the nodes in the config with the provided tag. If this |
| 140 | // is a single-node world, we assume that all tags match. |
| 141 | std::vector<const Node *> GetNodesWithTag(const Configuration *config, |
| 142 | std::string_view tag); |
| 143 | |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 144 | // Returns whether the given node has the provided tag. If this is a single-node |
| 145 | // world, we assume that all tags match. |
| 146 | bool NodeHasTag(const Node *node, std::string_view tag); |
| 147 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 148 | // Returns the node index for a node. Note: will be faster if node is a pointer |
| 149 | // to a node in config, but is not required. |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 150 | int GetNodeIndex(const Configuration *config, const Node *node); |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 151 | int GetNodeIndex(const Configuration *config, std::string_view name); |
Austin Schuh | 681a247 | 2020-12-31 23:55:40 -0800 | [diff] [blame] | 152 | // Returns the number of nodes. |
| 153 | size_t NodesCount(const Configuration *config); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 154 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 155 | // Returns true if we are running in a multinode configuration. |
| 156 | bool MultiNode(const Configuration *config); |
| 157 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 158 | // Returns true if the provided channel is sendable on the provided node. |
| 159 | bool ChannelIsSendableOnNode(const Channel *channel, const Node *node); |
| 160 | // Returns true if the provided channel is able to be watched or fetched on the |
| 161 | // provided node. |
| 162 | bool ChannelIsReadableOnNode(const Channel *channel, const Node *node); |
| 163 | |
James Kuszmaul | 24db2d3 | 2023-05-26 11:40:12 -0700 | [diff] [blame] | 164 | // Returns true if the provided channel is sent on the provided node and gets |
| 165 | // forwarded to any other nodes. |
| 166 | bool ChannelIsForwardedFromNode(const Channel *channel, const Node *node); |
| 167 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 168 | // Returns true if the message is supposed to be logged on this node. |
| 169 | bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node); |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 170 | bool ChannelMessageIsLoggedOnNode(const Channel *channel, |
| 171 | std::string_view node_name); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 172 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 173 | // Returns the number of connections. |
| 174 | size_t ConnectionCount(const Channel *channel); |
| 175 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 176 | const Connection *ConnectionToNode(const Channel *channel, const Node *node); |
| 177 | // Returns true if the delivery timestamps are supposed to be logged on this |
| 178 | // node. |
| 179 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel, |
| 180 | const Node *node, |
| 181 | const Node *logger_node); |
| 182 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection, |
| 183 | const Node *node); |
| 184 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 185 | // Prints a channel to json, but without the schema. |
| 186 | std::string CleanedChannelToString(const Channel *channel); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 187 | // Prints out a channel to json, but only with the name and type. |
| 188 | std::string StrippedChannelToString(const Channel *channel); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 189 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 190 | // Returns the node names that this node should be forwarding to. |
| 191 | std::vector<std::string_view> DestinationNodeNames(const Configuration *config, |
| 192 | const Node *my_node); |
| 193 | |
| 194 | // Returns the node names that this node should be receiving messages from. |
| 195 | std::vector<std::string_view> SourceNodeNames(const Configuration *config, |
| 196 | const Node *my_node); |
| 197 | |
Austin Schuh | fc7b6a0 | 2021-07-12 21:19:07 -0700 | [diff] [blame] | 198 | // Returns the source node index for each channel in a config. |
| 199 | std::vector<size_t> SourceNodeIndex(const Configuration *config); |
| 200 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 201 | // Returns the all nodes that are logging timestamps on our node. |
| 202 | std::vector<const Node *> TimestampNodes(const Configuration *config, |
| 203 | const Node *my_node); |
| 204 | |
Austin Schuh | d2e2f6a | 2021-02-07 20:46:16 -0800 | [diff] [blame] | 205 | // Returns the application for the provided node and name if it exists, or |
| 206 | // nullptr if it does not exist on this node. |
| 207 | const Application *GetApplication(const Configuration *config, |
| 208 | const Node *my_node, |
| 209 | std::string_view application_name); |
| 210 | |
Austin Schuh | c41fa3c | 2021-10-16 14:35:35 -0700 | [diff] [blame] | 211 | // Returns true if the provided application should start on the provided node. |
| 212 | bool ApplicationShouldStart(const Configuration *config, const Node *my_node, |
| 213 | const Application *application); |
| 214 | |
Austin Schuh | fb37c61 | 2022-08-11 15:24:51 -0700 | [diff] [blame] | 215 | // Returns the number of messages in the queue. |
Austin Schuh | 83cbb1e | 2023-06-23 12:59:02 -0700 | [diff] [blame] | 216 | size_t QueueSize(const Configuration *config, const Channel *channel); |
| 217 | size_t QueueSize(size_t frequency, |
| 218 | std::chrono::nanoseconds channel_storage_duration); |
Austin Schuh | fb37c61 | 2022-08-11 15:24:51 -0700 | [diff] [blame] | 219 | |
| 220 | // Returns the number of scratch buffers in the queue. |
| 221 | int QueueScratchBufferSize(const Channel *channel); |
| 222 | |
Nathan Leong | 307c969 | 2022-10-08 15:25:03 -0700 | [diff] [blame] | 223 | // Searches through configurations for schemas that include a certain type. |
| 224 | const reflection::Schema *GetSchema(const Configuration *config, |
| 225 | std::string_view schema_type); |
| 226 | |
| 227 | // GetSchema template |
| 228 | template <typename T> |
| 229 | const reflection::Schema *GetSchema(const Configuration *config) { |
| 230 | return GetSchema(config, T::GetFullyQualifiedName()); |
| 231 | } |
| 232 | |
| 233 | // Copy schema reflection into detached flatbuffer |
| 234 | std::optional<FlatbufferDetachedBuffer<reflection::Schema>> |
| 235 | GetSchemaDetachedBuffer(const Configuration *config, |
| 236 | std::string_view schema_type); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 237 | |
Austin Schuh | fff9c3a | 2023-06-16 18:48:23 -0700 | [diff] [blame] | 238 | // Returns the storage duration for a channel. |
| 239 | std::chrono::nanoseconds ChannelStorageDuration(const Configuration *config, |
| 240 | const Channel *channel); |
| 241 | |
James Kuszmaul | 741a4d0 | 2023-01-05 14:59:21 -0800 | [diff] [blame] | 242 | // Adds the specified channel to the config and returns the new, merged, config. |
| 243 | // The channel name is derived from the specified name, the type and schema from |
| 244 | // the provided schema, the source node from the specified node, and all other |
| 245 | // fields (e.g., frequency) will be derived from the overrides parameter. |
| 246 | aos::FlatbufferDetachedBuffer<Configuration> AddChannelToConfiguration( |
| 247 | const Configuration *config, std::string_view name, |
| 248 | aos::FlatbufferVector<reflection::Schema> schema, |
| 249 | const aos::Node *source_node = nullptr, ChannelT overrides = {}); |
| 250 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 251 | } // namespace configuration |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 252 | |
| 253 | // Compare and equality operators for Channel. Note: these only check the name |
| 254 | // and type for equality. |
| 255 | bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 256 | const FlatbufferDetachedBuffer<Channel> &rhs); |
| 257 | bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 258 | const FlatbufferDetachedBuffer<Channel> &rhs); |
Nathan Leong | 307c969 | 2022-10-08 15:25:03 -0700 | [diff] [blame] | 259 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 260 | } // namespace aos |
| 261 | |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 262 | #endif // AOS_CONFIGURATION_H_ |