management: Controller::CommandFailCallback exposes ControlResponse

refs #3739

Change-Id: Ib4b66cd99647ab930450fc3b472be565084be160
diff --git a/src/detail/face-impl.hpp b/src/detail/face-impl.hpp
index 92f102f..a7c332d 100644
--- a/src/detail/face-impl.hpp
+++ b/src/detail/face-impl.hpp
@@ -45,8 +45,6 @@
 
 namespace ndn {
 
-using ndn::nfd::ControlParameters;
-
 /**
  * @brief implementation detail of Face
  */
@@ -188,7 +186,7 @@
                  uint64_t flags,
                  const nfd::CommandOptions& options)
   {
-    ControlParameters params;
+    nfd::ControlParameters params;
     params.setName(prefix);
     params.setFlags(flags);
 
@@ -196,8 +194,8 @@
 
     m_face.m_nfdController->start<nfd::RibRegisterCommand>(
       params,
-      [=] (const ControlParameters&) { this->afterPrefixRegistered(prefixToRegister, onSuccess); },
-      [=] (uint32_t code, const std::string& reason) { onFailure(prefixToRegister->getPrefix(), reason); },
+      [=] (const nfd::ControlParameters&) { this->afterPrefixRegistered(prefixToRegister, onSuccess); },
+      [=] (const nfd::ControlResponse& resp) { onFailure(prefixToRegister->getPrefix(), resp.getText()); },
       options);
 
     return reinterpret_cast<const RegisteredPrefixId*>(prefixToRegister.get());
@@ -236,12 +234,12 @@
         m_interestFilterTable.remove(filter);
       }
 
-      ControlParameters params;
+      nfd::ControlParameters params;
       params.setName(record.getPrefix());
       m_face.m_nfdController->start<nfd::RibUnregisterCommand>(
         params,
-        [=] (const ControlParameters&) { this->finalizeUnregisterPrefix(i, onSuccess); },
-        [=] (uint32_t code, const std::string& reason) { onFailure(reason); },
+        [=] (const nfd::ControlParameters&) { this->finalizeUnregisterPrefix(i, onSuccess); },
+        [=] (const nfd::ControlResponse& resp) { onFailure(resp.getText()); },
         record.getCommandOptions());
     }
     else {
diff --git a/src/management/nfd-controller.cpp b/src/management/nfd-controller.cpp
index 72299f0..b395d6b 100644
--- a/src/management/nfd-controller.cpp
+++ b/src/management/nfd-controller.cpp
@@ -20,7 +20,8 @@
  */
 
 #include "nfd-controller.hpp"
-#include "nfd-control-response.hpp"
+#include "../face.hpp"
+#include "../security/key-chain.hpp"
 #include "../util/segment-fetcher.hpp"
 
 namespace ndn {
@@ -52,7 +53,7 @@
   const CommandSucceedCallback& onSuccess = onSuccess1 ?
     onSuccess1 : [] (const ControlParameters&) {};
   const CommandFailCallback& onFailure = onFailure1 ?
-    onFailure1 : [] (uint32_t, const std::string&) {};
+    onFailure1 : [] (const ControlResponse&) {};
 
   Name requestName = command->getRequestName(options.getPrefix(), parameters);
   Interest interest(requestName);
@@ -60,10 +61,15 @@
   m_keyChain.sign(interest, options.getSigningInfo());
 
   m_face.expressInterest(interest,
-                         bind(&Controller::processCommandResponse, this, _2,
-                              command, onSuccess, onFailure),
-                         bind(onFailure, ERROR_NACK, "network Nack received"),
-                         bind(onFailure, ERROR_TIMEOUT, "request timed out"));
+    [=] (const Interest&, const Data& data) {
+      this->processCommandResponse(data, command, onSuccess, onFailure);
+    },
+    [=] (const Interest&, const lp::Nack&) {
+      onFailure(ControlResponse(Controller::ERROR_NACK, "network Nack received"));
+    },
+    [=] (const Interest&) {
+      onFailure(ControlResponse(Controller::ERROR_TIMEOUT, "request timed out"));
+    });
 }
 
 void
