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; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 292 | fbb.ForceDefaults(true); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 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. |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 365 | if (result.message().has_nodes() && config.message().has_channels()) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 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 | |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 420 | FlatbufferDetachedBuffer<Configuration> MergeWithConfig( |
| 421 | const Configuration *config, std::string_view json) { |
| 422 | FlatbufferDetachedBuffer<Configuration> addition = |
| 423 | JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable()); |
| 424 | |
| 425 | return MergeConfiguration(MergeFlatBuffers(config, &addition.message())); |
| 426 | } |
| 427 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 428 | const Channel *GetChannel(const Configuration *config, std::string_view name, |
| 429 | std::string_view type, |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 430 | std::string_view application_name, const Node *node) { |
| 431 | const std::string_view original_name = name; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 432 | VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type |
| 433 | << "\" }"; |
| 434 | |
| 435 | // First handle application specific maps. Only do this if we have a matching |
| 436 | // application name, and it has maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 437 | if (config->has_applications()) { |
| 438 | auto application_iterator = std::lower_bound( |
| 439 | config->applications()->cbegin(), config->applications()->cend(), |
| 440 | application_name, CompareApplications); |
| 441 | if (application_iterator != config->applications()->cend() && |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 442 | EqualsApplications(*application_iterator, application_name)) { |
| 443 | if (application_iterator->has_maps()) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 444 | HandleMaps(application_iterator->maps(), &name, type, node); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | // Now do global maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 450 | if (config->has_maps()) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 451 | HandleMaps(config->maps(), &name, type, node); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 454 | if (original_name != name) { |
| 455 | VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \"" |
| 456 | << type << "\" }"; |
| 457 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 458 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 459 | // Then look for the channel. |
| 460 | auto channel_iterator = |
| 461 | std::lower_bound(config->channels()->cbegin(), |
| 462 | config->channels()->cend(), |
| 463 | std::make_pair(name, type), CompareChannels); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 464 | |
| 465 | // Make sure we actually found it, and it matches. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 466 | if (channel_iterator != config->channels()->cend() && |
| 467 | EqualsChannels(*channel_iterator, std::make_pair(name, type))) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 468 | if (VLOG_IS_ON(2)) { |
| 469 | VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator); |
| 470 | } else if (VLOG_IS_ON(1)) { |
| 471 | VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator); |
| 472 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 473 | return *channel_iterator; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 474 | } else { |
| 475 | VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \"" |
| 476 | << type << "\" }"; |
| 477 | return nullptr; |
| 478 | } |
| 479 | } |
| 480 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 481 | size_t ChannelIndex(const Configuration *configuration, |
| 482 | const Channel *channel) { |
| 483 | CHECK(configuration->channels() != nullptr) << ": No channels"; |
| 484 | |
| 485 | auto c = std::find(configuration->channels()->begin(), |
| 486 | configuration->channels()->end(), channel); |
| 487 | CHECK(c != configuration->channels()->end()) |
| 488 | << ": Channel pointer not found in configuration()->channels()"; |
| 489 | |
| 490 | return std::distance(configuration->channels()->begin(), c); |
| 491 | } |
| 492 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 493 | std::string CleanedChannelToString(const Channel *channel) { |
| 494 | FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel); |
| 495 | cleaned_channel.mutable_message()->clear_schema(); |
| 496 | return FlatbufferToJson(cleaned_channel); |
| 497 | } |
| 498 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 499 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 500 | const Flatbuffer<Configuration> &config, |
| 501 | const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) { |
| 502 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 503 | fbb.ForceDefaults(true); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 504 | |
| 505 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 506 | channels_offset; |
| 507 | if (config.message().has_channels()) { |
| 508 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 509 | for (const Channel *c : *config.message().channels()) { |
| 510 | flatbuffers::FlatBufferBuilder channel_fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 511 | channel_fbb.ForceDefaults(true); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 512 | |
| 513 | // Search for a schema with a matching type. |
| 514 | const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr; |
| 515 | for (const aos::FlatbufferString<reflection::Schema> &schema: schemas) { |
| 516 | if (schema.message().root_table() != nullptr) { |
| 517 | if (schema.message().root_table()->name()->string_view() == |
| 518 | c->type()->string_view()) { |
| 519 | found_schema = &schema; |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | CHECK(found_schema != nullptr) |
| 525 | << ": Failed to find schema for " << FlatbufferToJson(c); |
| 526 | |
| 527 | // The following is wasteful, but works. |
| 528 | // |
| 529 | // Copy it into a Channel object by creating an object with only the |
| 530 | // schema populated and merge that into the current channel. |
| 531 | flatbuffers::Offset<reflection::Schema> schema_offset = |
| 532 | CopyFlatBuffer<reflection::Schema>(&found_schema->message(), |
| 533 | &channel_fbb); |
| 534 | Channel::Builder channel_builder(channel_fbb); |
| 535 | channel_builder.add_schema(schema_offset); |
| 536 | channel_fbb.Finish(channel_builder.Finish()); |
| 537 | FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer( |
| 538 | channel_fbb.Release()); |
| 539 | |
| 540 | FlatbufferDetachedBuffer<Channel> merged_channel( |
| 541 | MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c))); |
| 542 | |
| 543 | channel_offsets.emplace_back( |
| 544 | CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb)); |
| 545 | } |
| 546 | channels_offset = fbb.CreateVector(channel_offsets); |
| 547 | } |
| 548 | |
| 549 | // Copy the applications and maps unmodified. |
| 550 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 551 | applications_offset; |
| 552 | { |
| 553 | ::std::vector<flatbuffers::Offset<Application>> applications_offsets; |
| 554 | if (config.message().has_applications()) { |
| 555 | for (const Application *a : *config.message().applications()) { |
| 556 | applications_offsets.emplace_back(CopyFlatBuffer<Application>(a, &fbb)); |
| 557 | } |
| 558 | } |
| 559 | applications_offset = fbb.CreateVector(applications_offsets); |
| 560 | } |
| 561 | |
| 562 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
| 563 | maps_offset; |
| 564 | { |
| 565 | ::std::vector<flatbuffers::Offset<Map>> map_offsets; |
| 566 | if (config.message().has_maps()) { |
| 567 | for (const Map *m : *config.message().maps()) { |
| 568 | map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb)); |
| 569 | } |
| 570 | maps_offset = fbb.CreateVector(map_offsets); |
| 571 | } |
| 572 | } |
| 573 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 574 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 575 | nodes_offset; |
| 576 | { |
| 577 | ::std::vector<flatbuffers::Offset<Node>> node_offsets; |
| 578 | if (config.message().has_nodes()) { |
| 579 | for (const Node *n : *config.message().nodes()) { |
| 580 | node_offsets.emplace_back(CopyFlatBuffer<Node>(n, &fbb)); |
| 581 | } |
| 582 | nodes_offset = fbb.CreateVector(node_offsets); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | // Now insert everything else in unmodified. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 587 | ConfigurationBuilder configuration_builder(fbb); |
| 588 | if (config.message().has_channels()) { |
| 589 | configuration_builder.add_channels(channels_offset); |
| 590 | } |
| 591 | if (config.message().has_maps()) { |
| 592 | configuration_builder.add_maps(maps_offset); |
| 593 | } |
| 594 | if (config.message().has_applications()) { |
| 595 | configuration_builder.add_applications(applications_offset); |
| 596 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 597 | if (config.message().has_nodes()) { |
| 598 | configuration_builder.add_nodes(nodes_offset); |
| 599 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 600 | |
| 601 | fbb.Finish(configuration_builder.Finish()); |
| 602 | return fbb.Release(); |
| 603 | } |
| 604 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 605 | const Node *GetNodeFromHostname(const Configuration *config, |
| 606 | std::string_view hostname) { |
| 607 | for (const Node *node : *config->nodes()) { |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 608 | if (node->has_hostname() && node->hostname()->string_view() == hostname) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 609 | return node; |
| 610 | } |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 611 | if (node->has_hostnames()) { |
| 612 | for (const auto &candidate : *node->hostnames()) { |
| 613 | if (candidate->string_view() == hostname) { |
| 614 | return node; |
| 615 | } |
| 616 | } |
| 617 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 618 | } |
| 619 | return nullptr; |
| 620 | } |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 621 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 622 | const Node *GetMyNode(const Configuration *config) { |
| 623 | const std::string hostname = (FLAGS_override_hostname.size() > 0) |
| 624 | ? FLAGS_override_hostname |
| 625 | : network::GetHostname(); |
| 626 | const Node *node = GetNodeFromHostname(config, hostname); |
| 627 | if (node != nullptr) return node; |
| 628 | |
| 629 | LOG(FATAL) << "Unknown node for host: " << hostname |
| 630 | << ". Consider using --override_hostname if hostname detection " |
| 631 | "is wrong."; |
| 632 | return nullptr; |
| 633 | } |
| 634 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 635 | const Node *GetNode(const Configuration *config, const Node *node) { |
| 636 | if (!MultiNode(config)) { |
| 637 | CHECK(node == nullptr) << ": Provided a node in a single node world."; |
| 638 | return nullptr; |
| 639 | } else { |
| 640 | CHECK(node != nullptr); |
| 641 | CHECK(node->has_name()); |
| 642 | return GetNode(config, node->name()->string_view()); |
| 643 | } |
| 644 | } |
| 645 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 646 | const Node *GetNode(const Configuration *config, std::string_view name) { |
Austin Schuh | fd96062 | 2020-01-01 13:22:55 -0800 | [diff] [blame] | 647 | CHECK(config->has_nodes()) |
| 648 | << ": Asking for a node from a single node configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 649 | for (const Node *node : *config->nodes()) { |
Austin Schuh | fd96062 | 2020-01-01 13:22:55 -0800 | [diff] [blame] | 650 | CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 651 | if (node->name()->string_view() == name) { |
| 652 | return node; |
| 653 | } |
| 654 | } |
| 655 | return nullptr; |
| 656 | } |
| 657 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 658 | const Node *GetNodeOrDie(const Configuration *config, const Node *node) { |
| 659 | if (!MultiNode(config)) { |
| 660 | CHECK(node == nullptr) << ": Provided a node in a single node world."; |
| 661 | return nullptr; |
| 662 | } else { |
| 663 | const Node *config_node = GetNode(config, node); |
| 664 | if (config_node == nullptr) { |
| 665 | LOG(FATAL) << "Couldn't find node matching " << FlatbufferToJson(node); |
| 666 | } |
| 667 | return config_node; |
| 668 | } |
| 669 | } |
| 670 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 671 | namespace { |
| 672 | int GetNodeIndexFromConfig(const Configuration *config, const Node *node) { |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 673 | int node_index = 0; |
| 674 | for (const Node *iterated_node : *config->nodes()) { |
| 675 | if (iterated_node == node) { |
| 676 | return node_index; |
| 677 | } |
| 678 | ++node_index; |
| 679 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 680 | return -1; |
| 681 | } |
| 682 | } // namespace |
| 683 | |
| 684 | int GetNodeIndex(const Configuration *config, const Node *node) { |
| 685 | if (!MultiNode(config)) { |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | { |
| 690 | int node_index = GetNodeIndexFromConfig(config, node); |
| 691 | if (node_index != -1) { |
| 692 | return node_index; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | const Node *result = GetNode(config, node); |
| 697 | CHECK(result != nullptr); |
| 698 | |
| 699 | { |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 700 | int node_index = GetNodeIndexFromConfig(config, result); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 701 | if (node_index != -1) { |
| 702 | return node_index; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | LOG(FATAL) << "Node " << FlatbufferToJson(node) |
| 707 | << " not found in the configuration."; |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 708 | } |
| 709 | |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 710 | int GetNodeIndex(const Configuration *config, std::string_view name) { |
| 711 | if (!MultiNode(config)) { |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | { |
| 716 | int node_index = 0; |
| 717 | for (const Node *iterated_node : *config->nodes()) { |
| 718 | if (iterated_node->name()->string_view() == name) { |
| 719 | return node_index; |
| 720 | } |
| 721 | ++node_index; |
| 722 | } |
| 723 | } |
| 724 | LOG(FATAL) << "Node " << name << " not found in the configuration."; |
| 725 | } |
| 726 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 727 | std::vector<const Node *> GetNodes(const Configuration *config) { |
| 728 | std::vector<const Node *> nodes; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 729 | if (MultiNode(config)) { |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 730 | for (const Node *node : *config->nodes()) { |
| 731 | nodes.emplace_back(node); |
| 732 | } |
| 733 | } else { |
| 734 | nodes.emplace_back(nullptr); |
| 735 | } |
| 736 | return nodes; |
| 737 | } |
| 738 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 739 | bool MultiNode(const Configuration *config) { return config->has_nodes(); } |
| 740 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 741 | bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 742 | if (node == nullptr) { |
| 743 | return true; |
| 744 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 745 | return (channel->source_node()->string_view() == node->name()->string_view()); |
| 746 | } |
| 747 | |
| 748 | bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 749 | if (node == nullptr) { |
| 750 | return true; |
| 751 | } |
| 752 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 753 | if (channel->source_node()->string_view() == node->name()->string_view()) { |
| 754 | return true; |
| 755 | } |
| 756 | |
| 757 | if (!channel->has_destination_nodes()) { |
| 758 | return false; |
| 759 | } |
| 760 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 761 | for (const Connection *connection : *channel->destination_nodes()) { |
| 762 | CHECK(connection->has_name()); |
| 763 | if (connection->name()->string_view() == node->name()->string_view()) { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 764 | return true; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | return false; |
| 769 | } |
| 770 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 771 | bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) { |
| 772 | switch(channel->logger()) { |
| 773 | case LoggerConfig::LOCAL_LOGGER: |
| 774 | if (node == nullptr) { |
| 775 | // Single node world. If there is a local logger, then we want to use |
| 776 | // it. |
| 777 | return true; |
| 778 | } |
| 779 | return channel->source_node()->string_view() == |
| 780 | node->name()->string_view(); |
| 781 | case LoggerConfig::REMOTE_LOGGER: |
| 782 | CHECK(channel->has_logger_node()); |
| 783 | |
| 784 | return channel->logger_node()->string_view() == |
| 785 | CHECK_NOTNULL(node)->name()->string_view(); |
| 786 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
| 787 | CHECK(channel->has_logger_node()); |
| 788 | |
| 789 | if (channel->source_node()->string_view() == |
| 790 | CHECK_NOTNULL(node)->name()->string_view()) { |
| 791 | return true; |
| 792 | } |
| 793 | if (channel->logger_node()->string_view() == node->name()->string_view()) { |
| 794 | return true; |
| 795 | } |
| 796 | |
| 797 | return false; |
| 798 | case LoggerConfig::NOT_LOGGED: |
| 799 | return false; |
| 800 | } |
| 801 | |
| 802 | LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger()); |
| 803 | } |
| 804 | |
| 805 | const Connection *ConnectionToNode(const Channel *channel, const Node *node) { |
| 806 | if (!channel->has_destination_nodes()) { |
| 807 | return nullptr; |
| 808 | } |
| 809 | for (const Connection *connection : *channel->destination_nodes()) { |
| 810 | if (connection->name()->string_view() == node->name()->string_view()) { |
| 811 | return connection; |
| 812 | } |
| 813 | } |
| 814 | return nullptr; |
| 815 | } |
| 816 | |
| 817 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel, |
| 818 | const Node *node, |
| 819 | const Node *logger_node) { |
| 820 | const Connection *connection = ConnectionToNode(channel, node); |
| 821 | if (connection == nullptr) { |
| 822 | return false; |
| 823 | } |
| 824 | return ConnectionDeliveryTimeIsLoggedOnNode(connection, logger_node); |
| 825 | } |
| 826 | |
| 827 | bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection, |
| 828 | const Node *node) { |
| 829 | switch (connection->timestamp_logger()) { |
| 830 | case LoggerConfig::LOCAL_AND_REMOTE_LOGGER: |
| 831 | CHECK(connection->has_timestamp_logger_node()); |
| 832 | if (connection->name()->string_view() == node->name()->string_view()) { |
| 833 | return true; |
| 834 | } |
| 835 | |
| 836 | if (connection->timestamp_logger_node()->string_view() == |
| 837 | node->name()->string_view()) { |
| 838 | return true; |
| 839 | } |
| 840 | |
| 841 | return false; |
| 842 | case LoggerConfig::LOCAL_LOGGER: |
| 843 | return connection->name()->string_view() == node->name()->string_view(); |
| 844 | case LoggerConfig::REMOTE_LOGGER: |
| 845 | CHECK(connection->has_timestamp_logger_node()); |
| 846 | |
| 847 | return connection->timestamp_logger_node()->string_view() == |
| 848 | node->name()->string_view(); |
| 849 | case LoggerConfig::NOT_LOGGED: |
| 850 | return false; |
| 851 | } |
| 852 | |
| 853 | LOG(FATAL) << "Unknown logger config " |
| 854 | << static_cast<int>(connection->timestamp_logger()); |
| 855 | } |
| 856 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 857 | std::vector<std::string_view> SourceNodeNames(const Configuration *config, |
| 858 | const Node *my_node) { |
| 859 | std::set<std::string_view> result_set; |
| 860 | |
| 861 | for (const Channel *channel : *config->channels()) { |
| 862 | if (channel->has_destination_nodes()) { |
| 863 | for (const Connection *connection : *channel->destination_nodes()) { |
| 864 | if (connection->name()->string_view() == |
| 865 | my_node->name()->string_view()) { |
| 866 | result_set.insert(channel->source_node()->string_view()); |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | std::vector<std::string_view> result; |
| 873 | for (const std::string_view source : result_set) { |
| 874 | VLOG(1) << "Found a source node of " << source; |
| 875 | result.emplace_back(source); |
| 876 | } |
| 877 | return result; |
| 878 | } |
| 879 | |
| 880 | std::vector<std::string_view> DestinationNodeNames(const Configuration *config, |
| 881 | const Node *my_node) { |
| 882 | std::vector<std::string_view> result; |
| 883 | |
| 884 | for (const Channel *channel : *config->channels()) { |
| 885 | if (channel->has_source_node() && channel->source_node()->string_view() == |
| 886 | my_node->name()->string_view()) { |
| 887 | if (!channel->has_destination_nodes()) continue; |
| 888 | |
| 889 | if (channel->source_node()->string_view() != |
| 890 | my_node->name()->string_view()) { |
| 891 | continue; |
| 892 | } |
| 893 | |
| 894 | for (const Connection *connection : *channel->destination_nodes()) { |
| 895 | if (std::find(result.begin(), result.end(), |
| 896 | connection->name()->string_view()) == result.end()) { |
| 897 | result.emplace_back(connection->name()->string_view()); |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | for (const std::string_view destination : result) { |
| 904 | VLOG(1) << "Found a destination node of " << destination; |
| 905 | } |
| 906 | return result; |
| 907 | } |
| 908 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 909 | } // namespace configuration |
| 910 | } // namespace aos |