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