@@ -77,7 +83,7 @@
       this->processValidatedCommandResponse(*data, command, onSuccess, onFailure);
     },
     [=] (const shared_ptr<const Data>&, const std::string& msg) {
-      onFailure(ERROR_VALIDATION, msg);
+      onFailure(ControlResponse(ERROR_VALIDATION, msg));
     }
   );
 }
@@ -93,13 +99,13 @@
     response.wireDecode(data.getContent().blockFromValue());
   }
   catch (const tlv::Error& e) {
-    onFailure(ERROR_SERVER, e.what());
+    onFailure(ControlResponse(ERROR_SERVER, e.what()));
     return;
   }
 
   uint32_t code = response.getCode();
   if (code >= ERROR_LBOUND) {
-    onFailure(code, response.getText());
+    onFailure(response);
     return;
   }
 
@@ -108,7 +114,7 @@
     parameters.wireDecode(response.getBody());
   }
   catch (const tlv::Error& e) {
-    onFailure(ERROR_SERVER, e.what());
+    onFailure(ControlResponse(ERROR_SERVER, e.what()));
     return;
   }
 
@@ -116,7 +122,7 @@
     command->validateResponse(parameters);
   }
   catch (const ControlCommand::ArgumentError& e) {
-    onFailure(ERROR_SERVER, e.what());
+    onFailure(ControlResponse(ERROR_SERVER, e.what()));
     return;
   }
 
@@ -126,7 +132,7 @@
 void
 Controller::fetchDataset(const Name& prefix,
                          const std::function<void(const ConstBufferPtr&)>& processResponse,
-                         const CommandFailCallback& onFailure,
+                         const DatasetFailCallback& onFailure,
                          const CommandOptions& options)
 {
   Interest baseInterest(prefix);
@@ -137,7 +143,7 @@
 }
 
 void
