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