face: internal KeyChain

refs #2039

This commit deprecates:

* nfd::Controller::Controller(Face&)

Change-Id: Iecc60c45ba6134dbcc34f58dcdf6a171e7b05253
diff --git a/src/detail/face-impl.hpp b/src/detail/face-impl.hpp
index 155185d..eb286d7 100644
--- a/src/detail/face-impl.hpp
+++ b/src/detail/face-impl.hpp
@@ -217,7 +217,7 @@
     }
 
     RegisteredPrefix::Unregistrator bindedUnregistrator =
-        ndn::bind(unregistrator, m_face.m_nfdController.get(), unregisterParameters, _1, _2,
+        ndn::bind(unregistrator, m_face.m_nfdController, unregisterParameters, _1, _2,
                   signatureGenerator,
                   m_face.m_nfdController->getDefaultCommandTimeout());
 
diff --git a/src/face.cpp b/src/face.cpp
index 42fc522..b24a436 100644
--- a/src/face.cpp
+++ b/src/face.cpp
@@ -24,9 +24,7 @@
 #include "face.hpp"
 #include "detail/face-impl.hpp"
 
-#include "interest.hpp"
-#include "data.hpp"
-#include "security/identity-certificate.hpp"
+#include "security/key-chain.hpp"
 
 #include "util/time.hpp"
 #include "util/random.hpp"
@@ -34,23 +32,25 @@
 namespace ndn {
 
 Face::Face()
-  : m_nfdController(new nfd::Controller(*this))
+  : m_internalKeyChain(new KeyChain())
   , m_isDirectNfdFibManagementRequested(false)
-  , m_impl(make_shared<Impl>(ref(*this)))
+  , m_impl(new Impl(*this))
 {
   const std::string socketName = UnixTransport::getDefaultSocketName(m_impl->m_config);
   construct(make_shared<UnixTransport>(socketName),
-            make_shared<boost::asio::io_service>());
+            make_shared<boost::asio::io_service>(),
+            m_internalKeyChain);
 }
 
 Face::Face(const shared_ptr<boost::asio::io_service>& ioService)
-  : m_nfdController(new nfd::Controller(*this))
+  : m_internalKeyChain(new KeyChain())
   , m_isDirectNfdFibManagementRequested(false)
-  , m_impl(make_shared<Impl>(ref(*this)))
+  , m_impl(new Impl(*this))
 {
   const std::string socketName = UnixTransport::getDefaultSocketName(m_impl->m_config);
   construct(make_shared<UnixTransport>(socketName),
-            ioService);
+            ioService,
+            m_internalKeyChain);
 }
 
 class NullIoDeleter
@@ -63,46 +63,65 @@
 };
 
 Face::Face(boost::asio::io_service& ioService)
-  : m_nfdController(make_shared<nfd::Controller>(ref(*this)))
+  : m_internalKeyChain(new KeyChain())
   , m_isDirectNfdFibManagementRequested(false)
-  , m_impl(make_shared<Impl>(ref(*this)))
+  , m_impl(new Impl(*this))
 {
   const std::string socketName = UnixTransport::getDefaultSocketName(m_impl->m_config);
   construct(make_shared<UnixTransport>(socketName),
-            shared_ptr<boost::asio::io_service>(&ioService, NullIoDeleter()));
+            shared_ptr<boost::asio::io_service>(&ioService, NullIoDeleter()),
+            m_internalKeyChain);
 }
 
 Face::Face(const std::string& host, const std::string& port/* = "6363"*/)
-  : m_nfdController(make_shared<nfd::Controller>(ref(*this)))
-  , m_impl(make_shared<Impl>(ref(*this)))
+  : m_internalKeyChain(new KeyChain())
+  , m_impl(new Impl(*this))
 {
   construct(make_shared<TcpTransport>(host, port),
-            make_shared<boost::asio::io_service>());
+            make_shared<boost::asio::io_service>(),
+            m_internalKeyChain);
 }
 
 Face::Face(const shared_ptr<Transport>& transport)
-  : m_nfdController(make_shared<nfd::Controller>(ref(*this)))
+  : m_internalKeyChain(new KeyChain())
   , m_isDirectNfdFibManagementRequested(false)
-  , m_impl(make_shared<Impl>(ref(*this)))
+  , m_impl(new Impl(*this))
 {
   construct(transport,
-            make_shared<boost::asio::io_service>());
+            make_shared<boost::asio::io_service>(),
+            m_internalKeyChain);
 }
 
 Face::Face(const shared_ptr<Transport>& transport,
            boost::asio::io_service& ioService)
-  : m_nfdController(make_shared<nfd::Controller>(ref(*this)))
+  : m_internalKeyChain(new KeyChain())
   , m_isDirectNfdFibManagementRequested(false)
