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