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