management: Implementing nrd::Controller and introducing new secret
environment option to enable NRD Prefix management protocol

Usage for the secret NFD and NRD environment variables

    export NFD=1
    export NRD=1
    <run app>

or

    NFD=1 NRD=1 <run app>

Change-Id: I874acbdcf7038f4d0fa05c59b34cd46d6ae627b0
diff --git a/src/management/nrd-controller.cpp b/src/management/nrd-controller.cpp
new file mode 100644
index 0000000..2d5557f
--- /dev/null
+++ b/src/management/nrd-controller.cpp
@@ -0,0 +1,109 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "common.hpp"
+#include "../face.hpp"
+
+// NRD
+#include "nrd-controller.hpp"
+#include "nrd-prefix-reg-options.hpp"
+
+// NFD
+#include "nfd-control-response.hpp"
+
+namespace ndn {
+namespace nrd {
+
+Controller::Controller(Face& face)
+  : m_face(face)
+  , m_faceId(0)
+{
+}
+
+void
+Controller::selfRegisterPrefix(const Name& prefixToRegister,
+                               const SuccessCallback& onSuccess,
+                               const FailCallback&    onFail)
+{
+  startCommand("register",
+               PrefixRegOptions()
+                 .setName(prefixToRegister)
+                 .setFaceId(0) // self-registration
+                 .setCost(0),
+               bind(&Controller::recordSelfRegisteredFaceId, this, _1, onSuccess),
+               onFail);
+}
+
+void
+Controller::selfDeregisterPrefix(const Name& prefixToRegister,
+                                 const SuccessCallback& onSuccess,
+                                 const FailCallback&    onFail)
+{
+  if (m_faceId == 0)
+    {
+      if (static_cast<bool>(onFail))
+        onFail("Face ID is not set, should have been set after a successful prefix registration command");
+      return;
+    }
+
+  startCommand("unregister",
+               PrefixRegOptions()
+                 .setName(prefixToRegister)
+                 .setFaceId(m_faceId),
+               bind(onSuccess), onFail);
+}
+
+void
+Controller::startCommand(const std::string& command,
+                         const PrefixRegOptions& options,
+                         const CommandSucceedCallback& onSuccess,
+                         const FailCallback& onFail)
+{
+  Name commandInterestName("/localhost/nrd");
+  commandInterestName
+    .append(command)
+    .append(options.wireEncode());
+
+  Interest commandInterest(commandInterestName);
+  // m_keyChain.sign(commandInterest);
+
+  m_face.expressInterest(commandInterest,
+                         bind(&Controller::processCommandResponse, this, _2,
+                              onSuccess, onFail),
+                         bind(onFail, "Command Interest timed out"));
+}
+
+void
+Controller::recordSelfRegisteredFaceId(const PrefixRegOptions& entry,
+                                       const SuccessCallback& onSuccess)
+{
+  m_faceId = entry.getFaceId();
+  onSuccess();
+}
+
+void
+Controller::processCommandResponse(Data& data,
+                                   const CommandSucceedCallback& onSuccess,
+                                   const FailCallback& onFail)
+{
+  try
+    {
+      nfd::ControlResponse response(data.getContent().blockFromValue());
+      if (response.getCode() != 200)
+        return onFail(response.getText());
+
+      PrefixRegOptions options(response.getBody());
+      return onSuccess(options);
+    }
+  catch(ndn::Tlv::Error& e)
+    {
+      if (static_cast<bool>(onFail))
+        return onFail(e.what());
+    }
+}
+
+} // namespace nrd
+} // namespace ndn