-Controller::processDatasetFetchError(const CommandFailCallback& onFailure,
+Controller::processDatasetFetchError(const DatasetFailCallback& onFailure,
                                      uint32_t code, std::string msg)
 {
   switch (static_cast<SegmentFetcher::ErrorCode>(code)) {
diff --git a/src/management/nfd-controller.hpp b/src/management/nfd-controller.hpp
index f1d6df9..297f113 100644
--- a/src/management/nfd-controller.hpp
+++ b/src/management/nfd-controller.hpp
@@ -23,13 +23,19 @@
 #define NDN_MANAGEMENT_NFD_CONTROLLER_HPP
 
 #include "nfd-control-command.hpp"
+#include "nfd-control-response.hpp"
 #include "nfd-status-dataset.hpp"
 #include "nfd-command-options.hpp"
-#include "../face.hpp"
-#include "../security/key-chain.hpp"
 #include "../security/validator-null.hpp"
 
 namespace ndn {
+
+namespace security {
+class KeyChain;
+} // namespace security
+class Face;
+class Validator;
+
 namespace nfd {
 
 /**
@@ -39,7 +45,8 @@
 
 /**
  * \ingroup management
- * \brief NFD Management protocol - ControlCommand client
+ * \brief NFD Management protocol client
+ * \sa https://redmine.named-data.net/projects/nfd/wiki/Management
  */
 class Controller : noncopyable
 {
@@ -50,12 +57,16 @@
 
   /** \brief a callback on command failure
    */
-  typedef function<void(uint32_t code, const std::string& reason)> CommandFailCallback;
+  typedef function<void(const ControlResponse&)> CommandFailCallback;
+
+  /** \brief a callback on dataset retrieval failure
+   */
+  typedef function<void(uint32_t code, const std::string& reason)> DatasetFailCallback;
 
   /** \brief construct a Controller that uses face for transport,
    *         and uses the passed KeyChain to sign commands
    */
-  Controller(Face& face, KeyChain& keyChain, Validator& validator = s_validatorNull);
+  Controller(Face& face, security::KeyChain& keyChain, Validator& validator = s_validatorNull);
 
   /** \brief start command execution
    */
@@ -75,7 +86,7 @@
   template<typename Dataset>
   typename std::enable_if<std::is_default_constructible<Dataset>::value>::type
   fetch(const std::function<void(typename Dataset::ResultType)>& onSuccess,
-        const CommandFailCallback& onFailure,
+        const DatasetFailCallback& onFailure,
         const CommandOptions& options = CommandOptions())
   {
     this->fetchDataset(make_shared<Dataset>(), onSuccess, onFailure, options);
@@ -87,7 +98,7 @@
   void
   fetch(const ParamType& param,
         const std::function<void(typename Dataset::ResultType)>& onSuccess,
-        const CommandFailCallback& onFailure,
+        const DatasetFailCallback& onFailure,
         const CommandOptions& options = CommandOptions())
   {
     this->fetchDataset(make_shared<Dataset>(param), onSuccess, onFailure, options);
@@ -117,24 +128,24 @@
   void
   fetchDataset(shared_ptr<Dataset> dataset,
                const std::function<void(typename Dataset::ResultType)>& onSuccess,
-               const CommandFailCallback& onFailure,
+               const DatasetFailCallback& onFailure,
                const CommandOptions& options);
 
   void
   fetchDataset(const Name& prefix,
                const std::function<void(const ConstBufferPtr&)>& processResponse,
-               const CommandFailCallback& onFailure,
+               const DatasetFailCallback& onFailure,
                const CommandOptions& options);
 
   template<typename Dataset>
   void
   processDatasetResponse(shared_ptr<Dataset> dataset,
                          const std::function<void(typename Dataset::ResultType)>& onSuccess,
-                         const CommandFailCallback& onFailure,
+                         const DatasetFailCallback& onFailure,
                          ConstBufferPtr payload);
 
   void
-  processDatasetFetchError(const CommandFailCallback& onFailure, uint32_t code, std::string msg);
+  processDatasetFetchError(const DatasetFailCallback& onFailure, uint32_t code, std::string msg);
 
 public:
   /** \brief error code for timeout
@@ -159,7 +170,7 @@
 
 protected:
   Face& m_face;
-  KeyChain& m_keyChain;
+  security::KeyChain& m_keyChain;
   Validator& m_validator;
 
 private:
@@ -170,12 +181,12 @@
 inline void
 Controller::fetchDataset(shared_ptr<Dataset> dataset,
                          const std::function<void(typename Dataset::ResultType)>& onSuccess1,
-                         const CommandFailCallback& onFailure1,
+                         const DatasetFailCallback& onFailure1,
                          const CommandOptions& options)
 {
   const std::function<void(typename Dataset::ResultType)>& onSuccess = onSuccess1 ?
     onSuccess1 : [] (const typename Dataset::ResultType&) {};
-  const CommandFailCallback& onFailure = onFailure1 ?
+  const DatasetFailCallback& onFailure = onFailure1 ?
     onFailure1 : [] (uint32_t, const std::string&) {};
 
   Name prefix = dataset->getDatasetPrefix(options.getPrefix());
@@ -189,15 +200,15 @@
 inline void
 Controller::processDatasetResponse(shared_ptr<Dataset> dataset,
                                    const std::function<void(typename Dataset::ResultType)>& onSuccess,
-                                   const CommandFailCallback& onFailure,
+                                   const DatasetFailCallback& onFailure,
                                    ConstBufferPtr payload)
 {
   typename Dataset::ResultType result;
   try {
     result = dataset->parseResult(payload);
   }
-  catch (const tlv::Error& ex) {
-    onFailure(ERROR_SERVER, ex.what());
+  catch (const tlv::Error& e) {
+    onFailure(ERROR_SERVER, e.what());
     return;
   }