blob: 6c4ac7ee2f658082dd608062715414ad1b6b1c4c [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#ifndef NDN_UTIL_FACE_URI_HPP
23#define NDN_UTIL_FACE_URI_HPP
24
25#include "common.hpp"
26#include <boost/asio/ip/udp.hpp>
27#include <boost/asio/ip/tcp.hpp>
28#include <boost/asio/local/stream_protocol.hpp>
29#include "ethernet.hpp"
30
31namespace ndn {
32namespace util {
33
34/** \brief represents the underlying protocol and address used by a Face
35 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#FaceUri
36 */
37class FaceUri
38{
39public:
40 class Error : public std::invalid_argument
41 {
42 public:
43 explicit
44 Error(const std::string& what)
45 : std::invalid_argument(what)
46 {
47 }
48 };
49
50 FaceUri();
51
52 /** \brief construct by parsing
53 *
54 * \param uri scheme://host[:port]/path
55 * \throw FaceUri::Error if URI cannot be parsed
56 */
57 explicit
58 FaceUri(const std::string& uri);
59
60 // This overload is needed so that calls with string literal won't be
61 // resolved to boost::asio::local::stream_protocol::endpoint overload.
62 explicit
63 FaceUri(const char* uri);
64
65 /// exception-safe parsing
66 bool
67 parse(const std::string& uri);
68
69public: // scheme-specific construction
70 /// construct udp4 or udp6 canonical FaceUri
71 explicit
72 FaceUri(const boost::asio::ip::udp::endpoint& endpoint);
73
74 /// construct tcp4 or tcp6 canonical FaceUri
75 explicit
76 FaceUri(const boost::asio::ip::tcp::endpoint& endpoint);
77
78 /// construct tcp canonical FaceUri with customized scheme
79 FaceUri(const boost::asio::ip::tcp::endpoint& endpoint, const std::string& scheme);
80
81#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
82 /// construct unix canonical FaceUri
83 explicit
84 FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint);
85#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS
86
87 /// create fd FaceUri from file descriptor
88 static FaceUri
89 fromFd(int fd);
90
91 /// construct ether canonical FaceUri
92 explicit
93 FaceUri(const ethernet::Address& address);
94
95 /// create dev FaceUri from network device name
96 static FaceUri
97 fromDev(const std::string& ifname);
98
99public: // getters
100 /// get scheme (protocol)
101 const std::string&
102 getScheme() const
103 {
104 return m_scheme;
105 }
106
107 /// get host (domain)
108 const std::string&
109 getHost() const
110 {
111 return m_host;
112 }
113
114 /// get port
115 const std::string&
116 getPort() const
117 {
118 return m_port;
119 }
120
121 /// get path
122 const std::string&
123 getPath() const
124 {
125 return m_path;
126 }
127
128 /// write as a string
129 std::string
130 toString() const;
131
132public: // EqualityComparable concept
133 bool
134 operator==(const FaceUri& rhs) const;
135
136 bool
137 operator!=(const FaceUri& rhs) const;
138
139private:
140 std::string m_scheme;
141 std::string m_host;
142 /// whether to add [] around host when writing string
143 bool m_isV6;
144 std::string m_port;
145 std::string m_path;
146
147 friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri);
148};
149
150std::ostream&
151operator<<(std::ostream& os, const FaceUri& uri);
152
153} // namespace util
154} // namespace ndn
155
156#endif // NDN_UTIL_FACE_URI_HPP