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, |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 235 | std::string_view *name) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 236 | // For the same reason we merge configs in reverse order, we want to process |
| 237 | // maps in reverse order. That lets the outer config overwrite channels from |
| 238 | // the inner configs. |
| 239 | for (auto i = maps->rbegin(); i != maps->rend(); ++i) { |
| 240 | if (i->has_match() && i->match()->has_name() && i->has_rename() && |
| 241 | i->rename()->has_name() && i->match()->name()->string_view() == *name) { |
| 242 | VLOG(1) << "Renamed \"" << *name << "\" to \"" |
| 243 | << i->rename()->name()->string_view() << "\""; |
| 244 | *name = i->rename()->name()->string_view(); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | } // namespace |
| 250 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 251 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 252 | const Flatbuffer<Configuration> &config) { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 253 | // 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] | 254 | // have seen before and merge the updates in. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 255 | absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 256 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 257 | if (config.message().has_channels()) { |
| 258 | for (const Channel *c : *config.message().channels()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 259 | // Ignore malformed entries. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 260 | if (!c->has_name()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 261 | continue; |
| 262 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 263 | if (!c->has_type()) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 264 | continue; |
| 265 | } |
| 266 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 267 | // Attempt to insert the channel. |
| 268 | auto result = channels.insert(CopyFlatBuffer(c)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 269 | if (!result.second) { |
| 270 | // Already there, so merge the new table into the original. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 271 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(c)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // Now repeat this for the application list. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 277 | absl::btree_set<FlatbufferDetachedBuffer<Application>> applications; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 278 | if (config.message().has_applications()) { |
| 279 | for (const Application *a : *config.message().applications()) { |
| 280 | if (!a->has_name()) { |
| 281 | continue; |
| 282 | } |
| 283 | |
| 284 | auto result = applications.insert(CopyFlatBuffer(a)); |
| 285 | if (!result.second) { |
| 286 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(a)); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 291 | // Now repeat this for the node list. |
| 292 | absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes; |
| 293 | if (config.message().has_nodes()) { |
| 294 | for (const Node *n : *config.message().nodes()) { |
| 295 | if (!n->has_name()) { |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | auto result = nodes.insert(CopyFlatBuffer(n)); |
| 300 | if (!result.second) { |
| 301 | *result.first = MergeFlatBuffers(*result.first, CopyFlatBuffer(n)); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 306 | flatbuffers::FlatBufferBuilder fbb; |
| 307 | fbb.ForceDefaults(1); |
| 308 | |
| 309 | // 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] | 310 | // Channels |
| 311 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 312 | channels_offset; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 313 | { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 314 | ::std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 315 | for (const FlatbufferDetachedBuffer<Channel> &c : channels) { |
| 316 | channel_offsets.emplace_back( |
| 317 | CopyFlatBuffer<Channel>(&c.message(), &fbb)); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 318 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 319 | channels_offset = fbb.CreateVector(channel_offsets); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | // Applications |
| 323 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 324 | applications_offset; |
| 325 | { |
| 326 | ::std::vector<flatbuffers::Offset<Application>> applications_offsets; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 327 | for (const FlatbufferDetachedBuffer<Application> &a : applications) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 328 | applications_offsets.emplace_back( |
| 329 | CopyFlatBuffer<Application>(&a.message(), &fbb)); |
| 330 | } |
| 331 | applications_offset = fbb.CreateVector(applications_offsets); |
| 332 | } |
| 333 | |
| 334 | // Just copy the maps |
| 335 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
| 336 | maps_offset; |
| 337 | { |
| 338 | ::std::vector<flatbuffers::Offset<Map>> map_offsets; |
| 339 | if (config.message().has_maps()) { |
| 340 | for (const Map *m : *config.message().maps()) { |
| 341 | map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb)); |
| 342 | } |
| 343 | maps_offset = fbb.CreateVector(map_offsets); |
| 344 | } |
| 345 | } |
| 346 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 347 | // Nodes |
| 348 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 349 | nodes_offset; |
| 350 | { |
| 351 | ::std::vector<flatbuffers::Offset<Node>> node_offsets; |
| 352 | for (const FlatbufferDetachedBuffer<Node> &n : nodes) { |
| 353 | node_offsets.emplace_back(CopyFlatBuffer<Node>(&n.message(), &fbb)); |
| 354 | } |
| 355 | nodes_offset = fbb.CreateVector(node_offsets); |
| 356 | } |
| 357 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 358 | // And then build a Configuration with them all. |
| 359 | ConfigurationBuilder configuration_builder(fbb); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 360 | configuration_builder.add_channels(channels_offset); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 361 | if (config.message().has_maps()) { |
| 362 | configuration_builder.add_maps(maps_offset); |
| 363 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 364 | if (config.message().has_applications()) { |
| 365 | configuration_builder.add_applications(applications_offset); |
| 366 | } |
| 367 | if (config.message().has_nodes()) { |
| 368 | configuration_builder.add_nodes(nodes_offset); |
| 369 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 370 | |
| 371 | fbb.Finish(configuration_builder.Finish()); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 372 | |
| 373 | // Now, validate that if there is a node list, every channel has a source |
| 374 | // node. |
| 375 | FlatbufferDetachedBuffer<Configuration> result(fbb.Release()); |
| 376 | |
| 377 | // Check that if there is a node list, all the source nodes are filled out and |
| 378 | // valid, and all the destination nodes are valid (and not the source). This |
| 379 | // is a basic consistency check. |
| 380 | if (result.message().has_nodes()) { |
| 381 | for (const Channel *c : *config.message().channels()) { |
| 382 | CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c) |
| 383 | << " is missing \"source_node\""; |
| 384 | CHECK(GetNode(&result.message(), c->source_node()->string_view()) != |
| 385 | nullptr) |
| 386 | << ": Channel " << FlatbufferToJson(c) |
| 387 | << " has an unknown \"source_node\""; |
| 388 | |
| 389 | if (c->has_destination_nodes()) { |
| 390 | for (const flatbuffers::String *n : *c->destination_nodes()) { |
| 391 | CHECK(GetNode(&result.message(), n->string_view()) != nullptr) |
| 392 | << ": Channel " << FlatbufferToJson(c) |
| 393 | << " has an unknown \"destination_nodes\" " << n->string_view(); |
| 394 | |
| 395 | CHECK_NE(n->string_view(), c->source_node()->string_view()) |
| 396 | << ": Channel " << FlatbufferToJson(c) |
| 397 | << " is forwarding data to itself"; |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | return result; |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 406 | const char *GetRootDirectory() { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 407 | static char *root_dir; // return value |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 408 | static absl::once_flag once_; |
| 409 | absl::call_once(once_, DoGetRootDirectory, &root_dir); |
| 410 | return root_dir; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | const char *GetLoggingDirectory() { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 414 | static char *retu; // return value |
John Park | 7bca981 | 2019-10-14 21:23:45 -0700 | [diff] [blame] | 415 | static absl::once_flag once_; |
| 416 | absl::call_once(once_, DoGetLoggingDirectory, &retu); |
| 417 | return retu; |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 420 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 421 | const std::string_view path) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 422 | // We only want to read a file once. So track the visited files in a set. |
| 423 | absl::btree_set<std::string> visited_paths; |
| 424 | return MergeConfiguration(ReadConfig(path, &visited_paths)); |
| 425 | } |
| 426 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 427 | const Channel *GetChannel(const Configuration *config, std::string_view name, |
| 428 | std::string_view type, |
| 429 | std::string_view application_name) { |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 430 | VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type |
| 431 | << "\" }"; |
| 432 | |
| 433 | // First handle application specific maps. Only do this if we have a matching |
| 434 | // application name, and it has maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 435 | if (config->has_applications()) { |
| 436 | auto application_iterator = std::lower_bound( |
| 437 | config->applications()->cbegin(), config->applications()->cend(), |
| 438 | application_name, CompareApplications); |
| 439 | if (application_iterator != config->applications()->cend() && |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 440 | EqualsApplications(*application_iterator, application_name)) { |
| 441 | if (application_iterator->has_maps()) { |
| 442 | HandleMaps(application_iterator->maps(), &name); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | // Now do global maps. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 448 | if (config->has_maps()) { |
| 449 | HandleMaps(config->maps(), &name); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 452 | VLOG(1) << "Actually looking up { \"name\": \"" << name << "\", \"type\": \"" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 453 | << type << "\" }"; |
| 454 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 455 | // Then look for the channel. |
| 456 | auto channel_iterator = |
| 457 | std::lower_bound(config->channels()->cbegin(), |
| 458 | config->channels()->cend(), |
| 459 | std::make_pair(name, type), CompareChannels); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 460 | |
| 461 | // Make sure we actually found it, and it matches. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 462 | if (channel_iterator != config->channels()->cend() && |
| 463 | EqualsChannels(*channel_iterator, std::make_pair(name, type))) { |
| 464 | VLOG(1) << "Found: " << FlatbufferToJson(*channel_iterator); |
| 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 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 473 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 474 | const Flatbuffer<Configuration> &config, |
| 475 | const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) { |
| 476 | flatbuffers::FlatBufferBuilder fbb; |
| 477 | fbb.ForceDefaults(1); |
| 478 | |
| 479 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 480 | channels_offset; |
| 481 | if (config.message().has_channels()) { |
| 482 | std::vector<flatbuffers::Offset<Channel>> channel_offsets; |
| 483 | for (const Channel *c : *config.message().channels()) { |
| 484 | flatbuffers::FlatBufferBuilder channel_fbb; |
| 485 | channel_fbb.ForceDefaults(1); |
| 486 | |
| 487 | // Search for a schema with a matching type. |
| 488 | const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr; |
| 489 | for (const aos::FlatbufferString<reflection::Schema> &schema: schemas) { |
| 490 | if (schema.message().root_table() != nullptr) { |
| 491 | if (schema.message().root_table()->name()->string_view() == |
| 492 | c->type()->string_view()) { |
| 493 | found_schema = &schema; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | CHECK(found_schema != nullptr) |
| 499 | << ": Failed to find schema for " << FlatbufferToJson(c); |
| 500 | |
| 501 | // The following is wasteful, but works. |
| 502 | // |
| 503 | // Copy it into a Channel object by creating an object with only the |
| 504 | // schema populated and merge that into the current channel. |
| 505 | flatbuffers::Offset<reflection::Schema> schema_offset = |
| 506 | CopyFlatBuffer<reflection::Schema>(&found_schema->message(), |
| 507 | &channel_fbb); |
| 508 | Channel::Builder channel_builder(channel_fbb); |
| 509 | channel_builder.add_schema(schema_offset); |
| 510 | channel_fbb.Finish(channel_builder.Finish()); |
| 511 | FlatbufferDetachedBuffer<Channel> channel_schema_flatbuffer( |
| 512 | channel_fbb.Release()); |
| 513 | |
| 514 | FlatbufferDetachedBuffer<Channel> merged_channel( |
| 515 | MergeFlatBuffers(channel_schema_flatbuffer, CopyFlatBuffer(c))); |
| 516 | |
| 517 | channel_offsets.emplace_back( |
| 518 | CopyFlatBuffer<Channel>(&merged_channel.message(), &fbb)); |
| 519 | } |
| 520 | channels_offset = fbb.CreateVector(channel_offsets); |
| 521 | } |
| 522 | |
| 523 | // Copy the applications and maps unmodified. |
| 524 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>> |
| 525 | applications_offset; |
| 526 | { |
| 527 | ::std::vector<flatbuffers::Offset<Application>> applications_offsets; |
| 528 | if (config.message().has_applications()) { |
| 529 | for (const Application *a : *config.message().applications()) { |
| 530 | applications_offsets.emplace_back(CopyFlatBuffer<Application>(a, &fbb)); |
| 531 | } |
| 532 | } |
| 533 | applications_offset = fbb.CreateVector(applications_offsets); |
| 534 | } |
| 535 | |
| 536 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>> |
| 537 | maps_offset; |
| 538 | { |
| 539 | ::std::vector<flatbuffers::Offset<Map>> map_offsets; |
| 540 | if (config.message().has_maps()) { |
| 541 | for (const Map *m : *config.message().maps()) { |
| 542 | map_offsets.emplace_back(CopyFlatBuffer<Map>(m, &fbb)); |
| 543 | } |
| 544 | maps_offset = fbb.CreateVector(map_offsets); |
| 545 | } |
| 546 | } |
| 547 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 548 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>> |
| 549 | nodes_offset; |
| 550 | { |
| 551 | ::std::vector<flatbuffers::Offset<Node>> node_offsets; |
| 552 | if (config.message().has_nodes()) { |
| 553 | for (const Node *n : *config.message().nodes()) { |
| 554 | node_offsets.emplace_back(CopyFlatBuffer<Node>(n, &fbb)); |
| 555 | } |
| 556 | nodes_offset = fbb.CreateVector(node_offsets); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | // Now insert everything else in unmodified. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 561 | ConfigurationBuilder configuration_builder(fbb); |
| 562 | if (config.message().has_channels()) { |
| 563 | configuration_builder.add_channels(channels_offset); |
| 564 | } |
| 565 | if (config.message().has_maps()) { |
| 566 | configuration_builder.add_maps(maps_offset); |
| 567 | } |
| 568 | if (config.message().has_applications()) { |
| 569 | configuration_builder.add_applications(applications_offset); |
| 570 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 571 | if (config.message().has_nodes()) { |
| 572 | configuration_builder.add_nodes(nodes_offset); |
| 573 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 574 | |
| 575 | fbb.Finish(configuration_builder.Finish()); |
| 576 | return fbb.Release(); |
| 577 | } |
| 578 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame^] | 579 | const Node *GetNodeFromHostname(const Configuration *config, |
| 580 | std::string_view hostname) { |
| 581 | for (const Node *node : *config->nodes()) { |
| 582 | if (node->hostname()->string_view() == hostname) { |
| 583 | return node; |
| 584 | } |
| 585 | } |
| 586 | return nullptr; |
| 587 | } |
| 588 | const Node *GetMyNode(const Configuration *config) { |
| 589 | const std::string hostname = (FLAGS_override_hostname.size() > 0) |
| 590 | ? FLAGS_override_hostname |
| 591 | : network::GetHostname(); |
| 592 | const Node *node = GetNodeFromHostname(config, hostname); |
| 593 | if (node != nullptr) return node; |
| 594 | |
| 595 | LOG(FATAL) << "Unknown node for host: " << hostname |
| 596 | << ". Consider using --override_hostname if hostname detection " |
| 597 | "is wrong."; |
| 598 | return nullptr; |
| 599 | } |
| 600 | |
| 601 | const Node *GetNode(const Configuration *config, std::string_view name) { |
| 602 | for (const Node *node : *config->nodes()) { |
| 603 | if (node->name()->string_view() == name) { |
| 604 | return node; |
| 605 | } |
| 606 | } |
| 607 | return nullptr; |
| 608 | } |
| 609 | |
| 610 | bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) { |
| 611 | return (channel->source_node()->string_view() == node->name()->string_view()); |
| 612 | } |
| 613 | |
| 614 | bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) { |
| 615 | if (channel->source_node()->string_view() == node->name()->string_view()) { |
| 616 | return true; |
| 617 | } |
| 618 | |
| 619 | if (!channel->has_destination_nodes()) { |
| 620 | return false; |
| 621 | } |
| 622 | |
| 623 | for (const flatbuffers::String *s : *channel->destination_nodes()) { |
| 624 | if (s->string_view() == node->name()->string_view()) { |
| 625 | return true; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | return false; |
| 630 | } |
| 631 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 632 | } // namespace configuration |
| 633 | } // namespace aos |