blob: f2786e17a03c522cb2f37b161e848a09e1b0e9e6 [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"
36
37namespace ndn {
38namespace util {
39
40/** \brief represents the underlying protocol and address used by a Face
41 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#FaceUri
42 */
43class FaceUri
44{
45public:
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
75public: // 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
84 /// construct tcp canonical FaceUri with customized scheme
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
105public: // getters
106 /// get scheme (protocol)
107 const std::string&
108 getScheme() const
109 {
110 return m_scheme;
111 }
112
113 /// get host (domain)
114 const std::string&
115 getHost() const
116 {
117 return m_host;
118 }
119
120 /// get port
121 const std::string&
122 getPort() const
123 {
124 return m_port;
125 }
126
127 /// get path
128 const std::string&
129 getPath() const
130 {
131 return m_path;
132 }
133
134 /// write as a string
135 std::string
136 toString() const;
137
138public: // EqualityComparable concept
139 bool
140 operator==(const FaceUri& rhs) const;
141
142 bool
143 operator!=(const FaceUri& rhs) const;
144
145private:
146 std::string m_scheme;
147 std::string m_host;
148 /// whether to add [] around host when writing string
149 bool m_isV6;
150 std::string m_port;
151 std::string m_path;
152
153 friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri);
154};
155
156std::ostream&
157operator<<(std::ostream& os, const FaceUri& uri);
158
159} // namespace util
160} // namespace ndn
161
162#endif // NDN_UTIL_FACE_URI_HPP