face+mgmt: simplify factories and FaceManager using C++11 features
Change-Id: I113a4c15ef82bff705f61f31b170f49727bc3794
Refs: #3258
diff --git a/core/config-file.cpp b/core/config-file.cpp
index d989455..19bf1cb 100644
--- a/core/config-file.cpp
+++ b/core/config-file.cpp
@@ -1,11 +1,12 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014 Regents of the University of California,
- * Arizona Board of Regents,
- * Colorado State University,
- * University Pierre & Marie Curie, Sorbonne University,
- * Washington University in St. Louis,
- * Beijing Institute of Technology
+ * Copyright (c) 2014-2015, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
#include "config-file.hpp"
#include "logger.hpp"
@@ -30,6 +31,11 @@
namespace nfd {
+ConfigFile::ConfigFile(UnknownConfigSectionHandler unknownSectionCallback)
+ : m_unknownSectionCallback(unknownSectionCallback)
+{
+}
+
void
ConfigFile::throwErrorOnUnknownSection(const std::string& filename,
const std::string& sectionName,
@@ -53,27 +59,20 @@
}
bool
-ConfigFile::parseYesNo(const ConfigSection::const_iterator& i,
- const std::string& optionName,
- const std::string& sectionName)
+ConfigFile::parseYesNo(const ConfigSection::value_type& option,
+ const std::string& sectionName)
{
- const std::string value = i->second.get_value<std::string>();
+ auto value = option.second.get_value<std::string>();
+
if (value == "yes") {
return true;
}
-
- if (value == "no") {
+ else if (value == "no") {
return false;
}
- BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
- optionName + "\" in \"" +
- sectionName + "\" section"));
-}
-
-ConfigFile::ConfigFile(UnknownConfigSectionHandler unknownSectionCallback)
- : m_unknownSectionCallback(unknownSectionCallback)
-{
+ BOOST_THROW_EXCEPTION(Error("Invalid value \"" + value + "\" for option \"" +
+ option.first + "\" in \"" + sectionName + "\" section"));
}
void
@@ -105,7 +104,6 @@
parse(inputStream, isDryRun, filename);
}
-
void
ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
{
@@ -133,35 +131,26 @@
}
void
-ConfigFile::process(bool isDryRun, const std::string& filename)
+ConfigFile::process(bool isDryRun, const std::string& filename) const
{
BOOST_ASSERT(!filename.empty());
- // NFD_LOG_DEBUG("processing..." << ((isDryRun)?("dry run"):("")));
- if (m_global.begin() == m_global.end())
- {
- std::string msg = "Error processing configuration file: ";
- msg += filename;
- msg += " no data";
- BOOST_THROW_EXCEPTION(Error(msg));
+ if (m_global.begin() == m_global.end()) {
+ std::string msg = "Error processing configuration file: ";
+ msg += filename;
+ msg += " no data";
+ BOOST_THROW_EXCEPTION(Error(msg));
+ }
+
+ for (const auto& i : m_global) {
+ try {
+ ConfigSectionHandler subscriber = m_subscriptions.at(i.first);
+ subscriber(i.second, isDryRun, filename);
}
-
- for (ConfigSection::const_iterator i = m_global.begin(); i != m_global.end(); ++i)
- {
- const std::string& sectionName = i->first;
- const ConfigSection& section = i->second;
-
- SubscriptionTable::iterator subscriberIt = m_subscriptions.find(sectionName);
- if (subscriberIt != m_subscriptions.end())
- {
- ConfigSectionHandler subscriber = subscriberIt->second;
- subscriber(section, isDryRun, filename);
- }
- else
- {
- m_unknownSectionCallback(filename, sectionName, section, isDryRun);
- }
+ catch (const std::out_of_range&) {
+ m_unknownSectionCallback(filename, i.first, i.second, isDryRun);
}
+ }
}
}
diff --git a/core/config-file.hpp b/core/config-file.hpp
index ae9102e..f6e4aaf 100644
--- a/core/config-file.hpp
+++ b/core/config-file.hpp
@@ -1,11 +1,12 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014 Regents of the University of California,
- * Arizona Board of Regents,
- * Colorado State University,
- * University Pierre & Marie Curie, Sorbonne University,
- * Washington University in St. Louis,
- * Beijing Institute of Technology
+ * Copyright (c) 2014-2015, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
#ifndef NFD_CORE_CONFIG_FILE_HPP
#define NFD_CORE_CONFIG_FILE_HPP
@@ -47,7 +48,6 @@
class ConfigFile : noncopyable
{
public:
-
class Error : public std::runtime_error
{
public:
@@ -55,10 +55,10 @@
Error(const std::string& what)
: std::runtime_error(what)
{
-
}
};
+ explicit
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection);
static void
@@ -73,22 +73,32 @@
const ConfigSection& section,
bool isDryRun);
- /** @brief parse a config option that can be either "yes" or "no"
+ /**
+ * \brief parse a config option that can be either "yes" or "no"
*
- * @throw ConfigFile::Error value is neither "yes" nor "no"
- * @return true if "yes", false if "no"
+ * \return true if "yes", false if "no"
+ * \throw Error the value is neither "yes" nor "no"
*/
static bool
- parseYesNo(const ConfigSection::const_iterator& i,
- const std::string& optionName,
+ parseYesNo(const ConfigSection::value_type& option,
const std::string& sectionName);
+ /**
+ * \brief parse a numeric (integral or floating point) config option
+ *
+ * \return the numeric value of the parsed option
+ * \throw Error the value cannot be converted to the specified type
+ */
+ template <typename T>
+ static typename std::enable_if<std::is_arithmetic<T>::value, T>::type
+ parseNumber(const ConfigSection::value_type& option,
+ const std::string& sectionName);
+
/// \brief setup notification of configuration file sections
void
addSectionHandler(const std::string& sectionName,
ConfigSectionHandler subscriber);
-
/**
* \param filename file to parse
* \param isDryRun true if performing a dry run of configuration, false otherwise
@@ -127,21 +137,31 @@
parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
private:
-
void
- process(bool isDryRun, const std::string& filename);
+ process(bool isDryRun, const std::string& filename) const;
private:
UnknownConfigSectionHandler m_unknownSectionCallback;
-
- typedef std::map<std::string, ConfigSectionHandler> SubscriptionTable;
-
- SubscriptionTable m_subscriptions;
-
+ std::map<std::string, ConfigSectionHandler> m_subscriptions;
ConfigSection m_global;
};
-} // namespace nfd
+template <typename T>
+inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
+ConfigFile::parseNumber(const ConfigSection::value_type& option,
+ const std::string& sectionName)
+{
+ auto value = option.second.get_value<std::string>();
+ try {
+ return boost::lexical_cast<T>(value);
+ }
+ catch (const boost::bad_lexical_cast&) {
+ BOOST_THROW_EXCEPTION(Error("Invalid value \"" + value + "\" for option \"" +
+ option.first + "\" in \"" + sectionName + "\" section"));
+ }
+}
+
+} // namespace nfd
#endif // NFD_CORE_CONFIG_FILE_HPP
diff --git a/daemon/face/ethernet-factory.cpp b/daemon/face/ethernet-factory.cpp
index 63f428c..3d88590 100644
--- a/daemon/face/ethernet-factory.cpp
+++ b/daemon/face/ethernet-factory.cpp
@@ -38,7 +38,7 @@
if (!address.isMulticast())
BOOST_THROW_EXCEPTION(Error(address.toString() + " is not a multicast address"));
- shared_ptr<EthernetFace> face = findMulticastFace(interface.name, address);
+ auto face = findMulticastFace(interface.name, address);
if (face)
return face;
@@ -49,35 +49,35 @@
face->onFail.connectSingleShot([this, key] (const std::string& reason) {
m_multicastFaces.erase(key);
});
- m_multicastFaces.insert({key, face});
+ m_multicastFaces[key] = face;
return face;
}
-shared_ptr<EthernetFace>
-EthernetFactory::findMulticastFace(const std::string& interfaceName,
- const ethernet::Address& address) const
-{
- auto it = m_multicastFaces.find({interfaceName, address});
- if (it != m_multicastFaces.end())
- return it->second;
- else
- return {};
-}
-
void
EthernetFactory::createFace(const FaceUri& uri,
ndn::nfd::FacePersistency persistency,
const FaceCreatedCallback& onCreated,
- const FaceConnectFailedCallback& onConnectFailed)
+ const FaceCreationFailedCallback& onConnectFailed)
{
BOOST_THROW_EXCEPTION(Error("EthernetFactory does not support 'createFace' operation"));
}
-std::list<shared_ptr<const Channel>>
+std::vector<shared_ptr<const Channel>>
EthernetFactory::getChannels() const
{
return {};
}
+shared_ptr<EthernetFace>
+EthernetFactory::findMulticastFace(const std::string& interfaceName,
+ const ethernet::Address& address) const
+{
+ auto i = m_multicastFaces.find({interfaceName, address});
+ if (i != m_multicastFaces.end())
+ return i->second;
+ else
+ return nullptr;
+}
+
} // namespace nfd
diff --git a/daemon/face/ethernet-factory.hpp b/daemon/face/ethernet-factory.hpp
index 5b1666c..c14e093 100644
--- a/daemon/face/ethernet-factory.hpp
+++ b/daemon/face/ethernet-factory.hpp
@@ -52,13 +52,6 @@
typedef std::map<std::pair<std::string, ethernet::Address>,
shared_ptr<EthernetFace>> MulticastFaceMap;
- // from ProtocolFactory
- virtual void
- createFace(const FaceUri& uri,
- ndn::nfd::FacePersistency persistency,
- const FaceCreatedCallback& onCreated,
- const FaceConnectFailedCallback& onConnectFailed) DECL_OVERRIDE;
-
/**
* \brief Create an EthernetFace to communicate with the given multicast group
*
@@ -84,17 +77,22 @@
const MulticastFaceMap&
getMulticastFaces() const;
- virtual std::list<shared_ptr<const Channel>>
+public: // from ProtocolFactory
+ virtual void
+ createFace(const FaceUri& uri,
+ ndn::nfd::FacePersistency persistency,
+ const FaceCreatedCallback& onCreated,
+ const FaceCreationFailedCallback& onConnectFailed) DECL_OVERRIDE;
+
+ virtual std::vector<shared_ptr<const Channel>>
getChannels() const DECL_OVERRIDE;
private:
/**
* \brief Look up EthernetFace using specified interface and address
*
- * \returns shared pointer to the existing EthernetFace object or
- * empty shared pointer when such face does not exist
- *
- * \throws never
+ * \returns shared pointer to the existing EthernetFace object
+ * or nullptr when such face does not exist
*/
shared_ptr<EthernetFace>
findMulticastFace(const std::string& interfaceName,
diff --git a/daemon/face/protocol-factory.hpp b/daemon/face/protocol-factory.hpp
index 49e1a31..e2491ef 100644
--- a/daemon/face/protocol-factory.hpp
+++ b/daemon/face/protocol-factory.hpp
@@ -1,12 +1,12 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014, Regents of the University of California,
- * Arizona Board of Regents,
- * Colorado State University,
- * University Pierre & Marie Curie, Sorbonne University,
- * Washington University in St. Louis,
- * Beijing Institute of Technology,
- * The University of Memphis
+ * Copyright (c) 2014-2015, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
@@ -34,28 +34,36 @@
class Face;
/**
- * \brief Prototype for the callback called when face is created
- * (as a response to incoming connection or after connection
- * is established)
+ * \brief Prototype for the callback that is invoked when the face
+ * is created (as a response to incoming connection or after
+ * connection is established)
*/
typedef function<void(const shared_ptr<Face>& newFace)> FaceCreatedCallback;
/**
- * \brief Prototype for the callback that is called when face is failed to
- * get created
+ * \brief Prototype for the callback that is invoked when the face
+ * fails to be created
*/
-typedef function<void(const std::string& reason)> FaceConnectFailedCallback;
+typedef function<void(const std::string& reason)> FaceCreationFailedCallback;
+/**
+ * \brief Abstract base class for all protocol factories
+ */
class ProtocolFactory
{
public:
/**
- * \brief Base class for all exceptions thrown by channel factories
+ * \brief Base class for all exceptions thrown by protocol factories
*/
- struct Error : public std::runtime_error
+ class Error : public std::runtime_error
{
- Error(const std::string& what) : std::runtime_error(what) {}
+ public:
+ explicit
+ Error(const std::string& what)
+ : std::runtime_error(what)
+ {
+ }
};
/** \brief Try to create Face using the supplied FaceUri
@@ -70,9 +78,9 @@
createFace(const FaceUri& uri,
ndn::nfd::FacePersistency persistency,
const FaceCreatedCallback& onCreated,
- const FaceConnectFailedCallback& onConnectFailed) = 0;
+ const FaceCreationFailedCallback& onConnectFailed) = 0;
- virtual std::list<shared_ptr<const Channel>>
+ virtual std::vector<shared_ptr<const Channel>>
getChannels() const = 0;
};
diff --git a/daemon/face/tcp-factory.cpp b/daemon/face/tcp-factory.cpp
index 4fb6059..a0fad93 100644
--- a/daemon/face/tcp-factory.cpp
+++ b/daemon/face/tcp-factory.cpp
@@ -26,52 +26,35 @@
#include "tcp-factory.hpp"
#include "core/logger.hpp"
#include "core/network-interface.hpp"
-#include "core/global-io.hpp"
-
-NFD_LOG_INIT("TcpFactory");
namespace nfd {
-static const boost::asio::ip::address_v4 ALL_V4_ENDPOINT(
- boost::asio::ip::address_v4::from_string("0.0.0.0"));
+namespace ip = boost::asio::ip;
-static const boost::asio::ip::address_v6 ALL_V6_ENDPOINT(
- boost::asio::ip::address_v6::from_string("::"));
-
-TcpFactory::TcpFactory(const std::string& defaultPort/* = "6363"*/)
- : m_defaultPort(defaultPort)
-{
-}
+NFD_LOG_INIT("TcpFactory");
void
TcpFactory::prohibitEndpoint(const tcp::Endpoint& endpoint)
{
- using namespace boost::asio::ip;
-
- const address& address = endpoint.address();
-
- if (address.is_v4() && address == ALL_V4_ENDPOINT)
- {
- prohibitAllIpv4Endpoints(endpoint.port());
- }
- else if (endpoint.address().is_v6() && address == ALL_V6_ENDPOINT)
- {
- prohibitAllIpv6Endpoints(endpoint.port());
- }
+ if (endpoint.address().is_v4() &&
+ endpoint.address() == ip::address_v4::any()) {
+ prohibitAllIpv4Endpoints(endpoint.port());
+ }
+ else if (endpoint.address().is_v6() &&
+ endpoint.address() == ip::address_v6::any()) {
+ prohibitAllIpv6Endpoints(endpoint.port());
+ }
NFD_LOG_TRACE("prohibiting TCP " << endpoint);
-
m_prohibitedEndpoints.insert(endpoint);
}
void
-TcpFactory::prohibitAllIpv4Endpoints(const uint16_t port)
+TcpFactory::prohibitAllIpv4Endpoints(uint16_t port)
{
- using namespace boost::asio::ip;
-
for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
- for (const address_v4& addr : nic.ipv4Addresses) {
- if (addr != ALL_V4_ENDPOINT) {
+ for (const auto& addr : nic.ipv4Addresses) {
+ if (addr != ip::address_v4::any()) {
prohibitEndpoint(tcp::Endpoint(addr, port));
}
}
@@ -79,13 +62,11 @@
}
void
-TcpFactory::prohibitAllIpv6Endpoints(const uint16_t port)
+TcpFactory::prohibitAllIpv6Endpoints(uint16_t port)
{
- using namespace boost::asio::ip;
-
for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
- for (const address_v6& addr : nic.ipv6Addresses) {
- if (addr != ALL_V6_ENDPOINT) {
+ for (const auto& addr : nic.ipv6Addresses) {
+ if (addr != ip::address_v6::any()) {
prohibitEndpoint(tcp::Endpoint(addr, port));
}
}
@@ -95,8 +76,8 @@
shared_ptr<TcpChannel>
TcpFactory::createChannel(const tcp::Endpoint& endpoint)
{
- shared_ptr<TcpChannel> channel = findChannel(endpoint);
- if (static_cast<bool>(channel))
+ auto channel = findChannel(endpoint);
+ if (channel)
return channel;
channel = make_shared<TcpChannel>(endpoint);
@@ -110,68 +91,64 @@
shared_ptr<TcpChannel>
TcpFactory::createChannel(const std::string& localIp, const std::string& localPort)
{
- using namespace boost::asio::ip;
- tcp::Endpoint endpoint(address::from_string(localIp), boost::lexical_cast<uint16_t>(localPort));
+ tcp::Endpoint endpoint(ip::address::from_string(localIp),
+ boost::lexical_cast<uint16_t>(localPort));
return createChannel(endpoint);
}
-shared_ptr<TcpChannel>
-TcpFactory::findChannel(const tcp::Endpoint& localEndpoint)
-{
- ChannelMap::iterator i = m_channels.find(localEndpoint);
- if (i != m_channels.end())
- return i->second;
- else
- return shared_ptr<TcpChannel>();
-}
-
void
TcpFactory::createFace(const FaceUri& uri,
ndn::nfd::FacePersistency persistency,
const FaceCreatedCallback& onCreated,
- const FaceConnectFailedCallback& onConnectFailed)
+ const FaceCreationFailedCallback& onConnectFailed)
{
+ BOOST_ASSERT(uri.isCanonical());
+
if (persistency != ndn::nfd::FACE_PERSISTENCY_PERSISTENT) {
- BOOST_THROW_EXCEPTION(Error("TcpFactory only supports persistent face"));
+ BOOST_THROW_EXCEPTION(Error("TcpFactory::createFace supports only FACE_PERSISTENCY_PERSISTENT"));
}
- BOOST_ASSERT(uri.isCanonical());
- boost::asio::ip::address ipAddress = boost::asio::ip::address::from_string(uri.getHost());
- tcp::Endpoint endpoint(ipAddress, boost::lexical_cast<uint16_t>(uri.getPort()));
+ tcp::Endpoint endpoint(ip::address::from_string(uri.getHost()),
+ boost::lexical_cast<uint16_t>(uri.getPort()));
- if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end())
- {
- onConnectFailed("Requested endpoint is prohibited "
- "(reserved by this NFD or disallowed by face management protocol)");
- return;
- }
+ if (m_prohibitedEndpoints.find(endpoint) != m_prohibitedEndpoints.end()) {
+ onConnectFailed("Requested endpoint is prohibited "
+ "(reserved by this NFD or disallowed by face management protocol)");
+ return;
+ }
// very simple logic for now
- for (ChannelMap::iterator channel = m_channels.begin();
- channel != m_channels.end();
- ++channel)
- {
- if ((channel->first.address().is_v4() && endpoint.address().is_v4()) ||
- (channel->first.address().is_v6() && endpoint.address().is_v6()))
- {
- channel->second->connect(endpoint, onCreated, onConnectFailed);
- return;
- }
+ for (const auto& i : m_channels) {
+ if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
+ (i.first.address().is_v6() && endpoint.address().is_v6())) {
+ i.second->connect(endpoint, onCreated, onConnectFailed);
+ return;
}
- onConnectFailed("No channels available to connect to "
- + boost::lexical_cast<std::string>(endpoint));
+ }
+
+ onConnectFailed("No channels available to connect to " + boost::lexical_cast<std::string>(endpoint));
}
-std::list<shared_ptr<const Channel> >
+std::vector<shared_ptr<const Channel>>
TcpFactory::getChannels() const
{
- std::list<shared_ptr<const Channel> > channels;
- for (ChannelMap::const_iterator i = m_channels.begin(); i != m_channels.end(); ++i)
- {
- channels.push_back(i->second);
- }
+ std::vector<shared_ptr<const Channel>> channels;
+ channels.reserve(m_channels.size());
+
+ for (const auto& i : m_channels)
+ channels.push_back(i.second);
return channels;
}
+shared_ptr<TcpChannel>
+TcpFactory::findChannel(const tcp::Endpoint& localEndpoint) const
+{
+ auto i = m_channels.find(localEndpoint);
+ if (i != m_channels.end())
+ return i->second;
+ else
+ return nullptr;
+}
+
} // namespace nfd
diff --git a/daemon/face/tcp-factory.hpp b/daemon/face/tcp-factory.hpp
index 315bacd..5bf3694 100644
--- a/daemon/face/tcp-factory.hpp
+++ b/daemon/face/tcp-factory.hpp
@@ -29,7 +29,6 @@
#include "protocol-factory.hpp"
#include "tcp-channel.hpp"
-
namespace nfd {
class TcpFactory : public ProtocolFactory
@@ -38,14 +37,16 @@
/**
* \brief Exception of TcpFactory
*/
- struct Error : public ProtocolFactory::Error
+ class Error : public ProtocolFactory::Error
{
- Error(const std::string& what) : ProtocolFactory::Error(what) {}
+ public:
+ explicit
+ Error(const std::string& what)
+ : ProtocolFactory::Error(what)
+ {
+ }
};
- explicit
- TcpFactory(const std::string& defaultPort = "6363");
-
/**
* \brief Create TCP-based channel using tcp::Endpoint
*
@@ -77,45 +78,40 @@
shared_ptr<TcpChannel>
createChannel(const std::string& localIp, const std::string& localPort);
- // from ProtocolFactory
-
+public: // from ProtocolFactory
virtual void
createFace(const FaceUri& uri,
ndn::nfd::FacePersistency persistency,
const FaceCreatedCallback& onCreated,
- const FaceConnectFailedCallback& onConnectFailed) DECL_OVERRIDE;
+ const FaceCreationFailedCallback& onConnectFailed) DECL_OVERRIDE;
- virtual std::list<shared_ptr<const Channel> >
+ virtual std::vector<shared_ptr<const Channel>>
getChannels() const DECL_OVERRIDE;
PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-
void
prohibitEndpoint(const tcp::Endpoint& endpoint);
void
- prohibitAllIpv4Endpoints(const uint16_t port);
+ prohibitAllIpv4Endpoints(uint16_t port);
void
- prohibitAllIpv6Endpoints(const uint16_t port);
+ prohibitAllIpv6Endpoints(uint16_t port);
+private:
/**
* \brief Look up TcpChannel using specified local endpoint
*
* \returns shared pointer to the existing TcpChannel object
* or empty shared pointer when such channel does not exist
- *
- * \throws never
*/
shared_ptr<TcpChannel>
- findChannel(const tcp::Endpoint& localEndpoint);
+ findChannel(const tcp::Endpoint& localEndpoint) const;
+
+private:
+ std::map<tcp::Endpoint, shared_ptr<TcpChannel>> m_channels;
PUBLIC_WITH_TESTS_ELSE_PRIVATE:
- typedef std::map< tcp::Endpoint, shared_ptr<TcpChannel> > ChannelMap;
- ChannelMap m_channels;
-
- std::string m_defaultPort;
-
std::set<tcp::Endpoint> m_prohibitedEndpoints;
};
diff --git a/daemon/face/udp-factory.cpp b/daemon/face/udp-factory.cpp
index 74636d8..2a39e36 100644
--- a/daemon/face/udp-factory.cpp
+++ b/daemon/face/udp-factory.cpp
@@ -38,72 +38,51 @@
namespace nfd {
-using namespace boost::asio;
+namespace ip = boost::asio::ip;
NFD_LOG_INIT("UdpFactory");
-static const boost::asio::ip::address_v4 ALL_V4_ENDPOINT(
- boost::asio::ip::address_v4::from_string("0.0.0.0"));
-
-static const boost::asio::ip::address_v6 ALL_V6_ENDPOINT(
- boost::asio::ip::address_v6::from_string("::"));
-
-UdpFactory::UdpFactory(const std::string& defaultPort/* = "6363"*/)
- : m_defaultPort(defaultPort)
-{
-}
-
void
UdpFactory::prohibitEndpoint(const udp::Endpoint& endpoint)
{
- using namespace boost::asio::ip;
-
- const address& address = endpoint.address();
-
- if (address.is_v4() && address == ALL_V4_ENDPOINT)
- {
- prohibitAllIpv4Endpoints(endpoint.port());
- }
- else if (endpoint.address().is_v6() && address == ALL_V6_ENDPOINT)
- {
- prohibitAllIpv6Endpoints(endpoint.port());
- }
+ if (endpoint.address().is_v4() &&
+ endpoint.address() == ip::address_v4::any()) {
+ prohibitAllIpv4Endpoints(endpoint.port());
+ }
+ else if (endpoint.address().is_v6() &&
+ endpoint.address() == ip::address_v6::any()) {
+ prohibitAllIpv6Endpoints(endpoint.port());
+ }
NFD_LOG_TRACE("prohibiting UDP " << endpoint);
-
m_prohibitedEndpoints.insert(endpoint);
}
void
-UdpFactory::prohibitAllIpv4Endpoints(const uint16_t port)
+UdpFactory::prohibitAllIpv4Endpoints(uint16_t port)
{
- using namespace boost::asio::ip;
-
for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
- for (const address_v4& addr : nic.ipv4Addresses) {
- if (addr != ALL_V4_ENDPOINT) {
+ for (const auto& addr : nic.ipv4Addresses) {
+ if (addr != ip::address_v4::any()) {
prohibitEndpoint(udp::Endpoint(addr, port));
}
}
- if (nic.isBroadcastCapable() && nic.broadcastAddress != ALL_V4_ENDPOINT)
- {
- NFD_LOG_TRACE("prohibiting broadcast address: " << nic.broadcastAddress.to_string());
+ if (nic.isBroadcastCapable() &&
+ nic.broadcastAddress != ip::address_v4::any()) {
prohibitEndpoint(udp::Endpoint(nic.broadcastAddress, port));
}
}
- prohibitEndpoint(udp::Endpoint(address::from_string("255.255.255.255"), port));
+ prohibitEndpoint(udp::Endpoint(ip::address_v4::broadcast(), port));
}
void
-UdpFactory::prohibitAllIpv6Endpoints(const uint16_t port)
+UdpFactory::prohibitAllIpv6Endpoints(uint16_t port)
{
- using namespace boost::asio::ip;
-
for (const NetworkInterfaceInfo& nic : listNetworkInterfaces()) {
- for (const address_v6& addr : nic.ipv6Addresses) {
- if (addr != ALL_V6_ENDPOINT) {
+ for (const auto& addr : nic.ipv6Addresses) {
+ if (addr != ip::address_v6::any()) {
prohibitEndpoint(udp::Endpoint(addr, port));
}
}
@@ -116,10 +95,15 @@
{
NFD_LOG_DEBUG("Creating unicast channel " << endpoint);
- shared_ptr<UdpChannel> channel = findChannel(endpoint);
- if (static_cast<bool>(channel))
+ auto channel = findChannel(endpoint);
+ if (channel)
return channel;
+ if (endpoint.address().is_multicast()) {
+ BOOST_THROW_EXCEPTION(Error("createChannel is only for unicast channels. The provided endpoint "
+ "is multicast. Use createMulticastFace to create a multicast face"));
+ }
+
// check if the endpoint is already used by a multicast face
auto face = findMulticastFace(endpoint);
if (face) {
@@ -127,12 +111,6 @@
"endpoint is already allocated for a UDP multicast face"));
}
- if (endpoint.address().is_multicast()) {
- BOOST_THROW_EXCEPTION(Error("This method is only for unicast channel. The provided "
- "endpoint is multicast. Use createMulticastFace to "
- "create a multicast face"));
- }
-
channel = make_shared<UdpChannel>(endpoint, timeout);
m_channels[endpoint] = channel;
prohibitEndpoint(endpoint);
@@ -141,12 +119,11 @@
}
shared_ptr<UdpChannel>
-UdpFactory::createChannel(const std::string& localIp,
- const std::string& localPort,
+UdpFactory::createChannel(const std::string& localIp, const std::string& localPort,
const time::seconds& timeout)
{
- using namespace boost::asio::ip;
- udp::Endpoint endpoint(address::from_string(localIp), boost::lexical_cast<uint16_t>(localPort));
+ udp::Endpoint endpoint(ip::address::from_string(localIp),
+ boost::lexical_cast<uint16_t>(localPort));
return createChannel(endpoint, timeout);
}
@@ -167,8 +144,8 @@
}
// checking if the local endpoint is already in use for a unicast channel
- shared_ptr<UdpChannel> unicast = findChannel(localEndpoint);
- if (static_cast<bool>(unicast)) {
+ auto unicastCh = findChannel(localEndpoint);
+ if (unicastCh) {
BOOST_THROW_EXCEPTION(Error("Cannot create the requested UDP multicast face, local "
"endpoint is already allocated for a UDP unicast channel"));
}
@@ -203,7 +180,7 @@
sendSocket.set_option(ip::udp::socket::reuse_address(true));
sendSocket.set_option(ip::multicast::enable_loopback(false));
sendSocket.bind(udp::Endpoint(ip::address_v4::any(), multicastEndpoint.port()));
- if (localEndpoint.address() != ALL_V4_ENDPOINT)
+ if (localEndpoint.address() != ip::address_v4::any())
sendSocket.set_option(ip::multicast::outbound_interface(localEndpoint.address().to_v4()));
sendSocket.set_option(ip::multicast::join_group(multicastEndpoint.address().to_v4(),
@@ -261,16 +238,16 @@
UdpFactory::createFace(const FaceUri& uri,
ndn::nfd::FacePersistency persistency,
const FaceCreatedCallback& onCreated,
- const FaceConnectFailedCallback& onConnectFailed)
+ const FaceCreationFailedCallback& onConnectFailed)
{
- if (persistency == ndn::nfd::FacePersistency::FACE_PERSISTENCY_ON_DEMAND) {
- BOOST_THROW_EXCEPTION(Error("UdpFactory::createFace does not support creating on-demand face"));
- }
-
BOOST_ASSERT(uri.isCanonical());
- boost::asio::ip::address ipAddress = boost::asio::ip::address::from_string(uri.getHost());
- udp::Endpoint endpoint(ipAddress, boost::lexical_cast<uint16_t>(uri.getPort()));
+ if (persistency == ndn::nfd::FACE_PERSISTENCY_ON_DEMAND) {
+ BOOST_THROW_EXCEPTION(Error("UdpFactory::createFace does not support FACE_PERSISTENCY_ON_DEMAND"));
+ }
+
+ udp::Endpoint endpoint(ip::address::from_string(uri.getHost()),
+ boost::lexical_cast<uint16_t>(uri.getPort()));
if (endpoint.address().is_multicast()) {
onConnectFailed("The provided address is multicast. Please use createMulticastFace method");
@@ -284,52 +261,47 @@
}
// very simple logic for now
- for (ChannelMap::iterator channel = m_channels.begin();
- channel != m_channels.end();
- ++channel)
- {
- if ((channel->first.address().is_v4() && endpoint.address().is_v4()) ||
- (channel->first.address().is_v6() && endpoint.address().is_v6()))
- {
- channel->second->connect(endpoint, persistency, onCreated, onConnectFailed);
+ for (const auto& i : m_channels) {
+ if ((i.first.address().is_v4() && endpoint.address().is_v4()) ||
+ (i.first.address().is_v6() && endpoint.address().is_v6())) {
+ i.second->connect(endpoint, persistency, onCreated, onConnectFailed);
return;
}
}
- onConnectFailed("No channels available to connect to " +
- boost::lexical_cast<std::string>(endpoint));
+ onConnectFailed("No channels available to connect to " + boost::lexical_cast<std::string>(endpoint));
+}
+
+std::vector<shared_ptr<const Channel>>
+UdpFactory::getChannels() const
+{
+ std::vector<shared_ptr<const Channel>> channels;
+ channels.reserve(m_channels.size());
+
+ for (const auto& i : m_channels)
+ channels.push_back(i.second);
+
+ return channels;
}
shared_ptr<UdpChannel>
-UdpFactory::findChannel(const udp::Endpoint& localEndpoint)
+UdpFactory::findChannel(const udp::Endpoint& localEndpoint) const
{
- ChannelMap::iterator i = m_channels.find(localEndpoint);
+ auto i = m_channels.find(localEndpoint);
if (i != m_channels.end())
return i->second;
else
- return shared_ptr<UdpChannel>();
-}
-
-shared_ptr<face::LpFaceWrapper>
-UdpFactory::findMulticastFace(const udp::Endpoint& localEndpoint)
-{
- MulticastFaceMap::iterator i = m_multicastFaces.find(localEndpoint);
- if (i != m_multicastFaces.end())
- return i->second;
- else
return nullptr;
}
-std::list<shared_ptr<const Channel>>
-UdpFactory::getChannels() const
+shared_ptr<face::LpFaceWrapper>
+UdpFactory::findMulticastFace(const udp::Endpoint& localEndpoint) const
{
- std::list<shared_ptr<const Channel>> channels;
- for (ChannelMap::const_iterator i = m_channels.begin(); i != m_channels.end(); ++i)
- {
- channels.push_back(i->second);
- }
-
- return channels;
+ auto i = m_multicastFaces.find(localEndpoint);
+ if (i != m_multicastFaces.end())
+ return i->second;
+ else
+ return nullptr;
}
} // namespace nfd
diff --git a/daemon/face/udp-factory.hpp b/daemon/face/udp-factory.hpp
index 0465ca5..1c8f90f 100644
--- a/daemon/face/udp-factory.hpp
+++ b/daemon/face/udp-factory.hpp
@@ -51,9 +51,6 @@
typedef std::map<udp::Endpoint, shared_ptr<face::LpFaceWrapper>> MulticastFaceMap;
- explicit
- UdpFactory(const std::string& defaultPort = "6363");
-
/**
* \brief Create UDP-based channel using udp::Endpoint
*
@@ -97,8 +94,7 @@
* \throws UdpFactory::Error
*/
shared_ptr<UdpChannel>
- createChannel(const std::string& localIp,
- const std::string& localPort,
+ createChannel(const std::string& localIp, const std::string& localPort,
const time::seconds& timeout = time::seconds(600));
/**
@@ -141,62 +137,56 @@
const std::string& multicastPort,
const std::string& networkInterfaceName = "");
- // from ProtocolFactory
- virtual void
- createFace(const FaceUri& uri,
- ndn::nfd::FacePersistency persistency,
- const FaceCreatedCallback& onCreated,
- const FaceConnectFailedCallback& onConnectFailed) DECL_OVERRIDE;
-
- virtual std::list<shared_ptr<const Channel>>
- getChannels() const DECL_OVERRIDE;
-
/**
* \brief Get map of configured multicast faces
*/
const MulticastFaceMap&
getMulticastFaces() const;
+public: // from ProtocolFactory
+ virtual void
+ createFace(const FaceUri& uri,
+ ndn::nfd::FacePersistency persistency,
+ const FaceCreatedCallback& onCreated,
+ const FaceCreationFailedCallback& onConnectFailed) DECL_OVERRIDE;
+
+ virtual std::vector<shared_ptr<const Channel>>
+ getChannels() const DECL_OVERRIDE;
+
PUBLIC_WITH_TESTS_ELSE_PRIVATE:
void
prohibitEndpoint(const udp::Endpoint& endpoint);
void
- prohibitAllIpv4Endpoints(const uint16_t port);
+ prohibitAllIpv4Endpoints(uint16_t port);
void
- prohibitAllIpv6Endpoints(const uint16_t port);
+ prohibitAllIpv6Endpoints(uint16_t port);
- void
- afterFaceFailed(udp::Endpoint& endpoint);
-
+private:
/**
* \brief Look up UdpChannel using specified local endpoint
*
* \returns shared pointer to the existing UdpChannel object
- * or empty shared pointer when such channel does not exist
- *
- * \throws never
+ * or nullptr when such channel does not exist
*/
shared_ptr<UdpChannel>
- findChannel(const udp::Endpoint& localEndpoint);
+ findChannel(const udp::Endpoint& localEndpoint) const;
/**
* \brief Look up multicast UdpFace using specified local endpoint
*
- * \returns shared pointer to the existing multicast MulticastUdpFace object
+ * \returns shared pointer to the existing multicast UdpFace object
* or nullptr when such face does not exist
*/
shared_ptr<face::LpFaceWrapper>
- findMulticastFace(const udp::Endpoint& localEndpoint);
+ findMulticastFace(const udp::Endpoint& localEndpoint) const;
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
- typedef std::map<udp::Endpoint, shared_ptr<UdpChannel>> ChannelMap;
-
- ChannelMap m_channels;
+private:
+ std::map<udp::Endpoint, shared_ptr<UdpChannel>> m_channels;
MulticastFaceMap m_multicastFaces;
- std::string m_defaultPort;
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
std::set<udp::Endpoint> m_prohibitedEndpoints;
};
diff --git a/daemon/face/unix-stream-factory.cpp b/daemon/face/unix-stream-factory.cpp
index 717f7b6..3de85b4 100644
--- a/daemon/face/unix-stream-factory.cpp
+++ b/daemon/face/unix-stream-factory.cpp
@@ -1,12 +1,12 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014, Regents of the University of California,
- * Arizona Board of Regents,
- * Colorado State University,
- * University Pierre & Marie Curie, Sorbonne University,
- * Washington University in St. Louis,
- * Beijing Institute of Technology,
- * The University of Memphis
+ * Copyright (c) 2014-2015, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
@@ -36,7 +36,7 @@
p = boost::filesystem::canonical(p.parent_path()) / p.filename();
unix_stream::Endpoint endpoint(p.string());
- shared_ptr<UnixStreamChannel> channel = findChannel(endpoint);
+ auto channel = findChannel(endpoint);
if (channel)
return channel;
@@ -45,35 +45,35 @@
return channel;
}
-shared_ptr<UnixStreamChannel>
-UnixStreamFactory::findChannel(const unix_stream::Endpoint& endpoint)
-{
- ChannelMap::iterator i = m_channels.find(endpoint);
- if (i != m_channels.end())
- return i->second;
- else
- return shared_ptr<UnixStreamChannel>();
-}
-
void
UnixStreamFactory::createFace(const FaceUri& uri,
ndn::nfd::FacePersistency persistency,
const FaceCreatedCallback& onCreated,
- const FaceConnectFailedCallback& onConnectFailed)
+ const FaceCreationFailedCallback& onConnectFailed)
{
BOOST_THROW_EXCEPTION(Error("UnixStreamFactory does not support 'createFace' operation"));
}
-std::list<shared_ptr<const Channel> >
+std::vector<shared_ptr<const Channel>>
UnixStreamFactory::getChannels() const
{
- std::list<shared_ptr<const Channel> > channels;
- for (ChannelMap::const_iterator i = m_channels.begin(); i != m_channels.end(); ++i)
- {
- channels.push_back(i->second);
- }
+ std::vector<shared_ptr<const Channel>> channels;
+ channels.reserve(m_channels.size());
+
+ for (const auto& i : m_channels)
+ channels.push_back(i.second);
return channels;
}
+shared_ptr<UnixStreamChannel>
+UnixStreamFactory::findChannel(const unix_stream::Endpoint& endpoint) const
+{
+ auto i = m_channels.find(endpoint);
+ if (i != m_channels.end())
+ return i->second;
+ else
+ return nullptr;
+}
+
} // namespace nfd
diff --git a/daemon/face/unix-stream-factory.hpp b/daemon/face/unix-stream-factory.hpp
index 61ef3de..43fe553 100644
--- a/daemon/face/unix-stream-factory.hpp
+++ b/daemon/face/unix-stream-factory.hpp
@@ -1,12 +1,12 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014, Regents of the University of California,
- * Arizona Board of Regents,
- * Colorado State University,
- * University Pierre & Marie Curie, Sorbonne University,
- * Washington University in St. Louis,
- * Beijing Institute of Technology,
- * The University of Memphis
+ * Copyright (c) 2014-2015, Regents of the University of California,
+ * Arizona Board of Regents,
+ * Colorado State University,
+ * University Pierre & Marie Curie, Sorbonne University,
+ * Washington University in St. Louis,
+ * Beijing Institute of Technology,
+ * The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
@@ -37,9 +37,14 @@
/**
* \brief Exception of UnixStreamFactory
*/
- struct Error : public ProtocolFactory::Error
+ class Error : public ProtocolFactory::Error
{
- Error(const std::string& what) : ProtocolFactory::Error(what) {}
+ public:
+ explicit
+ Error(const std::string& what)
+ : ProtocolFactory::Error(what)
+ {
+ }
};
/**
@@ -57,15 +62,14 @@
shared_ptr<UnixStreamChannel>
createChannel(const std::string& unixSocketPath);
- // from Factory
-
+public: // from ProtocolFactory
virtual void
createFace(const FaceUri& uri,
ndn::nfd::FacePersistency persistency,
const FaceCreatedCallback& onCreated,
- const FaceConnectFailedCallback& onConnectFailed) DECL_OVERRIDE;
+ const FaceCreationFailedCallback& onConnectFailed) DECL_OVERRIDE;
- virtual std::list<shared_ptr<const Channel> >
+ virtual std::vector<shared_ptr<const Channel>>
getChannels() const DECL_OVERRIDE;
private:
@@ -74,15 +78,12 @@
*
* \returns shared pointer to the existing UnixStreamChannel object
* or empty shared pointer when such channel does not exist
- *
- * \throws never
*/
shared_ptr<UnixStreamChannel>
- findChannel(const unix_stream::Endpoint& endpoint);
+ findChannel(const unix_stream::Endpoint& endpoint) const;
private:
- typedef std::map< unix_stream::Endpoint, shared_ptr<UnixStreamChannel> > ChannelMap;
- ChannelMap m_channels;
+ std::map<unix_stream::Endpoint, shared_ptr<UnixStreamChannel>> m_channels;
};
} // namespace nfd
diff --git a/daemon/face/websocket-channel.hpp b/daemon/face/websocket-channel.hpp
index f560ab3..027085c 100644
--- a/daemon/face/websocket-channel.hpp
+++ b/daemon/face/websocket-channel.hpp
@@ -31,6 +31,10 @@
namespace nfd {
+namespace websocket {
+typedef boost::asio::ip::tcp::endpoint Endpoint;
+} // namespace websocket
+
/**
* \brief Class implementing WebSocket-based channel to create faces
*/
diff --git a/daemon/face/websocket-face.hpp b/daemon/face/websocket-face.hpp
index 7838572..3c7fbf1 100644
--- a/daemon/face/websocket-face.hpp
+++ b/daemon/face/websocket-face.hpp
@@ -38,11 +38,9 @@
namespace nfd {
namespace websocket {
-typedef boost::asio::ip::tcp::endpoint Endpoint;
typedef websocketpp::server<websocketpp::config::asio> Server;
} // namespace websocket
-
/**
* \brief Implementation of Face abstraction that uses WebSocket
* as underlying transport mechanism
diff --git a/daemon/face/websocket-factory.cpp b/daemon/face/websocket-factory.cpp
index 80e8efa..bcb3970 100644
--- a/daemon/face/websocket-factory.cpp
+++ b/daemon/face/websocket-factory.cpp
@@ -27,18 +27,13 @@
namespace nfd {
-using namespace boost::asio;
-
-WebSocketFactory::WebSocketFactory(const std::string& defaultPort)
- : m_defaultPort(defaultPort)
-{
-}
+namespace ip = boost::asio::ip;
shared_ptr<WebSocketChannel>
WebSocketFactory::createChannel(const websocket::Endpoint& endpoint)
{
- shared_ptr<WebSocketChannel> channel = findChannel(endpoint);
- if (static_cast<bool>(channel))
+ auto channel = findChannel(endpoint);
+ if (channel)
return channel;
channel = make_shared<WebSocketChannel>(endpoint);
@@ -48,42 +43,42 @@
}
shared_ptr<WebSocketChannel>
-WebSocketFactory::createChannel(const std::string& localIp, const std::string& port)
+WebSocketFactory::createChannel(const std::string& localIp, const std::string& localPort)
{
- using namespace boost::asio::ip;
- websocket::Endpoint endpoint(address::from_string(localIp), boost::lexical_cast<uint16_t>(port));
+ websocket::Endpoint endpoint(ip::address::from_string(localIp),
+ boost::lexical_cast<uint16_t>(localPort));
return createChannel(endpoint);
}
-shared_ptr<WebSocketChannel>
-WebSocketFactory::findChannel(const websocket::Endpoint& localEndpoint)
-{
- ChannelMap::iterator i = m_channels.find(localEndpoint);
- if (i != m_channels.end())
- return i->second;
- else
- return shared_ptr<WebSocketChannel>();
-}
-
void
WebSocketFactory::createFace(const FaceUri& uri,
ndn::nfd::FacePersistency persistency,
const FaceCreatedCallback& onCreated,
- const FaceConnectFailedCallback& onConnectFailed)
+ const FaceCreationFailedCallback& onConnectFailed)
{
BOOST_THROW_EXCEPTION(Error("WebSocketFactory does not support 'createFace' operation"));
}
-std::list<shared_ptr<const Channel> >
+std::vector<shared_ptr<const Channel>>
WebSocketFactory::getChannels() const
{
- std::list<shared_ptr<const Channel> > channels;
- for (ChannelMap::const_iterator i = m_channels.begin(); i != m_channels.end(); ++i)
- {
- channels.push_back(i->second);
- }
+ std::vector<shared_ptr<const Channel>> channels;
+ channels.reserve(m_channels.size());
+
+ for (const auto& i : m_channels)
+ channels.push_back(i.second);
return channels;
}
+shared_ptr<WebSocketChannel>
+WebSocketFactory::findChannel(const websocket::Endpoint& endpoint) const
+{
+ auto i = m_channels.find(endpoint);
+ if (i != m_channels.end())
+ return i->second;
+ else
+ return nullptr;
+}
+
} // namespace nfd
diff --git a/daemon/face/websocket-factory.hpp b/daemon/face/websocket-factory.hpp
index 7271b0a..87947ba 100644
--- a/daemon/face/websocket-factory.hpp
+++ b/daemon/face/websocket-factory.hpp
@@ -47,9 +47,6 @@
}
};
- explicit
- WebSocketFactory(const std::string& defaultPort);
-
/**
* \brief Create WebSocket-based channel using websocket::Endpoint
*
@@ -76,16 +73,16 @@
* \throws WebSocketFactory::Error
*/
shared_ptr<WebSocketChannel>
- createChannel(const std::string& localIp, const std::string& port);
+ createChannel(const std::string& localIp, const std::string& localPort);
- // from ProtocolFactory
+public: // from ProtocolFactory
virtual void
createFace(const FaceUri& uri,
ndn::nfd::FacePersistency persistency,
const FaceCreatedCallback& onCreated,
- const FaceConnectFailedCallback& onConnectFailed) DECL_OVERRIDE;
+ const FaceCreationFailedCallback& onConnectFailed) DECL_OVERRIDE;
- virtual std::list<shared_ptr<const Channel> >
+ virtual std::vector<shared_ptr<const Channel>>
getChannels() const DECL_OVERRIDE;
private:
@@ -94,16 +91,12 @@
*
* \returns shared pointer to the existing WebSocketChannel object
* or empty shared pointer when such channel does not exist
- *
- * \throws never
*/
shared_ptr<WebSocketChannel>
- findChannel(const websocket::Endpoint& localEndpoint);
+ findChannel(const websocket::Endpoint& endpoint) const;
- typedef std::map< websocket::Endpoint, shared_ptr<WebSocketChannel> > ChannelMap;
- ChannelMap m_channels;
-
- std::string m_defaultPort;
+private:
+ std::map<websocket::Endpoint, shared_ptr<WebSocketChannel>> m_channels;
};
} // namespace nfd
diff --git a/daemon/mgmt/face-manager.cpp b/daemon/mgmt/face-manager.cpp
index 29cac39..6413334 100644
--- a/daemon/mgmt/face-manager.cpp
+++ b/daemon/mgmt/face-manager.cpp
@@ -32,8 +32,8 @@
#include "face/udp-factory.hpp"
#include "fw/face-table.hpp"
-#include <ndn-cxx/management/nfd-face-status.hpp>
#include <ndn-cxx/management/nfd-channel-status.hpp>
+#include <ndn-cxx/management/nfd-face-status.hpp>
#include <ndn-cxx/management/nfd-face-event-notification.hpp>
#ifdef HAVE_UNIX_SOCKETS
@@ -104,7 +104,7 @@
return done(ControlResponse(400, "Non-canonical URI"));
}
- FactoryMap::iterator factory = m_factories.find(uri.getScheme());
+ auto factory = m_factories.find(uri.getScheme());
if (factory == m_factories.end()) {
return done(ControlResponse(501, "Unsupported protocol"));
}
@@ -138,7 +138,7 @@
const shared_ptr<Face>& newFace,
const ndn::mgmt::CommandContinuation& done)
{
- addCreatedFaceToForwarder(newFace);
+ m_faceTable.add(newFace);
parameters.setFaceId(newFace->getId());
parameters.setUri(newFace->getRemoteUri().toString());
parameters.setFacePersistency(newFace->getPersistency());
@@ -240,15 +240,14 @@
result.lpFace = nullptr;
auto face = m_faceTable.get(request.getIncomingFaceId());
- if (!static_cast<bool>(face)) {
- NFD_LOG_DEBUG("command result: faceid " << request.getIncomingFaceId() << " not found");
+ if (face == nullptr) {
+ NFD_LOG_DEBUG("FaceId " << request.getIncomingFaceId() << " not found");
done(ControlResponse(410, "Face not found"));
return result;
}
if (!face->isLocal()) {
- NFD_LOG_DEBUG("command result: cannot enable local control on non-local faceid " <<
- face->getId());
+ NFD_LOG_DEBUG("Cannot enable local control on non-local FaceId " << face->getId());
done(ControlResponse(412, "Face is non-local"));
return result;
}
@@ -260,7 +259,7 @@
BOOST_ASSERT(lpFaceW != nullptr);
result.lpFace = lpFaceW->getLpFace();
}
- result.feature = static_cast<LocalControlFeature>(parameters.getLocalControlFeature());
+ result.feature = parameters.getLocalControlFeature();
return result;
}
@@ -279,22 +278,19 @@
FaceManager::listChannels(const Name& topPrefix, const Interest& interest,
ndn::mgmt::StatusDatasetContext& context)
{
- std::set<shared_ptr<ProtocolFactory>> seenFactories;
+ std::set<const ProtocolFactory*> seenFactories;
- for (auto i = m_factories.begin(); i != m_factories.end(); ++i) {
- const shared_ptr<ProtocolFactory>& factory = i->second;
+ for (const auto& kv : m_factories) {
+ const ProtocolFactory* factory = kv.second.get();
+ bool inserted;
+ std::tie(std::ignore, inserted) = seenFactories.insert(factory);
- if (seenFactories.find(factory) != seenFactories.end()) {
- continue;
- }
- seenFactories.insert(factory);
-
- std::list<shared_ptr<const Channel>> channels = factory->getChannels();
-
- for (auto j = channels.begin(); j != channels.end(); ++j) {
- ndn::nfd::ChannelStatus entry;
- entry.setLocalUri((*j)->getUri().toString());
- context.append(entry.wireEncode());
+ if (inserted) {
+ for (const auto& channel : factory->getChannels()) {
+ ndn::nfd::ChannelStatus entry;
+ entry.setLocalUri(channel->getUri().toString());
+ context.append(entry.wireEncode());
+ }
}
}
@@ -310,9 +306,9 @@
try {
faceFilter.wireDecode(query[-1].blockFromValue());
}
- catch (const tlv::Error&) {
- NFD_LOG_DEBUG("query result: malformed filter");
- return context.reject(ControlResponse(400, "malformed filter"));
+ catch (const tlv::Error& e) {
+ NFD_LOG_DEBUG("Malformed query filter: " << e.what());
+ return context.reject(ControlResponse(400, "Malformed filter"));
}
for (const auto& face : m_faceTable) {
@@ -320,6 +316,7 @@
context.append(face->getFaceStatus().wireEncode());
}
}
+
context.end();
}
@@ -397,8 +394,7 @@
bool hasSeenUdp = false;
bool hasSeenEther = false;
bool hasSeenWebSocket = false;
-
- const std::vector<NetworkInterfaceInfo> nicList(listNetworkInterfaces());
+ auto nicList = listNetworkInterfaces();
for (const auto& item : configSection) {
if (item.first == "unix") {
@@ -457,16 +453,15 @@
// }
#if defined(HAVE_UNIX_SOCKETS)
-
std::string path = "/var/run/nfd.sock";
- for (auto i = configSection.begin(); i != configSection.end(); ++i) {
- if (i->first == "path") {
- path = i->second.get_value<std::string>();
+ for (const auto& i : configSection) {
+ if (i.first == "path") {
+ path = i.second.get_value<std::string>();
}
else {
BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
- i->first + "\" in \"unix\" section"));
+ i.first + "\" in \"unix\" section"));
}
}
@@ -475,14 +470,11 @@
return;
}
- shared_ptr<UnixStreamFactory> factory = make_shared<UnixStreamFactory>();
- shared_ptr<UnixStreamChannel> unixChannel = factory->createChannel(path);
-
- // Should acceptFailed callback be used somehow?
- unixChannel->listen(bind(&FaceTable::add, &m_faceTable, _1),
- UnixStreamChannel::ConnectFailedCallback());
-
+ auto factory = make_shared<UnixStreamFactory>();
m_factories.insert(std::make_pair("unix", factory));
+
+ auto channel = factory->createChannel(path);
+ channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
}
#else
BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without Unix sockets support, "
@@ -500,40 +492,33 @@
// port 6363 ; TCP listener port number
// }
- std::string port = "6363";
+ uint16_t port = 6363;
bool needToListen = true;
bool enableV4 = true;
bool enableV6 = true;
- for (auto i = configSection.begin(); i != configSection.end(); ++i) {
- if (i->first == "port") {
- port = i->second.get_value<std::string>();
- try {
- uint16_t portNo = boost::lexical_cast<uint16_t>(port);
- NFD_LOG_TRACE("TCP port set to " << portNo);
- }
- catch (const std::bad_cast& error) {
- BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option " +
- i->first + "\" in \"tcp\" section"));
- }
+ for (const auto& i : configSection) {
+ if (i.first == "port") {
+ port = ConfigFile::parseNumber<uint16_t>(i, "tcp");
+ NFD_LOG_TRACE("TCP port set to " << port);
}
- else if (i->first == "listen") {
- needToListen = ConfigFile::parseYesNo(i, i->first, "tcp");
+ else if (i.first == "listen") {
+ needToListen = ConfigFile::parseYesNo(i, "tcp");
}
- else if (i->first == "enable_v4") {
- enableV4 = ConfigFile::parseYesNo(i, i->first, "tcp");
+ else if (i.first == "enable_v4") {
+ enableV4 = ConfigFile::parseYesNo(i, "tcp");
}
- else if (i->first == "enable_v6") {
- enableV6 = ConfigFile::parseYesNo(i, i->first, "tcp");
+ else if (i.first == "enable_v6") {
+ enableV6 = ConfigFile::parseYesNo(i, "tcp");
}
else {
BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
- i->first + "\" in \"tcp\" section"));
+ i.first + "\" in \"tcp\" section"));
}
}
if (!enableV4 && !enableV6) {
- BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 channels have been disabled."
+ BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 TCP channels have been disabled."
" Remove \"tcp\" section to disable TCP channels or"
" re-enable at least one channel type."));
}
@@ -543,26 +528,24 @@
return;
}
- shared_ptr<TcpFactory> factory = make_shared<TcpFactory>(port);
+ auto factory = make_shared<TcpFactory>();
m_factories.insert(std::make_pair("tcp", factory));
if (enableV4) {
- shared_ptr<TcpChannel> ipv4Channel = factory->createChannel("0.0.0.0", port);
+ tcp::Endpoint endpoint(boost::asio::ip::tcp::v4(), port);
+ shared_ptr<TcpChannel> v4Channel = factory->createChannel(endpoint);
if (needToListen) {
- // Should acceptFailed callback be used somehow?
- ipv4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1),
- TcpChannel::ConnectFailedCallback());
+ v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
}
m_factories.insert(std::make_pair("tcp4", factory));
}
if (enableV6) {
- shared_ptr<TcpChannel> ipv6Channel = factory->createChannel("::", port);
+ tcp::Endpoint endpoint(boost::asio::ip::tcp::v6(), port);
+ shared_ptr<TcpChannel> v6Channel = factory->createChannel(endpoint);
if (needToListen) {
- // Should acceptFailed callback be used somehow?
- ipv6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1),
- TcpChannel::ConnectFailedCallback());
+ v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
}
m_factories.insert(std::make_pair("tcp6", factory));
@@ -571,8 +554,7 @@
}
void
-FaceManager::processSectionUdp(const ConfigSection& configSection,
- bool isDryRun,
+FaceManager::processSectionUdp(const ConfigSection& configSection, bool isDryRun,
const std::vector<NetworkInterfaceInfo>& nicList)
{
// ; the udp section contains settings of UDP faces and channels
@@ -588,93 +570,70 @@
// mcast_group 224.0.23.170 ; UDP multicast group (IPv4 only)
// }
- std::string port = "6363";
+ uint16_t port = 6363;
bool enableV4 = true;
bool enableV6 = true;
size_t timeout = 600;
size_t keepAliveInterval = 25;
bool useMcast = true;
- std::string mcastGroup = "224.0.23.170";
- std::string mcastPort = "56363";
+ auto mcastGroup = boost::asio::ip::address_v4::from_string("224.0.23.170");
+ uint16_t mcastPort = 56363;
-
- for (auto i = configSection.begin(); i != configSection.end(); ++i) {
- if (i->first == "port") {
- port = i->second.get_value<std::string>();
+ for (const auto& i : configSection) {
+ if (i.first == "port") {
+ port = ConfigFile::parseNumber<uint16_t>(i, "udp");
+ NFD_LOG_TRACE("UDP unicast port set to " << port);
+ }
+ else if (i.first == "enable_v4") {
+ enableV4 = ConfigFile::parseYesNo(i, "udp");
+ }
+ else if (i.first == "enable_v6") {
+ enableV6 = ConfigFile::parseYesNo(i, "udp");
+ }
+ else if (i.first == "idle_timeout") {
try {
- uint16_t portNo = boost::lexical_cast<uint16_t>(port);
- NFD_LOG_TRACE("UDP port set to " << portNo);
+ timeout = i.second.get_value<size_t>();
}
- catch (const std::bad_cast& error) {
- BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option " +
- i->first + "\" in \"udp\" section"));
- }
- }
- else if (i->first == "enable_v4") {
- enableV4 = ConfigFile::parseYesNo(i, i->first, "udp");
- }
- else if (i->first == "enable_v6") {
- enableV6 = ConfigFile::parseYesNo(i, i->first, "udp");
- }
- else if (i->first == "idle_timeout") {
- try {
- timeout = i->second.get_value<size_t>();
- }
- catch (const std::exception& e) {
+ catch (const boost::property_tree::ptree_bad_data&) {
BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
- i->first + "\" in \"udp\" section"));
+ i.first + "\" in \"udp\" section"));
}
}
- else if (i->first == "keep_alive_interval") {
+ else if (i.first == "keep_alive_interval") {
try {
- keepAliveInterval = i->second.get_value<size_t>();
-
+ keepAliveInterval = i.second.get_value<size_t>();
/// \todo Make use of keepAliveInterval
- /// \todo what is keep alive interval used for?
(void)(keepAliveInterval);
}
- catch (const std::exception& e) {
+ catch (const boost::property_tree::ptree_bad_data&) {
BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
- i->first + "\" in \"udp\" section"));
+ i.first + "\" in \"udp\" section"));
}
}
- else if (i->first == "mcast") {
- useMcast = ConfigFile::parseYesNo(i, i->first, "udp");
+ else if (i.first == "mcast") {
+ useMcast = ConfigFile::parseYesNo(i, "udp");
}
- else if (i->first == "mcast_port") {
- mcastPort = i->second.get_value<std::string>();
- try {
- uint16_t portNo = boost::lexical_cast<uint16_t>(mcastPort);
- NFD_LOG_TRACE("UDP multicast port set to " << portNo);
- }
- catch (const std::bad_cast& error) {
- BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option " +
- i->first + "\" in \"udp\" section"));
- }
+ else if (i.first == "mcast_port") {
+ mcastPort = ConfigFile::parseNumber<uint16_t>(i, "udp");
+ NFD_LOG_TRACE("UDP multicast port set to " << mcastPort);
}
- else if (i->first == "mcast_group") {
- using namespace boost::asio::ip;
- mcastGroup = i->second.get_value<std::string>();
- try {
- address mcastGroupTest = address::from_string(mcastGroup);
- if (!mcastGroupTest.is_v4()) {
- BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
- i->first + "\" in \"udp\" section"));
- }
- }
- catch(const std::runtime_error& e) {
+ else if (i.first == "mcast_group") {
+ boost::system::error_code ec;
+ mcastGroup = boost::asio::ip::address_v4::from_string(i.second.get_value<std::string>(), ec);
+ if (ec) {
BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
- i->first + "\" in \"udp\" section"));
+ i.first + "\" in \"udp\" section"));
}
+ NFD_LOG_TRACE("UDP multicast group set to " << mcastGroup);
}
else {
BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
- i->first + "\" in \"udp\" section"));
+ i.first + "\" in \"udp\" section"));
}
}
if (!enableV4 && !enableV6) {
- BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 channels have been disabled."
+ BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 UDP channels have been disabled."
" Remove \"udp\" section to disable UDP channels or"
" re-enable at least one channel type."));
}
@@ -691,29 +650,31 @@
factory = static_pointer_cast<UdpFactory>(m_factories["udp"]);
}
else {
- factory = make_shared<UdpFactory>(port);
+ factory = make_shared<UdpFactory>();
m_factories.insert(std::make_pair("udp", factory));
}
if (!isReload && enableV4) {
- shared_ptr<UdpChannel> v4Channel =
- factory->createChannel("0.0.0.0", port, time::seconds(timeout));
-
- v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1),
- UdpChannel::ConnectFailedCallback());
+ udp::Endpoint endpoint(boost::asio::ip::udp::v4(), port);
+ shared_ptr<UdpChannel> v4Channel = factory->createChannel(endpoint, time::seconds(timeout));
+ v4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
m_factories.insert(std::make_pair("udp4", factory));
}
if (!isReload && enableV6) {
- shared_ptr<UdpChannel> v6Channel =
- factory->createChannel("::", port, time::seconds(timeout));
+ udp::Endpoint endpoint(boost::asio::ip::udp::v6(), port);
+ shared_ptr<UdpChannel> v6Channel = factory->createChannel(endpoint, time::seconds(timeout));
+ v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1), nullptr);
- v6Channel->listen(bind(&FaceTable::add, &m_faceTable, _1),
- UdpChannel::ConnectFailedCallback());
m_factories.insert(std::make_pair("udp6", factory));
}
+ std::set<shared_ptr<face::LpFaceWrapper>> multicastFacesToRemove;
+ for (const auto& i : factory->getMulticastFaces()) {
+ multicastFacesToRemove.insert(i.second);
+ }
+
if (useMcast && enableV4) {
std::vector<NetworkInterfaceInfo> ipv4MulticastInterfaces;
for (const auto& nic : nicList) {
@@ -731,43 +692,24 @@
}
#endif
- std::list<shared_ptr<face::LpFaceWrapper>> multicastFacesToRemove;
- for (auto i = factory->getMulticastFaces().begin();
- i != factory->getMulticastFaces().end();
- ++i) {
- multicastFacesToRemove.push_back(i->second);
- }
-
+ udp::Endpoint mcastEndpoint(mcastGroup, mcastPort);
for (const auto& nic : ipv4MulticastInterfaces) {
- auto newFace = factory->createMulticastFace(nic.ipv4Addresses[0].to_string(),
- mcastGroup, mcastPort,
+ udp::Endpoint localEndpoint(nic.ipv4Addresses[0], mcastPort);
+ auto newFace = factory->createMulticastFace(localEndpoint, mcastEndpoint,
isNicNameNecessary ? nic.name : "");
- addCreatedFaceToForwarder(newFace);
- multicastFacesToRemove.remove(newFace);
- }
-
- for (const auto& face : multicastFacesToRemove) {
- face->close();
+ m_faceTable.add(newFace);
+ multicastFacesToRemove.erase(newFace);
}
}
- else {
- std::list<shared_ptr<face::LpFaceWrapper>> multicastFacesToRemove;
- for (auto i = factory->getMulticastFaces().begin();
- i != factory->getMulticastFaces().end();
- ++i) {
- multicastFacesToRemove.push_back(i->second);
- }
- for (const auto& face : multicastFacesToRemove) {
- face->close();
- }
+ for (const auto& face : multicastFacesToRemove) {
+ face->close();
}
}
}
void
-FaceManager::processSectionEther(const ConfigSection& configSection,
- bool isDryRun,
+FaceManager::processSectionEther(const ConfigSection& configSection, bool isDryRun,
const std::vector<NetworkInterfaceInfo>& nicList)
{
// ; the ether section contains settings of Ethernet faces and channels
@@ -782,20 +724,21 @@
bool useMcast = true;
ethernet::Address mcastGroup(ethernet::getDefaultMulticastAddress());
- for (auto i = configSection.begin(); i != configSection.end(); ++i) {
- if (i->first == "mcast") {
- useMcast = ConfigFile::parseYesNo(i, i->first, "ether");
+ for (const auto& i : configSection) {
+ if (i.first == "mcast") {
+ useMcast = ConfigFile::parseYesNo(i, "ether");
}
- else if (i->first == "mcast_group") {
- mcastGroup = ethernet::Address::fromString(i->second.get_value<std::string>());
+ else if (i.first == "mcast_group") {
+ mcastGroup = ethernet::Address::fromString(i.second.get_value<std::string>());
if (mcastGroup.isNull()) {
BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
- i->first + "\" in \"ether\" section"));
+ i.first + "\" in \"ether\" section"));
}
+ NFD_LOG_TRACE("Ethernet multicast group set to " << mcastGroup);
}
else {
BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
- i->first + "\" in \"ether\" section"));
+ i.first + "\" in \"ether\" section"));
}
}
@@ -809,22 +752,18 @@
m_factories.insert(std::make_pair("ether", factory));
}
- if (useMcast) {
- std::list<shared_ptr<EthernetFace> > multicastFacesToRemove;
- for (auto i = factory->getMulticastFaces().begin();
- i != factory->getMulticastFaces().end();
- ++i) {
- multicastFacesToRemove.push_back(i->second);
- }
+ std::set<shared_ptr<EthernetFace>> multicastFacesToRemove;
+ for (const auto& i : factory->getMulticastFaces()) {
+ multicastFacesToRemove.insert(i.second);
+ }
+ if (useMcast) {
for (const auto& nic : nicList) {
if (nic.isUp() && nic.isMulticastCapable()) {
try {
- shared_ptr<EthernetFace> newFace =
- factory->createMulticastFace(nic, mcastGroup);
-
- addCreatedFaceToForwarder(newFace);
- multicastFacesToRemove.remove(newFace);
+ auto newFace = factory->createMulticastFace(nic, mcastGroup);
+ m_faceTable.add(newFace);
+ multicastFacesToRemove.erase(newFace);
}
catch (const EthernetFactory::Error& factoryError) {
NFD_LOG_ERROR(factoryError.what() << ", continuing");
@@ -834,26 +773,10 @@
}
}
}
-
- for (auto i = multicastFacesToRemove.begin();
- i != multicastFacesToRemove.end();
- ++i) {
- (*i)->close();
- }
}
- else {
- std::list<shared_ptr<EthernetFace> > multicastFacesToRemove;
- for (auto i = factory->getMulticastFaces().begin();
- i != factory->getMulticastFaces().end();
- ++i) {
- multicastFacesToRemove.push_back(i->second);
- }
- for (auto i = multicastFacesToRemove.begin();
- i != multicastFacesToRemove.end();
- ++i) {
- (*i)->close();
- }
+ for (const auto& face : multicastFacesToRemove) {
+ face->close();
}
}
#else
@@ -874,41 +797,33 @@
// }
#if defined(HAVE_WEBSOCKET)
-
- std::string port = "9696";
+ uint16_t port = 9696;
bool needToListen = true;
bool enableV4 = true;
bool enableV6 = true;
- for (auto i = configSection.begin(); i != configSection.end(); ++i) {
- if (i->first == "port") {
- port = i->second.get_value<std::string>();
- try {
- uint16_t portNo = boost::lexical_cast<uint16_t>(port);
- NFD_LOG_TRACE("WebSocket port set to " << portNo);
- }
- catch (const std::bad_cast& error) {
- BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option " +
- i->first + "\" in \"websocket\" section"));
- }
+ for (const auto& i : configSection) {
+ if (i.first == "port") {
+ port = ConfigFile::parseNumber<uint16_t>(i, "websocket");
+ NFD_LOG_TRACE("WebSocket port set to " << port);
}
- else if (i->first == "listen") {
- needToListen = ConfigFile::parseYesNo(i, i->first, "websocket");
+ else if (i.first == "listen") {
+ needToListen = ConfigFile::parseYesNo(i, "websocket");
}
- else if (i->first == "enable_v4") {
- enableV4 = ConfigFile::parseYesNo(i, i->first, "websocket");
+ else if (i.first == "enable_v4") {
+ enableV4 = ConfigFile::parseYesNo(i, "websocket");
}
- else if (i->first == "enable_v6") {
- enableV6 = ConfigFile::parseYesNo(i, i->first, "websocket");
+ else if (i.first == "enable_v6") {
+ enableV6 = ConfigFile::parseYesNo(i, "websocket");
}
else {
BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" +
- i->first + "\" in \"websocket\" section"));
+ i.first + "\" in \"websocket\" section"));
}
}
if (!enableV4 && !enableV6) {
- BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 channels have been disabled."
+ BOOST_THROW_EXCEPTION(ConfigFile::Error("IPv4 and IPv6 WebSocket channels have been disabled."
" Remove \"websocket\" section to disable WebSocket channels or"
" re-enable at least one channel type."));
}
@@ -922,25 +837,27 @@
return;
}
- shared_ptr<WebSocketFactory> factory = make_shared<WebSocketFactory>(port);
+ auto factory = make_shared<WebSocketFactory>();
m_factories.insert(std::make_pair("websocket", factory));
+ shared_ptr<WebSocketChannel> channel;
+
if (enableV6 && enableV4) {
- shared_ptr<WebSocketChannel> ip46Channel = factory->createChannel("::", port);
- if (needToListen) {
- ip46Channel->listen(bind(&FaceTable::add, &m_faceTable, _1));
- }
+ websocket::Endpoint endpoint(boost::asio::ip::address_v6::any(), port);
+ channel = factory->createChannel(endpoint);
m_factories.insert(std::make_pair("websocket46", factory));
}
else if (enableV4) {
- shared_ptr<WebSocketChannel> ipv4Channel = factory->createChannel("0.0.0.0", port);
- if (needToListen) {
- ipv4Channel->listen(bind(&FaceTable::add, &m_faceTable, _1));
- }
+ websocket::Endpoint endpoint(boost::asio::ip::address_v4::any(), port);
+ channel = factory->createChannel(endpoint);
m_factories.insert(std::make_pair("websocket4", factory));
}
+
+ if (channel && needToListen) {
+ channel->listen(bind(&FaceTable::add, &m_faceTable, _1));
+ }
}
#else
BOOST_THROW_EXCEPTION(ConfigFile::Error("NFD was compiled without WebSocket, "
@@ -948,10 +865,4 @@
#endif // HAVE_WEBSOCKET
}
-void
-FaceManager::addCreatedFaceToForwarder(shared_ptr<Face> newFace)
-{
- m_faceTable.add(newFace);
-}
-
-} // namespace
+} // namespace nfd
diff --git a/daemon/mgmt/face-manager.hpp b/daemon/mgmt/face-manager.hpp
index e3c955f..57788b7 100644
--- a/daemon/mgmt/face-manager.hpp
+++ b/daemon/mgmt/face-manager.hpp
@@ -129,7 +129,8 @@
private: // configuration
void
- processConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename);
+ processConfig(const ConfigSection& configSection, bool isDryRun,
+ const std::string& filename);
void
processSectionUnix(const ConfigSection& configSection, bool isDryRun);
@@ -138,25 +139,18 @@
processSectionTcp(const ConfigSection& configSection, bool isDryRun);
void
- processSectionUdp(const ConfigSection& configSection,
- bool isDryRun,
+ processSectionUdp(const ConfigSection& configSection, bool isDryRun,
const std::vector<NetworkInterfaceInfo>& nicList);
void
- processSectionEther(const ConfigSection& configSection,
- bool isDryRun,
+ processSectionEther(const ConfigSection& configSection, bool isDryRun,
const std::vector<NetworkInterfaceInfo>& nicList);
void
processSectionWebSocket(const ConfigSection& configSection, bool isDryRun);
-private: // helpers for configuration
- void
- addCreatedFaceToForwarder(shared_ptr<Face> newFace);
-
PUBLIC_WITH_TESTS_ELSE_PRIVATE:
- typedef std::map<std::string/*protocol*/, shared_ptr<ProtocolFactory>> FactoryMap;
- FactoryMap m_factories;
+ std::map<std::string /*protocol*/, shared_ptr<ProtocolFactory>> m_factories;
private:
FaceTable& m_faceTable;
diff --git a/tests/daemon/face/udp.t.cpp b/tests/daemon/face/udp.t.cpp
index 6869176..86c9bfe 100644
--- a/tests/daemon/face/udp.t.cpp
+++ b/tests/daemon/face/udp.t.cpp
@@ -44,22 +44,16 @@
UdpFactory factory;
BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
- std::vector<shared_ptr<const Channel> > expectedChannels;
-
+ std::vector<shared_ptr<const Channel>> expectedChannels;
expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
expectedChannels.push_back(factory.createChannel("::1", "20071"));
- std::list<shared_ptr<const Channel> > channels = factory.getChannels();
- for (std::list<shared_ptr<const Channel> >::const_iterator i = channels.begin();
- i != channels.end(); ++i)
- {
- std::vector<shared_ptr<const Channel> >::iterator pos =
- std::find(expectedChannels.begin(), expectedChannels.end(), *i);
-
- BOOST_REQUIRE(pos != expectedChannels.end());
- expectedChannels.erase(pos);
- }
+ for (const auto& i : factory.getChannels()) {
+ auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), i);
+ BOOST_REQUIRE(pos != expectedChannels.end());
+ expectedChannels.erase(pos);
+ }
BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
}
diff --git a/tests/daemon/face/websocket.t.cpp b/tests/daemon/face/websocket.t.cpp
index d3da744..a1d8a52 100644
--- a/tests/daemon/face/websocket.t.cpp
+++ b/tests/daemon/face/websocket.t.cpp
@@ -42,32 +42,26 @@
BOOST_AUTO_TEST_CASE(GetChannels)
{
- WebSocketFactory factory("19596");
+ WebSocketFactory factory;
BOOST_REQUIRE_EQUAL(factory.getChannels().empty(), true);
- std::vector<shared_ptr<const Channel> > expectedChannels;
-
+ std::vector<shared_ptr<const Channel>> expectedChannels;
expectedChannels.push_back(factory.createChannel("127.0.0.1", "20070"));
expectedChannels.push_back(factory.createChannel("127.0.0.1", "20071"));
expectedChannels.push_back(factory.createChannel("::1", "20071"));
- std::list<shared_ptr<const Channel> > channels = factory.getChannels();
- for (std::list<shared_ptr<const Channel> >::const_iterator i = channels.begin();
- i != channels.end(); ++i)
- {
- std::vector<shared_ptr<const Channel> >::iterator pos =
- std::find(expectedChannels.begin(), expectedChannels.end(), *i);
-
- BOOST_REQUIRE(pos != expectedChannels.end());
- expectedChannels.erase(pos);
- }
+ for (const auto& i : factory.getChannels()) {
+ auto pos = std::find(expectedChannels.begin(), expectedChannels.end(), i);
+ BOOST_REQUIRE(pos != expectedChannels.end());
+ expectedChannels.erase(pos);
+ }
BOOST_CHECK_EQUAL(expectedChannels.size(), 0);
}
BOOST_AUTO_TEST_CASE(UnsupportedFaceCreate)
{
- WebSocketFactory factory("19596");
+ WebSocketFactory factory;
BOOST_CHECK_THROW(factory.createFace(FaceUri("ws://127.0.0.1:20070"),
ndn::nfd::FACE_PERSISTENCY_PERMANENT,
@@ -226,7 +220,7 @@
BOOST_FIXTURE_TEST_CASE(EndToEnd4, EndToEndFixture)
{
- WebSocketFactory factory1("9696");
+ WebSocketFactory factory1;
shared_ptr<WebSocketChannel> channel1 = factory1.createChannel("127.0.0.1", "20070");
channel1->setPingInterval(time::milliseconds(3000));
diff --git a/tests/daemon/mgmt/face-manager.t.cpp b/tests/daemon/mgmt/face-manager.t.cpp
index db24029..b8944b4 100644
--- a/tests/daemon/mgmt/face-manager.t.cpp
+++ b/tests/daemon/mgmt/face-manager.t.cpp
@@ -370,7 +370,7 @@
BOOST_CHECK_NO_THROW(status.wireDecode(content.elements()[1]));
BOOST_CHECK_EQUAL(face3->getId(), status.getFaceId());
- ControlResponse expectedResponse(400, "malformed filter"); // nack, 400, malformed filter
+ ControlResponse expectedResponse(400, "Malformed filter"); // nack, 400, malformed filter
BOOST_CHECK_EQUAL(checkResponse(3, invalidQueryName, expectedResponse, tlv::ContentType_Nack),
CheckResponseResult::OK);
}
@@ -391,11 +391,11 @@
createFace(const FaceUri& uri,
ndn::nfd::FacePersistency persistency,
const FaceCreatedCallback& onCreated,
- const FaceConnectFailedCallback& onConnectFailed) DECL_OVERRIDE
+ const FaceCreationFailedCallback& onConnectFailed) DECL_OVERRIDE
{
}
- virtual std::list<shared_ptr<const Channel>>
+ virtual std::vector<shared_ptr<const Channel>>
getChannels() const DECL_OVERRIDE
{
return m_channels;
@@ -411,7 +411,7 @@
}
private:
- std::list<shared_ptr<const Channel> > m_channels;
+ std::vector<shared_ptr<const Channel>> m_channels;
};
BOOST_AUTO_TEST_CASE(ChannelDataset)