faces: Channel base class

refs #1261

Change-Id: If17037c4802cb6ccec0201c3bff7fc4b6db41c6a
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;
 }