blob: db4aef4587f1f73d98f07145e3734aa21945d2ed [file] [log] [blame]
Chengyu Fan36dca992014-09-25 13:42:03 -06001/* -*- 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_MANAGEMENT_NFD_FACE_TRAITS_HPP
23#define NDN_MANAGEMENT_NFD_FACE_TRAITS_HPP
24
25#include "../encoding/tlv-nfd.hpp"
26#include "../encoding/encoding-buffer.hpp"
27#include "../encoding/block-helpers.hpp"
28
29namespace ndn {
30namespace nfd {
31
32/**
33 * \ingroup management
34 * \deprecated provides additional information about a face
35 */
36enum FaceFlags {
37 /** \deprecated face is local (for scope control purpose)
38 */
39 FACE_IS_LOCAL = 1,
40 /** \deprecated face is created on demand (accepted incoming connection,
41 * not initiated outgoing connection)
42 */
43 FACE_IS_ON_DEMAND = 2
44};
45
46/**
47 * \ingroup management
48 * \brief provides additional information about a faceScope
49 */
50enum FaceScope {
51 /** \brief face is non-local
52 */
53 FACE_SCOPE_NON_LOCAL = 0,
54 /** \brief face is local
55 */
56 FACE_SCOPE_LOCAL = 1
57};
58
59std::ostream&
60operator<<(std::ostream& os, FaceScope faceScope);
61
62/**
63 * \ingroup management
64 * \brief provides additional information about a facePersistency
65 */
66enum FacePersistency {
67 /** \brief face is persistent
68 */
69 FACE_PERSISTENCY_PERSISTENT = 0,
70 /** \brief face is on-demand
71 */
72 FACE_PERSISTENCY_ON_DEMAND = 1,
73 /** \brief face is permanent
74 */
75 FACE_PERSISTENCY_PERMANENT = 2
76};
77
78std::ostream&
79operator<<(std::ostream& os, FacePersistency facePersistency);
80
81/**
82 * \ingroup management
83 * \brief provides additional information about a linkType
84 */
85enum LinkType {
86 /** \brief link is point-to-point
87 */
88 LINK_TYPE_POINT_TO_POINT = 0,
89 /** \brief link is multi-access
90 */
91 LINK_TYPE_MULTI_ACCESS = 1
92};
93
94std::ostream&
95operator<<(std::ostream& os, LinkType linkType);
96
97/** \ingroup management
98 * \brief providers getters and setters of face information fields
99 * \tparam C the concrete class; it must provide .wireReset() method
100 to clear wire encoding when a field changes
101 */
102template<class C>
103class FaceTraits
104{
105public:
106 class Error : public tlv::Error
107 {
108 public:
109 explicit
110 Error(const std::string& what)
111 : tlv::Error(what)
112 {
113 }
114 };
115
116 FaceTraits()
117 : m_faceId(0)
118 , m_faceScope(FACE_SCOPE_NON_LOCAL)
119 , m_facePersistency(FACE_PERSISTENCY_PERSISTENT)
120 , m_linkType(LINK_TYPE_POINT_TO_POINT)
121 {
122 }
123
124 uint64_t
125 getFaceId() const
126 {
127 return m_faceId;
128 }
129
130 C&
131 setFaceId(uint64_t faceId)
132 {
133 wireReset();
134 m_faceId = faceId;
135 return static_cast<C&>(*this);
136 }
137
138 const std::string&
139 getRemoteUri() const
140 {
141 return m_remoteUri;
142 }
143
144 C&
145 setRemoteUri(const std::string& remoteUri)
146 {
147 wireReset();
148 m_remoteUri = remoteUri;
149 return static_cast<C&>(*this);
150 }
151
152 const std::string&
153 getLocalUri() const
154 {
155 return m_localUri;
156 }
157
158 C&
159 setLocalUri(const std::string& localUri)
160 {
161 wireReset();
162 m_localUri = localUri;
163 return static_cast<C&>(*this);
164 }
165
166 FaceScope
167 getFaceScope() const
168 {
169 return m_faceScope;
170 }
171
172 C&
173 setFaceScope(FaceScope faceScope)
174 {
175 wireReset();
176 m_faceScope = faceScope;
177 return static_cast<C&>(*this);
178 }
179
180 FacePersistency
181 getFacePersistency() const
182 {
183 return m_facePersistency;
184 }
185
186 C&
187 setFacePersistency(FacePersistency facePersistency)
188 {
189 wireReset();
190 m_facePersistency = facePersistency;
191 return static_cast<C&>(*this);
192 }
193
194 LinkType
195 getLinkType() const
196 {
197 return m_linkType;
198 }
199
200 C&
201 setLinkType(LinkType linkType)
202 {
203 wireReset();
204 m_linkType = linkType;
205 return static_cast<C&>(*this);
206 }
207
208 /**
209 * \deprecated Use getFaceScope instead
210 */
211 bool
212 isLocal() const
213 {
214 return getFlags() & FACE_IS_LOCAL;
215 }
216
217 /**
218 * \deprecated Use getFacePersistency instead
219 */
220 bool
221 isOnDemand() const
222 {
223 return getFlags() & FACE_IS_ON_DEMAND;
224 }
225
226 /**
227 * \deprecated Use getFaceScope, and getFacePersistency instead
228 */
229 uint64_t
230 getFlags() const
231 {
232 uint64_t flags = 0;
233 if (m_faceScope == FACE_SCOPE_LOCAL)
234 flags |= FACE_IS_LOCAL;
235 if (m_facePersistency == FACE_PERSISTENCY_ON_DEMAND)
236 flags |= FACE_IS_ON_DEMAND;
237 return flags;
238 }
239
240 /**
241 * \deprecated Use setFaceScope, and setFacePersistency instead
242 */
243 C&
244 setFlags(uint64_t flags)
245 {
246 wireReset();
247 if ((flags & FACE_IS_LOCAL) != 0)
248 m_faceScope = FACE_SCOPE_LOCAL;
249 else
250 m_faceScope = FACE_SCOPE_NON_LOCAL;
251 if ((flags & FACE_IS_ON_DEMAND) != 0)
252 m_facePersistency = FACE_PERSISTENCY_ON_DEMAND;
253 else
254 m_facePersistency = FACE_PERSISTENCY_PERSISTENT;
255 return static_cast<C&>(*this);
256 }
257
258protected:
259 virtual void
260 wireReset() const = 0;
261
262protected:
263 uint64_t m_faceId;
264 std::string m_remoteUri;
265 std::string m_localUri;
266 FaceScope m_faceScope;
267 FacePersistency m_facePersistency;
268 LinkType m_linkType;
269};
270
271} // namespace nfd
272} // namespace ndn
273
274#endif // NDN_MANAGEMENT_NFD_FACE_TRAITS_HPP