John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 1 | #include "aos/configuration.h" |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 2 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 3 | #include <arpa/inet.h> |
| 4 | #include <ifaddrs.h> |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 5 | #include <netinet/in.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <sys/types.h> |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 9 | #include <unistd.h> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 10 | |
| 11 | #include <set> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 12 | #include <string_view> |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 13 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 14 | #include "absl/container/btree_set.h" |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 15 | #include "absl/strings/str_cat.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 16 | #include "aos/configuration_generated.h" |
| 17 | #include "aos/flatbuffer_merge.h" |
| 18 | #include "aos/json_to_flatbuffer.h" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 19 | #include "aos/network/team_number.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 20 | #include "aos/unique_malloc_ptr.h" |
| 21 | #include "aos/util/file.h" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 22 | #include "gflags/gflags.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 23 | #include "glog/logging.h" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 24 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 25 | namespace aos { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 26 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 27 | // Define the compare and equal operators for Channel and Application so we can |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 28 | // insert them in the btree below. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 29 | bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 30 | const FlatbufferDetachedBuffer<Channel> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 31 | int name_compare = lhs.message().name()->string_view().compare( |
| 32 | rhs.message().name()->string_view()); |
| 33 | if (name_compare == 0) { |
| 34 | return lhs.message().type()->string_view() < |
| 35 | rhs.message().type()->string_view(); |
| 36 | } else if (name_compare < 0) { |
| 37 | return true; |
| 38 | } else { |
| 39 | return false; |
| 40 | } |
| 41 | } |
| 42 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 43 | bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 44 | const FlatbufferDetachedBuffer<Channel> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 45 | return lhs.message().name()->string_view() == |
| 46 | rhs.message().name()->string_view() && |
| 47 | lhs.message().type()->string_view() == |
| 48 | rhs.message().type()->string_view(); |
| 49 | } |
| 50 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 51 | bool operator==(const FlatbufferDetachedBuffer<Application> &lhs, |
| 52 | const FlatbufferDetachedBuffer<Application> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 53 | return lhs.message().name()->string_view() == |
| 54 | rhs.message().name()->string_view(); |
| 55 | } |
| 56 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 57 | bool operator<(const FlatbufferDetachedBuffer<Application> &lhs, |
| 58 | const FlatbufferDetachedBuffer<Application> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 59 | return lhs.message().name()->string_view() < |
| 60 | rhs.message().name()->string_view(); |
| 61 | } |
| 62 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 63 | bool operator==(const FlatbufferDetachedBuffer<Node> &lhs, |
| 64 | const FlatbufferDetachedBuffer<Node> &rhs) { |
| 65 | return lhs.message().name()->string_view() == |
| 66 | rhs.message().name()->string_view(); |
| 67 | } |
| 68 | |
| 69 | bool operator<(const FlatbufferDetachedBuffer<Node> &lhs, |
| 70 | const FlatbufferDetachedBuffer<Node> &rhs) { |
| 71 | return lhs.message().name()->string_view() < |
| 72 | rhs.message().name()->string_view(); |
| 73 | } |
| 74 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 75 | namespace configuration { |
| 76 | namespace { |
| 77 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 78 | // Extracts the folder part of a path. Returns ./ if there is no path. |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 79 | std::string_view ExtractFolder(const std::string_view filename) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 80 | auto last_slash_pos = filename.find_last_of("/\\"); |
| 81 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 82 | return last_slash_pos == std::string_view::npos |
| 83 | ? std::string_view("./") |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 84 | : filename.substr(0, last_slash_pos + 1); |
| 85 | } |
| 86 | |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 87 | std::string AbsolutePath(const std::string_view filename) { |
| 88 | // Uses an std::string so that we know the input will be null-terminated. |
| 89 | const std::string terminated_file(filename); |
| 90 | char buffer[PATH_MAX]; |
| 91 | PCHECK(NULL != realpath(terminated_file.c_str(), buffer)); |
| 92 | return buffer; |
| 93 | } |
| 94 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 95 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 96 | const std::string_view path, absl::btree_set<std::string> *visited_paths, |
| 97 | const std::vector<std::string_view> &extra_import_paths) { |
| 98 | std::string raw_path(path); |
| 99 | if (!util::PathExists(path)) { |
| 100 | const bool path_is_absolute = path.size() > 0 && path[0] == '/'; |
| 101 | if (path_is_absolute) { |
| 102 | CHECK(extra_import_paths.empty()) |
| 103 | << "Can't specify extra import paths if attempting to read a config " |
| 104 | "file from an absolute path (path is " |
| 105 | << path << ")."; |
| 106 | } |
| 107 | |
| 108 | bool found_path = false; |
| 109 | for (const auto &import_path : extra_import_paths) { |
| 110 | raw_path = std::string(import_path) + "/" + std::string(path); |
| 111 | if (util::PathExists(raw_path)) { |
| 112 | found_path = true; |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | CHECK(found_path) << ": Failed to find file " << path << "."; |
| 117 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 118 | flatbuffers::DetachedBuffer buffer = JsonToFlatbuffer( |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 119 | util::ReadFileToStringOrDie(raw_path), ConfigurationTypeTable()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 120 | |
| 121 | CHECK_GT(buffer.size(), 0u) << ": Failed to parse JSON file"; |
| 122 | |
| 123 | FlatbufferDetachedBuffer<Configuration> config(std::move(buffer)); |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 124 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 125 | // Depth first. Take the following example: |
| 126 | // |
| 127 | // config1.json: |
| 128 | // { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 129 | // "channels": [ |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 130 | // { |
| 131 | // "name": "/foo", |
| 132 | // "type": ".aos.bar", |
| 133 | // "max_size": 5 |
| 134 | // } |
| 135 | // ], |
| 136 | // "imports": [ |
| 137 | // "config2.json", |
| 138 | // ] |
| 139 | // } |
| 140 | // |
| 141 | // config2.json: |
| 142 | // { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 143 | // "channels": [ |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 144 | // { |
| 145 | // "name": "/foo", |
| 146 | // "type": ".aos.bar", |
| 147 | // "max_size": 7 |
| 148 | // } |
| 149 | // ], |
| 150 | // } |
| 151 | // |
| 152 | // We want the main config (config1.json) to be able to override the imported |
| 153 | // config. That means that it needs to be merged into the imported configs, |
| 154 | // not the other way around. |
| 155 | |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 156 | const std::string absolute_path = AbsolutePath(raw_path); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 157 | // Track that we have seen this file before recursing. |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 158 | if (!visited_paths->insert(absolute_path).second) { |
| 159 | for (const auto &visited_path : *visited_paths) { |
| 160 | LOG(INFO) << "Already visited: " << visited_path; |
| 161 | } |
| 162 | LOG(FATAL) |
| 163 | << "Already imported " << path << " (i.e. " << absolute_path |
| 164 | << "). See above for the files that have already been processed."; |
| 165 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 166 | |
| 167 | if (config.message().has_imports()) { |
| 168 | // Capture the imports. |
| 169 | const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *v = |
| 170 | config.message().imports(); |
| 171 | |
| 172 | // And then wipe them. This gets GCed when we merge later. |
| 173 | config.mutable_message()->clear_imports(); |
| 174 | |
| 175 | // Start with an empty configuration to merge into. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 176 | FlatbufferDetachedBuffer<Configuration> merged_config = |
| 177 | FlatbufferDetachedBuffer<Configuration>::Empty(); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 178 | |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 179 | const std::string path_folder(ExtractFolder(path)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 180 | for (const flatbuffers::String *str : *v) { |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 181 | const std::string included_config = |
| 182 | path_folder + "/" + std::string(str->string_view()); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 183 | |
| 184 | // And them merge everything in. |
| 185 | merged_config = MergeFlatBuffers( |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 186 | merged_config, |
| 187 | ReadConfig(included_config, visited_paths, extra_import_paths)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // Finally, merge this file in. |
| 191 | config = MergeFlatBuffers(merged_config, config); |
| 192 | } |
| 193 | return config; |
| 194 | } |
| 195 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 196 | // Compares (c < p) a channel, and a name, type tuple. |
| 197 | bool CompareChannels(const Channel *c, |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 198 | ::std::pair<std::string_view, std::string_view> p) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 199 | int name_compare = c->name()->string_view().compare(p.first); |
| 200 | if (name_compare == 0) { |
| 201 | return c->type()->string_view() < p.second; |
| 202 | } else if (name_compare < 0) { |
| 203 | return true; |
| 204 | } else { |
| 205 | return false; |
| 206 | } |
| 207 | }; |
| 208 | |
| 209 | // Compares for equality (c == p) a channel, and a name, type tuple. |
| 210 | bool EqualsChannels(const Channel *c, |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 211 | ::std::pair<std::string_view, std::string_view> p) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 212 | return c->name()->string_view() == p.first && |
| 213 | c->type()->string_view() == p.second; |
| 214 | } |
| 215 | |
| 216 | // Compares (c < p) an application, and a name; |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 217 | bool CompareApplications(const Application *a, std::string_view name) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 218 | return a->name()->string_view() < name; |
| 219 | }; |
| 220 | |
| 221 | // Compares for equality (c == p) an application, and a name; |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 222 | bool EqualsApplications(const Application *a, std::string_view name) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 223 | return a->name()->string_view() == name; |
| 224 | } |
| 225 | |
| 226 | // Maps name for the provided maps. Modifies name. |
| 227 | void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps, |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 228 | std::string *name, std::string_view type, const Node *node) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 229 | // For the same reason we merge configs in reverse order, we want to process |
| 230 | // maps in reverse order. That lets the outer config overwrite channels from |
| 231 | // the inner configs. |
| 232 | for (auto i = maps->rbegin(); i != maps->rend(); ++i) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 233 | if (!i->has_match() || !i->match()->has_name()) { |
| 234 | continue; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 235 | } |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 236 | if (!i->has_rename() || !i->rename()->has_name()) { |
| 237 | continue; |
| 238 | } |
| 239 | |
| 240 | // Handle normal maps (now that we know that match and rename are filled |
| 241 | // out). |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 242 | const std::string_view match_name = i->match()->name()->string_view(); |
| 243 | if (match_name != *name) { |
| 244 | if (match_name.back() == '*' && |
| 245 | std::string_view(*name).substr( |
| 246 | 0, std::min(name->size(), match_name.size() - 1)) == |
| 247 | match_name.substr(0, match_name.size() - 1)) { |
| 248 | CHECK_EQ(match_name.find('*'), match_name.size() - 1); |
| 249 | } else { |
| 250 | continue; |
| 251 | } |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // Handle type specific maps. |
| 255 | if (i->match()->has_type() && i->match()->type()->string_view() != type) { |
| 256 | continue; |
| 257 | } |
| 258 | |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 259 | // Now handle node specific maps. |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 260 | if (node != nullptr && i->match()->has_source_node() && |
| 261 | i->match()->source_node()->string_view() != |
| 262 | node->name()->string_view()) { |
| 263 | continue; |
| 264 | } |
| 265 | |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 266 | std::string new_name(i->rename()->name()->string_view()); |
| 267 | if (match_name.back() == '*') { |
| 268 | new_name += std::string(name->substr(match_name.size() - 1)); |
| 269 | } |
| 270 | VLOG(1) << "Renamed \"" << *name << "\" to \"" << new_name << "\""; |
| 271 | *name = std::move(new_name); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | } // namespace |
| 276 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 277 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 278 | const Flatbuffer<Configuration> &config) { |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 279 | // auto_merge_config will contain all the fields of the Configuration that are |
| 280 | // to be passed through unmodified to the result of MergeConfiguration(). |
| 281 | // In the processing below, we mutate auto_merge_config to remove any fields |
| 282 | // which we do need to alter (hence why we can't use the input config |
| 283 | // directly), and then merge auto_merge_config back in at the end. |
| 284 | aos::FlatbufferDetachedBuffer<aos::Configuration> auto_merge_config = |
| 285 | aos::CopyFlatBuffer(&config.message()); |
| 286 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 287 | // Store all the channels in a sorted set. This lets us track channels we |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 288 | // have seen before and merge the updates in. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 289 | absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 290 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 291 | if (config.message().has_channels()) { |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 292 | auto_merge_config.mutable_message()->clear_channels(); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 293 | for (const Channel *c : *config.message().channels()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 294 | // Ignore malformed entries. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 295 | if (!c->has_name()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 296 | continue; |
| 297 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 298 | if (!c->has_type()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 299 | continue; |
| 300 | } |
| 301 | |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 302 | CHECK_EQ(c->read_method() == ReadMethod::PIN, c->num_readers() != 0) |
| 303 | << ": num_readers may be set if and only if read_method is PIN," |
| 304 | " if you want 0 readers do not set PIN: " |
| 305 | << CleanedChannelToString(c); |
| 306 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 307 | // Attempt to insert the channel. |
| 308 | auto result = channels.insert(CopyFlatBuffer(c)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 309 | if (!result.second) { |
| 310 | // Already there, so merge the new table into the original. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 311 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(c)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // Now repeat this for the application list. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 317 | absl::btree_set<FlatbufferDetachedBuffer<Application>> applications; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 318 | if (config.message().has_applications()) { |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 319 | auto_merge_config.mutable_message()->clear_applications(); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 320 | for (const Application *a : *config.message().applications()) { |
| 321 | if (!a->has_name()) { |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | auto result = applications.insert(CopyFlatBuffer(a)); |
| 326 | if (!result.second) { |
| 327 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(a)); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 332 | // Now repeat this for the node list. |
| 333 | absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes; |
| 334 | if (config.message().has_nodes()) { |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 335 | auto_merge_config.mutable_message()->clear_nodes(); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 336 | for (const Node *n : *config.message().nodes()) { |
| 337 | if (!n->has_name()) { |
| 338 | continue; |
| 339 | } |
| 340 | |
| 341 | auto result = nodes.insert(CopyFlatBuffer(n)); |
| 342 | if (!result.second) { |
| 343 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(n)); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 348 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 349 | fbb.ForceDefaults(true); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 350 | |
| 351 | // Start by building the vectors. They need to come before the final table. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 352 | // Channels |
| 353 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 354 | channels_offset; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 355 | { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 356 | ::std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 357 | for (const FlatbufferDetachedBuffer<Channel> &c : channels) { |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 358 | channel_offsets.emplace_back(CopyFlatBuffer<Channel>(&c.message(), &fbb)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 359 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 360 | channels_offset = fbb.CreateVector(channel_offsets); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // Applications |
| 364 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 365 | applications_offset; |
| 366 | { |
| 367 | ::std::vector<flatbuffers::Offset<Application>> applications_offsets; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 368 | for (const FlatbufferDetachedBuffer<Application> &a : applications) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 369 | applications_offsets.emplace_back( |
| 370 | CopyFlatBuffer<Application>(&a.message(), &fbb)); |
| 371 | } |
| 372 | applications_offset = fbb.CreateVector(applications_offsets); |
| 373 | } |
| 374 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 375 | // Nodes |
| 376 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 377 | nodes_offset; |
| 378 | { |
| 379 | ::std::vector<flatbuffers::Offset<Node>> node_offsets; |
| 380 | for (const FlatbufferDetachedBuffer<Node> &n : nodes) { |
| 381 | node_offsets.emplace_back(CopyFlatBuffer<Node>(&n.message(), &fbb)); |
| 382 | } |
| 383 | nodes_offset = fbb.CreateVector(node_offsets); |
| 384 | } |
| 385 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 386 | // And then build a Configuration with them all. |
| 387 | ConfigurationBuilder configuration_builder(fbb); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 388 | configuration_builder.add_channels(channels_offset); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 389 | if (config.message().has_applications()) { |
| 390 | configuration_builder.add_applications(applications_offset); |
| 391 | } |
| 392 | if (config.message().has_nodes()) { |
| 393 | configuration_builder.add_nodes(nodes_offset); |
| 394 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 395 | |
| 396 | fbb.Finish(configuration_builder.Finish()); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 397 | |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 398 | aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config( |
| 399 | fbb.Release()); |
| 400 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 401 | // Now, validate that if there is a node list, every channel has a source |
| 402 | // node. |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 403 | FlatbufferDetachedBuffer<Configuration> result = |
| 404 | MergeFlatBuffers(modified_config, auto_merge_config); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 405 | |
| 406 | // Check that if there is a node list, all the source nodes are filled out and |
| 407 | // valid, and all the destination nodes are valid (and not the source). This |
| 408 | // is a basic consistency check. |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 409 | if (result.message().has_channels()) { |
| 410 | for (const Channel *c : *result.message().channels()) { |
| 411 | if (c->name()->string_view().back() == '/') { |
| 412 | LOG(FATAL) << "Channel names can't end with '/'"; |
| 413 | } |
| 414 | if(c->name()->string_view().find("//")!= std::string_view::npos) { |
| 415 | LOG(FATAL) << ": Invalid channel name " << c->name()->string_view() |
| 416 | << ", can't use //."; |
| 417 | } |
| 418 | for (const char data : c->name()->string_view()) { |
| 419 | if (data >= '0' && data <= '9') { |
| 420 | continue; |
| 421 | } |
| 422 | if (data >= 'a' && data <= 'z') { |
| 423 | continue; |
| 424 | } |
| 425 | if (data >= 'A' && data <= 'Z') { |
| 426 | continue; |
| 427 | } |
| 428 | if (data == '-' || data == '_' || data == '/') { |
| 429 | continue; |
| 430 | } |
| 431 | LOG(FATAL) << "Invalid channel name " << c->name()->string_view() |
| 432 | << ", can only use [-a-zA-Z0-9_/]"; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if (result.message().has_nodes() && result.message().has_channels()) { |
| 438 | for (const Channel *c : *result.message().channels()) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 439 | CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c) |
| 440 | << " is missing \"source_node\""; |
| 441 | CHECK(GetNode(&result.message(), c->source_node()->string_view()) != |
| 442 | nullptr) |
| 443 | << ": Channel " << FlatbufferToJson(c) |
| 444 | << " has an unknown \"source_node\""; |
| 445 | |
| 446 | if (c->has_destination_nodes()) { |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 447 | for (const Connection *connection : *c->destination_nodes()) { |
| 448 | CHECK(connection->has_name()); |
| 449 | CHECK(GetNode(&result.message(), connection->name()->string_view()) != |
| 450 | nullptr) |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 451 | << ": Channel " << FlatbufferToJson(c) |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 452 | << " has an unknown \"destination_nodes\" " |
| 453 | << connection->name()->string_view(); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 454 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 455 | switch (connection->timestamp_logger()) { |
| 456 | case LoggerConfig::LOCAL_LOGGER: |
| 457 | case LoggerConfig::NOT_LOGGED: |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 458 | CHECK(!connection->has_timestamp_logger_nodes()); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 459 | break; |
| 460 | case LoggerConfig::REMOTE_LOGGER: |
| 461 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 462 | CHECK(connection->has_timestamp_logger_nodes()); |
| 463 | CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u); |
| 464 | for (const flatbuffers::String *timestamp_logger_node : |
| 465 | *connection->timestamp_logger_nodes()) { |
| 466 | CHECK(GetNode(&result.message(), |
| 467 | timestamp_logger_node->string_view()) != nullptr) |
| 468 | << ": Channel " << FlatbufferToJson(c) |
| 469 | << " has an unknown \"timestamp_logger_node\"" |
| 470 | << connection->name()->string_view(); |
| 471 | } |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 472 | break; |
| 473 | } |
| 474 | |
| 475 | CHECK_NE(connection->name()->string_view(), |
| 476 | c->source_node()->string_view()) |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 477 | << ": Channel " << FlatbufferToJson(c) |
| 478 | << " is forwarding data to itself"; |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | return result; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 487 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 488 | const std::string_view path, |
| 489 | const std::vector<std::string_view> &import_paths) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 490 | // We only want to read a file once. So track the visited files in a set. |
| 491 | absl::btree_set<std::string> visited_paths; |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 492 | return MergeConfiguration(ReadConfig(path, &visited_paths, import_paths)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 495 | FlatbufferDetachedBuffer<Configuration> MergeWithConfig( |
Brian Silverman | 24f5aa8 | 2020-06-23 16:21:28 -0700 | [diff] [blame] | 496 | const Configuration *config, const Flatbuffer<Configuration> &addition) { |
| 497 | return MergeConfiguration(MergeFlatBuffers(config, &addition.message())); |
| 498 | } |
| 499 | |
| 500 | FlatbufferDetachedBuffer<Configuration> MergeWithConfig( |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 501 | const Configuration *config, std::string_view json) { |
| 502 | FlatbufferDetachedBuffer<Configuration> addition = |
| 503 | JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable()); |
| 504 | |
Brian Silverman | 24f5aa8 | 2020-06-23 16:21:28 -0700 | [diff] [blame] | 505 | return MergeWithConfig(config, addition); |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 506 | } |
| 507 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 508 | const Channel *GetChannel(const Configuration *config, std::string_view name, |
| 509 | std::string_view type, |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 510 | std::string_view application_name, const Node *node) { |
| 511 | const std::string_view original_name = name; |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 512 | std::string mutable_name; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 513 | if (node != nullptr) { |
| 514 | VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type |
| 515 | << "\" } on " << aos::FlatbufferToJson(node); |
| 516 | } else { |
| 517 | VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type |
| 518 | << "\" }"; |
| 519 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 520 | |
| 521 | // First handle application specific maps. Only do this if we have a matching |
| 522 | // application name, and it has maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 523 | if (config->has_applications()) { |
| 524 | auto application_iterator = std::lower_bound( |
| 525 | config->applications()->cbegin(), config->applications()->cend(), |
| 526 | application_name, CompareApplications); |
| 527 | if (application_iterator != config->applications()->cend() && |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 528 | EqualsApplications(*application_iterator, application_name)) { |
| 529 | if (application_iterator->has_maps()) { |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 530 | mutable_name = std::string(name); |
| 531 | HandleMaps(application_iterator->maps(), &mutable_name, type, node); |
| 532 | name = std::string_view(mutable_name); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | // Now do global maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 538 | if (config->has_maps()) { |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 539 | mutable_name = std::string(name); |
| 540 | HandleMaps(config->maps(), &mutable_name, type, node); |
| 541 | name = std::string_view(mutable_name); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 544 | if (original_name != name) { |
| 545 | VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \"" |
| 546 | << type << "\" }"; |
| 547 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 548 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 549 | // Then look for the channel. |
| 550 | auto channel_iterator = |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 551 | std::lower_bound(config->channels()->cbegin(), config->channels()->cend(), |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 552 | std::make_pair(name, type), CompareChannels); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 553 | |
| 554 | // Make sure we actually found it, and it matches. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 555 | if (channel_iterator != config->channels()->cend() && |
| 556 | EqualsChannels(*channel_iterator, std::make_pair(name, type))) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 557 | if (VLOG_IS_ON(2)) { |
| 558 | VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator); |
| 559 | } else if (VLOG_IS_ON(1)) { |
| 560 | VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator); |
| 561 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 562 | return *channel_iterator; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 563 | } else { |
| 564 | VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \"" |
| 565 | << type << "\" }"; |
| 566 | return nullptr; |
| 567 | } |
| 568 | } |
| 569 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 570 | size_t ChannelIndex(const Configuration *configuration, |
| 571 | const Channel *channel) { |
| 572 | CHECK(configuration->channels() != nullptr) << ": No channels"; |
| 573 | |
| 574 | auto c = std::find(configuration->channels()->begin(), |
| 575 | configuration->channels()->end(), channel); |
| 576 | CHECK(c != configuration->channels()->end()) |
| 577 | << ": Channel pointer not found in configuration()->channels()"; |
| 578 | |
| 579 | return std::distance(configuration->channels()->begin(), c); |
| 580 | } |
| 581 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 582 | std::string CleanedChannelToString(const Channel *channel) { |
| 583 | FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel); |
| 584 | cleaned_channel.mutable_message()->clear_schema(); |
| 585 | return FlatbufferToJson(cleaned_channel); |
| 586 | } |
| 587 | |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 588 | std::string StrippedChannelToString(const Channel *channel) { |
| 589 | return absl::StrCat("{ \"name\": \"", channel->name()->string_view(), |
| 590 | "\", \"type\": \"", channel->type()->string_view(), |
| 591 | "\" }"); |
| 592 | } |
| 593 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 594 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 595 | const Flatbuffer<Configuration> &config, |
| 596 | const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) { |
| 597 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 598 | fbb.ForceDefaults(true); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 599 | |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 600 | // auto_merge_config will contain all the fields of the Configuration that are |
| 601 | // to be passed through unmodified to the result of MergeConfiguration(). |
| 602 | // In the processing below, we mutate auto_merge_config to remove any fields |
| 603 | // which we do need to alter (hence why we can't use the input config |
| 604 | // directly), and then merge auto_merge_config back in at the end. |
| 605 | aos::FlatbufferDetachedBuffer<aos::Configuration> auto_merge_config = |
| 606 | aos::CopyFlatBuffer(&config.message()); |
| 607 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 608 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 609 | channels_offset; |
| 610 | if (config.message().has_channels()) { |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 611 | auto_merge_config.mutable_message()->clear_channels(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 612 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 613 | for (const Channel *c : *config.message().channels()) { |
| 614 | flatbuffers::FlatBufferBuilder channel_fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 615 | channel_fbb.ForceDefaults(true); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 616 | |
| 617 | // Search for a schema with a matching type. |
| 618 | const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr; |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 619 | for (const aos::FlatbufferString<reflection::Schema> &schema : schemas) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 620 | if (schema.message().root_table() != nullptr) { |
| 621 | if (schema.message().root_table()->name()->string_view() == |
| 622 | c->type()->string_view()) { |
| 623 | found_schema = &schema; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | CHECK(found_schema != nullptr) |
| 629 | << ": Failed to find schema for " << FlatbufferToJson(c); |
| 630 | |
| 631 | // The following is wasteful, but works. |
| 632 | // |
| 633 | // Copy it into a Channel object by creating an object with only the |
| 634 | // schema populated and merge that into the current channel. |
| 635 | flatbuffers::Offset<reflection::Schema> schema_offset = |
| 636 | CopyFlatBuffer<reflection::Schema>(&found_schema->message(), |
| 637 | &channel_fbb); |
| 638 | Channel::Builder channel_builder(channel_fbb); |
| 639 | channel_builder.add_schema(schema_offset); |
| 640 | channel_fbb.Finish(channel_builder.Finish()); |
| 641 | FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer( |
| 642 | channel_fbb.Release()); |
| 643 | |
| 644 | FlatbufferDetachedBuffer<Channel> merged_channel( |
| 645 | MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c))); |
| 646 | |
| 647 | channel_offsets.emplace_back( |
| 648 | CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb)); |
| 649 | } |
| 650 | channels_offset = fbb.CreateVector(channel_offsets); |
| 651 | } |
| 652 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 653 | |
| 654 | // Now insert everything else in unmodified. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 655 | ConfigurationBuilder configuration_builder(fbb); |
| 656 | if (config.message().has_channels()) { |
| 657 | configuration_builder.add_channels(channels_offset); |
| 658 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 659 | fbb.Finish(configuration_builder.Finish()); |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 660 | aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config( |
| 661 | fbb.Release()); |
| 662 | |
| 663 | return MergeFlatBuffers(modified_config, auto_merge_config); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 666 | const Node *GetNodeFromHostname(const Configuration *config, |
| 667 | std::string_view hostname) { |
| 668 | for (const Node *node : *config->nodes()) { |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 669 | if (node->has_hostname() && node->hostname()->string_view() == hostname) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 670 | return node; |
| 671 | } |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 672 | if (node->has_hostnames()) { |
| 673 | for (const auto &candidate : *node->hostnames()) { |
| 674 | if (candidate->string_view() == hostname) { |
| 675 | return node; |
| 676 | } |
| 677 | } |
| 678 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 679 | } |
| 680 | return nullptr; |
| 681 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 682 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 683 | const Node *GetMyNode(const Configuration *config) { |
| 684 | const std::string hostname = (FLAGS_override_hostname.size() > 0) |
| 685 | ? FLAGS_override_hostname |
| 686 | : network::GetHostname(); |
| 687 | const Node *node = GetNodeFromHostname(config, hostname); |
| 688 | if (node != nullptr) return node; |
| 689 | |
| 690 | LOG(FATAL) << "Unknown node for host: " << hostname |
| 691 | << ". Consider using --override_hostname if hostname detection " |
| 692 | "is wrong."; |
| 693 | return nullptr; |
| 694 | } |
| 695 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 696 | const Node *GetNode(const Configuration *config, const Node *node) { |
| 697 | if (!MultiNode(config)) { |
| 698 | CHECK(node == nullptr) << ": Provided a node in a single node world."; |
| 699 | return nullptr; |
| 700 | } else { |
| 701 | CHECK(node != nullptr); |
| 702 | CHECK(node->has_name()); |
| 703 | return GetNode(config, node->name()->string_view()); |
| 704 | } |
| 705 | } |
| 706 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 707 | const Node *GetNode(const Configuration *config, std::string_view name) { |
Austin Schuh | fd96062 | 2020-01-01 13:22:55 -0800 | [diff] [blame] | 708 | CHECK(config->has_nodes()) |
| 709 | << ": Asking for a node from a single node configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 710 | for (const Node *node : *config->nodes()) { |
Austin Schuh | fd96062 | 2020-01-01 13:22:55 -0800 | [diff] [blame] | 711 | CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 712 | if (node->name()->string_view() == name) { |
| 713 | return node; |
| 714 | } |
| 715 | } |
| 716 | return nullptr; |
| 717 | } |
| 718 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 719 | const Node *GetNodeOrDie(const Configuration *config, const Node *node) { |
| 720 | if (!MultiNode(config)) { |
| 721 | CHECK(node == nullptr) << ": Provided a node in a single node world."; |
| 722 | return nullptr; |
| 723 | } else { |
| 724 | const Node *config_node = GetNode(config, node); |
| 725 | if (config_node == nullptr) { |
| 726 | LOG(FATAL) << "Couldn't find node matching " << FlatbufferToJson(node); |
| 727 | } |
| 728 | return config_node; |
| 729 | } |
| 730 | } |
| 731 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 732 | namespace { |
| 733 | int GetNodeIndexFromConfig(const Configuration *config, const Node *node) { |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 734 | int node_index = 0; |
| 735 | for (const Node *iterated_node : *config->nodes()) { |
| 736 | if (iterated_node == node) { |
| 737 | return node_index; |
| 738 | } |
| 739 | ++node_index; |
| 740 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 741 | return -1; |
| 742 | } |
| 743 | } // namespace |
| 744 | |
| 745 | int GetNodeIndex(const Configuration *config, const Node *node) { |
| 746 | if (!MultiNode(config)) { |
| 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | { |
| 751 | int node_index = GetNodeIndexFromConfig(config, node); |
| 752 | if (node_index != -1) { |
| 753 | return node_index; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | const Node *result = GetNode(config, node); |
| 758 | CHECK(result != nullptr); |
| 759 | |
| 760 | { |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 761 | int node_index = GetNodeIndexFromConfig(config, result); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 762 | if (node_index != -1) { |
| 763 | return node_index; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | LOG(FATAL) << "Node " << FlatbufferToJson(node) |
| 768 | << " not found in the configuration."; |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 769 | } |
| 770 | |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 771 | int GetNodeIndex(const Configuration *config, std::string_view name) { |
| 772 | if (!MultiNode(config)) { |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | { |
| 777 | int node_index = 0; |
| 778 | for (const Node *iterated_node : *config->nodes()) { |
| 779 | if (iterated_node->name()->string_view() == name) { |
| 780 | return node_index; |
| 781 | } |
| 782 | ++node_index; |
| 783 | } |
| 784 | } |
| 785 | LOG(FATAL) << "Node " << name << " not found in the configuration."; |
| 786 | } |
| 787 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 788 | std::vector<const Node *> GetNodes(const Configuration *config) { |
| 789 | std::vector<const Node *> nodes; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 790 | if (MultiNode(config)) { |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 791 | for (const Node *node : *config->nodes()) { |
| 792 | nodes.emplace_back(node); |
| 793 | } |
| 794 | } else { |
| 795 | nodes.emplace_back(nullptr); |
| 796 | } |
| 797 | return nodes; |
| 798 | } |
| 799 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 800 | bool MultiNode(const Configuration *config) { return config->has_nodes(); } |
| 801 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 802 | bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 803 | if (node == nullptr) { |
| 804 | return true; |
| 805 | } |
Austin Schuh | 196a445 | 2020-03-15 23:12:03 -0700 | [diff] [blame] | 806 | return (CHECK_NOTNULL(channel)->source_node()->string_view() == |
| 807 | node->name()->string_view()); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 811 | if (node == nullptr) { |
| 812 | return true; |
| 813 | } |
| 814 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 815 | if (channel->source_node()->string_view() == node->name()->string_view()) { |
| 816 | return true; |
| 817 | } |
| 818 | |
| 819 | if (!channel->has_destination_nodes()) { |
| 820 | return false; |
| 821 | } |
| 822 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 823 | for (const Connection *connection : *channel->destination_nodes()) { |
| 824 | CHECK(connection->has_name()); |
| 825 | if (connection->name()->string_view() == node->name()->string_view()) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 826 | return true; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | return false; |
| 831 | } |
| 832 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 833 | bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 834 | switch (channel->logger()) { |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 835 | case LoggerConfig::LOCAL_LOGGER: |
| 836 | if (node == nullptr) { |
| 837 | // Single node world. If there is a local logger, then we want to use |
| 838 | // it. |
| 839 | return true; |
| 840 | } |
| 841 | return channel->source_node()->string_view() == |
| 842 | node->name()->string_view(); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 843 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 844 | CHECK(channel->has_logger_nodes()); |
| 845 | CHECK_GT(channel->logger_nodes()->size(), 0u); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 846 | |
| 847 | if (channel->source_node()->string_view() == |
| 848 | CHECK_NOTNULL(node)->name()->string_view()) { |
| 849 | return true; |
| 850 | } |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 851 | |
| 852 | [[fallthrough]]; |
| 853 | case LoggerConfig::REMOTE_LOGGER: |
| 854 | CHECK(channel->has_logger_nodes()); |
| 855 | CHECK_GT(channel->logger_nodes()->size(), 0u); |
| 856 | for (const flatbuffers::String *logger_node : *channel->logger_nodes()) { |
| 857 | if (logger_node->string_view() == |
| 858 | CHECK_NOTNULL(node)->name()->string_view()) { |
| 859 | return true; |
| 860 | } |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | return false; |
| 864 | case LoggerConfig::NOT_LOGGED: |
| 865 | return false; |
| 866 | } |
| 867 | |
| 868 | LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger()); |
| 869 | } |
| 870 | |
| 871 | const Connection *ConnectionToNode(const Channel *channel, const Node *node) { |
| 872 | if (!channel->has_destination_nodes()) { |
| 873 | return nullptr; |
| 874 | } |
| 875 | for (const Connection *connection : *channel->destination_nodes()) { |
| 876 | if (connection->name()->string_view() == node->name()->string_view()) { |
| 877 | return connection; |
| 878 | } |
| 879 | } |
| 880 | return nullptr; |
| 881 | } |
| 882 | |
| 883 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel, |
| 884 | const Node *node, |
| 885 | const Node *logger_node) { |
| 886 | const Connection *connection = ConnectionToNode(channel, node); |
| 887 | if (connection == nullptr) { |
| 888 | return false; |
| 889 | } |
| 890 | return ConnectionDeliveryTimeIsLoggedOnNode(connection, logger_node); |
| 891 | } |
| 892 | |
| 893 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection, |
| 894 | const Node *node) { |
| 895 | switch (connection->timestamp_logger()) { |
| 896 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 897 | CHECK(connection->has_timestamp_logger_nodes()); |
| 898 | CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 899 | if (connection->name()->string_view() == node->name()->string_view()) { |
| 900 | return true; |
| 901 | } |
| 902 | |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 903 | [[fallthrough]]; |
| 904 | case LoggerConfig::REMOTE_LOGGER: |
| 905 | CHECK(connection->has_timestamp_logger_nodes()); |
| 906 | CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u); |
| 907 | for (const flatbuffers::String *timestamp_logger_node : |
| 908 | *connection->timestamp_logger_nodes()) { |
| 909 | if (timestamp_logger_node->string_view() == |
| 910 | node->name()->string_view()) { |
| 911 | return true; |
| 912 | } |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | return false; |
| 916 | case LoggerConfig::LOCAL_LOGGER: |
| 917 | return connection->name()->string_view() == node->name()->string_view(); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 918 | case LoggerConfig::NOT_LOGGED: |
| 919 | return false; |
| 920 | } |
| 921 | |
| 922 | LOG(FATAL) << "Unknown logger config " |
| 923 | << static_cast<int>(connection->timestamp_logger()); |
| 924 | } |
| 925 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 926 | std::vector<std::string_view> SourceNodeNames(const Configuration *config, |
| 927 | const Node *my_node) { |
| 928 | std::set<std::string_view> result_set; |
| 929 | |
| 930 | for (const Channel *channel : *config->channels()) { |
| 931 | if (channel->has_destination_nodes()) { |
| 932 | for (const Connection *connection : *channel->destination_nodes()) { |
| 933 | if (connection->name()->string_view() == |
| 934 | my_node->name()->string_view()) { |
| 935 | result_set.insert(channel->source_node()->string_view()); |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | std::vector<std::string_view> result; |
| 942 | for (const std::string_view source : result_set) { |
| 943 | VLOG(1) << "Found a source node of " << source; |
| 944 | result.emplace_back(source); |
| 945 | } |
| 946 | return result; |
| 947 | } |
| 948 | |
| 949 | std::vector<std::string_view> DestinationNodeNames(const Configuration *config, |
| 950 | const Node *my_node) { |
| 951 | std::vector<std::string_view> result; |
| 952 | |
| 953 | for (const Channel *channel : *config->channels()) { |
| 954 | if (channel->has_source_node() && channel->source_node()->string_view() == |
| 955 | my_node->name()->string_view()) { |
| 956 | if (!channel->has_destination_nodes()) continue; |
| 957 | |
| 958 | if (channel->source_node()->string_view() != |
| 959 | my_node->name()->string_view()) { |
| 960 | continue; |
| 961 | } |
| 962 | |
| 963 | for (const Connection *connection : *channel->destination_nodes()) { |
| 964 | if (std::find(result.begin(), result.end(), |
| 965 | connection->name()->string_view()) == result.end()) { |
| 966 | result.emplace_back(connection->name()->string_view()); |
| 967 | } |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | for (const std::string_view destination : result) { |
| 973 | VLOG(1) << "Found a destination node of " << destination; |
| 974 | } |
| 975 | return result; |
| 976 | } |
| 977 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 978 | } // namespace configuration |
| 979 | } // namespace aos |