tools: Update code to use unified NFD Management Protocol

Change-Id: I0e638f189afa87431292ca9471d95cba02ccd831
Refs: #1418
diff --git a/tools/ndn-autoconfig.cpp b/tools/ndn-autoconfig.cpp
index 1fb02b1..3059dc4 100644
--- a/tools/ndn-autoconfig.cpp
+++ b/tools/ndn-autoconfig.cpp
@@ -6,7 +6,6 @@
 
 #include <ndn-cpp-dev/face.hpp>
 #include <ndn-cpp-dev/management/nfd-controller.hpp>
-#include <ndn-cpp-dev/management/nfd-face-management-options.hpp>
 #include <ndn-cpp-dev/security/key-chain.hpp>
 
 #include <sys/types.h>
@@ -18,7 +17,9 @@
 #include <arpa/nameser_compat.h>
 #endif
 
-class NdnAutoconfig : public ndn::nfd::Controller
+namespace tools {
+
+class NdnAutoconfig
 {
 public:
   union QueryAnswer
@@ -26,17 +27,20 @@
     HEADER header;
     uint8_t buf[NS_PACKETSZ];
   };
-  
-  struct Error : public std::runtime_error
+
+  class Error : public std::runtime_error
   {
-    Error(const std::string& what) : std::runtime_error(what)
+  public:
+    explicit
+    Error(const std::string& what)
+      : std::runtime_error(what)
     {
     }
   };
-  
+
   explicit
-  NdnAutoconfig(ndn::Face& face)
-    : ndn::nfd::Controller(face)
+  NdnAutoconfig()
+    : m_controller(m_face)
   {
   }
 
@@ -47,41 +51,42 @@
     ndn::Interest interest(ndn::Name("/localhop/ndn-autoconf/hub"));
     interest.setInterestLifetime(ndn::time::milliseconds(4000)); // 4 seconds
     interest.setMustBeFresh(true);
-    
+
     std::cerr << "Stage 1: Trying muticast discovery..." << std::endl;
     m_face.expressInterest(interest,
                            ndn::bind(&NdnAutoconfig::onDiscoverHubStage1Success, this, _1, _2),
                            ndn::bind(&NdnAutoconfig::discoverHubStage2, this, _1, "Timeout"));
-    
+
     m_face.processEvents();
   }
-  
+
   // First stage OnData Callback
   void
   onDiscoverHubStage1Success(const ndn::Interest& interest, ndn::Data& data)
   {
     const ndn::Block& content = data.getContent();
     content.parse();
-    
+
     // Get Uri
-    ndn::Block::element_const_iterator bockValue = content.find(ndn::tlv::nfd::Uri);
-    if (bockValue == content.elements_end())
+    ndn::Block::element_const_iterator blockValue = content.find(ndn::tlv::nfd::Uri);
+    if (blockValue == content.elements_end())
     {
       discoverHubStage2(interest, "Incorrect reply to stage1");
       return;
     }
-    std::string faceMgmtUri(reinterpret_cast<const char*>(bockValue->value()), bockValue->value_size());
+    std::string faceMgmtUri(reinterpret_cast<const char*>(blockValue->value()),
+                            blockValue->value_size());
     connectToHub(faceMgmtUri);
   }
-  
+
   // First stage OnTimeout callback - start 2nd stage
   void
   discoverHubStage2(const ndn::Interest& interest, const std::string& message)
   {
     std::cerr << message << std::endl;
     std::cerr << "Stage 2: Trying DNS query with default suffix..." << std::endl;
-    
-    _res.retry = 2;   
+
+    _res.retry = 2;
     _res.ndots = 10;
 
     QueryAnswer queryAnswer;
@@ -91,7 +96,7 @@
                                 ns_t_srv,
                                 queryAnswer.buf,
                                 sizeof(queryAnswer));
-    
+
     // 2nd stage failed - move on to the third stage
     if (answerSize < 0)
     {
@@ -107,18 +112,18 @@
       }
     }
   }
