tools: refactor ndn-autoconfig

Hub face creation and prefix registrations are moved into a new
Procedure class. Main function is simplified.

refs #4158

Change-Id: I15b660e3b8a1bde89498a1cb549a87788de46c7a
diff --git a/tools/ndn-autoconfig/multicast-discovery.cpp b/tools/ndn-autoconfig/multicast-discovery.cpp
index 6692bc2..f9b4797 100644
--- a/tools/ndn-autoconfig/multicast-discovery.cpp
+++ b/tools/ndn-autoconfig/multicast-discovery.cpp
@@ -33,46 +33,44 @@
 
 static const Name LOCALHOP_HUB_DISCOVERY_PREFIX = "/localhop/ndn-autoconf/hub";
 
-MulticastDiscovery::MulticastDiscovery(Face& face, KeyChain& keyChain,
-                                       const NextStageCallback& nextStageOnFailure)
-  : Stage(face, keyChain, nextStageOnFailure)
+MulticastDiscovery::MulticastDiscovery(Face& face, nfd::Controller& controller)
+  : m_face(face)
+  , m_controller(controller)
   , m_nRequestedRegs(0)
   , m_nFinishedRegs(0)
 {
 }
 
 void
-MulticastDiscovery::start()
+MulticastDiscovery::doStart()
 {
-  std::cerr << "Trying multicast discovery..." << std::endl;
-
   this->collectMulticastFaces();
 }
 
 void
 MulticastDiscovery::collectMulticastFaces()
 {
-  ndn::nfd::FaceQueryFilter filter;
-  filter.setLinkType(ndn::nfd::LINK_TYPE_MULTI_ACCESS);
-  m_controller.fetch<ndn::nfd::FaceQueryDataset>(
+  nfd::FaceQueryFilter filter;
+  filter.setLinkType(nfd::LINK_TYPE_MULTI_ACCESS);
+  m_controller.fetch<nfd::FaceQueryDataset>(
     filter,
     bind(&MulticastDiscovery::registerHubDiscoveryPrefix, this, _1),
-    bind(m_nextStageOnFailure, _2)
+    bind(&MulticastDiscovery::fail, this, _2)
   );
 }
 
 void
-MulticastDiscovery::registerHubDiscoveryPrefix(const std::vector<ndn::nfd::FaceStatus>& dataset)
+MulticastDiscovery::registerHubDiscoveryPrefix(const std::vector<nfd::FaceStatus>& dataset)
 {
   std::vector<uint64_t> multicastFaces;
   std::transform(dataset.begin(), dataset.end(), std::back_inserter(multicastFaces),
-                 [] (const ndn::nfd::FaceStatus& faceStatus) { return faceStatus.getFaceId(); });
+                 [] (const nfd::FaceStatus& faceStatus) { return faceStatus.getFaceId(); });
 
   if (multicastFaces.empty()) {
-    m_nextStageOnFailure("No multicast faces available, skipping multicast discovery stage");
+    this->fail("No multicast faces available, skipping multicast discovery stage");
   }
   else {
-    ControlParameters parameters;
+    nfd::ControlParameters parameters;
     parameters
       .setName(LOCALHOP_HUB_DISCOVERY_PREFIX)
       .setCost(1)
@@ -83,7 +81,7 @@
 
     for (const auto& face : multicastFaces) {
       parameters.setFaceId(face);
-      m_controller.start<ndn::nfd::RibRegisterCommand>(
+      m_controller.start<nfd::RibRegisterCommand>(
         parameters,
         bind(&MulticastDiscovery::onRegisterSuccess, this),
         bind(&MulticastDiscovery::onRegisterFailure, this, _1));
@@ -102,7 +100,7 @@
 }
 
 void
-MulticastDiscovery::onRegisterFailure(const ControlResponse& response)
+MulticastDiscovery::onRegisterFailure(const nfd::ControlResponse& response)
 {
   std::cerr << "ERROR: " << response.getText() << " (code: " << response.getCode() << ")" << std::endl;
   --m_nRequestedRegs;
@@ -112,8 +110,8 @@
       MulticastDiscovery::setStrategy();
     }
     else {
-      m_nextStageOnFailure("Failed to register " + LOCALHOP_HUB_DISCOVERY_PREFIX.toUri() +
-                           " for all multicast faces, skipping multicast discovery stage");
+      this->fail("Failed to register " + LOCALHOP_HUB_DISCOVERY_PREFIX.toUri() +
+                 " for all multicast faces, skipping multicast discovery stage");
     }
   }
 }
@@ -121,23 +119,23 @@
 void
 MulticastDiscovery::setStrategy()
 {
-  ControlParameters parameters;
+  nfd::ControlParameters parameters;
   parameters
     .setName(LOCALHOP_HUB_DISCOVERY_PREFIX)
     .setStrategy("/localhost/nfd/strategy/multicast");
 
-  m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(
+  m_controller.start<nfd::StrategyChoiceSetCommand>(
     parameters,
     bind(&MulticastDiscovery::requestHubData, this),
     bind(&MulticastDiscovery::onSetStrategyFailure, this, _1));
 }
 
 void
-MulticastDiscovery::onSetStrategyFailure(const ControlResponse& response)
+MulticastDiscovery::onSetStrategyFailure(const nfd::ControlResponse& response)
 {
-  m_nextStageOnFailure("Failed to set multicast strategy for " +
-                       LOCALHOP_HUB_DISCOVERY_PREFIX.toUri() + " namespace (" + response.getText() + "). "
-                       "Skipping multicast discovery stage");
+  this->fail("Failed to set multicast strategy for " +
+             LOCALHOP_HUB_DISCOVERY_PREFIX.toUri() + " namespace (" + response.getText() + "). "
+             "Skipping multicast discovery stage");
 }
 
 void
@@ -149,8 +147,8 @@
 
   m_face.expressInterest(interest,
                          bind(&MulticastDiscovery::onSuccess, this, _2),
-                         bind(m_nextStageOnFailure, "HUB Data not received: nacked"),
-                         bind(m_nextStageOnFailure, "HUB Data not received: timeout"));
+                         bind(&MulticastDiscovery::fail, this, "HUB Data not received: nacked"),
+                         bind(&MulticastDiscovery::fail, this, "HUB Data not received: timeout"));
 }
 
 void
@@ -162,11 +160,10 @@
   // Get Uri
   Block::element_const_iterator blockValue = content.find(tlv::nfd::Uri);
   if (blockValue == content.elements_end()) {
-    m_nextStageOnFailure("Incorrect reply to multicast discovery stage");
+    this->fail("Incorrect reply to multicast discovery stage");
     return;
   }
-  std::string hubUri(reinterpret_cast<const char*>(blockValue->value()), blockValue->value_size());
-  this->connectToHub(hubUri);
+  this->provideHubFaceUri(std::string(reinterpret_cast<const char*>(blockValue->value()), blockValue->value_size()));
 }
 
 } // namespace autoconfig