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