-    
+
   // Second stage OnTimeout callback
   void
   discoverHubStage3(const std::string& message)
   {
     std::cerr << message << std::endl;
     std::cerr << "Stage 3: Trying to find home router..." << std::endl;
-  
+
     ndn::KeyChain keyChain;
     ndn::Name identity = keyChain.getDefaultIdentity();
     std::string serverName = "_ndn._udp.";
-    
+
     for (ndn::Name::const_reverse_iterator i = identity.rbegin(); i != identity.rend(); i++)
     {
       serverName.append(i->toEscapedString());
@@ -126,7 +131,7 @@
     }
     serverName += "_homehub._autoconf.named-data.net";
     std::cerr << "Stage3: About to query for a home router: " << serverName << std::endl;
-    
+
     QueryAnswer queryAnswer;
 
     int answerSize = res_query(serverName.c_str(),
@@ -135,7 +140,7 @@
                                queryAnswer.buf,
                                sizeof(queryAnswer));
 
-    
+
     // 3rd stage failed - abort
     if (answerSize < 0)
     {
@@ -151,69 +156,71 @@
         throw Error("Failed to parse DNS response");
       }
     }
-    
+
   }
-  
+
   void
   connectToHub(const std::string& uri)
   {
     std::cerr << "about to connect to: " << uri << std::endl;
-    ndn::nfd::FaceManagementOptions faceOptions;
-    
-    faceOptions.setUri(uri);
-    startFaceCommand("create",
-                     faceOptions,
-                     bind(&NdnAutoconfig::onHubConnectSuccess, this, _1, "Succesfully created face: "),
-                     bind(&NdnAutoconfig::onHubConnectError, this, _1, "Failed to create face: "));
+
+    m_controller.start<ndn::nfd::FaceCreateCommand>(
+       ndn::nfd::ControlParameters()
+         .setUri(uri),
+       bind(&NdnAutoconfig::onHubConnectSuccess, this,
+            _1, "Succesfully created face: "),
+       bind(&NdnAutoconfig::onHubConnectError, this,
+            _1, _2, "Failed to create face: ")
+    );
   }
-  
+
   void
-  onHubConnectSuccess(const ndn::nfd::FaceManagementOptions& resp, const std::string& message)
+  onHubConnectSuccess(const ndn::nfd::ControlParameters& resp, const std::string& message)
   {
     std::cerr << message << resp << std::endl;
   }
-  
+
   void
-  onHubConnectError(const std::string& error, const std::string& message)
+  onHubConnectError(uint32_t code, const std::string& error, const std::string& message)
   {
-    throw Error(message + ": " + error);
+    std::ostringstream os;
+    os << message << ": " << error << " (code: " << code << ")";
+    throw Error(os.str());
   }
 
-  
+
   bool parseHostAndConnectToHub(QueryAnswer& queryAnswer, int answerSize)
   {
     // The references of the next classes are:
     // http://www.diablotin.com/librairie/networking/dnsbind/ch14_02.htm
     // https://gist.github.com/mologie/6027597
-    
-    class rechdr
+
+    struct rechdr
     {
-    public:
       uint16_t type;
       uint16_t iclass;
       uint32_t ttl;
       uint16_t length;
     };
-    
-    class srv_t
+
+    struct srv_t
     {
-    public:
       uint16_t priority;
       uint16_t weight;
       uint16_t port;
       uint8_t* target;
     };
-    
+
     if (ntohs(queryAnswer.header.ancount) == 0)
     {
       std::cerr << "No records found\n" << std::endl;
       return false;
     }
-    
+
     uint8_t* blob = queryAnswer.buf + NS_HFIXEDSZ;
-    
+
     blob += dn_skipname(blob, queryAnswer.buf + answerSize) + NS_QFIXEDSZ;
-    
+
     for (int i = 0; i < ntohs(queryAnswer.header.ancount); i++)
     {
       char hostName[NS_MAXDNAME];
@@ -226,7 +233,7 @@
       {
         return false;
       }
-      
+
       blob += domainNameSize;
 
       domainNameSize = dn_expand(queryAnswer.buf,
@@ -245,28 +252,33 @@
       uri.append(hostName);
       uri.append(":");
       uri.append(boost::lexical_cast<std::string>(convertedPort));
-      
+
       connectToHub(uri);
       return true;
     }
 
     return false;
   }
+
+private:
+  ndn::Face m_face;
+  ndn::nfd::Controller m_controller;
 };
 
+} // namespace tools
 
 int
 main()
 {
-  ndn::Face face;
-  NdnAutoconfig autoConfig(face);
   try
-  {
-    autoConfig.discoverHubStage1();
-  }
+    {
+      tools::NdnAutoconfig autoConfigInstance;
+
+      autoConfigInstance.discoverHubStage1();
+    }
   catch (const std::exception& error)
-  {
-    std::cerr << "ERROR: " << error.what() << std::endl;
-  }
+    {
+      std::cerr << "ERROR: " << error.what() << std::endl;
+    }
   return 0;
 }