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 | |
| 3 | #include <string.h> |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 4 | #include <stdlib.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <netinet/in.h> |
| 7 | #include <arpa/inet.h> |
| 8 | #include <ifaddrs.h> |
| 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. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 83 | std::string_view ExtractFolder( |
| 84 | 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, |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [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 | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 198 | std::string_view *name, std::string_view type, |
| 199 | const Node *node) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 200 | // For the same reason we merge configs in reverse order, we want to process |
| 201 | // maps in reverse order. That lets the outer config overwrite channels from |
| 202 | // the inner configs. |
| 203 | for (auto i = maps->rbegin(); i != maps->rend(); ++i) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 204 | if (!i->has_match() || !i->match()->has_name()) { |
| 205 | continue; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 206 | } |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 207 | if (!i->has_rename() || !i->rename()->has_name()) { |
| 208 | continue; |
| 209 | } |
| 210 | |
| 211 | // Handle normal maps (now that we know that match and rename are filled |
| 212 | // out). |
| 213 | if (i->match()->name()->string_view() != *name) { |
| 214 | continue; |
| 215 | } |
| 216 | |
| 217 | // Handle type specific maps. |
| 218 | if (i->match()->has_type() && i->match()->type()->string_view() != type) { |
| 219 | continue; |
| 220 | } |
| 221 | |
| 222 | if (node != nullptr && i->match()->has_source_node() && |
| 223 | i->match()->source_node()->string_view() != |
| 224 | node->name()->string_view()) { |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | VLOG(1) << "Renamed \"" << *name << "\" to \"" |
| 229 | << i->rename()->name()->string_view() << "\""; |
| 230 | *name = i->rename()->name()->string_view(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | } // namespace |
| 235 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 236 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 237 | const Flatbuffer<Configuration> &config) { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 238 | // 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] | 239 | // have seen before and merge the updates in. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 240 | absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 241 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 242 | if (config.message().has_channels()) { |
| 243 | for (const Channel *c : *config.message().channels()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 244 | // Ignore malformed entries. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 245 | if (!c->has_name()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 246 | continue; |
| 247 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 248 | if (!c->has_type()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 249 | continue; |
| 250 | } |
| 251 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 252 | // Attempt to insert the channel. |
| 253 | auto result = channels.insert(CopyFlatBuffer(c)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 254 | if (!result.second) { |
| 255 | // Already there, so merge the new table into the original. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 256 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(c)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // Now repeat this for the application list. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 262 | absl::btree_set<FlatbufferDetachedBuffer<Application>> applications; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 263 | if (config.message().has_applications()) { |
| 264 | for (const Application *a : *config.message().applications()) { |
| 265 | if (!a->has_name()) { |
| 266 | continue; |
| 267 | } |
| 268 | |
| 269 | auto result = applications.insert(CopyFlatBuffer(a)); |
| 270 | if (!result.second) { |
| 271 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(a)); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 276 | // Now repeat this for the node list. |
| 277 | absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes; |
| 278 | if (config.message().has_nodes()) { |
| 279 | for (const Node *n : *config.message().nodes()) { |
| 280 | if (!n->has_name()) { |
| 281 | continue; |
| 282 | } |
| 283 | |
| 284 | auto result = nodes.insert(CopyFlatBuffer(n)); |
| 285 | if (!result.second) { |
| 286 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(n)); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 291 | flatbuffers::FlatBufferBuilder fbb; |
| 292 | fbb.ForceDefaults(1); |
| 293 | |
| 294 | // 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] | 295 | // Channels |
| 296 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 297 | channels_offset; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 298 | { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 299 | ::std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 300 | for (const FlatbufferDetachedBuffer<Channel> &c : channels) { |
| 301 | channel_offsets.emplace_back( |
| 302 | CopyFlatBuffer<Channel>(&c.message(), &fbb)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 303 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 304 | channels_offset = fbb.CreateVector(channel_offsets); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | // Applications |
| 308 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 309 | applications_offset; |
| 310 | { |
| 311 | ::std::vector<flatbuffers::Offset<Application>> applications_offsets; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 312 | for (const FlatbufferDetachedBuffer<Application> &a : applications) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 313 | applications_offsets.emplace_back( |
| 314 | CopyFlatBuffer<Application>(&a.message(), &fbb)); |
| 315 | } |
| 316 | applications_offset = fbb.CreateVector(applications_offsets); |
| 317 | } |
| 318 | |
| 319 | // Just copy the maps |
| 320 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
| 321 | maps_offset; |
| 322 | { |
| 323 | ::std::vector<flatbuffers::Offset<Map>> map_offsets; |
| 324 | if (config.message().has_maps()) { |
| 325 | for (const Map *m : *config.message().maps()) { |
| 326 | map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb)); |
| 327 | } |
| 328 | maps_offset = fbb.CreateVector(map_offsets); |
| 329 | } |
| 330 | } |
| 331 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 332 | // Nodes |
| 333 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 334 | nodes_offset; |
| 335 | { |
| 336 | ::std::vector<flatbuffers::Offset<Node>> node_offsets; |
| 337 | for (const FlatbufferDetachedBuffer<Node> &n : nodes) { |
| 338 | node_offsets.emplace_back(CopyFlatBuffer<Node>(&n.message(), &fbb)); |
| 339 | } |
| 340 | nodes_offset = fbb.CreateVector(node_offsets); |
| 341 | } |
| 342 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 343 | // And then build a Configuration with them all. |
| 344 | ConfigurationBuilder configuration_builder(fbb); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 345 | configuration_builder.add_channels(channels_offset); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 346 | if (config.message().has_maps()) { |
| 347 | configuration_builder.add_maps(maps_offset); |
| 348 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 349 | if (config.message().has_applications()) { |
| 350 | configuration_builder.add_applications(applications_offset); |
| 351 | } |
| 352 | if (config.message().has_nodes()) { |
| 353 | configuration_builder.add_nodes(nodes_offset); |
| 354 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 355 | |
| 356 | fbb.Finish(configuration_builder.Finish()); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 357 | |
| 358 | // Now, validate that if there is a node list, every channel has a source |
| 359 | // node. |
| 360 | FlatbufferDetachedBuffer<Configuration> result(fbb.Release()); |
| 361 | |
| 362 | // Check that if there is a node list, all the source nodes are filled out and |
| 363 | // valid, and all the destination nodes are valid (and not the source). This |
| 364 | // is a basic consistency check. |
| 365 | if (result.message().has_nodes()) { |
| 366 | for (const Channel *c : *config.message().channels()) { |
| 367 | CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c) |
| 368 | << " is missing \"source_node\""; |
| 369 | CHECK(GetNode(&result.message(), c->source_node()->string_view()) != |
| 370 | nullptr) |
| 371 | << ": Channel " << FlatbufferToJson(c) |
| 372 | << " has an unknown \"source_node\""; |
| 373 | |
| 374 | if (c->has_destination_nodes()) { |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 375 | for (const Connection *connection : *c->destination_nodes()) { |
| 376 | CHECK(connection->has_name()); |
| 377 | CHECK(GetNode(&result.message(), connection->name()->string_view()) != |
| 378 | nullptr) |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 379 | << ": Channel " << FlatbufferToJson(c) |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 380 | << " has an unknown \"destination_nodes\" " |
| 381 | << connection->name()->string_view(); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 382 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 383 | switch (connection->timestamp_logger()) { |
| 384 | case LoggerConfig::LOCAL_LOGGER: |
| 385 | case LoggerConfig::NOT_LOGGED: |
| 386 | CHECK(!connection->has_timestamp_logger_node()); |
| 387 | break; |
| 388 | case LoggerConfig::REMOTE_LOGGER: |
| 389 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
| 390 | CHECK(connection->has_timestamp_logger_node()); |
| 391 | CHECK( |
| 392 | GetNode(&result.message(), |
| 393 | connection->timestamp_logger_node()->string_view()) != |
| 394 | nullptr) |
| 395 | << ": Channel " << FlatbufferToJson(c) |
| 396 | << " has an unknown \"timestamp_logger_node\"" |
| 397 | << connection->name()->string_view(); |
| 398 | break; |
| 399 | } |
| 400 | |
| 401 | CHECK_NE(connection->name()->string_view(), |
| 402 | c->source_node()->string_view()) |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 403 | << ": Channel " << FlatbufferToJson(c) |
| 404 | << " is forwarding data to itself"; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | return result; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 413 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 414 | const std::string_view path) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 415 | // We only want to read a file once. So track the visited files in a set. |
| 416 | absl::btree_set<std::string> visited_paths; |
| 417 | return MergeConfiguration(ReadConfig(path, &visited_paths)); |
| 418 | } |
| 419 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 420 | const Channel *GetChannel(const Configuration *config, std::string_view name, |
| 421 | std::string_view type, |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 422 | std::string_view application_name, const Node *node) { |
| 423 | const std::string_view original_name = name; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 424 | VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type |
| 425 | << "\" }"; |
| 426 | |
| 427 | // First handle application specific maps. Only do this if we have a matching |
| 428 | // application name, and it has maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 429 | if (config->has_applications()) { |
| 430 | auto application_iterator = std::lower_bound( |
| 431 | config->applications()->cbegin(), config->applications()->cend(), |
| 432 | application_name, CompareApplications); |
| 433 | if (application_iterator != config->applications()->cend() && |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 434 | EqualsApplications(*application_iterator, application_name)) { |
| 435 | if (application_iterator->has_maps()) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 436 | HandleMaps(application_iterator->maps(), &name, type, node); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | // Now do global maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 442 | if (config->has_maps()) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 443 | HandleMaps(config->maps(), &name, type, node); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 446 | if (original_name != name) { |
| 447 | VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \"" |
| 448 | << type << "\" }"; |
| 449 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 450 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 451 | // Then look for the channel. |
| 452 | auto channel_iterator = |
| 453 | std::lower_bound(config->channels()->cbegin(), |
| 454 | config->channels()->cend(), |
| 455 | std::make_pair(name, type), CompareChannels); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 456 | |
| 457 | // Make sure we actually found it, and it matches. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 458 | if (channel_iterator != config->channels()->cend() && |
| 459 | EqualsChannels(*channel_iterator, std::make_pair(name, type))) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 460 | if (VLOG_IS_ON(2)) { |
| 461 | VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator); |
| 462 | } else if (VLOG_IS_ON(1)) { |
| 463 | VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator); |
| 464 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 465 | return *channel_iterator; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 466 | } else { |
| 467 | VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \"" |
| 468 | << type << "\" }"; |
| 469 | return nullptr; |
| 470 | } |
| 471 | } |
| 472 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 473 | size_t ChannelIndex(const Configuration *configuration, |
| 474 | const Channel *channel) { |
| 475 | CHECK(configuration->channels() != nullptr) << ": No channels"; |
| 476 | |
| 477 | auto c = std::find(configuration->channels()->begin(), |
| 478 | configuration->channels()->end(), channel); |
| 479 | CHECK(c != configuration->channels()->end()) |
| 480 | << ": Channel pointer not found in configuration()->channels()"; |
| 481 | |
| 482 | return std::distance(configuration->channels()->begin(), c); |
| 483 | } |
| 484 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 485 | std::string CleanedChannelToString(const Channel *channel) { |
| 486 | FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel); |
| 487 | cleaned_channel.mutable_message()->clear_schema(); |
| 488 | return FlatbufferToJson(cleaned_channel); |
| 489 | } |
| 490 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 491 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 492 | const Flatbuffer<Configuration> &config, |
| 493 | const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) { |
| 494 | flatbuffers::FlatBufferBuilder fbb; |
| 495 | fbb.ForceDefaults(1); |
| 496 | |
| 497 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 498 | channels_offset; |
| 499 | if (config.message().has_channels()) { |
| 500 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 501 | for (const Channel *c : *config.message().channels()) { |
| 502 | flatbuffers::FlatBufferBuilder channel_fbb; |
| 503 | channel_fbb.ForceDefaults(1); |
| 504 | |
| 505 | // Search for a schema with a matching type. |
| 506 | const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr; |
| 507 | for (const aos::FlatbufferString<reflection::Schema> &schema: schemas) { |
| 508 | if (schema.message().root_table() != nullptr) { |
| 509 | if (schema.message().root_table()->name()->string_view() == |
| 510 | c->type()->string_view()) { |
| 511 | found_schema = &schema; |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | CHECK(found_schema != nullptr) |
| 517 | << ": Failed to find schema for " << FlatbufferToJson(c); |
| 518 | |
| 519 | // The following is wasteful, but works. |
| 520 | // |
| 521 | // Copy it into a Channel object by creating an object with only the |
| 522 | // schema populated and merge that into the current channel. |
| 523 | flatbuffers::Offset<reflection::Schema> schema_offset = |
| 524 | CopyFlatBuffer<reflection::Schema>(&found_schema->message(), |
| 525 | &channel_fbb); |
| 526 | Channel::Builder channel_builder(channel_fbb); |
| 527 | channel_builder.add_schema(schema_offset); |
| 528 | channel_fbb.Finish(channel_builder.Finish()); |
| 529 | FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer( |
| 530 | channel_fbb.Release()); |
| 531 | |
| 532 | FlatbufferDetachedBuffer<Channel> merged_channel( |
| 533 | MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c))); |
| 534 | |
| 535 | channel_offsets.emplace_back( |
| 536 | CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb)); |
| 537 | } |
| 538 | channels_offset = fbb.CreateVector(channel_offsets); |
| 539 | } |
| 540 | |
| 541 | // Copy the applications and maps unmodified. |
| 542 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 543 | applications_offset; |
| 544 | { |
| 545 | ::std::vector<flatbuffers::Offset<Application>> applications_offsets; |
| 546 | if (config.message().has_applications()) { |
| 547 | for (const Application *a : *config.message().applications()) { |
| 548 | applications_offsets.emplace_back(CopyFlatBuffer<Application>(a, &fbb)); |
| 549 | } |
| 550 | } |
| 551 | applications_offset = fbb.CreateVector(applications_offsets); |
| 552 | } |
| 553 | |
| 554 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
| 555 | maps_offset; |
| 556 | { |
| 557 | ::std::vector<flatbuffers::Offset<Map>> map_offsets; |
| 558 | if (config.message().has_maps()) { |
| 559 | for (const Map *m : *config.message().maps()) { |
| 560 | map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb)); |
| 561 | } |
| 562 | maps_offset = fbb.CreateVector(map_offsets); |
| 563 | } |
| 564 | } |
| 565 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 566 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 567 | nodes_offset; |
| 568 | { |
| 569 | ::std::vector<flatbuffers::Offset<Node>> node_offsets; |
| 570 | if (config.message().has_nodes()) { |
| 571 | for (const Node *n : *config.message().nodes()) { |
| 572 | node_offsets.emplace_back(CopyFlatBuffer<Node>(n, &fbb)); |
| 573 | } |
| 574 | nodes_offset = fbb.CreateVector(node_offsets); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | // Now insert everything else in unmodified. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 579 | ConfigurationBuilder configuration_builder(fbb); |
| 580 | if (config.message().has_channels()) { |
| 581 | configuration_builder.add_channels(channels_offset); |
| 582 | } |
| 583 | if (config.message().has_maps()) { |
| 584 | configuration_builder.add_maps(maps_offset); |
| 585 | } |
| 586 | if (config.message().has_applications()) { |
| 587 | configuration_builder.add_applications(applications_offset); |
| 588 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 589 | if (config.message().has_nodes()) { |
| 590 | configuration_builder.add_nodes(nodes_offset); |
| 591 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 592 | |
| 593 | fbb.Finish(configuration_builder.Finish()); |
| 594 | return fbb.Release(); |
| 595 | } |
| 596 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 597 | const Node *GetNodeFromHostname(const Configuration *config, |
| 598 | std::string_view hostname) { |
| 599 | for (const Node *node : *config->nodes()) { |
| 600 | if (node->hostname()->string_view() == hostname) { |
| 601 | return node; |
| 602 | } |
| 603 | } |
| 604 | return nullptr; |
| 605 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 606 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 607 | const Node *GetMyNode(const Configuration *config) { |
| 608 | const std::string hostname = (FLAGS_override_hostname.size() > 0) |
| 609 | ? FLAGS_override_hostname |
| 610 | : network::GetHostname(); |
| 611 | const Node *node = GetNodeFromHostname(config, hostname); |
| 612 | if (node != nullptr) return node; |
| 613 | |
| 614 | LOG(FATAL) << "Unknown node for host: " << hostname |
| 615 | << ". Consider using --override_hostname if hostname detection " |
| 616 | "is wrong."; |
| 617 | return nullptr; |
| 618 | } |
| 619 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 620 | const Node *GetNode(const Configuration *config, const Node *node) { |
| 621 | if (!MultiNode(config)) { |
| 622 | CHECK(node == nullptr) << ": Provided a node in a single node world."; |
| 623 | return nullptr; |
| 624 | } else { |
| 625 | CHECK(node != nullptr); |
| 626 | CHECK(node->has_name()); |
| 627 | return GetNode(config, node->name()->string_view()); |
| 628 | } |
| 629 | } |
| 630 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 631 | const Node *GetNode(const Configuration *config, std::string_view name) { |
Austin Schuh | fd96062 | 2020-01-01 13:22:55 -0800 | [diff] [blame] | 632 | CHECK(config->has_nodes()) |
| 633 | << ": Asking for a node from a single node configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 634 | for (const Node *node : *config->nodes()) { |
Austin Schuh | fd96062 | 2020-01-01 13:22:55 -0800 | [diff] [blame] | 635 | CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 636 | if (node->name()->string_view() == name) { |
| 637 | return node; |
| 638 | } |
| 639 | } |
| 640 | return nullptr; |
| 641 | } |
| 642 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 643 | const Node *GetNodeOrDie(const Configuration *config, const Node *node) { |
| 644 | if (!MultiNode(config)) { |
| 645 | CHECK(node == nullptr) << ": Provided a node in a single node world."; |
| 646 | return nullptr; |
| 647 | } else { |
| 648 | const Node *config_node = GetNode(config, node); |
| 649 | if (config_node == nullptr) { |
| 650 | LOG(FATAL) << "Couldn't find node matching " << FlatbufferToJson(node); |
| 651 | } |
| 652 | return config_node; |
| 653 | } |
| 654 | } |
| 655 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 656 | namespace { |
| 657 | int GetNodeIndexFromConfig(const Configuration *config, const Node *node) { |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 658 | int node_index = 0; |
| 659 | for (const Node *iterated_node : *config->nodes()) { |
| 660 | if (iterated_node == node) { |
| 661 | return node_index; |
| 662 | } |
| 663 | ++node_index; |
| 664 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 665 | return -1; |
| 666 | } |
| 667 | } // namespace |
| 668 | |
| 669 | int GetNodeIndex(const Configuration *config, const Node *node) { |
| 670 | if (!MultiNode(config)) { |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | { |
| 675 | int node_index = GetNodeIndexFromConfig(config, node); |
| 676 | if (node_index != -1) { |
| 677 | return node_index; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | const Node *result = GetNode(config, node); |
| 682 | CHECK(result != nullptr); |
| 683 | |
| 684 | { |
| 685 | int node_index = GetNodeIndexFromConfig(config, node); |
| 686 | if (node_index != -1) { |
| 687 | return node_index; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | LOG(FATAL) << "Node " << FlatbufferToJson(node) |
| 692 | << " not found in the configuration."; |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | std::vector<const Node *> GetNodes(const Configuration *config) { |
| 696 | std::vector<const Node *> nodes; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 697 | if (MultiNode(config)) { |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 698 | for (const Node *node : *config->nodes()) { |
| 699 | nodes.emplace_back(node); |
| 700 | } |
| 701 | } else { |
| 702 | nodes.emplace_back(nullptr); |
| 703 | } |
| 704 | return nodes; |
| 705 | } |
| 706 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 707 | bool MultiNode(const Configuration *config) { return config->has_nodes(); } |
| 708 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 709 | bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 710 | if (node == nullptr) { |
| 711 | return true; |
| 712 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 713 | return (channel->source_node()->string_view() == node->name()->string_view()); |
| 714 | } |
| 715 | |
| 716 | bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 717 | if (node == nullptr) { |
| 718 | return true; |
| 719 | } |
| 720 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 721 | if (channel->source_node()->string_view() == node->name()->string_view()) { |
| 722 | return true; |
| 723 | } |
| 724 | |
| 725 | if (!channel->has_destination_nodes()) { |
| 726 | return false; |
| 727 | } |
| 728 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 729 | for (const Connection *connection : *channel->destination_nodes()) { |
| 730 | CHECK(connection->has_name()); |
| 731 | if (connection->name()->string_view() == node->name()->string_view()) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 732 | return true; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | return false; |
| 737 | } |
| 738 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 739 | bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) { |
| 740 | switch(channel->logger()) { |
| 741 | case LoggerConfig::LOCAL_LOGGER: |
| 742 | if (node == nullptr) { |
| 743 | // Single node world. If there is a local logger, then we want to use |
| 744 | // it. |
| 745 | return true; |
| 746 | } |
| 747 | return channel->source_node()->string_view() == |
| 748 | node->name()->string_view(); |
| 749 | case LoggerConfig::REMOTE_LOGGER: |
| 750 | CHECK(channel->has_logger_node()); |
| 751 | |
| 752 | return channel->logger_node()->string_view() == |
| 753 | CHECK_NOTNULL(node)->name()->string_view(); |
| 754 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
| 755 | CHECK(channel->has_logger_node()); |
| 756 | |
| 757 | if (channel->source_node()->string_view() == |
| 758 | CHECK_NOTNULL(node)->name()->string_view()) { |
| 759 | return true; |
| 760 | } |
| 761 | if (channel->logger_node()->string_view() == node->name()->string_view()) { |
| 762 | return true; |
| 763 | } |
| 764 | |
| 765 | return false; |
| 766 | case LoggerConfig::NOT_LOGGED: |
| 767 | return false; |
| 768 | } |
| 769 | |
| 770 | LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger()); |
| 771 | } |
| 772 | |
| 773 | const Connection *ConnectionToNode(const Channel *channel, const Node *node) { |
| 774 | if (!channel->has_destination_nodes()) { |
| 775 | return nullptr; |
| 776 | } |
| 777 | for (const Connection *connection : *channel->destination_nodes()) { |
| 778 | if (connection->name()->string_view() == node->name()->string_view()) { |
| 779 | return connection; |
| 780 | } |
| 781 | } |
| 782 | return nullptr; |
| 783 | } |
| 784 | |
| 785 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel, |
| 786 | const Node *node, |
| 787 | const Node *logger_node) { |
| 788 | const Connection *connection = ConnectionToNode(channel, node); |
| 789 | if (connection == nullptr) { |
| 790 | return false; |
| 791 | } |
| 792 | return ConnectionDeliveryTimeIsLoggedOnNode(connection, logger_node); |
| 793 | } |
| 794 | |
| 795 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection, |
| 796 | const Node *node) { |
| 797 | switch (connection->timestamp_logger()) { |
| 798 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
| 799 | CHECK(connection->has_timestamp_logger_node()); |
| 800 | if (connection->name()->string_view() == node->name()->string_view()) { |
| 801 | return true; |
| 802 | } |
| 803 | |
| 804 | if (connection->timestamp_logger_node()->string_view() == |
| 805 | node->name()->string_view()) { |
| 806 | return true; |
| 807 | } |
| 808 | |
| 809 | return false; |
| 810 | case LoggerConfig::LOCAL_LOGGER: |
| 811 | return connection->name()->string_view() == node->name()->string_view(); |
| 812 | case LoggerConfig::REMOTE_LOGGER: |
| 813 | CHECK(connection->has_timestamp_logger_node()); |
| 814 | |
| 815 | return connection->timestamp_logger_node()->string_view() == |
| 816 | node->name()->string_view(); |
| 817 | case LoggerConfig::NOT_LOGGED: |
| 818 | return false; |
| 819 | } |
| 820 | |
| 821 | LOG(FATAL) << "Unknown logger config " |
| 822 | << static_cast<int>(connection->timestamp_logger()); |
| 823 | } |
| 824 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 825 | std::vector<std::string_view> SourceNodeNames(const Configuration *config, |
| 826 | const Node *my_node) { |
| 827 | std::set<std::string_view> result_set; |
| 828 | |
| 829 | for (const Channel *channel : *config->channels()) { |
| 830 | if (channel->has_destination_nodes()) { |
| 831 | for (const Connection *connection : *channel->destination_nodes()) { |
| 832 | if (connection->name()->string_view() == |
| 833 | my_node->name()->string_view()) { |
| 834 | result_set.insert(channel->source_node()->string_view()); |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | std::vector<std::string_view> result; |
| 841 | for (const std::string_view source : result_set) { |
| 842 | VLOG(1) << "Found a source node of " << source; |
| 843 | result.emplace_back(source); |
| 844 | } |
| 845 | return result; |
| 846 | } |
| 847 | |
| 848 | std::vector<std::string_view> DestinationNodeNames(const Configuration *config, |
| 849 | const Node *my_node) { |
| 850 | std::vector<std::string_view> result; |
| 851 | |
| 852 | for (const Channel *channel : *config->channels()) { |
| 853 | if (channel->has_source_node() && channel->source_node()->string_view() == |
| 854 | my_node->name()->string_view()) { |
| 855 | if (!channel->has_destination_nodes()) continue; |
| 856 | |
| 857 | if (channel->source_node()->string_view() != |
| 858 | my_node->name()->string_view()) { |
| 859 | continue; |
| 860 | } |
| 861 | |
| 862 | for (const Connection *connection : *channel->destination_nodes()) { |
| 863 | if (std::find(result.begin(), result.end(), |
| 864 | connection->name()->string_view()) == result.end()) { |
| 865 | result.emplace_back(connection->name()->string_view()); |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | for (const std::string_view destination : result) { |
| 872 | VLOG(1) << "Found a destination node of " << destination; |
| 873 | } |
| 874 | return result; |
| 875 | } |
| 876 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 877 | } // namespace configuration |
| 878 | } // namespace aos |