face: Make default transport configurable

This commit also removes support for the ndnd-tlv-0.7
protocol's default socket location from UnixTransport.

Test HOME environment changing has also been centralized
into a single fixture (util/test-home-environment-fixture.hpp)

refs #2189

Change-Id: Iac1a6c88d7a2d9fcfb91a20356ac21b75daf7d17
diff --git a/src/face.cpp b/src/face.cpp
index 1db6371..8535924 100644
--- a/src/face.cpp
+++ b/src/face.cpp
@@ -28,6 +28,7 @@
 #include "security/key-chain.hpp"
 #include "util/time.hpp"
 #include "util/random.hpp"
+#include "util/face-uri.hpp"
 
 namespace ndn {
 
@@ -38,9 +39,7 @@
   , m_isDirectNfdFibManagementRequested(false)
   , m_impl(new Impl(*this))
 {
-  const std::string socketName = UnixTransport::getDefaultSocketName(m_impl->m_config);
-  construct(make_shared<UnixTransport>(socketName),
-            m_internalKeyChain);
+  construct(m_internalKeyChain);
 }
 
 Face::Face(boost::asio::io_service& ioService)
@@ -49,9 +48,7 @@
   , m_isDirectNfdFibManagementRequested(false)
   , m_impl(new Impl(*this))
 {
-  const std::string socketName = UnixTransport::getDefaultSocketName(m_impl->m_config);
-  construct(make_shared<UnixTransport>(socketName),
-            m_internalKeyChain);
+  construct(m_internalKeyChain);
 }
 
 Face::Face(const std::string& host, const std::string& port/* = "6363"*/)
@@ -99,6 +96,50 @@
 }
 
 void
+Face::construct(KeyChain* keyChain)
+{
+  // transport=unix:///var/run/nfd.sock
+  // transport=tcp://localhost:6363
+
+  const ConfigFile::Parsed& parsed = m_impl->m_config.getParsedConfiguration();
+
+  const auto transportType = parsed.get_optional<std::string>("transport");
+  if (!transportType)
+    {
+      // transport not specified, use default Unix transport.
+      construct(UnixTransport::create(m_impl->m_config), keyChain);
+      return;
+    }
+
+  unique_ptr<util::FaceUri> uri;
+  try
+    {
+      uri.reset(new util::FaceUri(*transportType));
+    }
+  catch (const util::FaceUri::Error& error)
+    {
+      throw ConfigFile::Error(error.what());
+    }
+
+  shared_ptr<Transport> transport;
+  const std::string protocol = uri->getScheme();
+
+  if (protocol == "unix")
+    {
+      construct(UnixTransport::create(m_impl->m_config), keyChain);
+
+    }
+  else if (protocol == "tcp" || protocol == "tcp4" || protocol == "tcp6")
+    {
+      construct(TcpTransport::create(m_impl->m_config), keyChain);
+    }
+  else
+    {
+      throw ConfigFile::Error("Unsupported transport protocol \"" + protocol + "\"");
+    }
+}
+
+void
 Face::construct(shared_ptr<Transport> transport,
                 KeyChain* keyChain)
 {
@@ -171,8 +212,8 @@
                       const OnData& onData, const OnTimeout& onTimeout/* = OnTimeout()*/)
 {
   return expressInterest(Interest(tmpl)
-                           .setName(name)
-                           .setNonce(0),
+                         .setName(name)
+                         .setNonce(0),
                          onData, onTimeout);
 }
 
diff --git a/src/face.hpp b/src/face.hpp
index abc1d74..96fae40 100644
--- a/src/face.hpp
+++ b/src/face.hpp
@@ -535,6 +535,13 @@
   }
 
 private:
+
+  /**
+   * @throws ConfigFile::Error on parse error and unsupported protocols
+   */
+  void
+  construct(KeyChain* keyChain);
+
   /**
    * @throws Face::Error on unsupported protocol
    * @note shared_ptr is passed by value because ownership is transferred to this function
diff --git a/src/transport/tcp-transport.cpp b/src/transport/tcp-transport.cpp
index 7ffe530..a5b311f 100644
--- a/src/transport/tcp-transport.cpp
+++ b/src/transport/tcp-transport.cpp
@@ -23,6 +23,7 @@
 
 #include "tcp-transport.hpp"
 #include "stream-transport.hpp"
+#include "util/face-uri.hpp"
 
 namespace ndn {
 
@@ -36,6 +37,58 @@
 {
 }
 
+shared_ptr<TcpTransport>
+TcpTransport::create(const ConfigFile& config)
+{
+  const auto hostAndPort(getDefaultSocketHostAndPort(config));
+  return make_shared<TcpTransport>(hostAndPort.first,
+                                   hostAndPort.second);
+}
+
+std::pair<std::string, std::string>
+TcpTransport::getDefaultSocketHostAndPort(const ConfigFile& config)
+{
+  const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
+  std::string host = "localhost";
+  std::string port = "6363";
+
+  try
+    {
+      const util::FaceUri uri(parsed.get<std::string>("transport"));
+
+      const std::string scheme = uri.getScheme();
+      if (scheme != "tcp" && scheme != "tcp4" && scheme != "tcp6")
+        {
+          throw Transport::Error("Cannot create TcpTransport from \"" +
+                                 scheme + "\" URI");
+        }
+
+      if (!uri.getHost().empty())
+        {
+          host = uri.getHost();
+        }
+
+      if (!uri.getPort().empty())
+        {
+          port = uri.getPort();
+        }
+    }
+  catch (const boost::property_tree::ptree_bad_path& error)
+    {
+      // no transport specified, use default host and port
+    }
+  catch (const boost::property_tree::ptree_bad_data& error)
+    {
+      throw ConfigFile::Error(error.what());
+    }
+  catch (const util::FaceUri::Error& error)
+    {
+      throw ConfigFile::Error(error.what());
+    }
+
+  return std::make_pair(host, port);
+}
+
 void
 TcpTransport::connect(boost::asio::io_service& ioService,
                       const ReceiveCallback& receiveCallback)
diff --git a/src/transport/tcp-transport.hpp b/src/transport/tcp-transport.hpp
index 8afe007..13be254 100644
--- a/src/transport/tcp-transport.hpp
+++ b/src/transport/tcp-transport.hpp
@@ -24,6 +24,8 @@
 
 #include "../common.hpp"
 #include "transport.hpp"
+#include "util/config-file.hpp"
+
 
 // forward declaration
 namespace boost { namespace asio { namespace ip { class tcp; } } }
@@ -60,6 +62,14 @@
   virtual void
   send(const Block& header, const Block& payload);
 
+  static shared_ptr<TcpTransport>
+  create(const ConfigFile& config);
+
+NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+
+  static std::pair<std::string, std::string>
+  getDefaultSocketHostAndPort(const ConfigFile& config);
+
 private:
   std::string m_host;
   std::string m_port;
diff --git a/src/transport/unix-transport.cpp b/src/transport/unix-transport.cpp
index 03ee8b6..df505d7 100644
--- a/src/transport/unix-transport.cpp
+++ b/src/transport/unix-transport.cpp
@@ -25,6 +25,7 @@
 #include "stream-transport.hpp"
 
 #include "../face.hpp"
+#include "util/face-uri.hpp"
 
 namespace ndn {
 
@@ -41,44 +42,45 @@
 UnixTransport::getDefaultSocketName(const ConfigFile& config)
 {
   const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
-  try
-    {
-      return parsed.get<std::string>("unix_socket");
-    }
-  catch (boost::property_tree::ptree_bad_path& error)
-    {
-      // unix_socket not present, continue
-    }
-  catch (boost::property_tree::ptree_bad_data& error)
-    {
-      throw ConfigFile::Error(error.what());
-    }
 
-  // no unix_socket specified so the default socket name
-  // depends on the protocol we're using
   try
     {
-      const std::string protocol = parsed.get<std::string>("protocol");
-      if (protocol == "ndnd-tlv-0.7")
+      const util::FaceUri uri(parsed.get<std::string>("transport"));
+
+      if (uri.getScheme() != "unix")
         {
-          return "/tmp/.ndnd.sock";
+          throw Transport::Error("Cannot create UnixTransport from \"" +
+                                 uri.getScheme() + "\" URI");
+        }
+
+      if (!uri.getPath().empty())
+        {
+          return uri.getPath();
         }
     }
-  catch (boost::property_tree::ptree_bad_path& error)
+  catch (const boost::property_tree::ptree_bad_path& error)
     {
-      return "/var/run/nfd.sock";
+      // no transport specified
     }
-  catch (boost::property_tree::ptree_bad_data& error)
+  catch (const boost::property_tree::ptree_bad_data& error)
+    {
+      throw ConfigFile::Error(error.what());
+    }
+  catch (const util::FaceUri::Error& error)
     {
       throw ConfigFile::Error(error.what());
     }
 
-  // A we made here, then there's no unix_socket specified in the configuration
-  // file. A protocol is present, but it's not ndnd.
   // Assume the default nfd.sock location.
   return "/var/run/nfd.sock";
 }
 
+shared_ptr<UnixTransport>
+UnixTransport::create(const ConfigFile& config)
+{
+  return make_shared<UnixTransport>(getDefaultSocketName(config));
+}
+
 void
 UnixTransport::connect(boost::asio::io_service& ioService,
                        const ReceiveCallback& receiveCallback)
diff --git a/src/transport/unix-transport.hpp b/src/transport/unix-transport.hpp
index 25fa698..1c94225 100644
--- a/src/transport/unix-transport.hpp
+++ b/src/transport/unix-transport.hpp
@@ -19,12 +19,12 @@
  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
  */
 
-#ifndef NDN_UDPTRANSPORT_HPP
-#define NDN_UDPTRANSPORT_HPP
+#ifndef NDN_TRANSPORT_UNIX_TRANSPORT_HPP
+#define NDN_TRANSPORT_UNIX_TRANSPORT_HPP
 
 #include "../common.hpp"
 #include "transport.hpp"
-#include "../util/config-file.hpp"
+#include "util/config-file.hpp"
 
 // forward declaration
 namespace boost { namespace asio { namespace local { class stream_protocol; } } }
@@ -69,11 +69,15 @@
   virtual void
   send(const Block& header, const Block& payload);
 
+  static shared_ptr<UnixTransport>
+  create(const ConfigFile& config);
+
+NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   /**
    * Determine the default NFD unix socket
    *
-   * @returns unix_socket value if present in config, else /var/run/nfd.sock
-   * @throws ConfigFile::Error if fail to parse value of a present "unix_socket" field
+   * @returns transport value if present in config, else /var/run/nfd.sock
+   * @throws ConfigFile::Error if fail to parse value of a present "transport" field
    */
   static std::string
   getDefaultSocketName(const ConfigFile& config);
@@ -86,6 +90,6 @@
   shared_ptr< Impl > m_impl;
 };
 
-}
+} // namespace ndn
 
-#endif
+#endif // NDN_TRANSPORT_UNIX_TRANSPORT_HPP