faces: Channel base class

refs #1261

Change-Id: If17037c4802cb6ccec0201c3bff7fc4b6db41c6a
diff --git a/daemon/core/face-uri.cpp b/daemon/core/face-uri.cpp
index a30bb7b..0a3f1d4 100644
--- a/daemon/core/face-uri.cpp
+++ b/daemon/core/face-uri.cpp
@@ -14,49 +14,81 @@
 
 FaceUri::FaceUri(const std::string& uri)
 {
-  if (!parse(uri))
-    {
-      throw Error("Malformed URI: " + uri);
-    }
+  if (!parse(uri)) {
+    throw Error("Malformed URI: " + uri);
+  }
+}
+
+FaceUri::FaceUri(const char* uri)
+{
+  if (!parse(uri)) {
+    throw Error("Malformed URI: " + std::string(uri));
+  }
 }
 
 bool
 FaceUri::parse(const std::string& uri)
 {
   m_scheme.clear();
-  m_domain.clear();
+  m_host.clear();
+  m_isV6 = false;
   m_port.clear();
-  
-  boost::regex protocolExp("(\\w+\\d?)://(.+)");
+  m_path.clear();
+
+  boost::regex protocolExp("(\\w+\\d?)://([^/]*)(\\/[^?]*)?");
   boost::smatch protocolMatch;
-  if (!boost::regex_match(uri, protocolMatch, protocolExp))
-    {
-      return false;
-    }
+  if (!boost::regex_match(uri, protocolMatch, protocolExp)) {
+    return false;
+  }
   m_scheme = protocolMatch[1];
+  m_path = protocolMatch[3];
 
-  const std::string& remote = protocolMatch[2];
+  const std::string& authority = protocolMatch[2];
 
-  boost::regex v6Exp("^\\[(([a-fA-F0-9:]+))\\](:(\\d+))?$"); // [stuff]:port
+  boost::regex v6Exp("^\\[(([a-fA-F0-9:]+))\\](:(\\d+))?$"); // [host]:port
   boost::regex v4Exp("^((\\d+\\.){3}\\d+)(:(\\d+))?$");
-  boost::regex hostExp("^(([^:]+))(:(\\d+))?$"); // stuff:port
+  boost::regex hostExp("^(([^:]+))(:(\\d+))?$"); // host:port
 
   boost::smatch match;
-  if (boost::regex_match(remote, match, v6Exp) ||
-      boost::regex_match(remote, match, v4Exp) ||
-      boost::regex_match(remote, match, hostExp))
-    {
-      m_domain = match[1];
-      m_port   = match[4];
-    }
-  else
-    {
+  m_isV6 = boost::regex_match(authority, match, v6Exp);
+  if (m_isV6 ||
+      boost::regex_match(authority, match, v4Exp) ||
+      boost::regex_match(authority, match, hostExp)) {
+    m_host = match[1];
+    m_port = match[4];
+  }
+  else {
+    if (m_path.empty()) {
       return false;
     }
-  
-  NFD_LOG_DEBUG("URI [" << uri << "] parsed into: "
-                << m_scheme << ", " << m_domain << ", " << m_port);
+  }
+
+  NFD_LOG_DEBUG("URI [" << uri << "] parsed into: " <<
+                m_scheme << ", " << m_host << ", " << m_port << ", " << m_path);
   return true;
 }
 
+FaceUri::FaceUri(const boost::asio::ip::tcp::endpoint& endpoint)
+{
+  m_isV6 = endpoint.address().is_v6();
+  m_scheme = m_isV6 ? "tcp6" : "tcp4";
+  m_host = endpoint.address().to_string();
+  m_port = boost::lexical_cast<std::string>(endpoint.port());
+}
+
+FaceUri::FaceUri(const boost::asio::ip::udp::endpoint& endpoint)
+{
+  m_isV6 = endpoint.address().is_v6();
+  m_scheme = m_isV6 ? "udp6" : "udp4";
+  m_host = endpoint.address().to_string();
+  m_port = boost::lexical_cast<std::string>(endpoint.port());
+}
+
+FaceUri::FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint)
+  : m_isV6(false)
+{
+  m_scheme = "unix";
+  m_path = endpoint.path();
+}
+
 } // namespace nfd
diff --git a/daemon/core/face-uri.hpp b/daemon/core/face-uri.hpp
index 91c7a30..29647bb 100644
--- a/daemon/core/face-uri.hpp
+++ b/daemon/core/face-uri.hpp
@@ -11,6 +11,7 @@
 
 namespace nfd {
 
+/// represents a URI in Face Management protocol
 class FaceUri
 {
 public:
@@ -21,44 +22,62 @@
   };
 
   FaceUri();
-  
-  /** \brief Construct URI object
+
+  /** \brief construct by parsing
    *
-   * Expected format: scheme://domain:port/path?query_string#fragment_id
-   *
+   * \param uri scheme://domain[:port]/path
    * \throw FaceUri::Error if URI cannot be parsed
    */
   explicit
   FaceUri(const std::string& uri);
 
-  /** \brief Exception-safe parsing of URI
-   */
+  // This overload is needed so that calls with string literal won't be
+  // resolved to boost::asio::local::stream_protocol::endpoint overload.
+  explicit
+  FaceUri(const char* uri);
+
+  explicit
+  FaceUri(const boost::asio::ip::tcp::endpoint& endpoint);
+
+  explicit
+  FaceUri(const boost::asio::ip::udp::endpoint& endpoint);
+
+  explicit
+  FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint);
+
+  /// exception-safe parsing
   bool
   parse(const std::string& uri);
 
