face+mgmt: simplify factories and FaceManager using C++11 features

Change-Id: I113a4c15ef82bff705f61f31b170f49727bc3794
Refs: #3258
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