nfdc: ensure FaceUri is in canonical form

refs #1909

Change-Id: If0560c03f3cafe3cd3a181246a7c185e07ade1ca
diff --git a/tools/nfdc.cpp b/tools/nfdc.cpp
index 8578e43..123cdb5 100644
--- a/tools/nfdc.cpp
+++ b/tools/nfdc.cpp
@@ -79,6 +79,7 @@
   , m_origin(ROUTE_ORIGIN_STATIC)
   , m_expires(DEFAULT_EXPIRATION_PERIOD)
   , m_controller(face)
+  , m_ioService(face.getIoService())
 {
 }
 
@@ -169,17 +170,28 @@
     fibAddNextHop(parameters);
   }
   else {
-    ControlParameters faceParameters;
-    faceParameters.setUri(m_commandLineArguments[1]);
+    ndn::util::FaceUri faceUri;
+    faceUri.parse(m_commandLineArguments[1]);
 
-    m_controller.start<FaceCreateCommand>(faceParameters,
-                                          bind(&Nfdc::fibAddNextHop, this, _1),
-                                          bind(&Nfdc::onError, this, _1, _2,
-                                               "Face creation failed"));
+    faceUri.canonize(bind(&Nfdc::startFibAddNextHop, this, _1),
+                     bind(&Nfdc::onCanonizeFailure, this, _1),
+                     m_ioService, ndn::time::seconds(4));
   }
 }
 
 void
+Nfdc::startFibAddNextHop(const ndn::util::FaceUri& canonicalUri)
+{
+  ControlParameters parameters;
+  parameters.setUri(canonicalUri.toString());
+
+  m_controller.start<FaceCreateCommand>(parameters,
+                                        bind(&Nfdc::fibAddNextHop, this, _1),
+                                        bind(&Nfdc::onError, this, _1, _2,
+                                             "Face creation failed"));
+}
+
+void
 Nfdc::fibAddNextHop(const ControlParameters& faceCreateResult)
 {
   ControlParameters ribParameters;
@@ -245,17 +257,28 @@
     ribRegisterPrefix(parameters);
   }
   else {
-    ControlParameters faceParameters;
-    faceParameters.setUri(m_commandLineArguments[1]);
+    ndn::util::FaceUri faceUri;
+    faceUri.parse(m_commandLineArguments[1]);
 
-    m_controller.start<FaceCreateCommand>(faceParameters,
-                                          bind(&Nfdc::ribRegisterPrefix, this, _1),
-                                          bind(&Nfdc::onError, this, _1, _2,
-                                               "Face creation failed"));
+    faceUri.canonize(bind(&Nfdc::startRibRegisterPrefix, this, _1),
+                     bind(&Nfdc::onCanonizeFailure, this, _1),
+                     m_ioService, ndn::time::seconds(4));
   }
 }
 
 void
+Nfdc::startRibRegisterPrefix(const ndn::util::FaceUri& canonicalUri)
+{
+  ControlParameters parameters;
+  parameters.setUri(canonicalUri.toString());
+
+  m_controller.start<FaceCreateCommand>(parameters,
+                                        bind(&Nfdc::ribRegisterPrefix, this, _1),
+                                        bind(&Nfdc::onError, this, _1, _2,
+                                             "Face creation failed"));
+}
+
+void
 Nfdc::ribRegisterPrefix(const ControlParameters& faceCreateResult)
 {
   ControlParameters ribParameters;
@@ -302,13 +325,30 @@
 }
 
 void
+Nfdc::onCanonizeFailure(const std::string& reason)
+{
+  std::cerr << reason << std::endl;
+}
+
+void
 Nfdc::faceCreate()
 {
   if (!isValidUri(m_commandLineArguments[0]))
     throw Error("invalid uri format");
 
+  ndn::util::FaceUri faceUri;
+  faceUri.parse(m_commandLineArguments[0]);
+
+  faceUri.canonize(bind(&Nfdc::startFaceCreate, this, _1),
+                   bind(&Nfdc::onCanonizeFailure, this, _1),
+                   m_ioService, ndn::time::seconds(4));
+}
+
+void
+Nfdc::startFaceCreate(const ndn::util::FaceUri& canonicalUri)
+{
   ControlParameters parameters;
-  parameters.setUri(m_commandLineArguments[0]);
+  parameters.setUri(canonicalUri.toString());
 
   m_controller.start<FaceCreateCommand>(parameters,
                                         bind(&Nfdc::onSuccess, this, _1,
@@ -333,16 +373,28 @@
     faceDestroy(parameters);
   }
   else{
-    ControlParameters faceParameters;
-    faceParameters.setUri(m_commandLineArguments[0]);
-    m_controller.start<FaceCreateCommand>(faceParameters,
-                                          bind(&Nfdc::faceDestroy, this, _1),
-                                          bind(&Nfdc::onError, this, _1, _2,
-                                               "Face destroy failed"));
+    ndn::util::FaceUri faceUri;
+    faceUri.parse(m_commandLineArguments[0]);
+
+    faceUri.canonize(bind(&Nfdc::startFaceDestroy, this, _1),
+                     bind(&Nfdc::onCanonizeFailure, this, _1),
+                     m_ioService, ndn::time::seconds(4));
   }
 }
 
 void
+Nfdc::startFaceDestroy(const ndn::util::FaceUri& canonicalUri)
+{
+  ControlParameters parameters;
+  parameters.setUri(canonicalUri.toString());
+
+  m_controller.start<FaceCreateCommand>(parameters,
+                                        bind(&Nfdc::faceDestroy, this, _1),
+                                        bind(&Nfdc::onError, this, _1, _2,
+                                             "Face destroy failed"));
+}
+
+void
 Nfdc::faceDestroy(const ControlParameters& faceCreateResult)
 {
   ControlParameters faceParameters;
diff --git a/tools/nfdc.hpp b/tools/nfdc.hpp
index 3624bb6..84a7fc8 100644
--- a/tools/nfdc.hpp
+++ b/tools/nfdc.hpp
@@ -29,10 +29,10 @@
 #include <ndn-cxx/face.hpp>
 #include <ndn-cxx/util/time.hpp>
 #include <ndn-cxx/management/nfd-controller.hpp>
+#include <ndn-cxx/util/face-uri.hpp>
 
 namespace nfdc {
 
-
 using namespace ndn::nfd;
 
 class Nfdc : boost::noncopyable
@@ -177,6 +177,21 @@
   void
   onError(uint32_t code, const std::string& error, const std::string& message);
 
+  void
+  onCanonizeFailure(const std::string& reason);
+
+  void
+  startFaceCreate(const ndn::util::FaceUri& canonicalUri);
+
+  void
+  startFaceDestroy(const ndn::util::FaceUri& canonicalUri);
+
+  void
+  startFibAddNextHop(const ndn::util::FaceUri& canonicalUri);
+
+  void
+  startRibRegisterPrefix(const ndn::util::FaceUri& canonicalUri);
+
 public:
   const char* m_programName;
 
@@ -193,6 +208,7 @@
 
 private:
   Controller m_controller;
+  boost::asio::io_service& m_ioService;
 };
 
 } // namespace nfdc