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> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 10 | #include <string_view> |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 11 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 12 | #include "absl/base/call_once.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 13 | #include "absl/container/btree_set.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 14 | #include "aos/configuration_generated.h" |
| 15 | #include "aos/flatbuffer_merge.h" |
| 16 | #include "aos/json_to_flatbuffer.h" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 17 | #include "aos/network/team_number.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 18 | #include "aos/unique_malloc_ptr.h" |
| 19 | #include "aos/util/file.h" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 20 | #include "gflags/gflags.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 21 | #include "glog/logging.h" |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 22 | |
| 23 | DEFINE_string( |
| 24 | override_hostname, "", |
| 25 | "If set, this forces the hostname of this node to be the provided " |
| 26 | "hostname."); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 27 | |
| 28 | namespace aos { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 29 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 30 | // 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] | 31 | // insert them in the btree below. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 32 | bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 33 | const FlatbufferDetachedBuffer<Channel> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 34 | int name_compare = lhs.message().name()->string_view().compare( |
| 35 | rhs.message().name()->string_view()); |
| 36 | if (name_compare == 0) { |
| 37 | return lhs.message().type()->string_view() < |
| 38 | rhs.message().type()->string_view(); |
| 39 | } else if (name_compare < 0) { |
| 40 | return true; |
| 41 | } else { |
| 42 | return false; |
| 43 | } |
| 44 | } |
| 45 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 46 | bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 47 | const FlatbufferDetachedBuffer<Channel> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 48 | return lhs.message().name()->string_view() == |
| 49 | rhs.message().name()->string_view() && |
| 50 | lhs.message().type()->string_view() == |
| 51 | rhs.message().type()->string_view(); |
| 52 | } |
| 53 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 54 | bool operator==(const FlatbufferDetachedBuffer<Application> &lhs, |
| 55 | const FlatbufferDetachedBuffer<Application> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 56 | return lhs.message().name()->string_view() == |
| 57 | rhs.message().name()->string_view(); |
| 58 | } |
| 59 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 60 | bool operator<(const FlatbufferDetachedBuffer<Application> &lhs, |
| 61 | const FlatbufferDetachedBuffer<Application> &rhs) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 62 | return lhs.message().name()->string_view() < |
| 63 | rhs.message().name()->string_view(); |
| 64 | } |
| 65 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 66 | bool operator==(const FlatbufferDetachedBuffer<Node> &lhs, |
| 67 | const FlatbufferDetachedBuffer<Node> &rhs) { |
| 68 | return lhs.message().name()->string_view() == |
| 69 | rhs.message().name()->string_view(); |
| 70 | } |
| 71 | |
| 72 | bool operator<(const FlatbufferDetachedBuffer<Node> &lhs, |
| 73 | const FlatbufferDetachedBuffer<Node> &rhs) { |
| 74 | return lhs.message().name()->string_view() < |
| 75 | rhs.message().name()->string_view(); |
| 76 | } |
| 77 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 78 | namespace configuration { |
| 79 | namespace { |
| 80 | |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 81 | void DoGetRootDirectory(char** retu) { |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 82 | ssize_t size = 0; |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 83 | *retu = NULL; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 84 | while (true) { |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 85 | size += 256; |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 86 | if (*retu != nullptr) delete *retu; |
| 87 | *retu = new char[size]; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 88 | |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 89 | ssize_t ret = readlink("/proc/self/exe", *retu, size); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 90 | if (ret < 0) { |
| 91 | if (ret != -1) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 92 | LOG(WARNING) << "it returned " << ret << ", not -1"; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 93 | } |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 94 | |
| 95 | PLOG(FATAL) << "readlink(\"/proc/self/exe\", " << *retu << ", " << size |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 96 | << ") failed"; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 97 | } |
| 98 | if (ret < size) { |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 99 | void *last_slash = memrchr(*retu, '/', ret); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 100 | if (last_slash == NULL) { |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 101 | *retu[ret] = '\0'; |
| 102 | LOG(FATAL) << "couldn't find a '/' in \"" << *retu << "\""; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 103 | } |
| 104 | *static_cast<char *>(last_slash) = '\0'; |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 105 | LOG(INFO) << "got a root dir of \"" << *retu << "\""; |
| 106 | return; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 111 | void DoGetLoggingDirectory(char** retu) { |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 112 | static const char kSuffix[] = "/../../tmp/robot_logs"; |
| 113 | const char *root = GetRootDirectory(); |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 114 | *retu = new char[strlen(root) + sizeof(kSuffix)]; |
| 115 | strcpy(*retu, root); |
| 116 | strcat(*retu, kSuffix); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 119 | // 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] | 120 | std::string_view ExtractFolder( |
| 121 | const std::string_view filename) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 122 | auto last_slash_pos = filename.find_last_of("/\\"); |
| 123 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 124 | return last_slash_pos == std::string_view::npos |
| 125 | ? std::string_view("./") |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 126 | : filename.substr(0, last_slash_pos + 1); |
| 127 | } |
| 128 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 129 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 130 | const std::string_view path, absl::btree_set<std::string> *visited_paths) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 131 | flatbuffers::DetachedBuffer buffer = JsonToFlatbuffer( |
| 132 | util::ReadFileToStringOrDie(path), ConfigurationTypeTable()); |
| 133 | |
| 134 | CHECK_GT(buffer.size(), 0u) << ": Failed to parse JSON file"; |
| 135 | |
| 136 | FlatbufferDetachedBuffer<Configuration> config(std::move(buffer)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 137 | // Depth first. Take the following example: |
| 138 | // |
| 139 | // config1.json: |
| 140 | // { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 141 | // "channels": [ |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 142 | // { |
| 143 | // "name": "/foo", |
| 144 | // "type": ".aos.bar", |
| 145 | // "max_size": 5 |
| 146 | // } |
| 147 | // ], |
| 148 | // "imports": [ |
| 149 | // "config2.json", |
| 150 | // ] |
| 151 | // } |
| 152 | // |
| 153 | // config2.json: |
| 154 | // { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 155 | // "channels": [ |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 156 | // { |
| 157 | // "name": "/foo", |
| 158 | // "type": ".aos.bar", |
| 159 | // "max_size": 7 |
| 160 | // } |
| 161 | // ], |
| 162 | // } |
| 163 | // |
| 164 | // We want the main config (config1.json) to be able to override the imported |
| 165 | // config. That means that it needs to be merged into the imported configs, |
| 166 | // not the other way around. |
| 167 | |
| 168 | // Track that we have seen this file before recursing. |
| 169 | visited_paths->insert(::std::string(path)); |
| 170 | |
| 171 | if (config.message().has_imports()) { |
| 172 | // Capture the imports. |
| 173 | const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *v = |
| 174 | config.message().imports(); |
| 175 | |
| 176 | // And then wipe them. This gets GCed when we merge later. |
| 177 | config.mutable_message()->clear_imports(); |
| 178 | |
| 179 | // Start with an empty configuration to merge into. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 180 | FlatbufferDetachedBuffer<Configuration> merged_config = |
| 181 | FlatbufferDetachedBuffer<Configuration>::Empty(); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 182 | |
| 183 | const ::std::string folder(ExtractFolder(path)); |
| 184 | |
| 185 | for (const flatbuffers::String *str : *v) { |
| 186 | const ::std::string included_config = folder + str->c_str(); |
| 187 | // Abort on any paths we have already seen. |
| 188 | CHECK(visited_paths->find(included_config) == visited_paths->end()) |
| 189 | << ": Found duplicate file " << included_config << " while reading " |
| 190 | << path; |
| 191 | |
| 192 | // And them merge everything in. |
| 193 | merged_config = MergeFlatBuffers( |
| 194 | merged_config, ReadConfig(included_config, visited_paths)); |
| 195 | } |
| 196 | |
| 197 | // Finally, merge this file in. |
| 198 | config = MergeFlatBuffers(merged_config, config); |
| 199 | } |
| 200 | return config; |
| 201 | } |
| 202 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 203 | // Compares (c < p) a channel, and a name, type tuple. |
| 204 | bool CompareChannels(const Channel *c, |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 205 | ::std::pair<std::string_view, std::string_view> p) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 206 | int name_compare = c->name()->string_view().compare(p.first); |
| 207 | if (name_compare == 0) { |
| 208 | return c->type()->string_view() < p.second; |
| 209 | } else if (name_compare < 0) { |
| 210 | return true; |
| 211 | } else { |
| 212 | return false; |
| 213 | } |
| 214 | }; |
| 215 | |
| 216 | // Compares for equality (c == p) a channel, and a name, type tuple. |
| 217 | bool EqualsChannels(const Channel *c, |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 218 | ::std::pair<std::string_view, std::string_view> p) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 219 | return c->name()->string_view() == p.first && |
| 220 | c->type()->string_view() == p.second; |
| 221 | } |
| 222 | |
| 223 | // Compares (c < p) an application, and a name; |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 224 | bool CompareApplications(const Application *a, std::string_view name) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 225 | return a->name()->string_view() < name; |
| 226 | }; |
| 227 | |
| 228 | // Compares for equality (c == p) an application, and a name; |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 229 | bool EqualsApplications(const Application *a, std::string_view name) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 230 | return a->name()->string_view() == name; |
| 231 | } |
| 232 | |
| 233 | // Maps name for the provided maps. Modifies name. |
| 234 | void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps, |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 235 | std::string_view *name, std::string_view type, |
| 236 | const Node *node) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 237 | // For the same reason we merge configs in reverse order, we want to process |
| 238 | // maps in reverse order. That lets the outer config overwrite channels from |
| 239 | // the inner configs. |
| 240 | for (auto i = maps->rbegin(); i != maps->rend(); ++i) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 241 | if (!i->has_match() || !i->match()->has_name()) { |
| 242 | continue; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 243 | } |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 244 | if (!i->has_rename() || !i->rename()->has_name()) { |
| 245 | continue; |
| 246 | } |
| 247 | |
| 248 | // Handle normal maps (now that we know that match and rename are filled |
| 249 | // out). |
| 250 | if (i->match()->name()->string_view() != *name) { |
| 251 | continue; |
| 252 | } |
| 253 | |
| 254 | // Handle type specific maps. |
| 255 | if (i->match()->has_type() && i->match()->type()->string_view() != type) { |
| 256 | continue; |
| 257 | } |
| 258 | |
| 259 | if (node != nullptr && i->match()->has_source_node() && |
| 260 | i->match()->source_node()->string_view() != |
| 261 | node->name()->string_view()) { |
| 262 | continue; |
| 263 | } |
| 264 | |
| 265 | VLOG(1) << "Renamed \"" << *name << "\" to \"" |
| 266 | << i->rename()->name()->string_view() << "\""; |
| 267 | *name = i->rename()->name()->string_view(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | } // namespace |
| 272 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 273 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 274 | const Flatbuffer<Configuration> &config) { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 275 | // 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] | 276 | // have seen before and merge the updates in. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 277 | absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 278 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 279 | if (config.message().has_channels()) { |
| 280 | for (const Channel *c : *config.message().channels()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 281 | // Ignore malformed entries. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 282 | if (!c->has_name()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 283 | continue; |
| 284 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 285 | if (!c->has_type()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 286 | continue; |
| 287 | } |
| 288 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 289 | // Attempt to insert the channel. |
| 290 | auto result = channels.insert(CopyFlatBuffer(c)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 291 | if (!result.second) { |
| 292 | // Already there, so merge the new table into the original. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 293 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(c)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // Now repeat this for the application list. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 299 | absl::btree_set<FlatbufferDetachedBuffer<Application>> applications; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 300 | if (config.message().has_applications()) { |
| 301 | for (const Application *a : *config.message().applications()) { |
| 302 | if (!a->has_name()) { |
| 303 | continue; |
| 304 | } |
| 305 | |
| 306 | auto result = applications.insert(CopyFlatBuffer(a)); |
| 307 | if (!result.second) { |
| 308 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(a)); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 313 | // Now repeat this for the node list. |
| 314 | absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes; |
| 315 | if (config.message().has_nodes()) { |
| 316 | for (const Node *n : *config.message().nodes()) { |
| 317 | if (!n->has_name()) { |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | auto result = nodes.insert(CopyFlatBuffer(n)); |
| 322 | if (!result.second) { |
| 323 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(n)); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 328 | flatbuffers::FlatBufferBuilder fbb; |
| 329 | fbb.ForceDefaults(1); |
| 330 | |
| 331 | // 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] | 332 | // Channels |
| 333 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 334 | channels_offset; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 335 | { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 336 | ::std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 337 | for (const FlatbufferDetachedBuffer<Channel> &c : channels) { |
| 338 | channel_offsets.emplace_back( |
| 339 | CopyFlatBuffer<Channel>(&c.message(), &fbb)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 340 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 341 | channels_offset = fbb.CreateVector(channel_offsets); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | // Applications |
| 345 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 346 | applications_offset; |
| 347 | { |
| 348 | ::std::vector<flatbuffers::Offset<Application>> applications_offsets; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 349 | for (const FlatbufferDetachedBuffer<Application> &a : applications) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 350 | applications_offsets.emplace_back( |
| 351 | CopyFlatBuffer<Application>(&a.message(), &fbb)); |
| 352 | } |
| 353 | applications_offset = fbb.CreateVector(applications_offsets); |
| 354 | } |
| 355 | |
| 356 | // Just copy the maps |
| 357 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
| 358 | maps_offset; |
| 359 | { |
| 360 | ::std::vector<flatbuffers::Offset<Map>> map_offsets; |
| 361 | if (config.message().has_maps()) { |
| 362 | for (const Map *m : *config.message().maps()) { |
| 363 | map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb)); |
| 364 | } |
| 365 | maps_offset = fbb.CreateVector(map_offsets); |
| 366 | } |
| 367 | } |
| 368 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 369 | // Nodes |
| 370 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 371 | nodes_offset; |
| 372 | { |
| 373 | ::std::vector<flatbuffers::Offset<Node>> node_offsets; |
| 374 | for (const FlatbufferDetachedBuffer<Node> &n : nodes) { |
| 375 | node_offsets.emplace_back(CopyFlatBuffer<Node>(&n.message(), &fbb)); |
| 376 | } |
| 377 | nodes_offset = fbb.CreateVector(node_offsets); |
| 378 | } |
| 379 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 380 | // And then build a Configuration with them all. |
| 381 | ConfigurationBuilder configuration_builder(fbb); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 382 | configuration_builder.add_channels(channels_offset); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 383 | if (config.message().has_maps()) { |
| 384 | configuration_builder.add_maps(maps_offset); |
| 385 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 386 | if (config.message().has_applications()) { |
| 387 | configuration_builder.add_applications(applications_offset); |
| 388 | } |
| 389 | if (config.message().has_nodes()) { |
| 390 | configuration_builder.add_nodes(nodes_offset); |
| 391 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 392 | |
| 393 | fbb.Finish(configuration_builder.Finish()); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 394 | |
| 395 | // Now, validate that if there is a node list, every channel has a source |
| 396 | // node. |
| 397 | FlatbufferDetachedBuffer<Configuration> result(fbb.Release()); |
| 398 | |
| 399 | // Check that if there is a node list, all the source nodes are filled out and |
| 400 | // valid, and all the destination nodes are valid (and not the source). This |
| 401 | // is a basic consistency check. |
| 402 | if (result.message().has_nodes()) { |
| 403 | for (const Channel *c : *config.message().channels()) { |
| 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()) { |
| 412 | for (const flatbuffers::String *n : *c->destination_nodes()) { |
| 413 | CHECK(GetNode(&result.message(), n->string_view()) != nullptr) |
| 414 | << ": Channel " << FlatbufferToJson(c) |
| 415 | << " has an unknown \"destination_nodes\" " << n->string_view(); |
| 416 | |
| 417 | CHECK_NE(n->string_view(), c->source_node()->string_view()) |
| 418 | << ": Channel " << FlatbufferToJson(c) |
| 419 | << " is forwarding data to itself"; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | return result; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 428 | const char *GetRootDirectory() { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 429 | static char *root_dir; // return value |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 430 | static absl::once_flag once_; |
| 431 | absl::call_once(once_, DoGetRootDirectory, &root_dir); |
| 432 | return root_dir; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | const char *GetLoggingDirectory() { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 436 | static char *retu; // return value |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 437 | static absl::once_flag once_; |
| 438 | absl::call_once(once_, DoGetLoggingDirectory, &retu); |
| 439 | return retu; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 442 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 443 | const std::string_view path) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 444 | // We only want to read a file once. So track the visited files in a set. |
| 445 | absl::btree_set<std::string> visited_paths; |
| 446 | return MergeConfiguration(ReadConfig(path, &visited_paths)); |
| 447 | } |
| 448 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 449 | const Channel *GetChannel(const Configuration *config, std::string_view name, |
| 450 | std::string_view type, |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 451 | std::string_view application_name, const Node *node) { |
| 452 | const std::string_view original_name = name; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 453 | VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type |
| 454 | << "\" }"; |
| 455 | |
| 456 | // First handle application specific maps. Only do this if we have a matching |
| 457 | // application name, and it has maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 458 | if (config->has_applications()) { |
| 459 | auto application_iterator = std::lower_bound( |
| 460 | config->applications()->cbegin(), config->applications()->cend(), |
| 461 | application_name, CompareApplications); |
| 462 | if (application_iterator != config->applications()->cend() && |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 463 | EqualsApplications(*application_iterator, application_name)) { |
| 464 | if (application_iterator->has_maps()) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 465 | HandleMaps(application_iterator->maps(), &name, type, node); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | // Now do global maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 471 | if (config->has_maps()) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 472 | HandleMaps(config->maps(), &name, type, node); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 475 | if (original_name != name) { |
| 476 | VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \"" |
| 477 | << type << "\" }"; |
| 478 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 479 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 480 | // Then look for the channel. |
| 481 | auto channel_iterator = |
| 482 | std::lower_bound(config->channels()->cbegin(), |
| 483 | config->channels()->cend(), |
| 484 | std::make_pair(name, type), CompareChannels); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 485 | |
| 486 | // Make sure we actually found it, and it matches. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 487 | if (channel_iterator != config->channels()->cend() && |
| 488 | EqualsChannels(*channel_iterator, std::make_pair(name, type))) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 489 | if (VLOG_IS_ON(2)) { |
| 490 | VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator); |
| 491 | } else if (VLOG_IS_ON(1)) { |
| 492 | VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator); |
| 493 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 494 | return *channel_iterator; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 495 | } else { |
| 496 | VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \"" |
| 497 | << type << "\" }"; |
| 498 | return nullptr; |
| 499 | } |
| 500 | } |
| 501 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 502 | std::string CleanedChannelToString(const Channel *channel) { |
| 503 | FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel); |
| 504 | cleaned_channel.mutable_message()->clear_schema(); |
| 505 | return FlatbufferToJson(cleaned_channel); |
| 506 | } |
| 507 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 508 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 509 | const Flatbuffer<Configuration> &config, |
| 510 | const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) { |
| 511 | flatbuffers::FlatBufferBuilder fbb; |
| 512 | fbb.ForceDefaults(1); |
| 513 | |
| 514 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 515 | channels_offset; |
| 516 | if (config.message().has_channels()) { |
| 517 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 518 | for (const Channel *c : *config.message().channels()) { |
| 519 | flatbuffers::FlatBufferBuilder channel_fbb; |
| 520 | channel_fbb.ForceDefaults(1); |
| 521 | |
| 522 | // Search for a schema with a matching type. |
| 523 | const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr; |
| 524 | for (const aos::FlatbufferString<reflection::Schema> &schema: schemas) { |
| 525 | if (schema.message().root_table() != nullptr) { |
| 526 | if (schema.message().root_table()->name()->string_view() == |
| 527 | c->type()->string_view()) { |
| 528 | found_schema = &schema; |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | CHECK(found_schema != nullptr) |
| 534 | << ": Failed to find schema for " << FlatbufferToJson(c); |
| 535 | |
| 536 | // The following is wasteful, but works. |
| 537 | // |
| 538 | // Copy it into a Channel object by creating an object with only the |
| 539 | // schema populated and merge that into the current channel. |
| 540 | flatbuffers::Offset<reflection::Schema> schema_offset = |
| 541 | CopyFlatBuffer<reflection::Schema>(&found_schema->message(), |
| 542 | &channel_fbb); |
| 543 | Channel::Builder channel_builder(channel_fbb); |
| 544 | channel_builder.add_schema(schema_offset); |
| 545 | channel_fbb.Finish(channel_builder.Finish()); |
| 546 | FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer( |
| 547 | channel_fbb.Release()); |
| 548 | |
| 549 | FlatbufferDetachedBuffer<Channel> merged_channel( |
| 550 | MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c))); |
| 551 | |
| 552 | channel_offsets.emplace_back( |
| 553 | CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb)); |
| 554 | } |
| 555 | channels_offset = fbb.CreateVector(channel_offsets); |
| 556 | } |
| 557 | |
| 558 | // Copy the applications and maps unmodified. |
| 559 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 560 | applications_offset; |
| 561 | { |
| 562 | ::std::vector<flatbuffers::Offset<Application>> applications_offsets; |
| 563 | if (config.message().has_applications()) { |
| 564 | for (const Application *a : *config.message().applications()) { |
| 565 | applications_offsets.emplace_back(CopyFlatBuffer<Application>(a, &fbb)); |
| 566 | } |
| 567 | } |
| 568 | applications_offset = fbb.CreateVector(applications_offsets); |
| 569 | } |
| 570 | |
| 571 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
| 572 | maps_offset; |
| 573 | { |
| 574 | ::std::vector<flatbuffers::Offset<Map>> map_offsets; |
| 575 | if (config.message().has_maps()) { |
| 576 | for (const Map *m : *config.message().maps()) { |
| 577 | map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb)); |
| 578 | } |
| 579 | maps_offset = fbb.CreateVector(map_offsets); |
| 580 | } |
| 581 | } |
| 582 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 583 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 584 | nodes_offset; |
| 585 | { |
| 586 | ::std::vector<flatbuffers::Offset<Node>> node_offsets; |
| 587 | if (config.message().has_nodes()) { |
| 588 | for (const Node *n : *config.message().nodes()) { |
| 589 | node_offsets.emplace_back(CopyFlatBuffer<Node>(n, &fbb)); |
| 590 | } |
| 591 | nodes_offset = fbb.CreateVector(node_offsets); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | // Now insert everything else in unmodified. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 596 | ConfigurationBuilder configuration_builder(fbb); |
| 597 | if (config.message().has_channels()) { |
| 598 | configuration_builder.add_channels(channels_offset); |
| 599 | } |
| 600 | if (config.message().has_maps()) { |
| 601 | configuration_builder.add_maps(maps_offset); |
| 602 | } |
| 603 | if (config.message().has_applications()) { |
| 604 | configuration_builder.add_applications(applications_offset); |
| 605 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 606 | if (config.message().has_nodes()) { |
| 607 | configuration_builder.add_nodes(nodes_offset); |
| 608 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 609 | |
| 610 | fbb.Finish(configuration_builder.Finish()); |
| 611 | return fbb.Release(); |
| 612 | } |
| 613 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 614 | const Node *GetNodeFromHostname(const Configuration *config, |
| 615 | std::string_view hostname) { |
| 616 | for (const Node *node : *config->nodes()) { |
| 617 | if (node->hostname()->string_view() == hostname) { |
| 618 | return node; |
| 619 | } |
| 620 | } |
| 621 | return nullptr; |
| 622 | } |
| 623 | const Node *GetMyNode(const Configuration *config) { |
| 624 | const std::string hostname = (FLAGS_override_hostname.size() > 0) |
| 625 | ? FLAGS_override_hostname |
| 626 | : network::GetHostname(); |
| 627 | const Node *node = GetNodeFromHostname(config, hostname); |
| 628 | if (node != nullptr) return node; |
| 629 | |
| 630 | LOG(FATAL) << "Unknown node for host: " << hostname |
| 631 | << ". Consider using --override_hostname if hostname detection " |
| 632 | "is wrong."; |
| 633 | return nullptr; |
| 634 | } |
| 635 | |
| 636 | const Node *GetNode(const Configuration *config, std::string_view name) { |
| 637 | for (const Node *node : *config->nodes()) { |
| 638 | if (node->name()->string_view() == name) { |
| 639 | return node; |
| 640 | } |
| 641 | } |
| 642 | return nullptr; |
| 643 | } |
| 644 | |
| 645 | bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) { |
| 646 | return (channel->source_node()->string_view() == node->name()->string_view()); |
| 647 | } |
| 648 | |
| 649 | bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) { |
| 650 | if (channel->source_node()->string_view() == node->name()->string_view()) { |
| 651 | return true; |
| 652 | } |
| 653 | |
| 654 | if (!channel->has_destination_nodes()) { |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | for (const flatbuffers::String *s : *channel->destination_nodes()) { |
| 659 | if (s->string_view() == node->name()->string_view()) { |
| 660 | return true; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | return false; |
| 665 | } |
| 666 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 667 | } // namespace configuration |
| 668 | } // namespace aos |