blob: a92e154ee8c3386623eee9fb98e86a5b6269f946 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070020 */
21
22#ifndef NDN_MANAGEMENT_NFD_FACE_STATUS_HPP
23#define NDN_MANAGEMENT_NFD_FACE_STATUS_HPP
24
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070025// This include must be kept as the first one, to ensure nfd-face-flags.hpp compiles on its own.
26#include "nfd-face-flags.hpp"
27
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070028#include "../encoding/tlv-nfd.hpp"
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070029#include "../encoding/encoding-buffer.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070030#include "../encoding/block-helpers.hpp"
Alexander Afanasyeve63eaf62014-06-30 12:40:55 -070031#include "../util/time.hpp"
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070032
33namespace ndn {
34namespace nfd {
35
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040036/**
37 * \ingroup management
38 * \brief represents Face status
39 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Dataset
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070040 */
41class FaceStatus : public FaceFlagsTraits<FaceStatus>
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070042{
43public:
44 class Error : public Tlv::Error
45 {
46 public:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070047 explicit
48 Error(const std::string& what)
49 : Tlv::Error(what)
50 {
51 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070052 };
53
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070054 FaceStatus();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070055
56 explicit
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070057 FaceStatus(const Block& block)
58 {
59 this->wireDecode(block);
60 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070061
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070062 /** \brief prepend FaceStatus to the encoder
63 */
64 template<bool T>
65 size_t
66 wireEncode(EncodingImpl<T>& encoder) const;
67
68 /** \brief encode FaceStatus
69 */
70 const Block&
71 wireEncode() const;
72
73 /** \brief decode FaceStatus
74 */
75 void
76 wireDecode(const Block& wire);
77
78public: // getters & setters
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070079 uint64_t
80 getFaceId() const
81 {
82 return m_faceId;
83 }
84
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070085 FaceStatus&
86 setFaceId(uint64_t faceId)
87 {
88 m_wire.reset();
89 m_faceId = faceId;
90 return *this;
91 }
92
93 const std::string&
94 getRemoteUri() const
95 {
96 return m_remoteUri;
97 }
98
99 FaceStatus&
100 setRemoteUri(const std::string& remoteUri)
101 {
102 m_wire.reset();
103 m_remoteUri = remoteUri;
104 return *this;
105 }
106
107 const std::string&
108 getLocalUri() const
109 {
110 return m_localUri;
111 }
112
113 FaceStatus&
114 setLocalUri(const std::string& localUri)
115 {
116 m_wire.reset();
117 m_localUri = localUri;
118 return *this;
119 }
120
Alexander Afanasyeve63eaf62014-06-30 12:40:55 -0700121 bool
122 hasExpirationPeriod() const
123 {
124 return m_hasExpirationPeriod;
125 }
126
127 const time::milliseconds&
128 getExpirationPeriod() const
129 {
130 BOOST_ASSERT(m_hasExpirationPeriod);
131 return m_expirationPeriod;
132 }
133
134 FaceStatus&
135 setExpirationPeriod(const time::milliseconds& expirationPeriod)
136 {
137 m_expirationPeriod = expirationPeriod;
138 m_hasExpirationPeriod = true;
139 return *this;
140 }
141
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700142 uint64_t
143 getFlags() const
144 {
145 return m_flags;
146 }
147
148 FaceStatus&
149 setFlags(uint64_t flags)
150 {
151 m_wire.reset();
152 m_flags = flags;
153 return *this;
154 }
155
156 uint64_t
157 getNInInterests() const
158 {
159 return m_nInInterests;
160 }
161
162 FaceStatus&
163 setNInInterests(uint64_t nInInterests)
164 {
165 m_wire.reset();
166 m_nInInterests = nInInterests;
167 return *this;
168 }
169
170 uint64_t
171 getNInDatas() const
172 {
173 return m_nInDatas;
174 }
175
176 FaceStatus&
177 setNInDatas(uint64_t nInDatas)
178 {
179 m_wire.reset();
180 m_nInDatas = nInDatas;
181 return *this;
182 }
183
184 uint64_t
185 getNOutInterests() const
186 {
187 return m_nOutInterests;
188 }
189
190 FaceStatus&
191 setNOutInterests(uint64_t nOutInterests)
192 {
193 m_wire.reset();
194 m_nOutInterests = nOutInterests;
195 return *this;
196 }
197
198 uint64_t
199 getNOutDatas() const
200 {
201 return m_nOutDatas;
202 }
203
204 FaceStatus&
205 setNOutDatas(uint64_t nOutDatas)
206 {
207 m_wire.reset();
208 m_nOutDatas = nOutDatas;
209 return *this;
210 }
211
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700212private:
213 uint64_t m_faceId;
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700214 std::string m_remoteUri;
215 std::string m_localUri;
Alexander Afanasyeve63eaf62014-06-30 12:40:55 -0700216 time::milliseconds m_expirationPeriod;
217 bool m_hasExpirationPeriod;
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700218 uint64_t m_flags;
219 uint64_t m_nInInterests;
220 uint64_t m_nInDatas;
221 uint64_t m_nOutInterests;
222 uint64_t m_nOutDatas;
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700223
224 mutable Block m_wire;
225};
226
227inline
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700228FaceStatus::FaceStatus()
229 : m_faceId(0)
Alexander Afanasyeve63eaf62014-06-30 12:40:55 -0700230 , m_hasExpirationPeriod(false)
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700231 , m_flags(0)
232 , m_nInInterests(0)
233 , m_nInDatas(0)
234 , m_nOutInterests(0)
235 , m_nOutDatas(0)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700236{
237}
238
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700239template<bool T>
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700240inline size_t
241FaceStatus::wireEncode(EncodingImpl<T>& encoder) const
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700242{
243 size_t totalLength = 0;
244
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700245 totalLength += prependNonNegativeIntegerBlock(encoder,
246 tlv::nfd::NOutDatas, m_nOutDatas);
247 totalLength += prependNonNegativeIntegerBlock(encoder,
248 tlv::nfd::NOutInterests, m_nOutInterests);
249 totalLength += prependNonNegativeIntegerBlock(encoder,
250 tlv::nfd::NInDatas, m_nInDatas);
251 totalLength += prependNonNegativeIntegerBlock(encoder,
252 tlv::nfd::NInInterests, m_nInInterests);
253 totalLength += prependNonNegativeIntegerBlock(encoder,
254 tlv::nfd::FaceFlags, m_flags);
Alexander Afanasyeve63eaf62014-06-30 12:40:55 -0700255 if (m_hasExpirationPeriod) {
256 totalLength += prependNonNegativeIntegerBlock(encoder,
257 tlv::nfd::ExpirationPeriod, m_expirationPeriod.count());
258 }
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700259 totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
260 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
261 totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
262 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
263 totalLength += prependNonNegativeIntegerBlock(encoder,
264 tlv::nfd::FaceId, m_faceId);
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700265
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700266 totalLength += encoder.prependVarNumber(totalLength);
267 totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700268 return totalLength;
269}
270
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700271inline const Block&
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700272FaceStatus::wireEncode() const
273{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700274 if (m_wire.hasWire())
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700275 return m_wire;
276
277 EncodingEstimator estimator;
278 size_t estimatedSize = wireEncode(estimator);
279
280 EncodingBuffer buffer(estimatedSize, 0);
281 wireEncode(buffer);
282
283 m_wire = buffer.block();
284 return m_wire;
285}
286
287inline void
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700288FaceStatus::wireDecode(const Block& block)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700289{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700290 if (block.type() != tlv::nfd::FaceStatus) {
291 throw Error("expecting FaceStatus block");
292 }
293 m_wire = block;
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700294 m_wire.parse();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700295 Block::element_const_iterator val = m_wire.elements_begin();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700296
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700297 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
Alexander Afanasyeve63eaf62014-06-30 12:40:55 -0700298 m_faceId = readNonNegativeInteger(*val);
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700299 ++val;
300 }
301 else {
302 throw Error("missing required FaceId field");
303 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700304
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700305 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
306 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
307 ++val;
308 }
309 else {
310 throw Error("missing required Uri field");
311 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700312
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700313 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
314 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
315 ++val;
316 }
317 else {
318 throw Error("missing required LocalUri field");
319 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700320
Alexander Afanasyeve63eaf62014-06-30 12:40:55 -0700321 if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
322 m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
323 m_hasExpirationPeriod = true;
324 ++val;
325 }
326 else {
327 m_hasExpirationPeriod = false;
328 // ExpirationPeriod is optional
329 }
330
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700331 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) {
Alexander Afanasyeve63eaf62014-06-30 12:40:55 -0700332 m_flags = readNonNegativeInteger(*val);
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700333 ++val;
334 }
335 else {
336 throw Error("missing required FaceFlags field");
337 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700338
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700339 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
Alexander Afanasyeve63eaf62014-06-30 12:40:55 -0700340 m_nInInterests = readNonNegativeInteger(*val);
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700341 ++val;
342 }
343 else {
344 throw Error("missing required NInInterests field");
345 }
346
347 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
Alexander Afanasyeve63eaf62014-06-30 12:40:55 -0700348 m_nInDatas = readNonNegativeInteger(*val);
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700349 ++val;
350 }
351 else {
352 throw Error("missing required NInDatas field");
353 }
354
355 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
Alexander Afanasyeve63eaf62014-06-30 12:40:55 -0700356 m_nOutInterests = readNonNegativeInteger(*val);
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700357 ++val;
358 }
359 else {
360 throw Error("missing required NOutInterests field");
361 }
362
363 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
Alexander Afanasyeve63eaf62014-06-30 12:40:55 -0700364 m_nOutDatas = readNonNegativeInteger(*val);
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700365 ++val;
366 }
367 else {
368 throw Error("missing required NOutDatas field");
369 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700370}
371
372inline std::ostream&
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700373operator<<(std::ostream& os, const FaceStatus& status)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700374{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700375 os << "FaceStatus("
376 << "FaceID: " << status.getFaceId() << ", "
377 << "RemoteUri: " << status.getRemoteUri() << ", "
Alexander Afanasyeve63eaf62014-06-30 12:40:55 -0700378 << "LocalUri: " << status.getLocalUri() << ", ";
379
380 if (status.hasExpirationPeriod()) {
381 os << "ExpirationPeriod: " << status.getExpirationPeriod() << ", ";
382 }
383 else {
384 os << "ExpirationPeriod: infinite, ";
385 }
386
387 os << "Flags: " << status.getFlags() << ", "
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700388 << "Counters: " << status.getNInInterests() << "|" << status.getNInDatas()
389 << "|" << status.getNOutInterests() << "|" << status.getNOutDatas()
390 << ")";
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700391 return os;
392}
393
394} // namespace nfd
395} // namespace ndn
396
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700397#endif // NDN_MANAGEMENT_NFD_FACE_STATUS_HPP