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