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/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