Add more GetNodeIndex options
We will want to use strings to find a node index.
Change-Id: I5b54b3e06a4804c05bf1dbe4ea125be50aac7262
diff --git a/aos/configuration.cc b/aos/configuration.cc
index bdbb463..2d4b867 100644
--- a/aos/configuration.cc
+++ b/aos/configuration.cc
@@ -682,7 +682,7 @@
CHECK(result != nullptr);
{
- int node_index = GetNodeIndexFromConfig(config, node);
+ int node_index = GetNodeIndexFromConfig(config, result);
if (node_index != -1) {
return node_index;
}
@@ -692,6 +692,23 @@
<< " not found in the configuration.";
}
+int GetNodeIndex(const Configuration *config, std::string_view name) {
+ if (!MultiNode(config)) {
+ return 0;
+ }
+
+ {
+ int node_index = 0;
+ for (const Node *iterated_node : *config->nodes()) {
+ if (iterated_node->name()->string_view() == name) {
+ return node_index;
+ }
+ ++node_index;
+ }
+ }
+ LOG(FATAL) << "Node " << name << " not found in the configuration.";
+}
+
std::vector<const Node *> GetNodes(const Configuration *config) {
std::vector<const Node *> nodes;
if (MultiNode(config)) {
diff --git a/aos/configuration.h b/aos/configuration.h
index c756215..bf2ec6c 100644
--- a/aos/configuration.h
+++ b/aos/configuration.h
@@ -83,6 +83,7 @@
// Returns the node index for a node. Note: will be faster if node is a pointer
// to a node in config, but is not required.
int GetNodeIndex(const Configuration *config, const Node *node);
+int GetNodeIndex(const Configuration *config, std::string_view name);
// Returns true if we are running in a multinode configuration.
bool MultiNode(const Configuration *config);
diff --git a/aos/configuration_test.cc b/aos/configuration_test.cc
index 70515fd..0c0874b 100644
--- a/aos/configuration_test.cc
+++ b/aos/configuration_test.cc
@@ -631,11 +631,22 @@
TEST_F(ConfigurationTest, GetNodeIndex) {
FlatbufferDetachedBuffer<Configuration> config =
ReadConfig("aos/testdata/good_multinode.json");
+ FlatbufferDetachedBuffer<Configuration> config2 =
+ ReadConfig("aos/testdata/good_multinode.json");
const Node *pi1 = GetNode(&config.message(), "pi1");
const Node *pi2 = GetNode(&config.message(), "pi2");
+ // Try the normal case.
EXPECT_EQ(GetNodeIndex(&config.message(), pi1), 0);
EXPECT_EQ(GetNodeIndex(&config.message(), pi2), 1);
+
+ // Now try if we have node pointers from a different message.
+ EXPECT_EQ(GetNodeIndex(&config2.message(), pi1), 0);
+ EXPECT_EQ(GetNodeIndex(&config2.message(), pi2), 1);
+
+ // And now try string names.
+ EXPECT_EQ(GetNodeIndex(&config2.message(), pi1->name()->string_view()), 0);
+ EXPECT_EQ(GetNodeIndex(&config2.message(), pi2->name()->string_view()), 1);
}
// Tests that GetNodeOrDie handles both single and multi-node worlds and returns