Refactor out GetApplication for reuse

No need to duplicate the logic elsewhere.

Change-Id: I86dcc716ca9a8c0e587d4c43ad8f09d8a01ea2c6
diff --git a/aos/configuration.cc b/aos/configuration.cc
index 912f91e..36868c4 100644
--- a/aos/configuration.cc
+++ b/aos/configuration.cc
@@ -609,17 +609,13 @@
 
   // First handle application specific maps.  Only do this if we have a matching
   // application name, and it has maps.
-  if (config->has_applications()) {
-    auto application_iterator = std::lower_bound(
-        config->applications()->cbegin(), config->applications()->cend(),
-        application_name, CompareApplications);
-    if (application_iterator != config->applications()->cend() &&
-        EqualsApplications(*application_iterator, application_name)) {
-      if (application_iterator->has_maps()) {
-        mutable_name = std::string(name);
-        HandleMaps(application_iterator->maps(), &mutable_name, type, node);
-        name = std::string_view(mutable_name);
-      }
+  {
+    const Application *application =
+        GetApplication(config, node, application_name);
+    if (application != nullptr && application->has_maps()) {
+      mutable_name = std::string(name);
+      HandleMaps(application->maps(), &mutable_name, type, node);
+      name = std::string_view(mutable_name);
     }
   }
 
@@ -1222,5 +1218,31 @@
   return result;
 }
 
+const Application *GetApplication(const Configuration *config,
+                                  const Node *my_node,
+                                  std::string_view application_name) {
+  if (config->has_applications()) {
+    auto application_iterator = std::lower_bound(
+        config->applications()->cbegin(), config->applications()->cend(),
+        application_name, CompareApplications);
+    if (application_iterator != config->applications()->cend() &&
+        EqualsApplications(*application_iterator, application_name)) {
+      if (MultiNode(config)) {
+        // Ok, we need
+        CHECK(application_iterator->has_nodes());
+        CHECK(my_node != nullptr);
+        for (const flatbuffers::String *str : *application_iterator->nodes()) {
+          if (str->string_view() == my_node->name()->string_view()) {
+            return *application_iterator;
+          }
+        }
+      } else {
+        return *application_iterator;
+      }
+    }
+  }
+  return nullptr;
+}
+
 }  // namespace configuration
 }  // namespace aos
diff --git a/aos/configuration.h b/aos/configuration.h
index 78522b6..88af5fe 100644
--- a/aos/configuration.h
+++ b/aos/configuration.h
@@ -152,6 +152,12 @@
 std::vector<const Node *> TimestampNodes(const Configuration *config,
                                          const Node *my_node);
 
+// Returns the application for the provided node and name if it exists, or
+// nullptr if it does not exist on this node.
+const Application *GetApplication(const Configuration *config,
+                                  const Node *my_node,
+                                  std::string_view application_name);
+
 // TODO(austin): GetSchema<T>(const Flatbuffer<Configuration> &config);
 
 }  // namespace configuration
diff --git a/aos/testdata/good_multinode.json b/aos/testdata/good_multinode.json
index f344691..109ffb8 100644
--- a/aos/testdata/good_multinode.json
+++ b/aos/testdata/good_multinode.json
@@ -42,6 +42,7 @@
   "applications": [
     {
       "name": "app1",
+      "nodes": ["pi1"],
       "maps": [
         {
           "match": {
@@ -64,7 +65,8 @@
       ]
     },
     {
-      "name": "app2"
+      "name": "app2",
+      "nodes": ["pi2"]
     }
   ],
   "nodes": [