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