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 | |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 11 | #include <map> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 12 | #include <set> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 13 | #include <string_view> |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 14 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 15 | #include "absl/container/btree_set.h" |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 16 | #include "absl/strings/str_cat.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 17 | #include "aos/configuration_generated.h" |
| 18 | #include "aos/flatbuffer_merge.h" |
| 19 | #include "aos/json_to_flatbuffer.h" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 20 | #include "aos/network/team_number.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 21 | #include "aos/unique_malloc_ptr.h" |
| 22 | #include "aos/util/file.h" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 23 | #include "gflags/gflags.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 24 | #include "glog/logging.h" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 25 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 26 | namespace aos { |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 27 | namespace { |
| 28 | bool EndsWith(std::string_view str, std::string_view end) { |
| 29 | if (str.size() < end.size()) { |
| 30 | return false; |
| 31 | } |
| 32 | if (str.substr(str.size() - end.size(), end.size()) != end) { |
| 33 | return false; |
| 34 | } |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | std::string MaybeReplaceExtension(std::string_view filename, |
| 39 | std::string_view extension, |
| 40 | std::string_view replacement) { |
| 41 | if (!EndsWith(filename, extension)) { |
| 42 | return std::string(filename); |
| 43 | } |
| 44 | filename.remove_suffix(extension.size()); |
| 45 | return absl::StrCat(filename, replacement); |
| 46 | } |
| 47 | |
| 48 | FlatbufferDetachedBuffer<Configuration> ReadConfigFile(std::string_view path, |
| 49 | bool binary) { |
| 50 | if (binary) { |
| 51 | FlatbufferVector<Configuration> config = |
| 52 | FileToFlatbuffer<Configuration>(path); |
| 53 | return CopySpanAsDetachedBuffer(config.span()); |
| 54 | } |
| 55 | |
| 56 | flatbuffers::DetachedBuffer buffer = JsonToFlatbuffer( |
| 57 | util::ReadFileToStringOrDie(path), ConfigurationTypeTable()); |
| 58 | |
| 59 | CHECK_GT(buffer.size(), 0u) << ": Failed to parse JSON file"; |
| 60 | |
| 61 | return FlatbufferDetachedBuffer<Configuration>(std::move(buffer)); |
| 62 | } |
| 63 | |
| 64 | } // namespace |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 65 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 66 | // 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] | 67 | // insert them in the btree below. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 68 | bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 69 | const FlatbufferDetachedBuffer<Channel> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 70 | int name_compare = lhs.message().name()->string_view().compare( |
| 71 | rhs.message().name()->string_view()); |
| 72 | if (name_compare == 0) { |
| 73 | return lhs.message().type()->string_view() < |
| 74 | rhs.message().type()->string_view(); |
| 75 | } else if (name_compare < 0) { |
| 76 | return true; |
| 77 | } else { |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 82 | bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 83 | const FlatbufferDetachedBuffer<Channel> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 84 | return lhs.message().name()->string_view() == |
| 85 | rhs.message().name()->string_view() && |
| 86 | lhs.message().type()->string_view() == |
| 87 | rhs.message().type()->string_view(); |
| 88 | } |
| 89 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 90 | bool operator==(const FlatbufferDetachedBuffer<Application> &lhs, |
| 91 | const FlatbufferDetachedBuffer<Application> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 92 | return lhs.message().name()->string_view() == |
| 93 | rhs.message().name()->string_view(); |
| 94 | } |
| 95 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 96 | bool operator<(const FlatbufferDetachedBuffer<Application> &lhs, |
| 97 | const FlatbufferDetachedBuffer<Application> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 98 | return lhs.message().name()->string_view() < |
| 99 | rhs.message().name()->string_view(); |
| 100 | } |
| 101 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 102 | bool operator==(const FlatbufferDetachedBuffer<Node> &lhs, |
| 103 | const FlatbufferDetachedBuffer<Node> &rhs) { |
| 104 | return lhs.message().name()->string_view() == |
| 105 | rhs.message().name()->string_view(); |
| 106 | } |
| 107 | |
| 108 | bool operator<(const FlatbufferDetachedBuffer<Node> &lhs, |
| 109 | const FlatbufferDetachedBuffer<Node> &rhs) { |
| 110 | return lhs.message().name()->string_view() < |
| 111 | rhs.message().name()->string_view(); |
| 112 | } |
| 113 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 114 | namespace configuration { |
| 115 | namespace { |
| 116 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 117 | // 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] | 118 | std::string_view ExtractFolder(const std::string_view filename) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 119 | auto last_slash_pos = filename.find_last_of("/\\"); |
| 120 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 121 | return last_slash_pos == std::string_view::npos |
| 122 | ? std::string_view("./") |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 123 | : filename.substr(0, last_slash_pos + 1); |
| 124 | } |
| 125 | |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 126 | std::string AbsolutePath(const std::string_view filename) { |
| 127 | // Uses an std::string so that we know the input will be null-terminated. |
| 128 | const std::string terminated_file(filename); |
| 129 | char buffer[PATH_MAX]; |
| 130 | PCHECK(NULL != realpath(terminated_file.c_str(), buffer)); |
| 131 | return buffer; |
| 132 | } |
| 133 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 134 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 135 | const std::string_view path, absl::btree_set<std::string> *visited_paths, |
| 136 | const std::vector<std::string_view> &extra_import_paths) { |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 137 | std::string binary_path = MaybeReplaceExtension(path, ".json", ".bfbs"); |
| 138 | bool binary_path_exists = util::PathExists(binary_path); |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 139 | std::string raw_path(path); |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 140 | // For each .json file, look and see if we can find a .bfbs file next to it |
| 141 | // with the same base name. If we can, assume it is the same and use it |
| 142 | // instead. It is much faster to load .bfbs files than .json files. |
| 143 | if (!binary_path_exists && !util::PathExists(raw_path)) { |
| 144 | const bool path_is_absolute = raw_path.size() > 0 && raw_path[0] == '/'; |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 145 | if (path_is_absolute) { |
| 146 | CHECK(extra_import_paths.empty()) |
| 147 | << "Can't specify extra import paths if attempting to read a config " |
| 148 | "file from an absolute path (path is " |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 149 | << raw_path << ")."; |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | bool found_path = false; |
| 153 | for (const auto &import_path : extra_import_paths) { |
| 154 | raw_path = std::string(import_path) + "/" + std::string(path); |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 155 | binary_path = MaybeReplaceExtension(raw_path, ".json", ".bfbs"); |
| 156 | binary_path_exists = util::PathExists(binary_path); |
| 157 | if (binary_path_exists) { |
| 158 | found_path = true; |
| 159 | break; |
| 160 | } |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 161 | if (util::PathExists(raw_path)) { |
| 162 | found_path = true; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | CHECK(found_path) << ": Failed to find file " << path << "."; |
| 167 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 168 | |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 169 | FlatbufferDetachedBuffer<Configuration> config = ReadConfigFile( |
| 170 | binary_path_exists ? binary_path : raw_path, binary_path_exists); |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 171 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 172 | // Depth first. Take the following example: |
| 173 | // |
| 174 | // config1.json: |
| 175 | // { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 176 | // "channels": [ |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 177 | // { |
| 178 | // "name": "/foo", |
| 179 | // "type": ".aos.bar", |
| 180 | // "max_size": 5 |
| 181 | // } |
| 182 | // ], |
| 183 | // "imports": [ |
| 184 | // "config2.json", |
| 185 | // ] |
| 186 | // } |
| 187 | // |
| 188 | // config2.json: |
| 189 | // { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 190 | // "channels": [ |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 191 | // { |
| 192 | // "name": "/foo", |
| 193 | // "type": ".aos.bar", |
| 194 | // "max_size": 7 |
| 195 | // } |
| 196 | // ], |
| 197 | // } |
| 198 | // |
| 199 | // We want the main config (config1.json) to be able to override the imported |
| 200 | // config. That means that it needs to be merged into the imported configs, |
| 201 | // not the other way around. |
| 202 | |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 203 | const std::string absolute_path = |
| 204 | AbsolutePath(binary_path_exists ? binary_path : raw_path); |
| 205 | // Track that we have seen this file before recursing. Track the path we |
| 206 | // actually loaded (which should be consistent if imported twice). |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 207 | if (!visited_paths->insert(absolute_path).second) { |
| 208 | for (const auto &visited_path : *visited_paths) { |
| 209 | LOG(INFO) << "Already visited: " << visited_path; |
| 210 | } |
| 211 | LOG(FATAL) |
| 212 | << "Already imported " << path << " (i.e. " << absolute_path |
| 213 | << "). See above for the files that have already been processed."; |
| 214 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 215 | |
| 216 | if (config.message().has_imports()) { |
| 217 | // Capture the imports. |
| 218 | const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *v = |
| 219 | config.message().imports(); |
| 220 | |
| 221 | // And then wipe them. This gets GCed when we merge later. |
| 222 | config.mutable_message()->clear_imports(); |
| 223 | |
| 224 | // Start with an empty configuration to merge into. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 225 | FlatbufferDetachedBuffer<Configuration> merged_config = |
| 226 | FlatbufferDetachedBuffer<Configuration>::Empty(); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 227 | |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 228 | const std::string path_folder(ExtractFolder(path)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 229 | for (const flatbuffers::String *str : *v) { |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 230 | const std::string included_config = |
| 231 | path_folder + "/" + std::string(str->string_view()); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 232 | |
| 233 | // And them merge everything in. |
| 234 | merged_config = MergeFlatBuffers( |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 235 | merged_config, |
| 236 | ReadConfig(included_config, visited_paths, extra_import_paths)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | // Finally, merge this file in. |
| 240 | config = MergeFlatBuffers(merged_config, config); |
| 241 | } |
| 242 | return config; |
| 243 | } |
| 244 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 245 | // Compares (c < p) a channel, and a name, type tuple. |
| 246 | bool CompareChannels(const Channel *c, |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 247 | ::std::pair<std::string_view, std::string_view> p) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 248 | int name_compare = c->name()->string_view().compare(p.first); |
| 249 | if (name_compare == 0) { |
| 250 | return c->type()->string_view() < p.second; |
| 251 | } else if (name_compare < 0) { |
| 252 | return true; |
| 253 | } else { |
| 254 | return false; |
| 255 | } |
| 256 | }; |
| 257 | |
| 258 | // Compares for equality (c == p) a channel, and a name, type tuple. |
| 259 | bool EqualsChannels(const Channel *c, |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 260 | ::std::pair<std::string_view, std::string_view> p) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 261 | return c->name()->string_view() == p.first && |
| 262 | c->type()->string_view() == p.second; |
| 263 | } |
| 264 | |
| 265 | // Compares (c < p) an application, and a name; |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 266 | bool CompareApplications(const Application *a, std::string_view name) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 267 | return a->name()->string_view() < name; |
| 268 | }; |
| 269 | |
| 270 | // Compares for equality (c == p) an application, and a name; |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 271 | bool EqualsApplications(const Application *a, std::string_view name) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 272 | return a->name()->string_view() == name; |
| 273 | } |
| 274 | |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 275 | void ValidateConfiguration(const Flatbuffer<Configuration> &config) { |
| 276 | // No imports should be left. |
| 277 | CHECK(!config.message().has_imports()); |
| 278 | |
| 279 | // Check that if there is a node list, all the source nodes are filled out and |
| 280 | // valid, and all the destination nodes are valid (and not the source). This |
| 281 | // is a basic consistency check. |
| 282 | if (config.message().has_channels()) { |
| 283 | const Channel *last_channel = nullptr; |
| 284 | for (const Channel *c : *config.message().channels()) { |
| 285 | CHECK(c->has_name()); |
| 286 | CHECK(c->has_type()); |
| 287 | if (c->name()->string_view().back() == '/') { |
| 288 | LOG(FATAL) << "Channel names can't end with '/'"; |
| 289 | } |
| 290 | if (c->name()->string_view().find("//") != std::string_view::npos) { |
| 291 | LOG(FATAL) << ": Invalid channel name " << c->name()->string_view() |
| 292 | << ", can't use //."; |
| 293 | } |
| 294 | for (const char data : c->name()->string_view()) { |
| 295 | if (data >= '0' && data <= '9') { |
| 296 | continue; |
| 297 | } |
| 298 | if (data >= 'a' && data <= 'z') { |
| 299 | continue; |
| 300 | } |
| 301 | if (data >= 'A' && data <= 'Z') { |
| 302 | continue; |
| 303 | } |
| 304 | if (data == '-' || data == '_' || data == '/') { |
| 305 | continue; |
| 306 | } |
| 307 | LOG(FATAL) << "Invalid channel name " << c->name()->string_view() |
| 308 | << ", can only use [-a-zA-Z0-9_/]"; |
| 309 | } |
| 310 | |
| 311 | // Make sure everything is sorted while we are here... If this fails, |
| 312 | // there will be a bunch of weird errors. |
| 313 | if (last_channel != nullptr) { |
| 314 | CHECK(CompareChannels( |
| 315 | last_channel, |
| 316 | std::make_pair(c->name()->string_view(), c->type()->string_view()))) |
| 317 | << ": Channels not sorted!"; |
| 318 | } |
| 319 | last_channel = c; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | if (config.message().has_nodes() && config.message().has_channels()) { |
| 324 | for (const Channel *c : *config.message().channels()) { |
| 325 | CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c) |
| 326 | << " is missing \"source_node\""; |
| 327 | CHECK(GetNode(&config.message(), c->source_node()->string_view()) != |
| 328 | nullptr) |
| 329 | << ": Channel " << FlatbufferToJson(c) |
| 330 | << " has an unknown \"source_node\""; |
| 331 | |
| 332 | if (c->has_destination_nodes()) { |
| 333 | for (const Connection *connection : *c->destination_nodes()) { |
| 334 | CHECK(connection->has_name()); |
| 335 | CHECK(GetNode(&config.message(), connection->name()->string_view()) != |
| 336 | nullptr) |
| 337 | << ": Channel " << FlatbufferToJson(c) |
| 338 | << " has an unknown \"destination_nodes\" " |
| 339 | << connection->name()->string_view(); |
| 340 | |
| 341 | switch (connection->timestamp_logger()) { |
| 342 | case LoggerConfig::LOCAL_LOGGER: |
| 343 | case LoggerConfig::NOT_LOGGED: |
| 344 | CHECK(!connection->has_timestamp_logger_nodes()); |
| 345 | break; |
| 346 | case LoggerConfig::REMOTE_LOGGER: |
| 347 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
| 348 | CHECK(connection->has_timestamp_logger_nodes()); |
| 349 | CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u); |
| 350 | for (const flatbuffers::String *timestamp_logger_node : |
| 351 | *connection->timestamp_logger_nodes()) { |
| 352 | CHECK(GetNode(&config.message(), |
| 353 | timestamp_logger_node->string_view()) != nullptr) |
| 354 | << ": Channel " << FlatbufferToJson(c) |
| 355 | << " has an unknown \"timestamp_logger_node\"" |
| 356 | << connection->name()->string_view(); |
| 357 | } |
| 358 | break; |
| 359 | } |
| 360 | |
| 361 | CHECK_NE(connection->name()->string_view(), |
| 362 | c->source_node()->string_view()) |
| 363 | << ": Channel " << FlatbufferToJson(c) |
| 364 | << " is forwarding data to itself"; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 371 | } // namespace |
| 372 | |
Austin Schuh | 006a9f5 | 2021-04-07 16:24:18 -0700 | [diff] [blame] | 373 | // Maps name for the provided maps. Modifies name. |
| 374 | void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps, |
| 375 | std::string *name, std::string_view type, const Node *node) { |
| 376 | // For the same reason we merge configs in reverse order, we want to process |
| 377 | // maps in reverse order. That lets the outer config overwrite channels from |
| 378 | // the inner configs. |
| 379 | for (auto i = maps->rbegin(); i != maps->rend(); ++i) { |
| 380 | if (!i->has_match() || !i->match()->has_name()) { |
| 381 | continue; |
| 382 | } |
| 383 | if (!i->has_rename() || !i->rename()->has_name()) { |
| 384 | continue; |
| 385 | } |
| 386 | |
| 387 | // Handle normal maps (now that we know that match and rename are filled |
| 388 | // out). |
| 389 | const std::string_view match_name = i->match()->name()->string_view(); |
| 390 | if (match_name != *name) { |
| 391 | if (match_name.back() == '*' && |
| 392 | std::string_view(*name).substr( |
| 393 | 0, std::min(name->size(), match_name.size() - 1)) == |
| 394 | match_name.substr(0, match_name.size() - 1)) { |
| 395 | CHECK_EQ(match_name.find('*'), match_name.size() - 1); |
| 396 | } else { |
| 397 | continue; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // Handle type specific maps. |
| 402 | if (i->match()->has_type() && i->match()->type()->string_view() != type) { |
| 403 | continue; |
| 404 | } |
| 405 | |
| 406 | // Now handle node specific maps. |
| 407 | if (node != nullptr && i->match()->has_source_node() && |
| 408 | i->match()->source_node()->string_view() != |
| 409 | node->name()->string_view()) { |
| 410 | continue; |
| 411 | } |
| 412 | |
| 413 | std::string new_name(i->rename()->name()->string_view()); |
| 414 | if (match_name.back() == '*') { |
| 415 | new_name += std::string(name->substr(match_name.size() - 1)); |
| 416 | } |
| 417 | VLOG(1) << "Renamed \"" << *name << "\" to \"" << new_name << "\""; |
| 418 | *name = std::move(new_name); |
| 419 | } |
| 420 | } |
| 421 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 422 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 423 | const Flatbuffer<Configuration> &config) { |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 424 | // auto_merge_config will contain all the fields of the Configuration that are |
| 425 | // to be passed through unmodified to the result of MergeConfiguration(). |
| 426 | // In the processing below, we mutate auto_merge_config to remove any fields |
| 427 | // which we do need to alter (hence why we can't use the input config |
| 428 | // directly), and then merge auto_merge_config back in at the end. |
| 429 | aos::FlatbufferDetachedBuffer<aos::Configuration> auto_merge_config = |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 430 | aos::RecursiveCopyFlatBuffer(&config.message()); |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 431 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 432 | // 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] | 433 | // have seen before and merge the updates in. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 434 | absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 435 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 436 | if (config.message().has_channels()) { |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 437 | auto_merge_config.mutable_message()->clear_channels(); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 438 | for (const Channel *c : *config.message().channels()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 439 | // Ignore malformed entries. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 440 | if (!c->has_name()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 441 | continue; |
| 442 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 443 | if (!c->has_type()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 444 | continue; |
| 445 | } |
| 446 | |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 447 | CHECK_EQ(c->read_method() == ReadMethod::PIN, c->num_readers() != 0) |
| 448 | << ": num_readers may be set if and only if read_method is PIN," |
| 449 | " if you want 0 readers do not set PIN: " |
| 450 | << CleanedChannelToString(c); |
| 451 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 452 | // Attempt to insert the channel. |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 453 | auto result = channels.insert(RecursiveCopyFlatBuffer(c)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 454 | if (!result.second) { |
| 455 | // Already there, so merge the new table into the original. |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 456 | *result.first = |
| 457 | MergeFlatBuffers(*result.first, RecursiveCopyFlatBuffer(c)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // Now repeat this for the application list. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 463 | absl::btree_set<FlatbufferDetachedBuffer<Application>> applications; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 464 | if (config.message().has_applications()) { |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 465 | auto_merge_config.mutable_message()->clear_applications(); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 466 | for (const Application *a : *config.message().applications()) { |
| 467 | if (!a->has_name()) { |
| 468 | continue; |
| 469 | } |
| 470 | |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 471 | auto result = applications.insert(RecursiveCopyFlatBuffer(a)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 472 | if (!result.second) { |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 473 | *result.first = |
| 474 | MergeFlatBuffers(*result.first, RecursiveCopyFlatBuffer(a)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 479 | // Now repeat this for the node list. |
| 480 | absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes; |
| 481 | if (config.message().has_nodes()) { |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 482 | auto_merge_config.mutable_message()->clear_nodes(); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 483 | for (const Node *n : *config.message().nodes()) { |
| 484 | if (!n->has_name()) { |
| 485 | continue; |
| 486 | } |
| 487 | |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 488 | auto result = nodes.insert(RecursiveCopyFlatBuffer(n)); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 489 | if (!result.second) { |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 490 | *result.first = |
| 491 | MergeFlatBuffers(*result.first, RecursiveCopyFlatBuffer(n)); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 496 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 497 | fbb.ForceDefaults(true); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 498 | |
| 499 | // 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] | 500 | // Channels |
| 501 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 502 | channels_offset; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 503 | { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 504 | ::std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 505 | for (const FlatbufferDetachedBuffer<Channel> &c : channels) { |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 506 | channel_offsets.emplace_back( |
| 507 | RecursiveCopyFlatBuffer<Channel>(&c.message(), &fbb)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 508 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 509 | channels_offset = fbb.CreateVector(channel_offsets); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | // Applications |
| 513 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 514 | applications_offset; |
| 515 | { |
| 516 | ::std::vector<flatbuffers::Offset<Application>> applications_offsets; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 517 | for (const FlatbufferDetachedBuffer<Application> &a : applications) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 518 | applications_offsets.emplace_back( |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 519 | RecursiveCopyFlatBuffer<Application>(&a.message(), &fbb)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 520 | } |
| 521 | applications_offset = fbb.CreateVector(applications_offsets); |
| 522 | } |
| 523 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 524 | // Nodes |
| 525 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 526 | nodes_offset; |
| 527 | { |
| 528 | ::std::vector<flatbuffers::Offset<Node>> node_offsets; |
| 529 | for (const FlatbufferDetachedBuffer<Node> &n : nodes) { |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 530 | node_offsets.emplace_back( |
| 531 | RecursiveCopyFlatBuffer<Node>(&n.message(), &fbb)); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 532 | } |
| 533 | nodes_offset = fbb.CreateVector(node_offsets); |
| 534 | } |
| 535 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 536 | // And then build a Configuration with them all. |
| 537 | ConfigurationBuilder configuration_builder(fbb); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 538 | configuration_builder.add_channels(channels_offset); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 539 | if (config.message().has_applications()) { |
| 540 | configuration_builder.add_applications(applications_offset); |
| 541 | } |
| 542 | if (config.message().has_nodes()) { |
| 543 | configuration_builder.add_nodes(nodes_offset); |
| 544 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 545 | |
| 546 | fbb.Finish(configuration_builder.Finish()); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 547 | |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 548 | aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config( |
| 549 | fbb.Release()); |
| 550 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 551 | // Now, validate that if there is a node list, every channel has a source |
| 552 | // node. |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 553 | FlatbufferDetachedBuffer<Configuration> result = |
| 554 | MergeFlatBuffers(modified_config, auto_merge_config); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 555 | |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 556 | ValidateConfiguration(result); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 557 | |
| 558 | return result; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 561 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
James Kuszmaul | c0c08da | 2020-05-10 18:56:07 -0700 | [diff] [blame] | 562 | const std::string_view path, |
| 563 | const std::vector<std::string_view> &import_paths) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 564 | // We only want to read a file once. So track the visited files in a set. |
| 565 | absl::btree_set<std::string> visited_paths; |
Austin Schuh | 1518232 | 2020-10-10 15:25:21 -0700 | [diff] [blame] | 566 | FlatbufferDetachedBuffer<Configuration> read_config = |
| 567 | ReadConfig(path, &visited_paths, import_paths); |
| 568 | |
| 569 | // If we only read one file, and it had a .bfbs extension, it has to be a |
| 570 | // fully formatted config. Do a quick verification and return it. |
| 571 | if (visited_paths.size() == 1 && EndsWith(*visited_paths.begin(), ".bfbs")) { |
| 572 | ValidateConfiguration(read_config); |
| 573 | return read_config; |
| 574 | } |
| 575 | |
| 576 | return MergeConfiguration(read_config); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 579 | FlatbufferDetachedBuffer<Configuration> MergeWithConfig( |
Brian Silverman | 24f5aa8 | 2020-06-23 16:21:28 -0700 | [diff] [blame] | 580 | const Configuration *config, const Flatbuffer<Configuration> &addition) { |
| 581 | return MergeConfiguration(MergeFlatBuffers(config, &addition.message())); |
| 582 | } |
| 583 | |
| 584 | FlatbufferDetachedBuffer<Configuration> MergeWithConfig( |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 585 | const Configuration *config, std::string_view json) { |
| 586 | FlatbufferDetachedBuffer<Configuration> addition = |
| 587 | JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable()); |
| 588 | |
Brian Silverman | 24f5aa8 | 2020-06-23 16:21:28 -0700 | [diff] [blame] | 589 | return MergeWithConfig(config, addition); |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 590 | } |
| 591 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 592 | const Channel *GetChannel(const Configuration *config, std::string_view name, |
| 593 | std::string_view type, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 594 | std::string_view application_name, const Node *node, |
| 595 | bool quiet) { |
Brian Silverman | 9fcf2c7 | 2020-12-21 18:30:58 -0800 | [diff] [blame] | 596 | if (!config->has_channels()) { |
| 597 | return nullptr; |
| 598 | } |
| 599 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 600 | const std::string_view original_name = name; |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 601 | std::string mutable_name; |
Austin Schuh | 4c3b970 | 2020-08-30 11:34:55 -0700 | [diff] [blame] | 602 | if (node != nullptr) { |
| 603 | VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type |
| 604 | << "\" } on " << aos::FlatbufferToJson(node); |
| 605 | } else { |
| 606 | VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type |
| 607 | << "\" }"; |
| 608 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 609 | |
| 610 | // First handle application specific maps. Only do this if we have a matching |
| 611 | // application name, and it has maps. |
Austin Schuh | d2e2f6a | 2021-02-07 20:46:16 -0800 | [diff] [blame] | 612 | { |
| 613 | const Application *application = |
| 614 | GetApplication(config, node, application_name); |
| 615 | if (application != nullptr && application->has_maps()) { |
| 616 | mutable_name = std::string(name); |
| 617 | HandleMaps(application->maps(), &mutable_name, type, node); |
| 618 | name = std::string_view(mutable_name); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
| 622 | // Now do global maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 623 | if (config->has_maps()) { |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 624 | mutable_name = std::string(name); |
| 625 | HandleMaps(config->maps(), &mutable_name, type, node); |
| 626 | name = std::string_view(mutable_name); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 629 | if (original_name != name) { |
| 630 | VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \"" |
| 631 | << type << "\" }"; |
| 632 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 633 | |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 634 | // Then look for the channel (note that this relies on the channels being |
| 635 | // sorted in the config). |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 636 | auto channel_iterator = |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 637 | std::lower_bound(config->channels()->cbegin(), config->channels()->cend(), |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 638 | std::make_pair(name, type), CompareChannels); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 639 | |
| 640 | // Make sure we actually found it, and it matches. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 641 | if (channel_iterator != config->channels()->cend() && |
| 642 | EqualsChannels(*channel_iterator, std::make_pair(name, type))) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 643 | if (VLOG_IS_ON(2)) { |
| 644 | VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator); |
| 645 | } else if (VLOG_IS_ON(1)) { |
| 646 | VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator); |
| 647 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 648 | return *channel_iterator; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 649 | } else { |
| 650 | VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \"" |
| 651 | << type << "\" }"; |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 652 | if (original_name != name && !quiet) { |
Austin Schuh | 4b42b25 | 2020-10-19 11:35:20 -0700 | [diff] [blame] | 653 | LOG(WARNING) << "Remapped from {\"name\": \"" << original_name |
| 654 | << "\", \"type\": \"" << type << "\"}, to {\"name\": \"" |
| 655 | << name << "\", \"type\": \"" << type |
| 656 | << "\"}, but no channel by that name exists."; |
| 657 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 658 | return nullptr; |
| 659 | } |
| 660 | } |
| 661 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 662 | size_t ChannelIndex(const Configuration *configuration, |
| 663 | const Channel *channel) { |
| 664 | CHECK(configuration->channels() != nullptr) << ": No channels"; |
| 665 | |
| 666 | auto c = std::find(configuration->channels()->begin(), |
| 667 | configuration->channels()->end(), channel); |
| 668 | CHECK(c != configuration->channels()->end()) |
| 669 | << ": Channel pointer not found in configuration()->channels()"; |
| 670 | |
| 671 | return std::distance(configuration->channels()->begin(), c); |
| 672 | } |
| 673 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 674 | std::string CleanedChannelToString(const Channel *channel) { |
| 675 | FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel); |
| 676 | cleaned_channel.mutable_message()->clear_schema(); |
| 677 | return FlatbufferToJson(cleaned_channel); |
| 678 | } |
| 679 | |
Austin Schuh | a81454b | 2020-05-12 19:58:36 -0700 | [diff] [blame] | 680 | std::string StrippedChannelToString(const Channel *channel) { |
| 681 | return absl::StrCat("{ \"name\": \"", channel->name()->string_view(), |
| 682 | "\", \"type\": \"", channel->type()->string_view(), |
| 683 | "\" }"); |
| 684 | } |
| 685 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 686 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 687 | const Flatbuffer<Configuration> &config, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 688 | const std::vector<aos::FlatbufferVector<reflection::Schema>> &schemas) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 689 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 690 | fbb.ForceDefaults(true); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 691 | |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 692 | // Cache for holding already inserted schemas. |
| 693 | std::map<std::string_view, flatbuffers::Offset<reflection::Schema>> |
| 694 | schema_cache; |
| 695 | |
| 696 | CHECK_EQ(Channel::MiniReflectTypeTable()->num_elems, 13u) |
| 697 | << ": Merging logic needs to be updated when the number of channel " |
| 698 | "fields changes."; |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 699 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 700 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 701 | channels_offset; |
| 702 | if (config.message().has_channels()) { |
| 703 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 704 | for (const Channel *c : *config.message().channels()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 705 | // Search for a schema with a matching type. |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 706 | const aos::FlatbufferVector<reflection::Schema> *found_schema = nullptr; |
| 707 | for (const aos::FlatbufferVector<reflection::Schema> &schema : schemas) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 708 | if (schema.message().root_table() != nullptr) { |
| 709 | if (schema.message().root_table()->name()->string_view() == |
| 710 | c->type()->string_view()) { |
| 711 | found_schema = &schema; |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | CHECK(found_schema != nullptr) |
| 717 | << ": Failed to find schema for " << FlatbufferToJson(c); |
| 718 | |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 719 | // Now copy the message manually. |
| 720 | auto cached_schema = schema_cache.find(c->type()->string_view()); |
| 721 | flatbuffers::Offset<reflection::Schema> schema_offset; |
| 722 | if (cached_schema != schema_cache.end()) { |
| 723 | schema_offset = cached_schema->second; |
| 724 | } else { |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 725 | schema_offset = RecursiveCopyFlatBuffer<reflection::Schema>( |
| 726 | &found_schema->message(), &fbb); |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 727 | schema_cache.emplace(c->type()->string_view(), schema_offset); |
| 728 | } |
| 729 | |
| 730 | flatbuffers::Offset<flatbuffers::String> name_offset = |
| 731 | fbb.CreateSharedString(c->name()->str()); |
| 732 | flatbuffers::Offset<flatbuffers::String> type_offset = |
| 733 | fbb.CreateSharedString(c->type()->str()); |
| 734 | flatbuffers::Offset<flatbuffers::String> source_node_offset = |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 735 | c->has_source_node() ? fbb.CreateSharedString(c->source_node()->str()) |
| 736 | : 0; |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 737 | |
| 738 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Connection>>> |
| 739 | destination_nodes_offset = |
Austin Schuh | 5c255aa | 2020-11-05 18:32:46 -0800 | [diff] [blame] | 740 | aos::RecursiveCopyVectorTable(c->destination_nodes(), &fbb); |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 741 | |
| 742 | flatbuffers::Offset< |
| 743 | flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>> |
| 744 | logger_nodes_offset = |
| 745 | aos::CopyVectorSharedString(c->logger_nodes(), &fbb); |
| 746 | |
| 747 | Channel::Builder channel_builder(fbb); |
| 748 | channel_builder.add_name(name_offset); |
| 749 | channel_builder.add_type(type_offset); |
| 750 | if (c->has_frequency()) { |
| 751 | channel_builder.add_frequency(c->frequency()); |
| 752 | } |
| 753 | if (c->has_max_size()) { |
| 754 | channel_builder.add_max_size(c->max_size()); |
| 755 | } |
| 756 | if (c->has_num_senders()) { |
| 757 | channel_builder.add_num_senders(c->num_senders()); |
| 758 | } |
| 759 | if (c->has_num_watchers()) { |
| 760 | channel_builder.add_num_watchers(c->num_watchers()); |
| 761 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 762 | channel_builder.add_schema(schema_offset); |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 763 | if (!source_node_offset.IsNull()) { |
| 764 | channel_builder.add_source_node(source_node_offset); |
| 765 | } |
| 766 | if (!destination_nodes_offset.IsNull()) { |
| 767 | channel_builder.add_destination_nodes(destination_nodes_offset); |
| 768 | } |
| 769 | if (c->has_logger()) { |
| 770 | channel_builder.add_logger(c->logger()); |
| 771 | } |
| 772 | if (!logger_nodes_offset.IsNull()) { |
| 773 | channel_builder.add_logger_nodes(logger_nodes_offset); |
| 774 | } |
| 775 | if (c->has_read_method()) { |
| 776 | channel_builder.add_read_method(c->read_method()); |
| 777 | } |
| 778 | if (c->has_num_readers()) { |
| 779 | channel_builder.add_num_readers(c->num_readers()); |
| 780 | } |
| 781 | channel_offsets.emplace_back(channel_builder.Finish()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 782 | } |
| 783 | channels_offset = fbb.CreateVector(channel_offsets); |
| 784 | } |
| 785 | |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 786 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
Austin Schuh | 5c255aa | 2020-11-05 18:32:46 -0800 | [diff] [blame] | 787 | maps_offset = |
| 788 | aos::RecursiveCopyVectorTable(config.message().maps(), &fbb); |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 789 | |
| 790 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
Austin Schuh | 5c255aa | 2020-11-05 18:32:46 -0800 | [diff] [blame] | 791 | nodes_offset = |
| 792 | aos::RecursiveCopyVectorTable(config.message().nodes(), &fbb); |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 793 | |
| 794 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 795 | applications_offset = |
Austin Schuh | 5c255aa | 2020-11-05 18:32:46 -0800 | [diff] [blame] | 796 | aos::RecursiveCopyVectorTable(config.message().applications(), &fbb); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 797 | |
| 798 | // Now insert everything else in unmodified. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 799 | ConfigurationBuilder configuration_builder(fbb); |
| 800 | if (config.message().has_channels()) { |
| 801 | configuration_builder.add_channels(channels_offset); |
| 802 | } |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 803 | if (!maps_offset.IsNull()) { |
| 804 | configuration_builder.add_maps(maps_offset); |
| 805 | } |
| 806 | if (!nodes_offset.IsNull()) { |
| 807 | configuration_builder.add_nodes(nodes_offset); |
| 808 | } |
| 809 | if (!applications_offset.IsNull()) { |
| 810 | configuration_builder.add_applications(applications_offset); |
| 811 | } |
| 812 | |
| 813 | if (config.message().has_channel_storage_duration()) { |
| 814 | configuration_builder.add_channel_storage_duration( |
| 815 | config.message().channel_storage_duration()); |
| 816 | } |
| 817 | |
| 818 | CHECK_EQ(Configuration::MiniReflectTypeTable()->num_elems, 6u) |
| 819 | << ": Merging logic needs to be updated when the number of configuration " |
| 820 | "fields changes."; |
| 821 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 822 | fbb.Finish(configuration_builder.Finish()); |
James Kuszmaul | 3c99859 | 2020-07-27 21:04:47 -0700 | [diff] [blame] | 823 | aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config( |
| 824 | fbb.Release()); |
| 825 | |
Austin Schuh | 68d9859 | 2020-11-01 23:22:57 -0800 | [diff] [blame] | 826 | return modified_config; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 829 | const Node *GetNodeFromHostname(const Configuration *config, |
| 830 | std::string_view hostname) { |
| 831 | for (const Node *node : *config->nodes()) { |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 832 | if (node->has_hostname() && node->hostname()->string_view() == hostname) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 833 | return node; |
| 834 | } |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 835 | if (node->has_hostnames()) { |
| 836 | for (const auto &candidate : *node->hostnames()) { |
| 837 | if (candidate->string_view() == hostname) { |
| 838 | return node; |
| 839 | } |
| 840 | } |
| 841 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 842 | } |
| 843 | return nullptr; |
| 844 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 845 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 846 | const Node *GetMyNode(const Configuration *config) { |
| 847 | const std::string hostname = (FLAGS_override_hostname.size() > 0) |
| 848 | ? FLAGS_override_hostname |
| 849 | : network::GetHostname(); |
| 850 | const Node *node = GetNodeFromHostname(config, hostname); |
| 851 | if (node != nullptr) return node; |
| 852 | |
| 853 | LOG(FATAL) << "Unknown node for host: " << hostname |
| 854 | << ". Consider using --override_hostname if hostname detection " |
| 855 | "is wrong."; |
| 856 | return nullptr; |
| 857 | } |
| 858 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 859 | const Node *GetNode(const Configuration *config, const Node *node) { |
| 860 | if (!MultiNode(config)) { |
| 861 | CHECK(node == nullptr) << ": Provided a node in a single node world."; |
| 862 | return nullptr; |
| 863 | } else { |
| 864 | CHECK(node != nullptr); |
| 865 | CHECK(node->has_name()); |
| 866 | return GetNode(config, node->name()->string_view()); |
| 867 | } |
| 868 | } |
| 869 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 870 | const Node *GetNode(const Configuration *config, std::string_view name) { |
Austin Schuh | fd96062 | 2020-01-01 13:22:55 -0800 | [diff] [blame] | 871 | CHECK(config->has_nodes()) |
| 872 | << ": Asking for a node from a single node configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 873 | for (const Node *node : *config->nodes()) { |
Austin Schuh | fd96062 | 2020-01-01 13:22:55 -0800 | [diff] [blame] | 874 | CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 875 | if (node->name()->string_view() == name) { |
| 876 | return node; |
| 877 | } |
| 878 | } |
| 879 | return nullptr; |
| 880 | } |
| 881 | |
Austin Schuh | 0ca1fd3 | 2020-12-18 22:53:05 -0800 | [diff] [blame] | 882 | const Node *GetNode(const Configuration *config, size_t node_index) { |
| 883 | if (!MultiNode(config)) { |
| 884 | CHECK_EQ(node_index, 0u) << ": Invalid node in a single node world."; |
| 885 | return nullptr; |
| 886 | } else { |
| 887 | CHECK_LT(node_index, config->nodes()->size()); |
| 888 | return config->nodes()->Get(node_index); |
| 889 | } |
| 890 | } |
| 891 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 892 | const Node *GetNodeOrDie(const Configuration *config, const Node *node) { |
| 893 | if (!MultiNode(config)) { |
| 894 | CHECK(node == nullptr) << ": Provided a node in a single node world."; |
| 895 | return nullptr; |
| 896 | } else { |
| 897 | const Node *config_node = GetNode(config, node); |
| 898 | if (config_node == nullptr) { |
| 899 | LOG(FATAL) << "Couldn't find node matching " << FlatbufferToJson(node); |
| 900 | } |
| 901 | return config_node; |
| 902 | } |
| 903 | } |
| 904 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 905 | namespace { |
| 906 | int GetNodeIndexFromConfig(const Configuration *config, const Node *node) { |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 907 | int node_index = 0; |
| 908 | for (const Node *iterated_node : *config->nodes()) { |
| 909 | if (iterated_node == node) { |
| 910 | return node_index; |
| 911 | } |
| 912 | ++node_index; |
| 913 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 914 | return -1; |
| 915 | } |
| 916 | } // namespace |
| 917 | |
Austin Schuh | a9df9ad | 2021-06-16 14:49:39 -0700 | [diff] [blame] | 918 | aos::FlatbufferDetachedBuffer<aos::Configuration> AddSchema( |
| 919 | std::string_view json, |
| 920 | const std::vector<aos::FlatbufferVector<reflection::Schema>> &schemas) { |
| 921 | FlatbufferDetachedBuffer<Configuration> addition = |
| 922 | JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable()); |
| 923 | return MergeConfiguration(addition, schemas); |
| 924 | } |
| 925 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 926 | int GetNodeIndex(const Configuration *config, const Node *node) { |
| 927 | if (!MultiNode(config)) { |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | { |
| 932 | int node_index = GetNodeIndexFromConfig(config, node); |
| 933 | if (node_index != -1) { |
| 934 | return node_index; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | const Node *result = GetNode(config, node); |
| 939 | CHECK(result != nullptr); |
| 940 | |
| 941 | { |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 942 | int node_index = GetNodeIndexFromConfig(config, result); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 943 | if (node_index != -1) { |
| 944 | return node_index; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | LOG(FATAL) << "Node " << FlatbufferToJson(node) |
| 949 | << " not found in the configuration."; |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 950 | } |
| 951 | |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 952 | int GetNodeIndex(const Configuration *config, std::string_view name) { |
| 953 | if (!MultiNode(config)) { |
| 954 | return 0; |
| 955 | } |
| 956 | |
| 957 | { |
| 958 | int node_index = 0; |
| 959 | for (const Node *iterated_node : *config->nodes()) { |
| 960 | if (iterated_node->name()->string_view() == name) { |
| 961 | return node_index; |
| 962 | } |
| 963 | ++node_index; |
| 964 | } |
| 965 | } |
| 966 | LOG(FATAL) << "Node " << name << " not found in the configuration."; |
| 967 | } |
| 968 | |
Austin Schuh | 681a247 | 2020-12-31 23:55:40 -0800 | [diff] [blame] | 969 | size_t NodesCount(const Configuration *config) { |
| 970 | if (!MultiNode(config)) { |
| 971 | return 1u; |
| 972 | } |
| 973 | |
| 974 | return config->nodes()->size(); |
| 975 | } |
| 976 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 977 | std::vector<const Node *> GetNodes(const Configuration *config) { |
| 978 | std::vector<const Node *> nodes; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 979 | if (MultiNode(config)) { |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 980 | for (const Node *node : *config->nodes()) { |
| 981 | nodes.emplace_back(node); |
| 982 | } |
| 983 | } else { |
| 984 | nodes.emplace_back(nullptr); |
| 985 | } |
| 986 | return nodes; |
| 987 | } |
| 988 | |
Austin Schuh | 6546533 | 2020-11-05 17:36:53 -0800 | [diff] [blame] | 989 | std::vector<const Node *> GetNodesWithTag(const Configuration *config, |
| 990 | std::string_view tag) { |
| 991 | std::vector<const Node *> nodes; |
| 992 | if (!MultiNode(config)) { |
| 993 | nodes.emplace_back(nullptr); |
| 994 | } else { |
| 995 | for (const Node *node : *config->nodes()) { |
| 996 | if (!node->has_tags()) { |
| 997 | continue; |
| 998 | } |
| 999 | bool did_found_tag = false; |
| 1000 | for (const flatbuffers::String *found_tag : *node->tags()) { |
| 1001 | if (found_tag->string_view() == tag) { |
| 1002 | did_found_tag = true; |
| 1003 | break; |
| 1004 | } |
| 1005 | } |
| 1006 | if (did_found_tag) { |
| 1007 | nodes.emplace_back(node); |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | return nodes; |
| 1012 | } |
| 1013 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 1014 | bool MultiNode(const Configuration *config) { return config->has_nodes(); } |
| 1015 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1016 | bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 1017 | if (node == nullptr) { |
| 1018 | return true; |
| 1019 | } |
Austin Schuh | 3c5dae5 | 2020-10-06 18:55:18 -0700 | [diff] [blame] | 1020 | CHECK(channel->has_source_node()) << FlatbufferToJson(channel); |
| 1021 | CHECK(node->has_name()) << FlatbufferToJson(node); |
Austin Schuh | 196a445 | 2020-03-15 23:12:03 -0700 | [diff] [blame] | 1022 | return (CHECK_NOTNULL(channel)->source_node()->string_view() == |
| 1023 | node->name()->string_view()); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 1027 | if (node == nullptr) { |
| 1028 | return true; |
| 1029 | } |
| 1030 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1031 | if (channel->source_node()->string_view() == node->name()->string_view()) { |
| 1032 | return true; |
| 1033 | } |
| 1034 | |
| 1035 | if (!channel->has_destination_nodes()) { |
| 1036 | return false; |
| 1037 | } |
| 1038 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1039 | for (const Connection *connection : *channel->destination_nodes()) { |
| 1040 | CHECK(connection->has_name()); |
| 1041 | if (connection->name()->string_view() == node->name()->string_view()) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 1042 | return true; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | return false; |
| 1047 | } |
| 1048 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1049 | bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | 48e9450 | 2021-06-18 18:35:53 -0700 | [diff] [blame] | 1050 | if (node == nullptr) { |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 1051 | // Single node world. If there is a local logger, then we want to use |
| 1052 | // it. |
Austin Schuh | 48e9450 | 2021-06-18 18:35:53 -0700 | [diff] [blame] | 1053 | if (channel->logger() == LoggerConfig::LOCAL_LOGGER) { |
| 1054 | return true; |
| 1055 | } else if (channel->logger() == LoggerConfig::NOT_LOGGED) { |
| 1056 | return false; |
| 1057 | } |
| 1058 | LOG(FATAL) << "Unsupported logging configuration in a single node world: " |
| 1059 | << CleanedChannelToString(channel); |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 1060 | } |
| 1061 | return ChannelMessageIsLoggedOnNode( |
| 1062 | channel, CHECK_NOTNULL(node)->name()->string_view()); |
| 1063 | } |
| 1064 | |
| 1065 | bool ChannelMessageIsLoggedOnNode(const Channel *channel, |
| 1066 | std::string_view node_name) { |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 1067 | switch (channel->logger()) { |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1068 | case LoggerConfig::LOCAL_LOGGER: |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 1069 | return channel->source_node()->string_view() == node_name; |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1070 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 1071 | CHECK(channel->has_logger_nodes()); |
| 1072 | CHECK_GT(channel->logger_nodes()->size(), 0u); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1073 | |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 1074 | if (channel->source_node()->string_view() == node_name) { |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1075 | return true; |
| 1076 | } |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 1077 | |
| 1078 | [[fallthrough]]; |
| 1079 | case LoggerConfig::REMOTE_LOGGER: |
| 1080 | CHECK(channel->has_logger_nodes()); |
| 1081 | CHECK_GT(channel->logger_nodes()->size(), 0u); |
| 1082 | for (const flatbuffers::String *logger_node : *channel->logger_nodes()) { |
Austin Schuh | 2bb80e0 | 2021-03-20 21:46:17 -0700 | [diff] [blame] | 1083 | if (logger_node->string_view() == node_name) { |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 1084 | return true; |
| 1085 | } |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | return false; |
| 1089 | case LoggerConfig::NOT_LOGGED: |
| 1090 | return false; |
| 1091 | } |
| 1092 | |
| 1093 | LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger()); |
| 1094 | } |
| 1095 | |
| 1096 | const Connection *ConnectionToNode(const Channel *channel, const Node *node) { |
| 1097 | if (!channel->has_destination_nodes()) { |
| 1098 | return nullptr; |
| 1099 | } |
| 1100 | for (const Connection *connection : *channel->destination_nodes()) { |
| 1101 | if (connection->name()->string_view() == node->name()->string_view()) { |
| 1102 | return connection; |
| 1103 | } |
| 1104 | } |
| 1105 | return nullptr; |
| 1106 | } |
| 1107 | |
| 1108 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel, |
| 1109 | const Node *node, |
| 1110 | const Node *logger_node) { |
| 1111 | const Connection *connection = ConnectionToNode(channel, node); |
| 1112 | if (connection == nullptr) { |
| 1113 | return false; |
| 1114 | } |
| 1115 | return ConnectionDeliveryTimeIsLoggedOnNode(connection, logger_node); |
| 1116 | } |
| 1117 | |
| 1118 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection, |
| 1119 | const Node *node) { |
| 1120 | switch (connection->timestamp_logger()) { |
| 1121 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 1122 | CHECK(connection->has_timestamp_logger_nodes()); |
| 1123 | CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1124 | if (connection->name()->string_view() == node->name()->string_view()) { |
| 1125 | return true; |
| 1126 | } |
| 1127 | |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 1128 | [[fallthrough]]; |
| 1129 | case LoggerConfig::REMOTE_LOGGER: |
| 1130 | CHECK(connection->has_timestamp_logger_nodes()); |
| 1131 | CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u); |
| 1132 | for (const flatbuffers::String *timestamp_logger_node : |
| 1133 | *connection->timestamp_logger_nodes()) { |
| 1134 | if (timestamp_logger_node->string_view() == |
| 1135 | node->name()->string_view()) { |
| 1136 | return true; |
| 1137 | } |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | return false; |
| 1141 | case LoggerConfig::LOCAL_LOGGER: |
| 1142 | return connection->name()->string_view() == node->name()->string_view(); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 1143 | case LoggerConfig::NOT_LOGGED: |
| 1144 | return false; |
| 1145 | } |
| 1146 | |
| 1147 | LOG(FATAL) << "Unknown logger config " |
| 1148 | << static_cast<int>(connection->timestamp_logger()); |
| 1149 | } |
| 1150 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1151 | std::vector<std::string_view> SourceNodeNames(const Configuration *config, |
| 1152 | const Node *my_node) { |
| 1153 | std::set<std::string_view> result_set; |
| 1154 | |
| 1155 | for (const Channel *channel : *config->channels()) { |
| 1156 | if (channel->has_destination_nodes()) { |
| 1157 | for (const Connection *connection : *channel->destination_nodes()) { |
| 1158 | if (connection->name()->string_view() == |
| 1159 | my_node->name()->string_view()) { |
| 1160 | result_set.insert(channel->source_node()->string_view()); |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | std::vector<std::string_view> result; |
| 1167 | for (const std::string_view source : result_set) { |
| 1168 | VLOG(1) << "Found a source node of " << source; |
| 1169 | result.emplace_back(source); |
| 1170 | } |
| 1171 | return result; |
| 1172 | } |
| 1173 | |
| 1174 | std::vector<std::string_view> DestinationNodeNames(const Configuration *config, |
| 1175 | const Node *my_node) { |
| 1176 | std::vector<std::string_view> result; |
| 1177 | |
| 1178 | for (const Channel *channel : *config->channels()) { |
| 1179 | if (channel->has_source_node() && channel->source_node()->string_view() == |
| 1180 | my_node->name()->string_view()) { |
| 1181 | if (!channel->has_destination_nodes()) continue; |
| 1182 | |
| 1183 | if (channel->source_node()->string_view() != |
| 1184 | my_node->name()->string_view()) { |
| 1185 | continue; |
| 1186 | } |
| 1187 | |
| 1188 | for (const Connection *connection : *channel->destination_nodes()) { |
| 1189 | if (std::find(result.begin(), result.end(), |
| 1190 | connection->name()->string_view()) == result.end()) { |
| 1191 | result.emplace_back(connection->name()->string_view()); |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | for (const std::string_view destination : result) { |
| 1198 | VLOG(1) << "Found a destination node of " << destination; |
| 1199 | } |
| 1200 | return result; |
| 1201 | } |
| 1202 | |
Austin Schuh | 8d7e0bb | 2020-10-02 17:57:00 -0700 | [diff] [blame] | 1203 | std::vector<const Node *> TimestampNodes(const Configuration *config, |
| 1204 | const Node *my_node) { |
| 1205 | if (!configuration::MultiNode(config)) { |
| 1206 | CHECK(my_node == nullptr); |
| 1207 | return std::vector<const Node *>{}; |
| 1208 | } |
| 1209 | |
| 1210 | std::set<const Node *> timestamp_logger_nodes; |
| 1211 | for (const Channel *channel : *config->channels()) { |
| 1212 | if (!configuration::ChannelIsSendableOnNode(channel, my_node)) { |
| 1213 | continue; |
| 1214 | } |
| 1215 | if (!channel->has_destination_nodes()) { |
| 1216 | continue; |
| 1217 | } |
| 1218 | for (const Connection *connection : *channel->destination_nodes()) { |
| 1219 | const Node *other_node = |
| 1220 | configuration::GetNode(config, connection->name()->string_view()); |
| 1221 | |
| 1222 | if (configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection, |
| 1223 | my_node)) { |
| 1224 | VLOG(1) << "Timestamps are logged from " |
| 1225 | << FlatbufferToJson(other_node); |
| 1226 | timestamp_logger_nodes.insert(other_node); |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | std::vector<const Node *> result; |
| 1232 | for (const Node *node : timestamp_logger_nodes) { |
| 1233 | result.emplace_back(node); |
| 1234 | } |
| 1235 | return result; |
| 1236 | } |
| 1237 | |
Austin Schuh | d2e2f6a | 2021-02-07 20:46:16 -0800 | [diff] [blame] | 1238 | const Application *GetApplication(const Configuration *config, |
| 1239 | const Node *my_node, |
| 1240 | std::string_view application_name) { |
| 1241 | if (config->has_applications()) { |
| 1242 | auto application_iterator = std::lower_bound( |
| 1243 | config->applications()->cbegin(), config->applications()->cend(), |
| 1244 | application_name, CompareApplications); |
| 1245 | if (application_iterator != config->applications()->cend() && |
| 1246 | EqualsApplications(*application_iterator, application_name)) { |
| 1247 | if (MultiNode(config)) { |
| 1248 | // Ok, we need |
| 1249 | CHECK(application_iterator->has_nodes()); |
| 1250 | CHECK(my_node != nullptr); |
| 1251 | for (const flatbuffers::String *str : *application_iterator->nodes()) { |
| 1252 | if (str->string_view() == my_node->name()->string_view()) { |
| 1253 | return *application_iterator; |
| 1254 | } |
| 1255 | } |
| 1256 | } else { |
| 1257 | return *application_iterator; |
| 1258 | } |
| 1259 | } |
| 1260 | } |
| 1261 | return nullptr; |
| 1262 | } |
| 1263 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 1264 | } // namespace configuration |
| 1265 | } // namespace aos |