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