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-2023 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 #include <boost/operators.hpp>
39 
40 namespace ndn {
41 
46 class FaceUri : private boost::totally_ordered<FaceUri>
47 {
48 public:
49  class Error : public std::invalid_argument
50  {
51  public:
52  using std::invalid_argument::invalid_argument;
53  };
54 
57 
63  explicit
64  FaceUri(const std::string& uri);
65 
73  explicit
74  FaceUri(const char* uri);
75 
77  [[nodiscard]] bool
78  parse(std::string_view uri);
79 
80 public: // scheme-specific construction
82  explicit
83  FaceUri(const boost::asio::ip::udp::endpoint& endpoint);
84 
86  explicit
87  FaceUri(const boost::asio::ip::tcp::endpoint& endpoint);
88 
90  FaceUri(const boost::asio::ip::tcp::endpoint& endpoint, std::string_view scheme);
91 
92 #ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
94  explicit
95  FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint);
96 #endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
97 
99  explicit
100  FaceUri(const ethernet::Address& address);
101 
103  static FaceUri
104  fromFd(int fd);
105 
107  static FaceUri
108  fromDev(std::string_view ifname);
109 
111  static FaceUri
112  fromUdpDev(const boost::asio::ip::udp::endpoint& endpoint, std::string_view ifname);
113 
114 public: // getters
116  const std::string&
117  getScheme() const
118  {
119  return m_scheme;
120  }
121 
123  const std::string&
124  getHost() const
125  {
126  return m_host;
127  }
128 
130  const std::string&
131  getPort() const
132  {
133  return m_port;
134  }
135 
137  const std::string&
138  getPath() const
139  {
140  return m_path;
141  }
142 
144  std::string
145  toString() const;
146 
147 public: // canonical FaceUri
151  static bool
152  canCanonize(const std::string& scheme);
153 
159  bool
160  isCanonical() const;
161 
162  using CanonizeSuccessCallback = std::function<void(const FaceUri&)>;
163  using CanonizeFailureCallback = std::function<void(const std::string& reason)>;
164 
175  void
176  canonize(const CanonizeSuccessCallback& onSuccess,
177  const CanonizeFailureCallback& onFailure,
178  boost::asio::io_context& io,
179  time::nanoseconds timeout) const;
180 
181 private:
182  void
183  print(std::ostream& os) const;
184 
185 private: // non-member operators
186  // NOTE: the following "hidden friend" operators are available via
187  // argument-dependent lookup only and must be defined inline.
188  // boost::totally_ordered provides !=, <=, >=, and > operators.
189 
190  friend bool
191  operator==(const FaceUri& lhs, const FaceUri& rhs) noexcept
192  {
193  return lhs.m_isV6 == rhs.m_isV6 &&
194  lhs.m_scheme == rhs.m_scheme &&
195  lhs.m_host == rhs.m_host &&
196  lhs.m_port == rhs.m_port &&
197  lhs.m_path == rhs.m_path;
198  }
199 
200  friend bool
201  operator<(const FaceUri& lhs, const FaceUri& rhs) noexcept
202  {
203  return std::tie(lhs.m_scheme, lhs.m_isV6, lhs.m_host, lhs.m_port, lhs.m_path) <
204  std::tie(rhs.m_scheme, rhs.m_isV6, rhs.m_host, rhs.m_port, rhs.m_path);
205  }
206 
207  friend std::ostream&
208  operator<<(std::ostream& os, const FaceUri& uri)
209  {
210  uri.print(os);
211  return os;
212  }
213 
214 private:
215  std::string m_scheme;
216  std::string m_host;
217  std::string m_port;
218  std::string m_path;
219  bool m_isV6 = false;
220 };
221 
222 } // namespace ndn
223 
224 #endif // NDN_CXX_NET_FACE_URI_HPP
The underlying protocol and address used by a Face.
Definition: face-uri.hpp:47
std::string toString() const
Return string representation.
Definition: face-uri.cpp:184
void canonize(const CanonizeSuccessCallback &onSuccess, const CanonizeFailureCallback &onFailure, boost::asio::io_context &io, time::nanoseconds timeout) const
Asynchronously convert this FaceUri to canonical form.
Definition: face-uri.cpp:617
std::function< void(const std::string &reason)> CanonizeFailureCallback
Definition: face-uri.hpp:163
friend bool operator==(const FaceUri &lhs, const FaceUri &rhs) noexcept
Definition: face-uri.hpp:191
static FaceUri fromDev(std::string_view ifname)
Construct a dev FaceUri from a network device name.
Definition: face-uri.cpp:165
FaceUri()
Construct an empty FaceUri.
friend std::ostream & operator<<(std::ostream &os, const FaceUri &uri)
Definition: face-uri.hpp:208
const std::string & getScheme() const
Get scheme (protocol)
Definition: face-uri.hpp:117
static bool canCanonize(const std::string &scheme)
Return whether a FaceUri of the specified scheme can be canonized.
Definition: face-uri.cpp:600
bool isCanonical() const
Determine whether this FaceUri is in canonical form.
Definition: face-uri.cpp:606
const std::string & getHost() const
Get host (domain or address)
Definition: face-uri.hpp:124
const std::string & getPath() const
Get path.
Definition: face-uri.hpp:138
bool parse(std::string_view uri)
Exception-safe parsing.
Definition: face-uri.cpp:64
const std::string & getPort() const
Get port.
Definition: face-uri.hpp:131
friend bool operator<(const FaceUri &lhs, const FaceUri &rhs) noexcept
Definition: face-uri.hpp:201
std::function< void(const FaceUri &)> CanonizeSuccessCallback
Definition: face-uri.hpp:162
static FaceUri fromUdpDev(const boost::asio::ip::udp::endpoint &endpoint, std::string_view ifname)
Construct a udp4 or udp6 NIC-associated FaceUri from endpoint and network device name.
Definition: face-uri.cpp:174
static FaceUri fromFd(int fd)
Construct an fd FaceUri from a file descriptor.
Definition: face-uri.cpp:156
Represents an Ethernet hardware address.
Definition: ethernet.hpp:52
::boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:54
Definition: data.cpp:25