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