-  /** \brief Get scheme (protocol) extracted from URI
-   */
+  /// get scheme (protocol)
   const std::string&
   getScheme() const;
 
-  /** \brief Get domain extracted from URI
-   */
+  /// get host (domain)
   const std::string&
-  getDomain() const;
+  getHost() const;
 
-  /** \brief Get port extracted from URI
-   *
-   *  \return Extracted port or empty string if port wasn't present
-   */
+  /// get port
   const std::string&
   getPort() const;
 
-  // other getters should be added, when necessary
+  /// get path
+  const std::string&
+  getPath() const;
+
+  /// write as a string
+  std::string
+  toString() const;
 
 private:
   std::string m_scheme;
-  std::string m_domain;
+  std::string m_host;
+  /// whether to add [] around host when writing string
+  bool m_isV6;
   std::string m_port;
+  std::string m_path;
+  
+  friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri);
 };
 
 inline
@@ -73,9 +92,9 @@
 }
 
 inline const std::string&
-FaceUri::getDomain() const
+FaceUri::getHost() const
 {
-  return m_domain;
+  return m_host;
 }
 
 inline const std::string&
@@ -84,6 +103,37 @@
   return m_port;
 }
 
+inline const std::string&
+FaceUri::getPath() const
+{
+  return m_path;
+}
+
+inline std::string
+FaceUri::toString() const
+{
+  std::ostringstream os;
+  os << *this;
+  return os.str();
+}
+
+inline std::ostream&
+operator<<(std::ostream& os, const FaceUri& uri)
+{
+  os << uri.m_scheme << "://";
+  if (uri.m_isV6) {
+    os << "[" << uri.m_host << "]";
+  }
+  else {
+    os << uri.m_host;
+  }
+  if (!uri.m_port.empty()) {
+    os << ":" << uri.m_port;
+  }
+  os << uri.m_path;
+  return os;
+}
+
 } // namespace nfd
 
 #endif // NFD_CORE_FACE_URI_H
diff --git a/daemon/face/channel.cpp b/daemon/face/channel.cpp
new file mode 100644
index 0000000..e862cfe
--- /dev/null
+++ b/daemon/face/channel.cpp
@@ -0,0 +1,21 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "channel.hpp"
+
+namespace nfd {
+
+Channel::~Channel()
+{
+}
+
+void
+Channel::setUri(const FaceUri& uri)
+{
+  m_uri = uri;
+}
+
+} // namespace nfd
diff --git a/daemon/face/channel.hpp b/daemon/face/channel.hpp
new file mode 100644
index 0000000..5993363
--- /dev/null
+++ b/daemon/face/channel.hpp
@@ -0,0 +1,50 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NFD_FACE_CHANNEL_HPP
+#define NFD_FACE_CHANNEL_HPP
+
+#include "face.hpp"
+
+namespace nfd {
+
+class Channel : noncopyable
+{
+public:
+  /** \brief Prototype for the callback called when 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
+   */
+  typedef function<void(const std::string& reason)> ConnectFailedCallback;
+
+  virtual
+  ~Channel();
+
+  const FaceUri&
+  getUri() const;
+
+protected:
+  void
+  setUri(const FaceUri& uri);
+
+private:
+  FaceUri m_uri;
+};
+
+inline const FaceUri&
+Channel::getUri() const
+{
+  return m_uri;
+}
+
+} // namespace nfd
+
+#endif // NFD_FACE_CHANNEL_HPP
diff --git a/daemon/face/face.hpp b/daemon/face/face.hpp
index 4c48860..f20c37b 100644
--- a/daemon/face/face.hpp
+++ b/daemon/face/face.hpp
@@ -9,6 +9,7 @@
 
 #include "common.hpp"
 #include "core/event-emitter.hpp"
