blob: 95d2c8799c49576f2cf0e521ba180afa301ed9d4 [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shie3ef6ee2014-10-05 14:40:54 -07003 * Copyright (c) 2014, 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
Junxiao Shi77dcadd2014-10-05 14:40:54 -070010 *
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_UTIL_FACE_URI_HPP
29#define NDN_UTIL_FACE_URI_HPP
30
31#include "common.hpp"
32#include <boost/asio/ip/udp.hpp>
33#include <boost/asio/ip/tcp.hpp>
34#include <boost/asio/local/stream_protocol.hpp>
35#include "ethernet.hpp"
Junxiao Shi4083c8d2014-10-12 16:43:16 -070036#include "time.hpp"
Junxiao Shi77dcadd2014-10-05 14:40:54 -070037
38namespace ndn {
39namespace util {
40
41/** \brief represents the underlying protocol and address used by a Face
42 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#FaceUri
43 */
44class FaceUri
45{
46public:
47 class Error : public std::invalid_argument
48 {
49 public:
50 explicit
51 Error(const std::string& what)
52 : std::invalid_argument(what)
53 {
54 }
55 };
56
57 FaceUri();
58
59 /** \brief construct by parsing
60 *
61 * \param uri scheme://host[:port]/path
62 * \throw FaceUri::Error if URI cannot be parsed
63 */
64 explicit
65 FaceUri(const std::string& uri);
66
67 // This overload is needed so that calls with string literal won't be
68 // resolved to boost::asio::local::stream_protocol::endpoint overload.
69 explicit
70 FaceUri(const char* uri);
71
72 /// exception-safe parsing
73 bool
74 parse(const std::string& uri);
75
76public: // scheme-specific construction
77 /// construct udp4 or udp6 canonical FaceUri
78 explicit
79 FaceUri(const boost::asio::ip::udp::endpoint& endpoint);
80
81 /// construct tcp4 or tcp6 canonical FaceUri
82 explicit
83 FaceUri(const boost::asio::ip::tcp::endpoint& endpoint);
84
85 /// construct tcp canonical FaceUri with customized scheme
86 FaceUri(const boost::asio::ip::tcp::endpoint& endpoint, const std::string& scheme);
87
88#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
89 /// construct unix canonical FaceUri
90 explicit
91 FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint);
92#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
93
94 /// create fd FaceUri from file descriptor
95 static FaceUri
96 fromFd(int fd);
97
98 /// construct ether canonical FaceUri
99 explicit
100 FaceUri(const ethernet::Address& address);
101
102 /// create dev FaceUri from network device name
103 static FaceUri
104 fromDev(const std::string& ifname);
105
106public: // getters
107 /// get scheme (protocol)
108 const std::string&
109 getScheme() const
110 {
111 return m_scheme;
112 }
113
114 /// get host (domain)
115 const std::string&
116 getHost() const
117 {
118 return m_host;
119 }
120
121 /// get port
122 const std::string&
123 getPort() const
124 {
125 return m_port;
126 }
127
128 /// get path
129 const std::string&
130 getPath() const
131 {
132 return m_path;
133 }
134
135 /// write as a string
136 std::string
137 toString() const;
138
139public: // EqualityComparable concept
140 bool
141 operator==(const FaceUri& rhs) const;
142
143 bool
144 operator!=(const FaceUri& rhs) const;
145
Junxiao Shi4083c8d2014-10-12 16:43:16 -0700146public: // canonical FaceUri
147 /** \return whether a FaceUri of the scheme can be canonized
148 */
149 static bool
150 canCanonize(const std::string& scheme);
151
152 /** \brief determine whether this FaceUri is in canonical form
153 * \return true if this FaceUri is in canonical form,
154 * false if this FaceUri is not in canonical form or
155 * or it's undetermined whether this FaceUri is in canonical form
156 */
157 bool
158 isCanonical() const;
159
160 typedef function<void(const FaceUri&)> CanonizeSuccessCallback;
161 typedef function<void(const std::string& reason)> CanonizeFailureCallback;
162
163 /** \brief asynchronously convert this FaceUri to canonical form
164 * \param onSuccess function to call after this FaceUri is converted to canonical form
165 * \note A new FaceUri in canonical form will be created; this FaceUri is unchanged.
166 * \param onFailure function to call if this FaceUri cannot be converted to canonical form
167 * \param timeout maximum allowable duration of the operations.
168 * It's intentional not to provide a default value: the caller should set
169 * a reasonable value in balance between network delay and user experience.
170 */
171 void
172 canonize(const CanonizeSuccessCallback& onSuccess,
173 const CanonizeFailureCallback& onFailure,
174 boost::asio::io_service& io, const time::nanoseconds& timeout) const;
175
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700176private:
177 std::string m_scheme;
178 std::string m_host;
179 /// whether to add [] around host when writing string
180 bool m_isV6;
181 std::string m_port;
182 std::string m_path;
183
184 friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri);
185};
186
187std::ostream&
188operator<<(std::ostream& os, const FaceUri& uri);
189
190} // namespace util
191} // namespace ndn
192
193#endif // NDN_UTIL_FACE_URI_HPP