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-2022 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_CXX_NET_FACE_URI_HPP
29 #define NDN_CXX_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 
45 class FaceUri
46 {
47 public:
48  class Error : public std::invalid_argument
49  {
50  public:
51  using std::invalid_argument::invalid_argument;
52  };
53 
54  FaceUri();
55 
61  explicit
62  FaceUri(const std::string& uri);
63 
64  // This overload is needed so that calls with string literal won't be
65  // resolved to boost::asio::local::stream_protocol::endpoint overload.
66  explicit
67  FaceUri(const char* uri);
68 
71  parse(const std::string& uri);
72 
73 public: // scheme-specific construction
75  explicit
76  FaceUri(const boost::asio::ip::udp::endpoint& endpoint);
77 
79  explicit
80  FaceUri(const boost::asio::ip::tcp::endpoint& endpoint);
81 
83  FaceUri(const boost::asio::ip::tcp::endpoint& endpoint, const std::string& scheme);
84 
85 #ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
87  explicit
88  FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint);
89 #endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
90 
92  static FaceUri
93  fromFd(int fd);
94 
96  explicit
97  FaceUri(const ethernet::Address& address);
98 
100  static FaceUri
101  fromDev(const std::string& ifname);
102 
104  static FaceUri
105  fromUdpDev(const boost::asio::ip::udp::endpoint& endpoint, const std::string& ifname);
106 
107 public: // getters
109  const std::string&
110  getScheme() const
111  {
112  return m_scheme;
113  }
114 
116  const std::string&
117  getHost() const
118  {
119  return m_host;
120  }
121 
123  const std::string&
124  getPort() const
125  {
126  return m_port;
127  }
128 
130  const std::string&
131  getPath() const
132  {
133  return m_path;
134  }
135 
137  std::string
138  toString() const;
139 
140 public: // canonical FaceUri
144  static bool
145  canCanonize(const std::string& scheme);
146 
152  bool
153  isCanonical() const;
154 
155  typedef function<void(const FaceUri&)> CanonizeSuccessCallback;
156  typedef function<void(const std::string& reason)> CanonizeFailureCallback;
157 
167  void
168  canonize(const CanonizeSuccessCallback& onSuccess,
169  const CanonizeFailureCallback& onFailure,
170  boost::asio::io_service& io,
171  time::nanoseconds timeout) const;
172 
173 private: // non-member operators
174  // NOTE: the following "hidden friend" operators are available via
175  // argument-dependent lookup only and must be defined inline.
176 
177  friend bool
178  operator==(const FaceUri& lhs, const FaceUri& rhs)
179  {
180  return !(lhs != rhs);
181  }
182 
183  friend bool
184  operator!=(const FaceUri& lhs, const FaceUri& rhs)
185  {
186  return lhs.m_isV6 != rhs.m_isV6 ||
187  lhs.m_scheme != rhs.m_scheme ||
188  lhs.m_host != rhs.m_host ||
189  lhs.m_port != rhs.m_port ||
190  lhs.m_path != rhs.m_path;
191  }
192 
193 private:
194  std::string m_scheme;
195  std::string m_host;
196  std::string m_port;
197  std::string m_path;
199  bool m_isV6;
200 
201  friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri);
202 };
203 
204 std::ostream&
205 operator<<(std::ostream& os, const FaceUri& uri);
206 
207 } // namespace ndn
208 
209 #endif // NDN_CXX_NET_FACE_URI_HPP
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
The underlying protocol and address used by a Face.
Definition: face-uri.hpp:46
std::string toString() const
Serialize as a string.
Definition: face-uri.cpp:189
friend bool operator!=(const FaceUri &lhs, const FaceUri &rhs)
Definition: face-uri.hpp:184
function< void(const FaceUri &)> CanonizeSuccessCallback
Definition: face-uri.hpp:155
friend std::ostream & operator<<(std::ostream &os, const FaceUri &uri)
Definition: face-uri.cpp:197
const std::string & getScheme() const
Get scheme (protocol)
Definition: face-uri.hpp:110
bool parse(const std::string &uri)
Exception-safe parsing.
Definition: face-uri.cpp:64
static bool canCanonize(const std::string &scheme)
Return whether a FaceUri of the specified scheme can be canonized.
Definition: face-uri.cpp:613
bool isCanonical() const
Determine whether this FaceUri is in canonical form.
Definition: face-uri.cpp:619
const std::string & getHost() const
Get host (domain)
Definition: face-uri.hpp:117
const std::string & getPath() const
Get path.
Definition: face-uri.hpp:131
friend bool operator==(const FaceUri &lhs, const FaceUri &rhs)
Definition: face-uri.hpp:178
static FaceUri fromDev(const std::string &ifname)
Construct a dev FaceUri from a network device name.
Definition: face-uri.cpp:170
static FaceUri fromUdpDev(const boost::asio::ip::udp::endpoint &endpoint, const std::string &ifname)
Construct a udp4 or udp6 NIC-associated FaceUri from endpoint and network device name.
Definition: face-uri.cpp:179
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
const std::string & getPort() const
Get port.
Definition: face-uri.hpp:124
function< void(const std::string &reason)> CanonizeFailureCallback
Definition: face-uri.hpp:156
static FaceUri fromFd(int fd)
Construct an fd FaceUri from a file descriptor.
Definition: face-uri.cpp:154
Represents an Ethernet hardware address.
Definition: ethernet.hpp:53
boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:50
Definition: data.cpp:25
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:374