+#include "core/face-uri.hpp"
 #include "face-counter.hpp"
 
 namespace nfd {
diff --git a/daemon/face/tcp-channel.cpp b/daemon/face/tcp-channel.cpp
index 246cdb0..616ca62 100644
--- a/daemon/face/tcp-channel.cpp
+++ b/daemon/face/tcp-channel.cpp
@@ -5,6 +5,8 @@
  */
 
 #include "tcp-channel.hpp"
+#include "core/global-io.hpp"
+#include "core/face-uri.hpp"
 
 namespace nfd {
 
@@ -12,10 +14,13 @@
 
 using namespace boost::asio;
 
-TcpChannel::TcpChannel(io_service& ioService,
-                       const tcp::Endpoint& localEndpoint)
-  : m_ioService(ioService)
-  , m_localEndpoint(localEndpoint)
+TcpChannel::TcpChannel(const tcp::Endpoint& localEndpoint)
+  : m_localEndpoint(localEndpoint)
+{
+  this->setUri(FaceUri(localEndpoint));
+}
+
+TcpChannel::~TcpChannel()
 {
 }
 
@@ -24,14 +29,14 @@
                    const ConnectFailedCallback& onAcceptFailed,
                    int backlog/* = tcp::acceptor::max_connections*/)
 {
-  m_acceptor = make_shared<ip::tcp::acceptor>(boost::ref(m_ioService));
+  m_acceptor = make_shared<ip::tcp::acceptor>(boost::ref(getGlobalIoService()));
   m_acceptor->open(m_localEndpoint.protocol());
   m_acceptor->set_option(ip::tcp::acceptor::reuse_address(true));
   m_acceptor->bind(m_localEndpoint);
   m_acceptor->listen(backlog);
 
   shared_ptr<ip::tcp::socket> clientSocket =
-    make_shared<ip::tcp::socket>(boost::ref(m_ioService));
+    make_shared<ip::tcp::socket>(boost::ref(getGlobalIoService()));
   m_acceptor->async_accept(*clientSocket,
                            bind(&TcpChannel::handleSuccessfulAccept, this, _1,
                                 clientSocket,
@@ -51,10 +56,10 @@
   }
 
   shared_ptr<ip::tcp::socket> clientSocket =
-    make_shared<ip::tcp::socket>(boost::ref(m_ioService));
+    make_shared<ip::tcp::socket>(boost::ref(getGlobalIoService()));
 
   shared_ptr<monotonic_deadline_timer> connectTimeoutTimer =
-    make_shared<monotonic_deadline_timer>(boost::ref(m_ioService));
+    make_shared<monotonic_deadline_timer>(boost::ref(getGlobalIoService()));
 
   clientSocket->open(m_localEndpoint.protocol());
 
@@ -82,10 +87,10 @@
                     const time::Duration& timeout/* = time::seconds(4)*/)
 {
   shared_ptr<ip::tcp::socket> clientSocket =
-    make_shared<ip::tcp::socket>(boost::ref(m_ioService));
+    make_shared<ip::tcp::socket>(boost::ref(getGlobalIoService()));
 
   shared_ptr<monotonic_deadline_timer> connectTimeoutTimer =
-    make_shared<monotonic_deadline_timer>(boost::ref(m_ioService));
+    make_shared<monotonic_deadline_timer>(boost::ref(getGlobalIoService()));
 
   clientSocket->open(m_localEndpoint.protocol());
 
@@ -97,7 +102,7 @@
 
   ip::tcp::resolver::query query(remoteHost, remotePort);
   shared_ptr<ip::tcp::resolver> resolver =
-    make_shared<ip::tcp::resolver>(boost::ref(m_ioService));
+    make_shared<ip::tcp::resolver>(boost::ref(getGlobalIoService()));
 
   resolver->async_resolve(query,
                           bind(&TcpChannel::handleEndpointResolution, this, _1, _2,
@@ -128,7 +133,7 @@
     face = make_shared<TcpLocalFace>(boost::cref(socket));
   else
     face = make_shared<TcpFace>(boost::cref(socket));
-  
+
   face->onFail += bind(&TcpChannel::afterFaceFailed, this, remoteEndpoint);
 
   onFaceCreated(face);
@@ -163,7 +168,7 @@
 
   // prepare accepting the next connection
   shared_ptr<ip::tcp::socket> clientSocket =
-    make_shared<ip::tcp::socket>(boost::ref(m_ioService));
+    make_shared<ip::tcp::socket>(boost::ref(getGlobalIoService()));
   m_acceptor->async_accept(*clientSocket,
                            bind(&TcpChannel::handleSuccessfulAccept, this, _1,
                                 clientSocket,
@@ -188,10 +193,10 @@
       return;
 
     socket->close();
-    
+
     NFD_LOG_DEBUG("Connect to remote endpoint failed: "
                   << error.category().message(error.value()));
-    
+
     onConnectFailed("Connect to remote endpoint failed: " +
                     error.category().message(error.value()));
     return;
@@ -199,7 +204,7 @@
 
   NFD_LOG_DEBUG("[" << m_localEndpoint << "] "
                 ">> Connection to " << socket->remote_endpoint());
-  
+
   createFace(socket, onFaceCreated);
 }
 
@@ -215,7 +220,7 @@
 
   NFD_LOG_DEBUG("Connect to remote endpoint timed out: "
                 << error.category().message(error.value()));
-  
+
   onConnectFailed("Connect to remote endpoint timed out: " +
                   error.category().message(error.value()));
   socket->close(); // abort the connection
@@ -241,7 +246,7 @@
 
       NFD_LOG_DEBUG("Remote endpoint hostname or port cannot be resolved: "
                     << error.category().message(error.value()));
-      
+
       onConnectFailed("Remote endpoint hostname or port cannot be resolved: " +
                       error.category().message(error.value()));
       return;
diff --git a/daemon/face/tcp-channel.hpp b/daemon/face/tcp-channel.hpp
index ca05dee..808d0f1 100644
--- a/daemon/face/tcp-channel.hpp
+++ b/daemon/face/tcp-channel.hpp
@@ -7,7 +7,7 @@
 #ifndef NFD_FACE_TCP_CHANNEL_HPP
 #define NFD_FACE_TCP_CHANNEL_HPP
 
-#include "common.hpp"
+#include "channel.hpp"
 #include "core/time.hpp"
 #include <ndn-cpp-dev/util/monotonic_deadline_timer.hpp>
 #include "tcp-face.hpp"
@@ -15,7 +15,7 @@
 namespace nfd {
 
 namespace tcp {
-  typedef boost::asio::ip::tcp::endpoint Endpoint;
+typedef boost::asio::ip::tcp::endpoint Endpoint;
 } // namespace tcp
 
 /**
@@ -25,30 +25,20 @@
  * connections (TcpChannel::listen needs to be called for that
  * to work) or explicitly after using TcpChannel::connect method.
  */
-class TcpChannel // : protected SessionBasedChannel
+class TcpChannel : public Channel
 {
 public:
   /**
-   * \brief Prototype for the callback called when 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
-   */
-  typedef function<void(const std::string& reason)> ConnectFailedCallback;
-  
-  /**
    * \brief Create TCP channel for the local endpoint
    *
    * To enable creation faces upon incoming connections,
    * one needs to explicitly call TcpChannel::listen method.
    */
-  TcpChannel(boost::asio::io_service& ioService,
-             const tcp::Endpoint& localEndpoint);
+  explicit
+  TcpChannel(const tcp::Endpoint& localEndpoint);
+
+  virtual
+  ~TcpChannel();
 
   /**
    * \brief Enable listening on the local endpoint, accept connections,
@@ -94,8 +84,8 @@
    * \brief Get number of faces in the channel
    */
   size_t
-  size() const;  
-  
+  size() const;
+
 private:
   void
   createFace(const shared_ptr<boost::asio::ip::tcp::socket>& socket,
@@ -116,7 +106,7 @@
                           const shared_ptr<boost::asio::monotonic_deadline_timer>& timer,
                           const FaceCreatedCallback& onFaceCreated,
                           const ConnectFailedCallback& onConnectFailed);
-  
+
   void
   handleFailedConnect(const boost::system::error_code& error,
                       const shared_ptr<boost::asio::ip::tcp::socket>& socket,
@@ -133,7 +123,6 @@
                            const shared_ptr<boost::asio::ip::tcp::resolver>& resolver);
 
 private:
-  boost::asio::io_service& m_ioService;
   tcp::Endpoint m_localEndpoint;
 
   typedef std::map< tcp::Endpoint, shared_ptr<Face> > ChannelFaceMap;
@@ -144,5 +133,5 @@
 };
 
 } // namespace nfd
- 
+
 #endif // NFD_FACE_TCP_CHANNEL_HPP
diff --git a/daemon/face/tcp-factory.cpp b/daemon/face/tcp-factory.cpp
index 0994719..d301350 100644
--- a/daemon/face/tcp-factory.cpp
+++ b/daemon/face/tcp-factory.cpp
@@ -5,7 +5,6 @@
  */
 
 #include "tcp-factory.hpp"
-#include "core/global-io.hpp"
 #include "core/resolver.hpp"
 #include "core/logger.hpp"
 
@@ -25,7 +24,7 @@
   if(static_cast<bool>(channel))
     return channel;
 
-  channel = make_shared<TcpChannel>(boost::ref(getGlobalIoService()), boost::cref(endpoint));
+  channel = make_shared<TcpChannel>(boost::cref(endpoint));
   m_channels[endpoint] = channel;
   NFD_LOG_DEBUG("Channel [" << endpoint << "] created");
   return channel;
@@ -58,7 +57,7 @@
   else if (uri.getScheme() == "tcp6")
     addressSelector = resolver::Ipv6Address();
 
-  TcpResolver::asyncResolve(uri.getDomain(),
+  TcpResolver::asyncResolve(uri.getHost(),
                             uri.getPort().empty() ? m_defaultPort : uri.getPort(),
                             bind(&TcpFactory::continueCreateFaceAfterResolve, this, _1,
                                  onCreated, onConnectFailed),
diff --git a/daemon/face/udp-channel.cpp b/daemon/face/udp-channel.cpp
index 2127cd6..cb4faa7 100644
--- a/daemon/face/udp-channel.cpp
+++ b/daemon/face/udp-channel.cpp
@@ -6,6 +6,7 @@
 
 #include "udp-channel.hpp"
 #include "core/global-io.hpp"
+#include "core/face-uri.hpp"
 
 namespace nfd {
 
@@ -35,6 +36,12 @@
     throw Error("Failed to properly configure the socket. "
                 "UdpChannel creation aborted, check the address (" + std::string(e.what()) + ")");
   }
+  
+  this->setUri(FaceUri(localEndpoint));
+}
+
+UdpChannel::~UdpChannel()
+{
 }
 
 void
diff --git a/daemon/face/udp-channel.hpp b/daemon/face/udp-channel.hpp
index b987b5e..91b34cd 100644
--- a/daemon/face/udp-channel.hpp
+++ b/daemon/face/udp-channel.hpp
@@ -7,14 +7,15 @@
 #ifndef NFD_FACE_UDP_CHANNEL_HPP
 #define NFD_FACE_UDP_CHANNEL_HPP
 
-#include "common.hpp"
+#include "channel.hpp"
 #include "core/time.hpp"
+#include "core/global-io.hpp"
 #include "udp-face.hpp"
 
 namespace nfd {
 
 namespace udp {
-  typedef boost::asio::ip::udp::endpoint Endpoint;
+typedef boost::asio::ip::udp::endpoint Endpoint;
 } // namespace udp
 
 /**
@@ -22,10 +23,9 @@
  *
  *
  */
-class UdpChannel
+class UdpChannel : public Channel
 {
 public:
-  
   /**
    * \brief Exception of UdpChannel
    */
@@ -33,19 +33,6 @@
   {
     Error(const std::string& what) : runtime_error(what) {}
   };
-  
-  /**
-   * \brief Prototype for the callback called when face is created
-   *        (as a response to new incoming communication not managed
-   *        by any faces yet or after the connect function is created)
-   */
-  typedef function<void(const shared_ptr<UdpFace>& newFace)> FaceCreatedCallback;
-
-  /**
-   * \brief Prototype for the callback that is called when face is failed to
-   *        get created
-   */
-  typedef function<void(const std::string& reason)> ConnectFailedCallback;
 
   /**
    * \brief Create UDP channel for the local endpoint
@@ -60,6 +47,9 @@
   UdpChannel(const udp::Endpoint& localEndpoint,
              const time::Duration& timeout);
 
+  virtual
+  ~UdpChannel();
+
   /**
    * \brief Enable listening on the local endpoint, accept connections,
    *        and create faces when remote host makes a connection
@@ -110,7 +100,7 @@
   afterFaceFailed(udp::Endpoint& endpoint);
 
   /**
-   * \brief The UdpChannel has received a new pkt from a remote endpoint not yet 
+   * \brief The UdpChannel has received a new pkt from a remote endpoint not yet
    *        associated with any UdpFace
    */
   void
@@ -138,7 +128,7 @@
    * Its handler has a fixed signature. No space for the face callback
    */
   FaceCreatedCallback onFaceCreatedNewPeerCallback;
-  
+
   // @todo remove the onConnectFailedNewPeerCallback if it remains unused
   ConnectFailedCallback onConnectFailedNewPeerCallback;
 
diff --git a/daemon/face/udp-factory.cpp b/daemon/face/udp-factory.cpp
index 8dafb57..a311d81 100644
--- a/daemon/face/udp-factory.cpp
+++ b/daemon/face/udp-factory.cpp
@@ -149,7 +149,7 @@
   else if (uri.getScheme() == "udp6")
     addressSelector = resolver::Ipv6Address();
   
-  UdpResolver::asyncResolve(uri.getDomain(),
+  UdpResolver::asyncResolve(uri.getHost(),
                             uri.getPort().empty() ? m_defaultPort : uri.getPort(),
                             bind(&UdpFactory::continueCreateFaceAfterResolve, this, _1,
                                  onCreated, onConnectFailed),
diff --git a/daemon/face/unix-stream-channel.cpp b/daemon/face/unix-stream-channel.cpp
index ebc7e77..1a92f73 100644
--- a/daemon/face/unix-stream-channel.cpp
+++ b/daemon/face/unix-stream-channel.cpp
@@ -5,6 +5,7 @@
  */
 
 #include "unix-stream-channel.hpp"
+#include "core/global-io.hpp"
 
 #include <boost/filesystem.hpp>
 #include <sys/stat.h> // for chmod()
@@ -15,11 +16,10 @@
 
 using namespace boost::asio::local;
 
-UnixStreamChannel::UnixStreamChannel(boost::asio::io_service& ioService,
-                                     const unix_stream::Endpoint& endpoint)
-  : m_ioService(ioService)
-  , m_endpoint(endpoint)
+UnixStreamChannel::UnixStreamChannel(const unix_stream::Endpoint& endpoint)
+  : m_endpoint(endpoint)
 {
+  this->setUri(FaceUri(endpoint));
 }
 
 UnixStreamChannel::~UnixStreamChannel()
@@ -50,7 +50,7 @@
       fs::remove(p);
     }
 
-  m_acceptor = make_shared<stream_protocol::acceptor>(boost::ref(m_ioService));
+  m_acceptor = make_shared<stream_protocol::acceptor>(boost::ref(getGlobalIoService()));
   m_acceptor->open(m_endpoint.protocol());
   m_acceptor->bind(m_endpoint);
   m_acceptor->listen(backlog);
@@ -61,7 +61,7 @@
     }
 
   shared_ptr<stream_protocol::socket> clientSocket =
-    make_shared<stream_protocol::socket>(boost::ref(m_ioService));
+    make_shared<stream_protocol::socket>(boost::ref(getGlobalIoService()));
   m_acceptor->async_accept(*clientSocket,
                            bind(&UnixStreamChannel::handleSuccessfulAccept, this, _1,
                                 clientSocket, onFaceCreated, onAcceptFailed));
@@ -95,7 +95,7 @@
 
   // prepare accepting the next connection
   shared_ptr<stream_protocol::socket> clientSocket =
-    make_shared<stream_protocol::socket>(boost::ref(m_ioService));
+    make_shared<stream_protocol::socket>(boost::ref(getGlobalIoService()));
   m_acceptor->async_accept(*clientSocket,
                            bind(&UnixStreamChannel::handleSuccessfulAccept, this, _1,
                                 clientSocket, onFaceCreated, onAcceptFailed));
diff --git a/daemon/face/unix-stream-channel.hpp b/daemon/face/unix-stream-channel.hpp
index 3bbc8a3..3c6b493 100644
--- a/daemon/face/unix-stream-channel.hpp
+++ b/daemon/face/unix-stream-channel.hpp
@@ -7,13 +7,13 @@
 #ifndef NFD_FACE_UNIX_STREAM_CHANNEL_HPP
 #define NFD_FACE_UNIX_STREAM_CHANNEL_HPP
 
-#include "common.hpp"
+#include "channel.hpp"
 #include "unix-stream-face.hpp"
 
 namespace nfd {
 
 namespace unix_stream {
-  typedef boost::asio::local::stream_protocol::endpoint Endpoint;
+typedef boost::asio::local::stream_protocol::endpoint Endpoint;
 } // namespace unix_stream
 
 /**
@@ -22,22 +22,10 @@
  * Channel can create faces as a response to incoming IPC connections
  * (UnixStreamChannel::listen needs to be called for that to work).
  */
-class UnixStreamChannel // : protected SessionBasedChannel
+class UnixStreamChannel : public Channel
 {
 public:
   /**
-   * \brief Prototype for the callback called when a face is created
-   *        (as a response to an incoming connection)
-   */
-  typedef function<void(const shared_ptr<UnixStreamFace>& newFace)> FaceCreatedCallback;
-
-  /**
-   * \brief Prototype for the callback that is called when a face
-   *        fails to be created
-   */
-  typedef function<void(const std::string& reason)> ConnectFailedCallback;
-
-  /**
    * \brief UnixStreamChannel-related error
    */
   struct Error : public std::runtime_error
@@ -51,9 +39,10 @@
    * To enable creation of faces upon incoming connections, one
    * needs to explicitly call UnixStreamChannel::listen method.
    */
-  UnixStreamChannel(boost::asio::io_service& ioService,
-                    const unix_stream::Endpoint& endpoint);
+  explicit
+  UnixStreamChannel(const unix_stream::Endpoint& endpoint);
 
+  virtual
   ~UnixStreamChannel();
 
   /**
@@ -82,7 +71,6 @@
                          const ConnectFailedCallback& onConnectFailed);
 
 private:
-  boost::asio::io_service& m_ioService;
   unix_stream::Endpoint m_endpoint;
   shared_ptr<boost::asio::local::stream_protocol::acceptor> m_acceptor;
 };
diff --git a/daemon/face/unix-stream-factory.cpp b/daemon/face/unix-stream-factory.cpp
index 20c01d9..c38cab5 100644
--- a/daemon/face/unix-stream-factory.cpp
+++ b/daemon/face/unix-stream-factory.cpp
@@ -5,7 +5,6 @@
  */
 
 #include "unix-stream-factory.hpp"
-#include "core/global-io.hpp"
 
 #include <boost/filesystem.hpp> // for canonical()
 
@@ -22,8 +21,7 @@
   if (channel)
     return channel;
 
-  channel = make_shared<UnixStreamChannel>(boost::ref(getGlobalIoService()),
-                                           boost::cref(endpoint));
+  channel = make_shared<UnixStreamChannel>(boost::cref(endpoint));
   m_channels[endpoint] = channel;
   return channel;
 }
diff --git a/tests/core/face-uri.cpp b/tests/core/face-uri.cpp
index 0b6a633..7eb9b62 100644
--- a/tests/core/face-uri.cpp
+++ b/tests/core/face-uri.cpp
@@ -24,32 +24,44 @@
   
   BOOST_CHECK(uri.parse("udp://hostname:80"));
   BOOST_CHECK_EQUAL(uri.getScheme(), "udp");
-  BOOST_CHECK_EQUAL(uri.getDomain(), "hostname");
+  BOOST_CHECK_EQUAL(uri.getHost(), "hostname");
   BOOST_CHECK_EQUAL(uri.getPort(), "80");
+  BOOST_CHECK_EQUAL(uri.getPath(), "");
 
   BOOST_CHECK(uri.parse("udp4://192.0.2.1:20"));
   BOOST_CHECK_EQUAL(uri.getScheme(), "udp4");
-  BOOST_CHECK_EQUAL(uri.getDomain(), "192.0.2.1");
+  BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1");
   BOOST_CHECK_EQUAL(uri.getPort(), "20");
+  BOOST_CHECK_EQUAL(uri.getPath(), "");
 
   BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363"));
   BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
-  BOOST_CHECK_EQUAL(uri.getDomain(), "2001:db8:3f9:0::1");
+  BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1");
   BOOST_CHECK_EQUAL(uri.getPort(), "6363");
+  BOOST_CHECK_EQUAL(uri.getPath(), "");
   
   BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363"));
   BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
-  BOOST_CHECK_EQUAL(uri.getDomain(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
+  BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
   BOOST_CHECK_EQUAL(uri.getPort(), "6363");
+  BOOST_CHECK_EQUAL(uri.getPath(), "");
 
   BOOST_CHECK(uri.parse("tcp://random.host.name"));
   BOOST_CHECK_EQUAL(uri.getScheme(), "tcp");
-  BOOST_CHECK_EQUAL(uri.getDomain(), "random.host.name");
+  BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name");
   BOOST_CHECK_EQUAL(uri.getPort(), "");
+  BOOST_CHECK_EQUAL(uri.getPath(), "");
+
+  BOOST_CHECK(uri.parse("unix:///var/run/example.sock"));
+  BOOST_CHECK_EQUAL(uri.getScheme(), "unix");
+  BOOST_CHECK_EQUAL(uri.getHost(), "");
+  BOOST_CHECK_EQUAL(uri.getPort(), "");
+  BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock");
 
   BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false);
   BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false);
   BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false);  
+  BOOST_CHECK_EQUAL(uri.getPath(), "");
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/tests/face/tcp.cpp b/tests/face/tcp.cpp
index 2be6331..9223283 100644
--- a/tests/face/tcp.cpp
+++ b/tests/face/tcp.cpp
@@ -22,9 +22,14 @@
   shared_ptr<TcpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
   shared_ptr<TcpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
   BOOST_CHECK_EQUAL(channel1, channel1a);
+  BOOST_CHECK_EQUAL(channel1->getUri().toString(), "tcp4://127.0.0.1:20070");
 
   shared_ptr<TcpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
   BOOST_CHECK_NE(channel1, channel2);
+
+  shared_ptr<TcpChannel> channel3 = factory.createChannel("::1", "20071");
+  BOOST_CHECK_NE(channel2, channel3);
+  BOOST_CHECK_EQUAL(channel3->getUri().toString(), "tcp6://[::1]:20071");
 }
 
 class EndToEndFixture : protected BaseFixture
diff --git a/tests/face/udp.cpp b/tests/face/udp.cpp
index 12c9eb3..f877e2f 100644
--- a/tests/face/udp.cpp
+++ b/tests/face/udp.cpp
@@ -57,12 +57,17 @@
   shared_ptr<UdpChannel> channel1 = factory.createChannel("127.0.0.1", "20070");
   shared_ptr<UdpChannel> channel1a = factory.createChannel("127.0.0.1", "20070");
   BOOST_CHECK_EQUAL(channel1, channel1a);
+  BOOST_CHECK_EQUAL(channel1->getUri().toString(), "udp4://127.0.0.1:20070");
 
   shared_ptr<UdpChannel> channel2 = factory.createChannel("127.0.0.1", "20071");
   BOOST_CHECK_NE(channel1, channel2);
 
   shared_ptr<UdpChannel> channel3 = factory.createChannel(interfaceIp, "20070");
 
+  shared_ptr<UdpChannel> channel4 = factory.createChannel("::1", "20071");
+  BOOST_CHECK_NE(channel2, channel4);
+  BOOST_CHECK_EQUAL(channel4->getUri().toString(), "udp6://[::1]:20071");
+
   //same endpoint of a unicast channel
   BOOST_CHECK_EXCEPTION(factory.createMulticastFace(interfaceIp,
                                                     "224.0.0.1",
@@ -162,7 +167,7 @@
 {
 public:
   void
-  channel1_onFaceCreated(const shared_ptr<UdpFace>& newFace)
+  channel1_onFaceCreated(const shared_ptr<Face>& newFace)
   {
     BOOST_CHECK(!static_cast<bool>(m_face1));
     m_face1 = newFace;
@@ -271,7 +276,7 @@
 
 
   void
-  channel_onFaceCreated(const shared_ptr<UdpFace>& newFace)
+  channel_onFaceCreated(const shared_ptr<Face>& newFace)
   {
     m_faces.push_back(newFace);
     m_limitedIo.afterOp();
diff --git a/tests/face/unix-stream.cpp b/tests/face/unix-stream.cpp
index d143251..921c1ff 100644
--- a/tests/face/unix-stream.cpp
+++ b/tests/face/unix-stream.cpp
@@ -15,17 +15,24 @@
 
 using namespace boost::asio::local;
 
+#define CHANNEL_PATH1 "unix-stream-test.1.sock"
+#define CHANNEL_PATH2 "unix-stream-test.2.sock"
+
 BOOST_FIXTURE_TEST_SUITE(FaceUnixStream, BaseFixture)
 
 BOOST_AUTO_TEST_CASE(ChannelMap)
 {
   UnixStreamFactory factory;
 
-  shared_ptr<UnixStreamChannel> channel1 = factory.createChannel("foo");
-  shared_ptr<UnixStreamChannel> channel1a = factory.createChannel("foo");
+  shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
+  shared_ptr<UnixStreamChannel> channel1a = factory.createChannel(CHANNEL_PATH1);
   BOOST_CHECK_EQUAL(channel1, channel1a);
+  std::string channel1uri = channel1->getUri().toString();
+  BOOST_CHECK_EQUAL(channel1uri.find("unix:///"), 0); // third '/' is the path separator
+  BOOST_CHECK_EQUAL(channel1uri.rfind(CHANNEL_PATH1),
+                    channel1uri.size() - std::string(CHANNEL_PATH1).size());
 
-  shared_ptr<UnixStreamChannel> channel2 = factory.createChannel("bar");
+  shared_ptr<UnixStreamChannel> channel2 = factory.createChannel(CHANNEL_PATH2);
   BOOST_CHECK_NE(channel1, channel2);
 }
 
@@ -41,10 +48,10 @@
   }
 
   void
-  channel1_onFaceCreated(const shared_ptr<UnixStreamFace>& newFace)
+  channel1_onFaceCreated(const shared_ptr<Face>& newFace)
   {
     BOOST_CHECK(!static_cast<bool>(m_face1));
-    m_face1 = newFace;
+    m_face1 = static_pointer_cast<UnixStreamFace>(newFace);
     m_face1->onReceiveInterest +=
       bind(&EndToEndFixture::face1_onReceiveInterest, this, _1);
     m_face1->onReceiveData +=
@@ -94,9 +101,9 @@
   }
 
   void
-  channel_onFaceCreated(const shared_ptr<UnixStreamFace>& newFace)
+  channel_onFaceCreated(const shared_ptr<Face>& newFace)
   {
-    m_faces.push_back(newFace);
+    m_faces.push_back(static_pointer_cast<UnixStreamFace>(newFace));
 
     m_limitedIo.afterOp();
   }
@@ -127,13 +134,13 @@
 {
   UnixStreamFactory factory;
 
-  shared_ptr<UnixStreamChannel> channel1 = factory.createChannel("foo");
+  shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
   channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated,   this, _1),
                    bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
 
   shared_ptr<stream_protocol::socket> client =
       make_shared<stream_protocol::socket>(boost::ref(g_io));
-  client->async_connect(stream_protocol::endpoint("foo"),
+  client->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
                         bind(&EndToEndFixture::client_onConnect, this, _1));
 
   BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
@@ -184,13 +191,13 @@
 {
   UnixStreamFactory factory;
 
-  shared_ptr<UnixStreamChannel> channel = factory.createChannel("foo");
+  shared_ptr<UnixStreamChannel> channel = factory.createChannel(CHANNEL_PATH1);
   channel->listen(bind(&EndToEndFixture::channel_onFaceCreated,   this, _1),
                   bind(&EndToEndFixture::channel_onConnectFailed, this, _1));
 
   shared_ptr<stream_protocol::socket> client1 =
       make_shared<stream_protocol::socket>(boost::ref(g_io));
-  client1->async_connect(stream_protocol::endpoint("foo"),
+  client1->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
                          bind(&EndToEndFixture::client_onConnect, this, _1));
 
   BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
@@ -200,7 +207,7 @@
 
   shared_ptr<stream_protocol::socket> client2 =
       make_shared<stream_protocol::socket>(boost::ref(g_io));
-  client2->async_connect(stream_protocol::endpoint("foo"),
+  client2->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
                          bind(&EndToEndFixture::client_onConnect, this, _1));
 
   BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,
@@ -266,13 +273,13 @@
 {
   UnixStreamFactory factory;
 
-  shared_ptr<UnixStreamChannel> channel1 = factory.createChannel("foo");
+  shared_ptr<UnixStreamChannel> channel1 = factory.createChannel(CHANNEL_PATH1);
   channel1->listen(bind(&EndToEndFixture::channel1_onFaceCreated,   this, _1),
                    bind(&EndToEndFixture::channel1_onConnectFailed, this, _1));
 
   shared_ptr<stream_protocol::socket> client =
       make_shared<stream_protocol::socket>(boost::ref(g_io));
-  client->async_connect(stream_protocol::endpoint("foo"),
+  client->async_connect(stream_protocol::endpoint(CHANNEL_PATH1),
                         bind(&EndToEndFixture::client_onConnect, this, _1));
 
   BOOST_CHECK_MESSAGE(m_limitedIo.run(2, time::seconds(1)) == LimitedIo::EXCEED_OPS,