face: Making environment-based selection of the daemon (ndnd-tlv vs. nfd)

If ``NFD`` environment variable is set to any value, a ndn-cpp-dev
application will assume it is connected to nfd and will invoke
appropriate prefix registration protocol.

Change-Id: I33f3a3a29e17b5b5ddd9474427036402d9d657e3
diff --git a/src/face.cpp b/src/face.cpp
index 0a967e3..5e1efed 100644
--- a/src/face.cpp
+++ b/src/face.cpp
@@ -13,47 +13,46 @@
 
 #include "util/time.hpp"
 #include "util/random.hpp"
+#include <cstdlib>
 
 #include "management/ndnd-controller.hpp"
 #include "management/nfd-controller.hpp"
 
 namespace ndn {
 
-Face::Face(bool nfdMode/* = false*/)
+Face::Face()
 {
   construct(shared_ptr<Transport>(new UnixTransport()),
-            make_shared<boost::asio::io_service>(), nfdMode);
+            make_shared<boost::asio::io_service>());
 }
 
-Face::Face(const shared_ptr<boost::asio::io_service> &ioService, bool nfdMode/* = false*/)
+Face::Face(const shared_ptr<boost::asio::io_service> &ioService)
 {
   construct(shared_ptr<Transport>(new UnixTransport()),
-            ioService, nfdMode);
+            ioService);
 }
 
-Face::Face(const std::string &host, const std::string &port/* = "6363"*/, bool nfdMode/* = false*/)
+Face::Face(const std::string &host, const std::string &port/* = "6363"*/)
 {
   construct(shared_ptr<Transport>(new TcpTransport(host, port)),
-            make_shared<boost::asio::io_service>(), nfdMode);
+            make_shared<boost::asio::io_service>());
 }
 
-Face::Face(const shared_ptr<Transport>& transport, bool nfdMode/* = false*/)
+Face::Face(const shared_ptr<Transport>& transport)
 {
   construct(transport,
-            make_shared<boost::asio::io_service>(), nfdMode);
+            make_shared<boost::asio::io_service>());
 }
 
 Face::Face(const shared_ptr<Transport>& transport,
-           const shared_ptr<boost::asio::io_service> &ioService,
-           bool nfdMode/* = false*/)
+           const shared_ptr<boost::asio::io_service> &ioService)
 {
-  construct(transport, ioService, nfdMode);
+  construct(transport, ioService);
 }
 
 void
 Face::construct(const shared_ptr<Transport>& transport,
-                const shared_ptr<boost::asio::io_service> &ioService,
-                bool nfdMode)
+                const shared_ptr<boost::asio::io_service> &ioService)
 {
   pitTimeoutCheckTimerActive_ = false;
   transport_ = transport;
@@ -62,7 +61,7 @@
   pitTimeoutCheckTimer_      = make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_));
   processEventsTimeoutTimer_ = make_shared<boost::asio::deadline_timer>(boost::ref(*ioService_));
 
-  if (nfdMode)
+  if (std::getenv("NFD") != 0)
       m_fwController = make_shared<nfd::Controller>(boost::ref(*this));
   else
       m_fwController = make_shared<ndnd::Controller>(boost::ref(*this));
diff --git a/src/face.hpp b/src/face.hpp
index 42f4fd1..9b1fbe4 100644
--- a/src/face.hpp
+++ b/src/face.hpp
@@ -57,27 +57,29 @@
   /**
    * @brief Create a new Face for communication with an NDN Forwarder using the default UnixTransport.
    */
-  Face(bool nfdMode = false);
+  Face();
 
   /**
    * @brief Create a new Face for communication with an NDN Forwarder using the default UnixTransport.
    * @param ioService A shared pointer to boost::io_service object that should control all IO operations
    */
-  Face(const ptr_lib::shared_ptr<boost::asio::io_service> &ioService, bool nfdMode = false);
+  explicit
+  Face(const ptr_lib::shared_ptr<boost::asio::io_service> &ioService);
   
   /**
    * Create a new Face for communication with an NDN hub at host:port using the default TcpTransport.
    * @param host The host of the NDN hub.
    * @param port The port or service name of the NDN hub. If omitted. use 6363.
    */
-  Face(const std::string &host, const std::string &port = "6363", bool nfdMode = false);
+  Face(const std::string &host, const std::string &port = "6363");
 
   /**
    * Create a new Face for communication with an NDN hub with the given Transport object and connectionInfo.
    * @param transport A shared_ptr to a Transport object used for communication.
    * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport.
    */
