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 | |
| 4 | #include <stdint.h> |
| 5 | #include <sys/socket.h> |
| 6 | #include <netinet/in.h> |
| 7 | #include <arpa/inet.h> |
| 8 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 9 | #include <string_view> |
| 10 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 11 | #include "aos/configuration_generated.h" |
| 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 | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 23 | const std::string_view path); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 24 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | // Sorts and merges entries in a config. |
| 26 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 27 | const Flatbuffer<Configuration> &config); |
| 28 | |
| 29 | // Adds schema definitions to a sorted and merged config from the provided |
| 30 | // schema list. |
| 31 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 32 | const Flatbuffer<Configuration> &config, |
| 33 | const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas); |
| 34 | |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 35 | // Merges a configuration json snippet into the provided configuration and |
| 36 | // returns the modified config. |
| 37 | FlatbufferDetachedBuffer<Configuration> MergeWithConfig( |
| 38 | const Configuration *config, std::string_view json); |
| 39 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 40 | // Returns the resolved location for a name, type, and application name. Returns |
| 41 | // nullptr if none is found. |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 42 | // |
| 43 | // If the application name is empty, it is ignored. Maps are processed in |
| 44 | // reverse order, and application specific first. |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 45 | const Channel *GetChannel(const 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 | inline const Channel *GetChannel(const Flatbuffer<Configuration> &config, |
| 51 | const std::string_view name, |
| 52 | const std::string_view type, |
| 53 | const std::string_view application_name, |
| 54 | const Node *node) { |
| 55 | return GetChannel(&config.message(), name, type, application_name, node); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 56 | } |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 57 | // Convenience wrapper for getting a channel from a specified config if you |
| 58 | // already have the name/type in a Channel object--this is useful if you Channel |
| 59 | // object you have does not point to memory within config. |
| 60 | inline const Channel *GetChannel(const Configuration *config, |
| 61 | const Channel *channel, |
| 62 | const std::string_view application_name, |
| 63 | const Node *node) { |
| 64 | return GetChannel(config, channel->name()->string_view(), |
| 65 | channel->type()->string_view(), application_name, node); |
| 66 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 67 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 68 | // Returns the channel index (or dies) of channel in the provided config. |
| 69 | size_t ChannelIndex(const Configuration *config, const Channel *channel); |
| 70 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 71 | // Returns the Node out of the config with the matching name, or nullptr if it |
| 72 | // can't be found. |
| 73 | const Node *GetNode(const Configuration *config, std::string_view name); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 74 | const Node *GetNode(const Configuration *config, const Node *node); |
| 75 | // Returns a matching node, or nullptr if the provided node is nullptr and we |
| 76 | // are in a single node world. |
| 77 | const Node *GetNodeOrDie(const Configuration *config, const Node *node); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 78 | // Returns the Node out of the configuration which matches our hostname. |
| 79 | // CHECKs if it can't be found. |
| 80 | const Node *GetMyNode(const Configuration *config); |
| 81 | const Node *GetNodeFromHostname(const Configuration *config, |
| 82 | std::string_view name); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 83 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 84 | // Returns a vector of the nodes in the config. (nullptr is considered the node |
| 85 | // in a single node world.) |
| 86 | std::vector<const Node *> GetNodes(const Configuration *config); |
| 87 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 88 | // Returns the node index for a node. Note: will be faster if node is a pointer |
| 89 | // to a node in config, but is not required. |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 90 | int GetNodeIndex(const Configuration *config, const Node *node); |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 91 | int GetNodeIndex(const Configuration *config, std::string_view name); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 92 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 93 | // Returns true if we are running in a multinode configuration. |
| 94 | bool MultiNode(const Configuration *config); |
| 95 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 96 | // Returns true if the provided channel is sendable on the provided node. |
| 97 | bool ChannelIsSendableOnNode(const Channel *channel, const Node *node); |
| 98 | // Returns true if the provided channel is able to be watched or fetched on the |
| 99 | // provided node. |
| 100 | bool ChannelIsReadableOnNode(const Channel *channel, const Node *node); |
| 101 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 102 | // Returns true if the message is supposed to be logged on this node. |
| 103 | bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node); |
| 104 | |
| 105 | const Connection *ConnectionToNode(const Channel *channel, const Node *node); |
| 106 | // Returns true if the delivery timestamps are supposed to be logged on this |
| 107 | // node. |
| 108 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel, |
| 109 | const Node *node, |
| 110 | const Node *logger_node); |
| 111 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection, |
| 112 | const Node *node); |
| 113 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 114 | // Prints a channel to json, but without the schema. |
| 115 | std::string CleanedChannelToString(const Channel *channel); |
| 116 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 117 | // Returns the node names that this node should be forwarding to. |
| 118 | std::vector<std::string_view> DestinationNodeNames(const Configuration *config, |
| 119 | const Node *my_node); |
| 120 | |
| 121 | // Returns the node names that this node should be receiving messages from. |
| 122 | std::vector<std::string_view> SourceNodeNames(const Configuration *config, |
| 123 | const Node *my_node); |
| 124 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 125 | // TODO(austin): GetSchema<T>(const Flatbuffer<Configuration> &config); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 126 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 127 | } // namespace configuration |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 128 | |
| 129 | // Compare and equality operators for Channel. Note: these only check the name |
| 130 | // and type for equality. |
| 131 | bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 132 | const FlatbufferDetachedBuffer<Channel> &rhs); |
| 133 | bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 134 | const FlatbufferDetachedBuffer<Channel> &rhs); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 135 | } // namespace aos |
| 136 | |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 137 | #endif // AOS_CONFIGURATION_H_ |