blob: 1d39f271c8574eb95536ca9fae4d09dc3db5cadc [file] [log] [blame]
John Park398c74a2018-10-20 21:17:39 -07001#include "aos/configuration.h"
Brian Silverman66f079a2013-08-26 16:24:30 -07002
Brian Silverman66f079a2013-08-26 16:24:30 -07003#include <arpa/inet.h>
4#include <ifaddrs.h>
Austin Schuhf1fff282020-03-28 16:57:32 -07005#include <netinet/in.h>
6#include <stdlib.h>
7#include <string.h>
8#include <sys/types.h>
Brian Silverman66f079a2013-08-26 16:24:30 -07009#include <unistd.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080010
Austin Schuh68d98592020-11-01 23:22:57 -080011#include <map>
Austin Schuhe84c3ed2019-12-14 15:29:48 -080012#include <set>
James Kuszmaul3ae42262019-11-08 12:33:41 -080013#include <string_view>
Brian Silverman66f079a2013-08-26 16:24:30 -070014
Austin Schuhcb108412019-10-13 16:09:54 -070015#include "absl/container/btree_set.h"
Austin Schuha81454b2020-05-12 19:58:36 -070016#include "absl/strings/str_cat.h"
Austin Schuhcb108412019-10-13 16:09:54 -070017#include "aos/configuration_generated.h"
18#include "aos/flatbuffer_merge.h"
19#include "aos/json_to_flatbuffer.h"
Austin Schuh217a9782019-12-21 23:02:50 -080020#include "aos/network/team_number.h"
Austin Schuhcb108412019-10-13 16:09:54 -070021#include "aos/unique_malloc_ptr.h"
22#include "aos/util/file.h"
Austin Schuh217a9782019-12-21 23:02:50 -080023#include "gflags/gflags.h"
Austin Schuhcb108412019-10-13 16:09:54 -070024#include "glog/logging.h"
Austin Schuh217a9782019-12-21 23:02:50 -080025
Brian Silverman66f079a2013-08-26 16:24:30 -070026namespace aos {
Austin Schuh15182322020-10-10 15:25:21 -070027namespace {
28bool EndsWith(std::string_view str, std::string_view end) {
29 if (str.size() < end.size()) {
30 return false;
31 }
32 if (str.substr(str.size() - end.size(), end.size()) != end) {
33 return false;
34 }
35 return true;
36}
37
38std::string MaybeReplaceExtension(std::string_view filename,
39 std::string_view extension,
40 std::string_view replacement) {
41 if (!EndsWith(filename, extension)) {
42 return std::string(filename);
43 }
44 filename.remove_suffix(extension.size());
45 return absl::StrCat(filename, replacement);
46}
47
48FlatbufferDetachedBuffer<Configuration> ReadConfigFile(std::string_view path,
49 bool binary) {
50 if (binary) {
51 FlatbufferVector<Configuration> config =
52 FileToFlatbuffer<Configuration>(path);
53 return CopySpanAsDetachedBuffer(config.span());
54 }
55
56 flatbuffers::DetachedBuffer buffer = JsonToFlatbuffer(
57 util::ReadFileToStringOrDie(path), ConfigurationTypeTable());
58
59 CHECK_GT(buffer.size(), 0u) << ": Failed to parse JSON file";
60
61 return FlatbufferDetachedBuffer<Configuration>(std::move(buffer));
62}
63
64} // namespace
Austin Schuhcb108412019-10-13 16:09:54 -070065
Austin Schuh40485ed2019-10-26 21:51:44 -070066// Define the compare and equal operators for Channel and Application so we can
Austin Schuhcb108412019-10-13 16:09:54 -070067// insert them in the btree below.
Austin Schuh40485ed2019-10-26 21:51:44 -070068bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs,
69 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070070 int name_compare = lhs.message().name()->string_view().compare(
71 rhs.message().name()->string_view());
72 if (name_compare == 0) {
73 return lhs.message().type()->string_view() <
74 rhs.message().type()->string_view();
75 } else if (name_compare < 0) {
76 return true;
77 } else {
78 return false;
79 }
80}
81
Austin Schuh40485ed2019-10-26 21:51:44 -070082bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs,
83 const FlatbufferDetachedBuffer<Channel> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070084 return lhs.message().name()->string_view() ==
85 rhs.message().name()->string_view() &&
86 lhs.message().type()->string_view() ==
87 rhs.message().type()->string_view();
88}
89
Austin Schuh40485ed2019-10-26 21:51:44 -070090bool operator==(const FlatbufferDetachedBuffer<Application> &lhs,
91 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070092 return lhs.message().name()->string_view() ==
93 rhs.message().name()->string_view();
94}
95
Austin Schuh40485ed2019-10-26 21:51:44 -070096bool operator<(const FlatbufferDetachedBuffer<Application> &lhs,
97 const FlatbufferDetachedBuffer<Application> &rhs) {
Austin Schuhcb108412019-10-13 16:09:54 -070098 return lhs.message().name()->string_view() <
99 rhs.message().name()->string_view();
100}
101
Austin Schuh217a9782019-12-21 23:02:50 -0800102bool operator==(const FlatbufferDetachedBuffer<Node> &lhs,
103 const FlatbufferDetachedBuffer<Node> &rhs) {
104 return lhs.message().name()->string_view() ==
105 rhs.message().name()->string_view();
106}
107
108bool operator<(const FlatbufferDetachedBuffer<Node> &lhs,
109 const FlatbufferDetachedBuffer<Node> &rhs) {
110 return lhs.message().name()->string_view() <
111 rhs.message().name()->string_view();
112}
113
Brian Silverman66f079a2013-08-26 16:24:30 -0700114namespace configuration {
115namespace {
116
Austin Schuhcb108412019-10-13 16:09:54 -0700117// Extracts the folder part of a path. Returns ./ if there is no path.
Austin Schuhf1fff282020-03-28 16:57:32 -0700118std::string_view ExtractFolder(const std::string_view filename) {
Austin Schuhcb108412019-10-13 16:09:54 -0700119 auto last_slash_pos = filename.find_last_of("/\\");
120
James Kuszmaul3ae42262019-11-08 12:33:41 -0800121 return last_slash_pos == std::string_view::npos
122 ? std::string_view("./")
Austin Schuhcb108412019-10-13 16:09:54 -0700123 : filename.substr(0, last_slash_pos + 1);
124}
125
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700126std::string AbsolutePath(const std::string_view filename) {
127 // Uses an std::string so that we know the input will be null-terminated.
128 const std::string terminated_file(filename);
129 char buffer[PATH_MAX];
130 PCHECK(NULL != realpath(terminated_file.c_str(), buffer));
131 return buffer;
132}
133
Austin Schuh40485ed2019-10-26 21:51:44 -0700134FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700135 const std::string_view path, absl::btree_set<std::string> *visited_paths,
136 const std::vector<std::string_view> &extra_import_paths) {
Austin Schuh15182322020-10-10 15:25:21 -0700137 std::string binary_path = MaybeReplaceExtension(path, ".json", ".bfbs");
138 bool binary_path_exists = util::PathExists(binary_path);
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700139 std::string raw_path(path);
Austin Schuh15182322020-10-10 15:25:21 -0700140 // For each .json file, look and see if we can find a .bfbs file next to it
141 // with the same base name. If we can, assume it is the same and use it
142 // instead. It is much faster to load .bfbs files than .json files.
143 if (!binary_path_exists && !util::PathExists(raw_path)) {
144 const bool path_is_absolute = raw_path.size() > 0 && raw_path[0] == '/';
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700145 if (path_is_absolute) {
146 CHECK(extra_import_paths.empty())
147 << "Can't specify extra import paths if attempting to read a config "
148 "file from an absolute path (path is "
Austin Schuh15182322020-10-10 15:25:21 -0700149 << raw_path << ").";
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700150 }
151
152 bool found_path = false;
153 for (const auto &import_path : extra_import_paths) {
154 raw_path = std::string(import_path) + "/" + std::string(path);
Austin Schuh15182322020-10-10 15:25:21 -0700155 binary_path = MaybeReplaceExtension(raw_path, ".json", ".bfbs");
156 binary_path_exists = util::PathExists(binary_path);
157 if (binary_path_exists) {
158 found_path = true;
159 break;
160 }
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700161 if (util::PathExists(raw_path)) {
162 found_path = true;
163 break;
164 }
165 }
166 CHECK(found_path) << ": Failed to find file " << path << ".";
167 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700168
Austin Schuh15182322020-10-10 15:25:21 -0700169 FlatbufferDetachedBuffer<Configuration> config = ReadConfigFile(
170 binary_path_exists ? binary_path : raw_path, binary_path_exists);
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700171
Austin Schuhcb108412019-10-13 16:09:54 -0700172 // Depth first. Take the following example:
173 //
174 // config1.json:
175 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700176 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700177 // {
178 // "name": "/foo",
179 // "type": ".aos.bar",
180 // "max_size": 5
181 // }
182 // ],
183 // "imports": [
184 // "config2.json",
185 // ]
186 // }
187 //
188 // config2.json:
189 // {
Austin Schuh40485ed2019-10-26 21:51:44 -0700190 // "channels": [
Austin Schuhcb108412019-10-13 16:09:54 -0700191 // {
192 // "name": "/foo",
193 // "type": ".aos.bar",
194 // "max_size": 7
195 // }
196 // ],
197 // }
198 //
199 // We want the main config (config1.json) to be able to override the imported
200 // config. That means that it needs to be merged into the imported configs,
201 // not the other way around.
202
Austin Schuh15182322020-10-10 15:25:21 -0700203 const std::string absolute_path =
204 AbsolutePath(binary_path_exists ? binary_path : raw_path);
205 // Track that we have seen this file before recursing. Track the path we
206 // actually loaded (which should be consistent if imported twice).
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700207 if (!visited_paths->insert(absolute_path).second) {
208 for (const auto &visited_path : *visited_paths) {
209 LOG(INFO) << "Already visited: " << visited_path;
210 }
211 LOG(FATAL)
212 << "Already imported " << path << " (i.e. " << absolute_path
213 << "). See above for the files that have already been processed.";
214 }
Austin Schuhcb108412019-10-13 16:09:54 -0700215
216 if (config.message().has_imports()) {
217 // Capture the imports.
218 const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> *v =
219 config.message().imports();
220
221 // And then wipe them. This gets GCed when we merge later.
222 config.mutable_message()->clear_imports();
223
224 // Start with an empty configuration to merge into.
Austin Schuh40485ed2019-10-26 21:51:44 -0700225 FlatbufferDetachedBuffer<Configuration> merged_config =
226 FlatbufferDetachedBuffer<Configuration>::Empty();
Austin Schuhcb108412019-10-13 16:09:54 -0700227
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700228 const std::string path_folder(ExtractFolder(path));
Austin Schuhcb108412019-10-13 16:09:54 -0700229 for (const flatbuffers::String *str : *v) {
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700230 const std::string included_config =
231 path_folder + "/" + std::string(str->string_view());
Austin Schuhcb108412019-10-13 16:09:54 -0700232
233 // And them merge everything in.
234 merged_config = MergeFlatBuffers(
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700235 merged_config,
236 ReadConfig(included_config, visited_paths, extra_import_paths));
Austin Schuhcb108412019-10-13 16:09:54 -0700237 }
238
239 // Finally, merge this file in.
240 config = MergeFlatBuffers(merged_config, config);
241 }
242 return config;
243}
244
Alex Perrycb7da4b2019-08-28 19:35:56 -0700245// Compares (c < p) a channel, and a name, type tuple.
246bool CompareChannels(const Channel *c,
James Kuszmaul3ae42262019-11-08 12:33:41 -0800247 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700248 int name_compare = c->name()->string_view().compare(p.first);
249 if (name_compare == 0) {
250 return c->type()->string_view() < p.second;
251 } else if (name_compare < 0) {
252 return true;
253 } else {
254 return false;
255 }
256};
257
258// Compares for equality (c == p) a channel, and a name, type tuple.
259bool EqualsChannels(const Channel *c,
Austin Schuhf1fff282020-03-28 16:57:32 -0700260 ::std::pair<std::string_view, std::string_view> p) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700261 return c->name()->string_view() == p.first &&
262 c->type()->string_view() == p.second;
263}
264
265// Compares (c < p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800266bool CompareApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700267 return a->name()->string_view() < name;
268};
269
270// Compares for equality (c == p) an application, and a name;
James Kuszmaul3ae42262019-11-08 12:33:41 -0800271bool EqualsApplications(const Application *a, std::string_view name) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700272 return a->name()->string_view() == name;
273}
274
275// Maps name for the provided maps. Modifies name.
276void HandleMaps(const flatbuffers::Vector<flatbuffers::Offset<aos::Map>> *maps,
Austin Schuhf1fff282020-03-28 16:57:32 -0700277 std::string *name, std::string_view type, const Node *node) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700278 // For the same reason we merge configs in reverse order, we want to process
279 // maps in reverse order. That lets the outer config overwrite channels from
280 // the inner configs.
281 for (auto i = maps->rbegin(); i != maps->rend(); ++i) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800282 if (!i->has_match() || !i->match()->has_name()) {
283 continue;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700284 }
Austin Schuhbca6cf02019-12-22 17:28:34 -0800285 if (!i->has_rename() || !i->rename()->has_name()) {
286 continue;
287 }
288
289 // Handle normal maps (now that we know that match and rename are filled
290 // out).
Austin Schuhf1fff282020-03-28 16:57:32 -0700291 const std::string_view match_name = i->match()->name()->string_view();
292 if (match_name != *name) {
293 if (match_name.back() == '*' &&
294 std::string_view(*name).substr(
295 0, std::min(name->size(), match_name.size() - 1)) ==
296 match_name.substr(0, match_name.size() - 1)) {
297 CHECK_EQ(match_name.find('*'), match_name.size() - 1);
298 } else {
299 continue;
300 }
Austin Schuhbca6cf02019-12-22 17:28:34 -0800301 }
302
303 // Handle type specific maps.
304 if (i->match()->has_type() && i->match()->type()->string_view() != type) {
305 continue;
306 }
307
Austin Schuhf1fff282020-03-28 16:57:32 -0700308 // Now handle node specific maps.
Austin Schuhbca6cf02019-12-22 17:28:34 -0800309 if (node != nullptr && i->match()->has_source_node() &&
310 i->match()->source_node()->string_view() !=
311 node->name()->string_view()) {
312 continue;
313 }
314
Austin Schuhf1fff282020-03-28 16:57:32 -0700315 std::string new_name(i->rename()->name()->string_view());
316 if (match_name.back() == '*') {
317 new_name += std::string(name->substr(match_name.size() - 1));
318 }
319 VLOG(1) << "Renamed \"" << *name << "\" to \"" << new_name << "\"";
320 *name = std::move(new_name);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700321 }
322}
323
Austin Schuh15182322020-10-10 15:25:21 -0700324void ValidateConfiguration(const Flatbuffer<Configuration> &config) {
325 // No imports should be left.
326 CHECK(!config.message().has_imports());
327
328 // Check that if there is a node list, all the source nodes are filled out and
329 // valid, and all the destination nodes are valid (and not the source). This
330 // is a basic consistency check.
331 if (config.message().has_channels()) {
332 const Channel *last_channel = nullptr;
333 for (const Channel *c : *config.message().channels()) {
334 CHECK(c->has_name());
335 CHECK(c->has_type());
336 if (c->name()->string_view().back() == '/') {
337 LOG(FATAL) << "Channel names can't end with '/'";
338 }
339 if (c->name()->string_view().find("//") != std::string_view::npos) {
340 LOG(FATAL) << ": Invalid channel name " << c->name()->string_view()
341 << ", can't use //.";
342 }
343 for (const char data : c->name()->string_view()) {
344 if (data >= '0' && data <= '9') {
345 continue;
346 }
347 if (data >= 'a' && data <= 'z') {
348 continue;
349 }
350 if (data >= 'A' && data <= 'Z') {
351 continue;
352 }
353 if (data == '-' || data == '_' || data == '/') {
354 continue;
355 }
356 LOG(FATAL) << "Invalid channel name " << c->name()->string_view()
357 << ", can only use [-a-zA-Z0-9_/]";
358 }
359
360 // Make sure everything is sorted while we are here... If this fails,
361 // there will be a bunch of weird errors.
362 if (last_channel != nullptr) {
363 CHECK(CompareChannels(
364 last_channel,
365 std::make_pair(c->name()->string_view(), c->type()->string_view())))
366 << ": Channels not sorted!";
367 }
368 last_channel = c;
369 }
370 }
371
372 if (config.message().has_nodes() && config.message().has_channels()) {
373 for (const Channel *c : *config.message().channels()) {
374 CHECK(c->has_source_node()) << ": Channel " << FlatbufferToJson(c)
375 << " is missing \"source_node\"";
376 CHECK(GetNode(&config.message(), c->source_node()->string_view()) !=
377 nullptr)
378 << ": Channel " << FlatbufferToJson(c)
379 << " has an unknown \"source_node\"";
380
381 if (c->has_destination_nodes()) {
382 for (const Connection *connection : *c->destination_nodes()) {
383 CHECK(connection->has_name());
384 CHECK(GetNode(&config.message(), connection->name()->string_view()) !=
385 nullptr)
386 << ": Channel " << FlatbufferToJson(c)
387 << " has an unknown \"destination_nodes\" "
388 << connection->name()->string_view();
389
390 switch (connection->timestamp_logger()) {
391 case LoggerConfig::LOCAL_LOGGER:
392 case LoggerConfig::NOT_LOGGED:
393 CHECK(!connection->has_timestamp_logger_nodes());
394 break;
395 case LoggerConfig::REMOTE_LOGGER:
396 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
397 CHECK(connection->has_timestamp_logger_nodes());
398 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
399 for (const flatbuffers::String *timestamp_logger_node :
400 *connection->timestamp_logger_nodes()) {
401 CHECK(GetNode(&config.message(),
402 timestamp_logger_node->string_view()) != nullptr)
403 << ": Channel " << FlatbufferToJson(c)
404 << " has an unknown \"timestamp_logger_node\""
405 << connection->name()->string_view();
406 }
407 break;
408 }
409
410 CHECK_NE(connection->name()->string_view(),
411 c->source_node()->string_view())
412 << ": Channel " << FlatbufferToJson(c)
413 << " is forwarding data to itself";
414 }
415 }
416 }
417 }
418}
419
Alex Perrycb7da4b2019-08-28 19:35:56 -0700420} // namespace
421
Austin Schuh40485ed2019-10-26 21:51:44 -0700422FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
Austin Schuhcb108412019-10-13 16:09:54 -0700423 const Flatbuffer<Configuration> &config) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700424 // auto_merge_config will contain all the fields of the Configuration that are
425 // to be passed through unmodified to the result of MergeConfiguration().
426 // In the processing below, we mutate auto_merge_config to remove any fields
427 // which we do need to alter (hence why we can't use the input config
428 // directly), and then merge auto_merge_config back in at the end.
429 aos::FlatbufferDetachedBuffer<aos::Configuration> auto_merge_config =
Austin Schuha4fc60f2020-11-01 23:06:47 -0800430 aos::RecursiveCopyFlatBuffer(&config.message());
James Kuszmaul3c998592020-07-27 21:04:47 -0700431
Austin Schuh40485ed2019-10-26 21:51:44 -0700432 // Store all the channels in a sorted set. This lets us track channels we
Austin Schuhcb108412019-10-13 16:09:54 -0700433 // have seen before and merge the updates in.
Austin Schuh40485ed2019-10-26 21:51:44 -0700434 absl::btree_set<FlatbufferDetachedBuffer<Channel>> channels;
Austin Schuhcb108412019-10-13 16:09:54 -0700435
Austin Schuh40485ed2019-10-26 21:51:44 -0700436 if (config.message().has_channels()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700437 auto_merge_config.mutable_message()->clear_channels();
Austin Schuh40485ed2019-10-26 21:51:44 -0700438 for (const Channel *c : *config.message().channels()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700439 // Ignore malformed entries.
Austin Schuh40485ed2019-10-26 21:51:44 -0700440 if (!c->has_name()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700441 continue;
442 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700443 if (!c->has_type()) {
Austin Schuhcb108412019-10-13 16:09:54 -0700444 continue;
445 }
446
Brian Silverman77162972020-08-12 19:52:40 -0700447 CHECK_EQ(c->read_method() == ReadMethod::PIN, c->num_readers() != 0)
448 << ": num_readers may be set if and only if read_method is PIN,"
449 " if you want 0 readers do not set PIN: "
450 << CleanedChannelToString(c);
451
Austin Schuh40485ed2019-10-26 21:51:44 -0700452 // Attempt to insert the channel.
Austin Schuha4fc60f2020-11-01 23:06:47 -0800453 auto result = channels.insert(RecursiveCopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700454 if (!result.second) {
455 // Already there, so merge the new table into the original.
Austin Schuha4fc60f2020-11-01 23:06:47 -0800456 *result.first =
457 MergeFlatBuffers(*result.first, RecursiveCopyFlatBuffer(c));
Austin Schuhcb108412019-10-13 16:09:54 -0700458 }
459 }
460 }
461
462 // Now repeat this for the application list.
Austin Schuh40485ed2019-10-26 21:51:44 -0700463 absl::btree_set<FlatbufferDetachedBuffer<Application>> applications;
Austin Schuhcb108412019-10-13 16:09:54 -0700464 if (config.message().has_applications()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700465 auto_merge_config.mutable_message()->clear_applications();
Austin Schuhcb108412019-10-13 16:09:54 -0700466 for (const Application *a : *config.message().applications()) {
467 if (!a->has_name()) {
468 continue;
469 }
470
Austin Schuha4fc60f2020-11-01 23:06:47 -0800471 auto result = applications.insert(RecursiveCopyFlatBuffer(a));
Austin Schuhcb108412019-10-13 16:09:54 -0700472 if (!result.second) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800473 *result.first =
474 MergeFlatBuffers(*result.first, RecursiveCopyFlatBuffer(a));
Austin Schuhcb108412019-10-13 16:09:54 -0700475 }
476 }
477 }
478
Austin Schuh217a9782019-12-21 23:02:50 -0800479 // Now repeat this for the node list.
480 absl::btree_set<FlatbufferDetachedBuffer<Node>> nodes;
481 if (config.message().has_nodes()) {
James Kuszmaul3c998592020-07-27 21:04:47 -0700482 auto_merge_config.mutable_message()->clear_nodes();
Austin Schuh217a9782019-12-21 23:02:50 -0800483 for (const Node *n : *config.message().nodes()) {
484 if (!n->has_name()) {
485 continue;
486 }
487
Austin Schuha4fc60f2020-11-01 23:06:47 -0800488 auto result = nodes.insert(RecursiveCopyFlatBuffer(n));
Austin Schuh217a9782019-12-21 23:02:50 -0800489 if (!result.second) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800490 *result.first =
491 MergeFlatBuffers(*result.first, RecursiveCopyFlatBuffer(n));
Austin Schuh217a9782019-12-21 23:02:50 -0800492 }
493 }
494 }
495
Austin Schuhcb108412019-10-13 16:09:54 -0700496 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800497 fbb.ForceDefaults(true);
Austin Schuhcb108412019-10-13 16:09:54 -0700498
499 // Start by building the vectors. They need to come before the final table.
Austin Schuh40485ed2019-10-26 21:51:44 -0700500 // Channels
501 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
502 channels_offset;
Austin Schuhcb108412019-10-13 16:09:54 -0700503 {
Austin Schuh40485ed2019-10-26 21:51:44 -0700504 ::std::vector<flatbuffers::Offset<Channel>> channel_offsets;
505 for (const FlatbufferDetachedBuffer<Channel> &c : channels) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800506 channel_offsets.emplace_back(
507 RecursiveCopyFlatBuffer<Channel>(&c.message(), &fbb));
Austin Schuhcb108412019-10-13 16:09:54 -0700508 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700509 channels_offset = fbb.CreateVector(channel_offsets);
Austin Schuhcb108412019-10-13 16:09:54 -0700510 }
511
512 // Applications
513 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
514 applications_offset;
515 {
516 ::std::vector<flatbuffers::Offset<Application>> applications_offsets;
Austin Schuh40485ed2019-10-26 21:51:44 -0700517 for (const FlatbufferDetachedBuffer<Application> &a : applications) {
Austin Schuhcb108412019-10-13 16:09:54 -0700518 applications_offsets.emplace_back(
Austin Schuha4fc60f2020-11-01 23:06:47 -0800519 RecursiveCopyFlatBuffer<Application>(&a.message(), &fbb));
Austin Schuhcb108412019-10-13 16:09:54 -0700520 }
521 applications_offset = fbb.CreateVector(applications_offsets);
522 }
523
Austin Schuh217a9782019-12-21 23:02:50 -0800524 // Nodes
525 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
526 nodes_offset;
527 {
528 ::std::vector<flatbuffers::Offset<Node>> node_offsets;
529 for (const FlatbufferDetachedBuffer<Node> &n : nodes) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800530 node_offsets.emplace_back(
531 RecursiveCopyFlatBuffer<Node>(&n.message(), &fbb));
Austin Schuh217a9782019-12-21 23:02:50 -0800532 }
533 nodes_offset = fbb.CreateVector(node_offsets);
534 }
535
Austin Schuhcb108412019-10-13 16:09:54 -0700536 // And then build a Configuration with them all.
537 ConfigurationBuilder configuration_builder(fbb);
Austin Schuh40485ed2019-10-26 21:51:44 -0700538 configuration_builder.add_channels(channels_offset);
Austin Schuh217a9782019-12-21 23:02:50 -0800539 if (config.message().has_applications()) {
540 configuration_builder.add_applications(applications_offset);
541 }
542 if (config.message().has_nodes()) {
543 configuration_builder.add_nodes(nodes_offset);
544 }
Austin Schuhcb108412019-10-13 16:09:54 -0700545
546 fbb.Finish(configuration_builder.Finish());
Austin Schuh217a9782019-12-21 23:02:50 -0800547
James Kuszmaul3c998592020-07-27 21:04:47 -0700548 aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config(
549 fbb.Release());
550
Austin Schuh217a9782019-12-21 23:02:50 -0800551 // Now, validate that if there is a node list, every channel has a source
552 // node.
James Kuszmaul3c998592020-07-27 21:04:47 -0700553 FlatbufferDetachedBuffer<Configuration> result =
554 MergeFlatBuffers(modified_config, auto_merge_config);
Austin Schuh217a9782019-12-21 23:02:50 -0800555
Austin Schuh15182322020-10-10 15:25:21 -0700556 ValidateConfiguration(result);
Austin Schuh217a9782019-12-21 23:02:50 -0800557
558 return result;
Austin Schuhcb108412019-10-13 16:09:54 -0700559}
560
Austin Schuh40485ed2019-10-26 21:51:44 -0700561FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaulc0c08da2020-05-10 18:56:07 -0700562 const std::string_view path,
563 const std::vector<std::string_view> &import_paths) {
Austin Schuhcb108412019-10-13 16:09:54 -0700564 // We only want to read a file once. So track the visited files in a set.
565 absl::btree_set<std::string> visited_paths;
Austin Schuh15182322020-10-10 15:25:21 -0700566 FlatbufferDetachedBuffer<Configuration> read_config =
567 ReadConfig(path, &visited_paths, import_paths);
568
569 // If we only read one file, and it had a .bfbs extension, it has to be a
570 // fully formatted config. Do a quick verification and return it.
571 if (visited_paths.size() == 1 && EndsWith(*visited_paths.begin(), ".bfbs")) {
572 ValidateConfiguration(read_config);
573 return read_config;
574 }
575
576 return MergeConfiguration(read_config);
Austin Schuhcb108412019-10-13 16:09:54 -0700577}
578
Austin Schuh8d6cea82020-02-28 12:17:16 -0800579FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
Brian Silverman24f5aa82020-06-23 16:21:28 -0700580 const Configuration *config, const Flatbuffer<Configuration> &addition) {
581 return MergeConfiguration(MergeFlatBuffers(config, &addition.message()));
582}
583
584FlatbufferDetachedBuffer<Configuration> MergeWithConfig(
Austin Schuh8d6cea82020-02-28 12:17:16 -0800585 const Configuration *config, std::string_view json) {
586 FlatbufferDetachedBuffer<Configuration> addition =
587 JsonToFlatbuffer(json, Configuration::MiniReflectTypeTable());
588
Brian Silverman24f5aa82020-06-23 16:21:28 -0700589 return MergeWithConfig(config, addition);
Austin Schuh8d6cea82020-02-28 12:17:16 -0800590}
591
James Kuszmaul3ae42262019-11-08 12:33:41 -0800592const Channel *GetChannel(const Configuration *config, std::string_view name,
593 std::string_view type,
Austin Schuhbca6cf02019-12-22 17:28:34 -0800594 std::string_view application_name, const Node *node) {
595 const std::string_view original_name = name;
Austin Schuhf1fff282020-03-28 16:57:32 -0700596 std::string mutable_name;
Austin Schuh4c3b9702020-08-30 11:34:55 -0700597 if (node != nullptr) {
598 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
599 << "\" } on " << aos::FlatbufferToJson(node);
600 } else {
601 VLOG(1) << "Looking up { \"name\": \"" << name << "\", \"type\": \"" << type
602 << "\" }";
603 }
Austin Schuhcb108412019-10-13 16:09:54 -0700604
605 // First handle application specific maps. Only do this if we have a matching
606 // application name, and it has maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700607 if (config->has_applications()) {
608 auto application_iterator = std::lower_bound(
609 config->applications()->cbegin(), config->applications()->cend(),
610 application_name, CompareApplications);
611 if (application_iterator != config->applications()->cend() &&
Austin Schuhcb108412019-10-13 16:09:54 -0700612 EqualsApplications(*application_iterator, application_name)) {
613 if (application_iterator->has_maps()) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700614 mutable_name = std::string(name);
615 HandleMaps(application_iterator->maps(), &mutable_name, type, node);
616 name = std::string_view(mutable_name);
Austin Schuhcb108412019-10-13 16:09:54 -0700617 }
618 }
619 }
620
621 // Now do global maps.
Austin Schuh40485ed2019-10-26 21:51:44 -0700622 if (config->has_maps()) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700623 mutable_name = std::string(name);
624 HandleMaps(config->maps(), &mutable_name, type, node);
625 name = std::string_view(mutable_name);
Austin Schuhcb108412019-10-13 16:09:54 -0700626 }
627
Austin Schuhbca6cf02019-12-22 17:28:34 -0800628 if (original_name != name) {
629 VLOG(1) << "Remapped to { \"name\": \"" << name << "\", \"type\": \""
630 << type << "\" }";
631 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700632
Austin Schuh40485ed2019-10-26 21:51:44 -0700633 // Then look for the channel.
634 auto channel_iterator =
Austin Schuhf1fff282020-03-28 16:57:32 -0700635 std::lower_bound(config->channels()->cbegin(), config->channels()->cend(),
Austin Schuh40485ed2019-10-26 21:51:44 -0700636 std::make_pair(name, type), CompareChannels);
Austin Schuhcb108412019-10-13 16:09:54 -0700637
638 // Make sure we actually found it, and it matches.
Austin Schuh40485ed2019-10-26 21:51:44 -0700639 if (channel_iterator != config->channels()->cend() &&
640 EqualsChannels(*channel_iterator, std::make_pair(name, type))) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800641 if (VLOG_IS_ON(2)) {
642 VLOG(2) << "Found: " << FlatbufferToJson(*channel_iterator);
643 } else if (VLOG_IS_ON(1)) {
644 VLOG(1) << "Found: " << CleanedChannelToString(*channel_iterator);
645 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700646 return *channel_iterator;
Austin Schuhcb108412019-10-13 16:09:54 -0700647 } else {
648 VLOG(1) << "No match for { \"name\": \"" << name << "\", \"type\": \""
649 << type << "\" }";
Austin Schuh4b42b252020-10-19 11:35:20 -0700650 if (original_name != name) {
651 LOG(WARNING) << "Remapped from {\"name\": \"" << original_name
652 << "\", \"type\": \"" << type << "\"}, to {\"name\": \""
653 << name << "\", \"type\": \"" << type
654 << "\"}, but no channel by that name exists.";
655 }
Austin Schuhcb108412019-10-13 16:09:54 -0700656 return nullptr;
657 }
658}
659
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800660size_t ChannelIndex(const Configuration *configuration,
661 const Channel *channel) {
662 CHECK(configuration->channels() != nullptr) << ": No channels";
663
664 auto c = std::find(configuration->channels()->begin(),
665 configuration->channels()->end(), channel);
666 CHECK(c != configuration->channels()->end())
667 << ": Channel pointer not found in configuration()->channels()";
668
669 return std::distance(configuration->channels()->begin(), c);
670}
671
Austin Schuhbca6cf02019-12-22 17:28:34 -0800672std::string CleanedChannelToString(const Channel *channel) {
673 FlatbufferDetachedBuffer<Channel> cleaned_channel = CopyFlatBuffer(channel);
674 cleaned_channel.mutable_message()->clear_schema();
675 return FlatbufferToJson(cleaned_channel);
676}
677
Austin Schuha81454b2020-05-12 19:58:36 -0700678std::string StrippedChannelToString(const Channel *channel) {
679 return absl::StrCat("{ \"name\": \"", channel->name()->string_view(),
680 "\", \"type\": \"", channel->type()->string_view(),
681 "\" }");
682}
683
Alex Perrycb7da4b2019-08-28 19:35:56 -0700684FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
685 const Flatbuffer<Configuration> &config,
686 const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas) {
687 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800688 fbb.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700689
Austin Schuh68d98592020-11-01 23:22:57 -0800690 // Cache for holding already inserted schemas.
691 std::map<std::string_view, flatbuffers::Offset<reflection::Schema>>
692 schema_cache;
693
694 CHECK_EQ(Channel::MiniReflectTypeTable()->num_elems, 13u)
695 << ": Merging logic needs to be updated when the number of channel "
696 "fields changes.";
James Kuszmaul3c998592020-07-27 21:04:47 -0700697
Alex Perrycb7da4b2019-08-28 19:35:56 -0700698 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
699 channels_offset;
700 if (config.message().has_channels()) {
701 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
702 for (const Channel *c : *config.message().channels()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700703 // Search for a schema with a matching type.
704 const aos::FlatbufferString<reflection::Schema> *found_schema = nullptr;
Austin Schuhf1fff282020-03-28 16:57:32 -0700705 for (const aos::FlatbufferString<reflection::Schema> &schema : schemas) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700706 if (schema.message().root_table() != nullptr) {
707 if (schema.message().root_table()->name()->string_view() ==
708 c->type()->string_view()) {
709 found_schema = &schema;
710 }
711 }
712 }
713
714 CHECK(found_schema != nullptr)
715 << ": Failed to find schema for " << FlatbufferToJson(c);
716
Austin Schuh68d98592020-11-01 23:22:57 -0800717 // Now copy the message manually.
718 auto cached_schema = schema_cache.find(c->type()->string_view());
719 flatbuffers::Offset<reflection::Schema> schema_offset;
720 if (cached_schema != schema_cache.end()) {
721 schema_offset = cached_schema->second;
722 } else {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800723 schema_offset = RecursiveCopyFlatBuffer<reflection::Schema>(
724 &found_schema->message(), &fbb);
Austin Schuh68d98592020-11-01 23:22:57 -0800725 schema_cache.emplace(c->type()->string_view(), schema_offset);
726 }
727
728 flatbuffers::Offset<flatbuffers::String> name_offset =
729 fbb.CreateSharedString(c->name()->str());
730 flatbuffers::Offset<flatbuffers::String> type_offset =
731 fbb.CreateSharedString(c->type()->str());
732 flatbuffers::Offset<flatbuffers::String> source_node_offset =
Austin Schuha4fc60f2020-11-01 23:06:47 -0800733 c->has_source_node() ? fbb.CreateSharedString(c->source_node()->str())
734 : 0;
Austin Schuh68d98592020-11-01 23:22:57 -0800735
736 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Connection>>>
737 destination_nodes_offset =
738 aos::CopyVectorTable(c->destination_nodes(), &fbb);
739
740 flatbuffers::Offset<
741 flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>>>
742 logger_nodes_offset =
743 aos::CopyVectorSharedString(c->logger_nodes(), &fbb);
744
745 Channel::Builder channel_builder(fbb);
746 channel_builder.add_name(name_offset);
747 channel_builder.add_type(type_offset);
748 if (c->has_frequency()) {
749 channel_builder.add_frequency(c->frequency());
750 }
751 if (c->has_max_size()) {
752 channel_builder.add_max_size(c->max_size());
753 }
754 if (c->has_num_senders()) {
755 channel_builder.add_num_senders(c->num_senders());
756 }
757 if (c->has_num_watchers()) {
758 channel_builder.add_num_watchers(c->num_watchers());
759 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700760 channel_builder.add_schema(schema_offset);
Austin Schuh68d98592020-11-01 23:22:57 -0800761 if (!source_node_offset.IsNull()) {
762 channel_builder.add_source_node(source_node_offset);
763 }
764 if (!destination_nodes_offset.IsNull()) {
765 channel_builder.add_destination_nodes(destination_nodes_offset);
766 }
767 if (c->has_logger()) {
768 channel_builder.add_logger(c->logger());
769 }
770 if (!logger_nodes_offset.IsNull()) {
771 channel_builder.add_logger_nodes(logger_nodes_offset);
772 }
773 if (c->has_read_method()) {
774 channel_builder.add_read_method(c->read_method());
775 }
776 if (c->has_num_readers()) {
777 channel_builder.add_num_readers(c->num_readers());
778 }
779 channel_offsets.emplace_back(channel_builder.Finish());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700780 }
781 channels_offset = fbb.CreateVector(channel_offsets);
782 }
783
Austin Schuh68d98592020-11-01 23:22:57 -0800784 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Map>>>
785 maps_offset = aos::CopyVectorTable(config.message().maps(), &fbb);
786
787 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Node>>>
788 nodes_offset = aos::CopyVectorTable(config.message().nodes(), &fbb);
789
790 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Application>>>
791 applications_offset =
792 aos::CopyVectorTable(config.message().applications(), &fbb);
Austin Schuh217a9782019-12-21 23:02:50 -0800793
794 // Now insert everything else in unmodified.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700795 ConfigurationBuilder configuration_builder(fbb);
796 if (config.message().has_channels()) {
797 configuration_builder.add_channels(channels_offset);
798 }
Austin Schuh68d98592020-11-01 23:22:57 -0800799 if (!maps_offset.IsNull()) {
800 configuration_builder.add_maps(maps_offset);
801 }
802 if (!nodes_offset.IsNull()) {
803 configuration_builder.add_nodes(nodes_offset);
804 }
805 if (!applications_offset.IsNull()) {
806 configuration_builder.add_applications(applications_offset);
807 }
808
809 if (config.message().has_channel_storage_duration()) {
810 configuration_builder.add_channel_storage_duration(
811 config.message().channel_storage_duration());
812 }
813
814 CHECK_EQ(Configuration::MiniReflectTypeTable()->num_elems, 6u)
815 << ": Merging logic needs to be updated when the number of configuration "
816 "fields changes.";
817
Alex Perrycb7da4b2019-08-28 19:35:56 -0700818 fbb.Finish(configuration_builder.Finish());
James Kuszmaul3c998592020-07-27 21:04:47 -0700819 aos::FlatbufferDetachedBuffer<aos::Configuration> modified_config(
820 fbb.Release());
821
Austin Schuh68d98592020-11-01 23:22:57 -0800822 return modified_config;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700823}
824
Austin Schuh217a9782019-12-21 23:02:50 -0800825const Node *GetNodeFromHostname(const Configuration *config,
826 std::string_view hostname) {
827 for (const Node *node : *config->nodes()) {
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800828 if (node->has_hostname() && node->hostname()->string_view() == hostname) {
Austin Schuh217a9782019-12-21 23:02:50 -0800829 return node;
830 }
Brian Silvermanaa2633f2020-02-17 21:04:14 -0800831 if (node->has_hostnames()) {
832 for (const auto &candidate : *node->hostnames()) {
833 if (candidate->string_view() == hostname) {
834 return node;
835 }
836 }
837 }
Austin Schuh217a9782019-12-21 23:02:50 -0800838 }
839 return nullptr;
840}
Austin Schuhac0771c2020-01-07 18:36:30 -0800841
Austin Schuh217a9782019-12-21 23:02:50 -0800842const Node *GetMyNode(const Configuration *config) {
843 const std::string hostname = (FLAGS_override_hostname.size() > 0)
844 ? FLAGS_override_hostname
845 : network::GetHostname();
846 const Node *node = GetNodeFromHostname(config, hostname);
847 if (node != nullptr) return node;
848
849 LOG(FATAL) << "Unknown node for host: " << hostname
850 << ". Consider using --override_hostname if hostname detection "
851 "is wrong.";
852 return nullptr;
853}
854
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800855const Node *GetNode(const Configuration *config, const Node *node) {
856 if (!MultiNode(config)) {
857 CHECK(node == nullptr) << ": Provided a node in a single node world.";
858 return nullptr;
859 } else {
860 CHECK(node != nullptr);
861 CHECK(node->has_name());
862 return GetNode(config, node->name()->string_view());
863 }
864}
865
Austin Schuh217a9782019-12-21 23:02:50 -0800866const Node *GetNode(const Configuration *config, std::string_view name) {
Austin Schuhfd960622020-01-01 13:22:55 -0800867 CHECK(config->has_nodes())
868 << ": Asking for a node from a single node configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800869 for (const Node *node : *config->nodes()) {
Austin Schuhfd960622020-01-01 13:22:55 -0800870 CHECK(node->has_name()) << ": Malformed node " << FlatbufferToJson(node);
Austin Schuh217a9782019-12-21 23:02:50 -0800871 if (node->name()->string_view() == name) {
872 return node;
873 }
874 }
875 return nullptr;
876}
877
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800878const Node *GetNodeOrDie(const Configuration *config, const Node *node) {
879 if (!MultiNode(config)) {
880 CHECK(node == nullptr) << ": Provided a node in a single node world.";
881 return nullptr;
882 } else {
883 const Node *config_node = GetNode(config, node);
884 if (config_node == nullptr) {
885 LOG(FATAL) << "Couldn't find node matching " << FlatbufferToJson(node);
886 }
887 return config_node;
888 }
889}
890
Austin Schuh8bd96322020-02-13 21:18:22 -0800891namespace {
892int GetNodeIndexFromConfig(const Configuration *config, const Node *node) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800893 int node_index = 0;
894 for (const Node *iterated_node : *config->nodes()) {
895 if (iterated_node == node) {
896 return node_index;
897 }
898 ++node_index;
899 }
Austin Schuh8bd96322020-02-13 21:18:22 -0800900 return -1;
901}
902} // namespace
903
904int GetNodeIndex(const Configuration *config, const Node *node) {
905 if (!MultiNode(config)) {
906 return 0;
907 }
908
909 {
910 int node_index = GetNodeIndexFromConfig(config, node);
911 if (node_index != -1) {
912 return node_index;
913 }
914 }
915
916 const Node *result = GetNode(config, node);
917 CHECK(result != nullptr);
918
919 {
Austin Schuh04408fc2020-02-16 21:48:54 -0800920 int node_index = GetNodeIndexFromConfig(config, result);
Austin Schuh8bd96322020-02-13 21:18:22 -0800921 if (node_index != -1) {
922 return node_index;
923 }
924 }
925
926 LOG(FATAL) << "Node " << FlatbufferToJson(node)
927 << " not found in the configuration.";
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800928}
929
Austin Schuh04408fc2020-02-16 21:48:54 -0800930int GetNodeIndex(const Configuration *config, std::string_view name) {
931 if (!MultiNode(config)) {
932 return 0;
933 }
934
935 {
936 int node_index = 0;
937 for (const Node *iterated_node : *config->nodes()) {
938 if (iterated_node->name()->string_view() == name) {
939 return node_index;
940 }
941 ++node_index;
942 }
943 }
944 LOG(FATAL) << "Node " << name << " not found in the configuration.";
945}
946
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800947std::vector<const Node *> GetNodes(const Configuration *config) {
948 std::vector<const Node *> nodes;
Austin Schuh8bd96322020-02-13 21:18:22 -0800949 if (MultiNode(config)) {
Austin Schuhc9e10ec2020-01-26 16:08:28 -0800950 for (const Node *node : *config->nodes()) {
951 nodes.emplace_back(node);
952 }
953 } else {
954 nodes.emplace_back(nullptr);
955 }
956 return nodes;
957}
958
Austin Schuhac0771c2020-01-07 18:36:30 -0800959bool MultiNode(const Configuration *config) { return config->has_nodes(); }
960
Austin Schuh217a9782019-12-21 23:02:50 -0800961bool ChannelIsSendableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800962 if (node == nullptr) {
963 return true;
964 }
Austin Schuh3c5dae52020-10-06 18:55:18 -0700965 CHECK(channel->has_source_node()) << FlatbufferToJson(channel);
966 CHECK(node->has_name()) << FlatbufferToJson(node);
Austin Schuh196a4452020-03-15 23:12:03 -0700967 return (CHECK_NOTNULL(channel)->source_node()->string_view() ==
968 node->name()->string_view());
Austin Schuh217a9782019-12-21 23:02:50 -0800969}
970
971bool ChannelIsReadableOnNode(const Channel *channel, const Node *node) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800972 if (node == nullptr) {
973 return true;
974 }
975
Austin Schuh217a9782019-12-21 23:02:50 -0800976 if (channel->source_node()->string_view() == node->name()->string_view()) {
977 return true;
978 }
979
980 if (!channel->has_destination_nodes()) {
981 return false;
982 }
983
Austin Schuh719946b2019-12-28 14:51:01 -0800984 for (const Connection *connection : *channel->destination_nodes()) {
985 CHECK(connection->has_name());
986 if (connection->name()->string_view() == node->name()->string_view()) {
Austin Schuh217a9782019-12-21 23:02:50 -0800987 return true;
988 }
989 }
990
991 return false;
992}
993
Austin Schuh719946b2019-12-28 14:51:01 -0800994bool ChannelMessageIsLoggedOnNode(const Channel *channel, const Node *node) {
Austin Schuhf1fff282020-03-28 16:57:32 -0700995 switch (channel->logger()) {
Austin Schuh719946b2019-12-28 14:51:01 -0800996 case LoggerConfig::LOCAL_LOGGER:
997 if (node == nullptr) {
998 // Single node world. If there is a local logger, then we want to use
999 // it.
1000 return true;
1001 }
1002 return channel->source_node()->string_view() ==
1003 node->name()->string_view();
Austin Schuh719946b2019-12-28 14:51:01 -08001004 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -07001005 CHECK(channel->has_logger_nodes());
1006 CHECK_GT(channel->logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -08001007
1008 if (channel->source_node()->string_view() ==
1009 CHECK_NOTNULL(node)->name()->string_view()) {
1010 return true;
1011 }
Austin Schuhda40e472020-03-28 15:15:29 -07001012
1013 [[fallthrough]];
1014 case LoggerConfig::REMOTE_LOGGER:
1015 CHECK(channel->has_logger_nodes());
1016 CHECK_GT(channel->logger_nodes()->size(), 0u);
1017 for (const flatbuffers::String *logger_node : *channel->logger_nodes()) {
1018 if (logger_node->string_view() ==
1019 CHECK_NOTNULL(node)->name()->string_view()) {
1020 return true;
1021 }
Austin Schuh719946b2019-12-28 14:51:01 -08001022 }
1023
1024 return false;
1025 case LoggerConfig::NOT_LOGGED:
1026 return false;
1027 }
1028
1029 LOG(FATAL) << "Unknown logger config " << static_cast<int>(channel->logger());
1030}
1031
1032const Connection *ConnectionToNode(const Channel *channel, const Node *node) {
1033 if (!channel->has_destination_nodes()) {
1034 return nullptr;
1035 }
1036 for (const Connection *connection : *channel->destination_nodes()) {
1037 if (connection->name()->string_view() == node->name()->string_view()) {
1038 return connection;
1039 }
1040 }
1041 return nullptr;
1042}
1043
1044bool ConnectionDeliveryTimeIsLoggedOnNode(const Channel *channel,
1045 const Node *node,
1046 const Node *logger_node) {
1047 const Connection *connection = ConnectionToNode(channel, node);
1048 if (connection == nullptr) {
1049 return false;
1050 }
1051 return ConnectionDeliveryTimeIsLoggedOnNode(connection, logger_node);
1052}
1053
1054bool ConnectionDeliveryTimeIsLoggedOnNode(const Connection *connection,
1055 const Node *node) {
1056 switch (connection->timestamp_logger()) {
1057 case LoggerConfig::LOCAL_AND_REMOTE_LOGGER:
Austin Schuhda40e472020-03-28 15:15:29 -07001058 CHECK(connection->has_timestamp_logger_nodes());
1059 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
Austin Schuh719946b2019-12-28 14:51:01 -08001060 if (connection->name()->string_view() == node->name()->string_view()) {
1061 return true;
1062 }
1063
Austin Schuhda40e472020-03-28 15:15:29 -07001064 [[fallthrough]];
1065 case LoggerConfig::REMOTE_LOGGER:
1066 CHECK(connection->has_timestamp_logger_nodes());
1067 CHECK_GT(connection->timestamp_logger_nodes()->size(), 0u);
1068 for (const flatbuffers::String *timestamp_logger_node :
1069 *connection->timestamp_logger_nodes()) {
1070 if (timestamp_logger_node->string_view() ==
1071 node->name()->string_view()) {
1072 return true;
1073 }
Austin Schuh719946b2019-12-28 14:51:01 -08001074 }
1075
1076 return false;
1077 case LoggerConfig::LOCAL_LOGGER:
1078 return connection->name()->string_view() == node->name()->string_view();
Austin Schuh719946b2019-12-28 14:51:01 -08001079 case LoggerConfig::NOT_LOGGED:
1080 return false;
1081 }
1082
1083 LOG(FATAL) << "Unknown logger config "
1084 << static_cast<int>(connection->timestamp_logger());
1085}
1086
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001087std::vector<std::string_view> SourceNodeNames(const Configuration *config,
1088 const Node *my_node) {
1089 std::set<std::string_view> result_set;
1090
1091 for (const Channel *channel : *config->channels()) {
1092 if (channel->has_destination_nodes()) {
1093 for (const Connection *connection : *channel->destination_nodes()) {
1094 if (connection->name()->string_view() ==
1095 my_node->name()->string_view()) {
1096 result_set.insert(channel->source_node()->string_view());
1097 }
1098 }
1099 }
1100 }
1101
1102 std::vector<std::string_view> result;
1103 for (const std::string_view source : result_set) {
1104 VLOG(1) << "Found a source node of " << source;
1105 result.emplace_back(source);
1106 }
1107 return result;
1108}
1109
1110std::vector<std::string_view> DestinationNodeNames(const Configuration *config,
1111 const Node *my_node) {
1112 std::vector<std::string_view> result;
1113
1114 for (const Channel *channel : *config->channels()) {
1115 if (channel->has_source_node() && channel->source_node()->string_view() ==
1116 my_node->name()->string_view()) {
1117 if (!channel->has_destination_nodes()) continue;
1118
1119 if (channel->source_node()->string_view() !=
1120 my_node->name()->string_view()) {
1121 continue;
1122 }
1123
1124 for (const Connection *connection : *channel->destination_nodes()) {
1125 if (std::find(result.begin(), result.end(),
1126 connection->name()->string_view()) == result.end()) {
1127 result.emplace_back(connection->name()->string_view());
1128 }
1129 }
1130 }
1131 }
1132
1133 for (const std::string_view destination : result) {
1134 VLOG(1) << "Found a destination node of " << destination;
1135 }
1136 return result;
1137}
1138
Austin Schuh8d7e0bb2020-10-02 17:57:00 -07001139std::vector<const Node *> TimestampNodes(const Configuration *config,
1140 const Node *my_node) {
1141 if (!configuration::MultiNode(config)) {
1142 CHECK(my_node == nullptr);
1143 return std::vector<const Node *>{};
1144 }
1145
1146 std::set<const Node *> timestamp_logger_nodes;
1147 for (const Channel *channel : *config->channels()) {
1148 if (!configuration::ChannelIsSendableOnNode(channel, my_node)) {
1149 continue;
1150 }
1151 if (!channel->has_destination_nodes()) {
1152 continue;
1153 }
1154 for (const Connection *connection : *channel->destination_nodes()) {
1155 const Node *other_node =
1156 configuration::GetNode(config, connection->name()->string_view());
1157
1158 if (configuration::ConnectionDeliveryTimeIsLoggedOnNode(connection,
1159 my_node)) {
1160 VLOG(1) << "Timestamps are logged from "
1161 << FlatbufferToJson(other_node);
1162 timestamp_logger_nodes.insert(other_node);
1163 }
1164 }
1165 }
1166
1167 std::vector<const Node *> result;
1168 for (const Node *node : timestamp_logger_nodes) {
1169 result.emplace_back(node);
1170 }
1171 return result;
1172}
1173
Brian Silverman66f079a2013-08-26 16:24:30 -07001174} // namespace configuration
1175} // namespace aos