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 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 8 | #include <cstdint> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 9 | #include <string_view> |
| 10 | |
Austin Schuh | b807581 | 2020-10-19 09:36:49 -0700 | [diff] [blame] | 11 | #include "aos/configuration_generated.h" // IWYU pragma: export |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 12 | #include "aos/flatbuffers.h" |
| 13 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 14 | namespace aos { |
| 15 | |
| 16 | // Holds global configuration data. All of the functions are safe to call |
| 17 | // from wherever. |
| 18 | namespace configuration { |
| 19 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 20 | // Reads a json configuration. This includes all imports and merges. Note: |
| 21 | // duplicate imports will result in a CHECK. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 22 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 23 | const std::string_view path, |
| 24 | const std::vector<std::string_view> &extra_import_paths = {}); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 25 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 26 | // Sorts and merges entries in a config. |
| 27 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 28 | const Flatbuffer<Configuration> &config); |
| 29 | |
| 30 | // Adds schema definitions to a sorted and merged config from the provided |
| 31 | // schema list. |
| 32 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 33 | const Flatbuffer<Configuration> &config, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 34 | const std::vector<aos::FlatbufferVector<reflection::Schema>> &schemas); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 35 | |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 36 | // Merges a configuration json snippet into the provided configuration and |
| 37 | // returns the modified config. |
| 38 | FlatbufferDetachedBuffer<Configuration> MergeWithConfig( |
| 39 | const Configuration *config, std::string_view json); |
Brian Silverman | 24f5aa8 | 2020-06-23 16:21:28 -0700 | [diff] [blame] | 40 | FlatbufferDetachedBuffer<Configuration> MergeWithConfig( |
| 41 | const Configuration *config, const Flatbuffer<Configuration> &addition); |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 42 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 43 | // Adds the list of schemas to the provide config json. This should mostly be |
| 44 | // used for testing and in conjunction with MergeWithConfig. |
| 45 | FlatbufferDetachedBuffer<aos::Configuration> AddSchema( |
| 46 | std::string_view json, |
| 47 | const std::vector<FlatbufferVector<reflection::Schema>> &schemas); |
| 48 | |
Austin Schuh | 681a247 | 2020-12-31 23:55:40 -0800 | [diff] [blame] | 49 | // Returns the resolved Channel for a name, type, and application name. Returns |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 50 | // nullptr if none is found. |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 51 | // |
| 52 | // If the application name is empty, it is ignored. Maps are processed in |
| 53 | // reverse order, and application specific first. |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 54 | // |
| 55 | // The config should already be fully merged and sorted (as produced by |
| 56 | // MergeConfiguration() or any of the associated functions). |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 57 | const 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 Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 61 | const Node *node, bool quiet = false); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 62 | inline 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 Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 68 | } |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 69 | template <typename T> |
| 70 | inline 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 Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 77 | // 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. |
| 80 | inline 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 Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 87 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 88 | // Returns the channel index (or dies) of channel in the provided config. |
| 89 | size_t ChannelIndex(const Configuration *config, const Channel *channel); |
| 90 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 91 | // Returns the Node out of the config with the matching name, or nullptr if it |
| 92 | // can't be found. |
| 93 | const Node *GetNode(const Configuration *config, std::string_view name); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 94 | const Node *GetNode(const Configuration *config, const Node *node); |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 95 | // Returns the node with the provided index. This works in both a single and |
| 96 | // multi-node world. |
| 97 | const Node *GetNode(const Configuration *config, size_t node_index); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 98 | // Returns a matching node, or nullptr if the provided node is nullptr and we |
| 99 | // are in a single node world. |
| 100 | const Node *GetNodeOrDie(const Configuration *config, const Node *node); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 101 | // Returns the Node out of the configuration which matches our hostname. |
| 102 | // CHECKs if it can't be found. |
| 103 | const Node *GetMyNode(const Configuration *config); |
| 104 | const Node *GetNodeFromHostname(const Configuration *config, |
| 105 | std::string_view name); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 106 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 107 | // Returns a vector of the nodes in the config. (nullptr is considered the node |
| 108 | // in a single node world.) |
| 109 | std::vector<const Node *> GetNodes(const Configuration *config); |
| 110 | |
Austin Schuh | 6546533 | 2020-11-05 17:36:53 -0800 | [diff] [blame] | 111 | // 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. |
| 113 | std::vector<const Node *> GetNodesWithTag(const Configuration *config, |
| 114 | std::string_view tag); |
| 115 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 116 | // 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 Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 118 | int GetNodeIndex(const Configuration *config, const Node *node); |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 119 | int GetNodeIndex(const Configuration *config, std::string_view name); |
Austin Schuh | 681a247 | 2020-12-31 23:55:40 -0800 | [diff] [blame] | 120 | // Returns the number of nodes. |
| 121 | size_t NodesCount(const Configuration *config); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 122 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 123 | // Returns true if we are running in a multinode configuration. |
| 124 | bool MultiNode(const Configuration *config); |
| 125 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 126 | // Returns true if the provided channel is sendable on the provided node. |
| 127 | bool 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. |
| 130 | bool ChannelIsReadableOnNode(const Channel *channel, const Node *node); |
| 131 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 132 | // Returns true if the message is supposed to be logged on this node. |
| 133 | bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node); |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 134 | bool ChannelMessageIsLoggedOnNode(const Channel *channel, |
| 135 | std::string_view node_name); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 136 | |
| 137 | const Connection *ConnectionToNode(const Channel *channel, const Node *node); |
| 138 | // Returns true if the delivery timestamps are supposed to be logged on this |
| 139 | // node. |
| 140 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel, |
| 141 | const Node *node, |
| 142 | const Node *logger_node); |
| 143 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection, |
| 144 | const Node *node); |
| 145 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 146 | // Prints a channel to json, but without the schema. |
| 147 | std::string CleanedChannelToString(const Channel *channel); |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 148 | // Prints out a channel to json, but only with the name and type. |
| 149 | std::string StrippedChannelToString(const Channel *channel); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 150 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 151 | // Returns the node names that this node should be forwarding to. |
| 152 | std::vector<std::string_view> DestinationNodeNames(const Configuration *config, |
| 153 | const Node *my_node); |
| 154 | |
| 155 | // Returns the node names that this node should be receiving messages from. |
| 156 | std::vector<std::string_view> SourceNodeNames(const Configuration *config, |
| 157 | const Node *my_node); |
| 158 | |
Austin Schuh | fc7b6a0 | 2021-07-12 21:19:07 -0700 | [diff] [blame] | 159 | // Returns the source node index for each channel in a config. |
| 160 | std::vector<size_t> SourceNodeIndex(const Configuration *config); |
| 161 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 162 | // Returns the all nodes that are logging timestamps on our node. |
| 163 | std::vector<const Node *> TimestampNodes(const Configuration *config, |
| 164 | const Node *my_node); |
| 165 | |
Austin Schuh | d2e2f6a | 2021-02-07 20:46:16 -0800 | [diff] [blame] | 166 | // Returns the application for the provided node and name if it exists, or |
| 167 | // nullptr if it does not exist on this node. |
| 168 | const Application *GetApplication(const Configuration *config, |
| 169 | const Node *my_node, |
| 170 | std::string_view application_name); |
| 171 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 172 | // TODO(austin): GetSchema<T>(const Flatbuffer<Configuration> &config); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 173 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 174 | } // namespace configuration |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 175 | |
| 176 | // Compare and equality operators for Channel. Note: these only check the name |
| 177 | // and type for equality. |
| 178 | bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 179 | const FlatbufferDetachedBuffer<Channel> &rhs); |
| 180 | bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 181 | const FlatbufferDetachedBuffer<Channel> &rhs); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 182 | } // namespace aos |
| 183 | |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 184 | #endif // AOS_CONFIGURATION_H_ |