face-uri.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
12  *
13  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
14  * terms of the GNU Lesser General Public License as published by the Free Software
15  * Foundation, either version 3 of the License, or (at your option) any later version.
16  *
17  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
20  *
21  * You should have received copies of the GNU General Public License and GNU Lesser
22  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
23  * <http://www.gnu.org/licenses/>.
24  *
25  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
26  */
27 
28 #ifndef NDN_NET_FACE_URI_HPP
29 #define NDN_NET_FACE_URI_HPP
30 
32 #include "ndn-cxx/net/ethernet.hpp"
33 #include "ndn-cxx/util/time.hpp"
34 
35 #include <boost/asio/ip/tcp.hpp>
36 #include <boost/asio/ip/udp.hpp>
37 #include <boost/asio/local/stream_protocol.hpp>
38 
39 namespace ndn {
40 
44 class FaceUri
45 {
46 public:
47  class Error : public std::invalid_argument
48  {
49  public:
50  using std::invalid_argument::invalid_argument;
51  };
52 
53  FaceUri();
54 
60  explicit
61  FaceUri(const std::string& uri);
62 
63  // This overload is needed so that calls with string literal won't be
64  // resolved to boost::asio::local::stream_protocol::endpoint overload.
65  explicit
66  FaceUri(const char* uri);
67 
70  parse(const std::string& uri);
71 
72 public: // scheme-specific construction
74  explicit
75  FaceUri(const boost::asio::ip::udp::endpoint& endpoint);
76 
78  explicit
79  FaceUri(const boost::asio::ip::tcp::endpoint& endpoint);
80 
82  FaceUri(const boost::asio::ip::tcp::endpoint& endpoint, const std::string& scheme);
83 
84 #ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
85  explicit
87  FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint);
88 #endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
89 
91  static FaceUri
92  fromFd(int fd);
93 
95  explicit
96  FaceUri(const ethernet::Address& address);
97 
99  static FaceUri
100  fromDev(const std::string& ifname);
101 
103  static FaceUri
104  fromUdpDev(const boost::asio::ip::udp::endpoint& endpoint, const std::string& ifname);
105 
106 public: // getters
108  const std::string&
109  getScheme() const
110  {
111  return m_scheme;
112  }
113 
115  const std::string&
116  getHost() const
117  {
118  return m_host;
119  }
120 
122  const std::string&
123  getPort() const
124  {
125  return m_port;
126  }
127 
129  const std::string&
130  getPath() const
131  {
132  return m_path;
133  }
134 
136  std::string
137  toString() const;
138 
139 public: // canonical FaceUri
142  static bool
143  canCanonize(const std::string& scheme);
144 
150  bool
151  isCanonical() const;
152 
153  typedef function<void(const FaceUri&)> CanonizeSuccessCallback;
154  typedef function<void(const std::string& reason)> CanonizeFailureCallback;
155 
165  void
166  canonize(const CanonizeSuccessCallback& onSuccess,
167  const CanonizeFailureCallback& onFailure,
168  boost::asio::io_service& io,
169  time::nanoseconds timeout) const;
170 
171 private: // non-member operators
172  // NOTE: the following "hidden friend" operators are available via
173  // argument-dependent lookup only and must be defined inline.
174 
175  friend bool
176  operator==(const FaceUri& lhs, const FaceUri& rhs)
177  {
178  return !(lhs != rhs);
179  }
180 
181  friend bool
182  operator!=(const FaceUri& lhs, const FaceUri& rhs)
183  {
184  return lhs.m_isV6 != rhs.m_isV6 ||
185  lhs.m_scheme != rhs.m_scheme ||
186  lhs.m_host != rhs.m_host ||
187  lhs.m_port != rhs.m_port ||
188  lhs.m_path != rhs.m_path;
189  }
190 
191 private:
192  std::string m_scheme;
193  std::string m_host;
194  std::string m_port;
195  std::string m_path;
197  bool m_isV6;
198 
199  friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri);
200 };
201 
202 std::ostream&
203 operator<<(std::ostream& os, const FaceUri& uri);
204 
205 } // namespace ndn
206 
207 #endif // NDN_NET_FACE_URI_HPP
Definition: data.cpp:26
function< void(const std::string &reason)> CanonizeFailureCallback
Definition: face-uri.hpp:154
static FaceUri fromFd(int fd)
create fd FaceUri from file descriptor
Definition: face-uri.cpp:154
const std::string & getHost() const
get host (domain)
Definition: face-uri.hpp:116
function< void(const FaceUri &)> CanonizeSuccessCallback
Definition: face-uri.hpp:153
static bool canCanonize(const std::string &scheme)
Definition: face-uri.cpp:613
std::string toString() const
write as a string
Definition: face-uri.cpp:189
const std::string & getPort() const
get port
Definition: face-uri.hpp:123
friend bool operator==(const FaceUri &lhs, const FaceUri &rhs)
Definition: face-uri.hpp:176
static FaceUri fromUdpDev(const boost::asio::ip::udp::endpoint &endpoint, const std::string &ifname)
create udp4 or udp6 NIC-associated FaceUri from endpoint and network device name
Definition: face-uri.cpp:179
bool parse(const std::string &uri)
exception-safe parsing
Definition: face-uri.cpp:64
friend bool operator!=(const FaceUri &lhs, const FaceUri &rhs)
Definition: face-uri.hpp:182
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
void canonize(const CanonizeSuccessCallback &onSuccess, const CanonizeFailureCallback &onFailure, boost::asio::io_service &io, time::nanoseconds timeout) const
asynchronously convert this FaceUri to canonical form
Definition: face-uri.cpp:630
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:44
represents an Ethernet hardware address
Definition: ethernet.hpp:52
const std::string & getScheme() const
get scheme (protocol)
Definition: face-uri.hpp:109
bool isCanonical() const
determine whether this FaceUri is in canonical form
Definition: face-uri.cpp:619
const std::string & getPath() const
get path
Definition: face-uri.hpp:130
static FaceUri fromDev(const std::string &ifname)
create dev FaceUri from network device name
Definition: face-uri.cpp:170
friend std::ostream & operator<<(std::ostream &os, const FaceUri &uri)
Definition: face-uri.cpp:197