-  Face(const shared_ptr<Transport>& transport, bool nfdMode = false);
+  explicit
+  Face(const shared_ptr<Transport>& transport);
 
   /**
    * @brief Alternative (special use case) version of the constructor, can be used to aggregate
@@ -92,8 +94,7 @@
    * </code>
    */
   Face(const shared_ptr<Transport>& transport,
-       const shared_ptr<boost::asio::io_service> &ioService,
-       bool nfdMode = false);
+       const shared_ptr<boost::asio::io_service> &ioService);
   
   /**
    * @brief Express Interest
@@ -194,7 +195,7 @@
 
 private:
   void
-  construct(const shared_ptr<Transport>& transport, const shared_ptr<boost::asio::io_service> &ioService, bool nfdMode);
+  construct(const shared_ptr<Transport>& transport, const shared_ptr<boost::asio::io_service>& ioService);
   
   struct ProcessEventsTimeout {};
   typedef std::list<shared_ptr<PendingInterest> > PendingInterestTable;
diff --git a/src/transport/unix-transport.cpp b/src/transport/unix-transport.cpp
index 4da0502..84424f3 100644
--- a/src/transport/unix-transport.cpp
+++ b/src/transport/unix-transport.cpp
@@ -10,6 +10,7 @@
 #include "unix-transport.hpp"
 
 #include "../face.hpp"
+#include <cstdlib>
 
 using namespace std;
 typedef boost::asio::local::stream_protocol protocol;
@@ -70,7 +71,7 @@
     socket_.close();
     throw Transport::Error(error, "error while connecting to the forwarder");
   }
-  
+
   void
   connect()
   {
@@ -81,14 +82,14 @@
       /// @todo Decide whether this number should be configurable
       connectTimer_.expires_from_now(boost::posix_time::seconds(4));
       connectTimer_.async_wait(func_lib::bind(&Impl::connectTimeoutHandler, this, _1));
-      
+
       socket_.open();
       socket_.async_connect(protocol::endpoint(transport_.unixSocket_),
                             func_lib::bind(&Impl::connectHandler, this, _1));
     }
   }
 
-  void 
+  void
   close()
   {
     connectTimer_.cancel();
@@ -96,8 +97,8 @@
     transport_.isConnected_ = false;
   }
 
-  void 
-  send(const Block &wire)
+  void
+  send(const Block& wire)
   {
     if (!transport_.isConnected_)
       sendQueue_.push_back(wire);
@@ -107,7 +108,7 @@
   }
 
   inline void
-  processAll(uint8_t *buffer, size_t &offset, size_t availableSize)
+  processAll(uint8_t* buffer, size_t& offset, size_t availableSize)
   {
     while(offset < availableSize)
       {
@@ -117,7 +118,7 @@
         offset += element.size();
       }
   }
-  
+
   void
   handle_async_receive(const boost::system::error_code& error, std::size_t bytes_recvd)
   {
@@ -129,12 +130,12 @@
           // async receive has been explicitly cancelled (e.g., socket close)
           return;
         }
-        
+
         socket_.close(); // closing at this point may not be that necessary
         transport_.isConnected_ = true;
         throw Transport::Error(error, "error while receiving data from socket");
       }
-    
+
     if (!error && bytes_recvd > 0)
       {
         // inputBuffer_ has bytes_recvd received bytes of data
@@ -144,7 +145,7 @@
             std::copy(inputBuffer_, inputBuffer_ + newDataSize, partialData_ + partialDataSize_);
 
             partialDataSize_ += newDataSize;
-              
+
             size_t offset = 0;
             try
               {
@@ -154,7 +155,7 @@
                 if (bytes_recvd - newDataSize > 0)
                   {
                     // there is a little bit more data available
-                        
+
                     offset = 0;
                     partialDataSize_ = bytes_recvd - newDataSize;
                     std::copy(inputBuffer_ + newDataSize, inputBuffer_ + newDataSize + partialDataSize_, partialData_);
@@ -213,10 +214,10 @@
   {
     // pass (needed to keep data block alive during the send)
   }
-  
+
 private:
   UnixTransport &transport_;
-  
+
   protocol::socket socket_;
   uint8_t inputBuffer_[MAX_LENGTH];
 
@@ -229,7 +230,16 @@
   boost::asio::deadline_timer connectTimer_;
 };
 
-UnixTransport::UnixTransport(const std::string &unixSocket/* = "/tmp/.ndnd.sock"*/) 
+
+UnixTransport::UnixTransport()
+{
+  if (std::getenv("NFD") != 0)
+      unixSocket_ = "/var/run/nfd.sock";
+  else
+      unixSocket_ = "/tmp/.ndnd.sock";
+}
+
+UnixTransport::UnixTransport(const std::string& unixSocket)
   : unixSocket_(unixSocket)
 {
 }
@@ -238,25 +248,25 @@
 {
 }
 
-void 
-UnixTransport::connect(boost::asio::io_service &ioService,
-                       const ReceiveCallback &receiveCallback)
+void
+UnixTransport::connect(boost::asio::io_service& ioService,
+                       const ReceiveCallback& receiveCallback)
 {
   if (!static_cast<bool>(impl_)) {
     Transport::connect(ioService, receiveCallback);
-  
+
     impl_ = ptr_lib::make_shared<UnixTransport::Impl> (ptr_lib::ref(*this));
   }
   impl_->connect();
 }
 
-void 
-UnixTransport::send(const Block &wire)
+void
+UnixTransport::send(const Block& wire)
 {
   impl_->send(wire);
 }
 
-void 
+void
 UnixTransport::close()
 {
   impl_->close();
diff --git a/src/transport/unix-transport.hpp b/src/transport/unix-transport.hpp
index 26621d7..dc85eac 100644
--- a/src/transport/unix-transport.hpp
+++ b/src/transport/unix-transport.hpp
@@ -12,24 +12,26 @@
 #include "transport.hpp"
 
 namespace ndn {
-  
+
 class UnixTransport : public Transport
 {
 public:
-  UnixTransport(const std::string &unixSocket = "/tmp/.ndnd.sock");
+  UnixTransport();
+
+  UnixTransport(const std::string& unixSocket);
   ~UnixTransport();
 
   // from Transport
-  virtual void 
-  connect(boost::asio::io_service &ioService,
-          const ReceiveCallback &receiveCallback);
-  
-  virtual void 
+  virtual void
+  connect(boost::asio::io_service& ioService,
+          const ReceiveCallback& receiveCallback);
+
+  virtual void
   close();
 
-  virtual void 
-  send(const Block &wire);
-  
+  virtual void
+  send(const Block& wire);
+
 private:
   std::string unixSocket_;