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