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