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