-  , m_impl(make_shared<Impl>(ref(*this)))
+  , m_impl(new Impl(*this))
 {
   construct(transport,
-            shared_ptr<boost::asio::io_service>(&ioService, NullIoDeleter()));
+            shared_ptr<boost::asio::io_service>(&ioService, NullIoDeleter()),
+            m_internalKeyChain);
+}
+
+Face::Face(shared_ptr<Transport> transport,
+           boost::asio::io_service& ioService,
+           KeyChain& keyChain)
+  : m_internalKeyChain(nullptr)
+  , m_isDirectNfdFibManagementRequested(false)
+  , m_impl(new Impl(*this))
+{
+  construct(transport,
+            shared_ptr<boost::asio::io_service>(&ioService, NullIoDeleter()),
+            &keyChain);
 }
 
 void
-Face::construct(const shared_ptr<Transport>& transport,
-                const shared_ptr<boost::asio::io_service>& ioService)
+Face::construct(shared_ptr<Transport> transport,
+                shared_ptr<boost::asio::io_service> ioService,
+                KeyChain* keyChain)
 {
+  m_nfdController = new nfd::Controller(*this, *keyChain);
+
   m_impl->m_pitTimeoutCheckTimerActive = false;
   m_transport = transport;
   m_ioService = ioService;
@@ -139,6 +158,16 @@
     }
 }
 
+Face::~Face()
+{
+  if (m_internalKeyChain != nullptr) {
+    delete m_internalKeyChain;
+  }
+
+  delete m_nfdController;
+  delete m_impl;
+}
+
 const PendingInterestId*
 Face::expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout)
 {
diff --git a/src/face.hpp b/src/face.hpp
index d95d04a..19587dc 100644
--- a/src/face.hpp
+++ b/src/face.hpp
@@ -41,6 +41,7 @@
 namespace ndn {
 
 class Transport;
+class KeyChain;
 
 class PendingInterestId;
 class RegisteredPrefixId;
@@ -102,6 +103,7 @@
     }
   };
 
+public: // constructors
   /**
    * @brief Create a new Face using the default transport (UnixTransport)
    *
@@ -185,6 +187,21 @@
        boost::asio::io_service& ioService);
 
   /**
+   * @brief Create a new Face using the given Transport and IO service object
+   * @param transport the Transport used for communication
+   * @param ioService the io_service that controls all IO operations
+   * @param keyChain the KeyChain to sign commands
+   * @throws Face::Error on unsupported protocol
+   * @note shared_ptr is passed by value because ownership is shared with this class
+   */
+  Face(shared_ptr<Transport> transport,
+       boost::asio::io_service& ioService,
+       KeyChain& keyChain);
+
+  ~Face();
+
+public: // consumer
+  /**
    * @brief Express Interest
    *
    * @param interest  An Interest to be expressed
@@ -230,6 +247,7 @@
   size_t
   getNPendingInterests() const;
 
+public: // producer
   /**
    * @brief Set InterestFilter to dispatch incoming matching interest to onInterest
    * callback and register the filtered prefix with the connected NDN forwarder
@@ -485,6 +503,7 @@
   void
   put(const Data& data);
 
+public: // IO routine
   /**
    * @brief Process any data to receive or call timeout callbacks.
    *
@@ -545,10 +564,12 @@
 private:
   /**
    * @throws Face::Error on unsupported protocol
+   * @note shared_ptrs are passed by value because ownership is transferred to this function
    */
   void
-  construct(const shared_ptr<Transport>& transport,
-            const shared_ptr<boost::asio::io_service>& ioService);
+  construct(shared_ptr<Transport> transport,
+            shared_ptr<boost::asio::io_service> ioService,
+            KeyChain* keyChain);
 
   bool
   isSupportedNfdProtocol(const std::string& protocol);
@@ -570,15 +591,24 @@
   fireProcessEventsTimeout(const boost::system::error_code& error);
 
 private:
+  /// @todo change to regular pointer after #2097
   shared_ptr<boost::asio::io_service> m_ioService;
 
   shared_ptr<Transport> m_transport;
 
-  shared_ptr<nfd::Controller> m_nfdController;
+  /** @brief if not null, a pointer to an internal KeyChain owned by Face
+   *  @note if a KeyChain is supplied to constructor, this pointer will be null,
+   *        and the passed KeyChain is given to nfdController;
+   *        currently Face does not keep the KeyChain passed in constructor
+   *        because it's not needed, but this may change in the future
+   */
+  KeyChain* m_internalKeyChain;
+
+  nfd::Controller* m_nfdController;
   bool m_isDirectNfdFibManagementRequested;
 
   class Impl;
-  shared_ptr<Impl> m_impl;
+  Impl* m_impl;
 };
 
 inline bool
diff --git a/src/management/nfd-controller.hpp b/src/management/nfd-controller.hpp
index cd8db1d..c94fa10 100644
--- a/src/management/nfd-controller.hpp
+++ b/src/management/nfd-controller.hpp
@@ -56,6 +56,7 @@
 
   /** \brief construct a Controller that uses face for transport,
    *         and has an internal default KeyChain to sign commands
+   *  \deprecated use two-parameter overload
    */
   explicit
   Controller(Face& face);
@@ -208,7 +209,11 @@
 
 protected:
   Face& m_face;
+
+  /** \deprecated
+   */
   shared_ptr<KeyChain> m_internalKeyChain;
+
   KeyChain& m_keyChain;
 };