blob: 1c2e636b315c54181642f2753adedf23e5d9fef7 [file] [log] [blame]
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -07002/**
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -07003 * Copyright (C) 2013 Regents of the University of California.
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -07004 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NDN_MANAGEMENT_NFD_FACE_STATUS_HPP
8#define NDN_MANAGEMENT_NFD_FACE_STATUS_HPP
9
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070010// This include must be kept as the first one, to ensure nfd-face-flags.hpp compiles on its own.
11#include "nfd-face-flags.hpp"
12
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070013#include "../encoding/tlv-nfd.hpp"
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070014#include "../encoding/encoding-buffer.hpp"
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070015
16namespace ndn {
17namespace nfd {
18
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070019/** \brief represents Face status
20 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Dataset
21 */
22class FaceStatus : public FaceFlagsTraits<FaceStatus>
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070023{
24public:
25 class Error : public Tlv::Error
26 {
27 public:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070028 explicit
29 Error(const std::string& what)
30 : Tlv::Error(what)
31 {
32 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070033 };
34
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070035 FaceStatus();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070036
37 explicit
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070038 FaceStatus(const Block& block)
39 {
40 this->wireDecode(block);
41 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070042
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070043 /** \brief prepend FaceStatus to the encoder
44 */
45 template<bool T>
46 size_t
47 wireEncode(EncodingImpl<T>& encoder) const;
48
49 /** \brief encode FaceStatus
50 */
51 const Block&
52 wireEncode() const;
53
54 /** \brief decode FaceStatus
55 */
56 void
57 wireDecode(const Block& wire);
58
59public: // getters & setters
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070060 uint64_t
61 getFaceId() const
62 {
63 return m_faceId;
64 }
65
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070066 FaceStatus&
67 setFaceId(uint64_t faceId)
68 {
69 m_wire.reset();
70 m_faceId = faceId;
71 return *this;
72 }
73
74 const std::string&
75 getRemoteUri() const
76 {
77 return m_remoteUri;
78 }
79
80 FaceStatus&
81 setRemoteUri(const std::string& remoteUri)
82 {
83 m_wire.reset();
84 m_remoteUri = remoteUri;
85 return *this;
86 }
87
88 const std::string&
89 getLocalUri() const
90 {
91 return m_localUri;
92 }
93
94 FaceStatus&
95 setLocalUri(const std::string& localUri)
96 {
97 m_wire.reset();
98 m_localUri = localUri;
99 return *this;
100 }
101
102 uint64_t
103 getFlags() const
104 {
105 return m_flags;
106 }
107
108 FaceStatus&
109 setFlags(uint64_t flags)
110 {
111 m_wire.reset();
112 m_flags = flags;
113 return *this;
114 }
115
116 uint64_t
117 getNInInterests() const
118 {
119 return m_nInInterests;
120 }
121
122 FaceStatus&
123 setNInInterests(uint64_t nInInterests)
124 {
125 m_wire.reset();
126 m_nInInterests = nInInterests;
127 return *this;
128 }
129
130 uint64_t
131 getNInDatas() const
132 {
133 return m_nInDatas;
134 }
135
136 FaceStatus&
137 setNInDatas(uint64_t nInDatas)
138 {
139 m_wire.reset();
140 m_nInDatas = nInDatas;
141 return *this;
142 }
143
144 uint64_t
145 getNOutInterests() const
146 {
147 return m_nOutInterests;
148 }
149
150 FaceStatus&
151 setNOutInterests(uint64_t nOutInterests)
152 {
153 m_wire.reset();
154 m_nOutInterests = nOutInterests;
155 return *this;
156 }
157
158 uint64_t
159 getNOutDatas() const
160 {
161 return m_nOutDatas;
162 }
163
164 FaceStatus&
165 setNOutDatas(uint64_t nOutDatas)
166 {
167 m_wire.reset();
168 m_nOutDatas = nOutDatas;
169 return *this;
170 }
171
172public: // deprecated
173 /** \deprecated
174 */
175 FaceStatus(const uint64_t faceId,
176 const std::string& uri,
177 uint64_t inInterest, uint64_t inData, uint64_t outInterest, uint64_t outData);
178
179 /** \deprecated
180 */
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700181 const std::string&
182 getUri() const
183 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700184 return this->getRemoteUri();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700185 }
186
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700187 /** \deprecated
188 */
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700189 const uint64_t
190 getInInterest() const
191 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700192 return this->getNInInterests();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700193 }
194
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700195 /** \deprecated
196 */
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700197 const uint64_t
198 getInData() const
199 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700200 return this->getNInDatas();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700201 }
202
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700203 /** \deprecated
204 */
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700205 const uint64_t
206 getOutInterest() const
207 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700208 return this->getNOutInterests();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700209 }
210
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700211 /** \deprecated
212 */
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700213 const uint64_t
214 getOutData() const
215 {
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700216 return this->getNOutDatas();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700217 }
218
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700219private:
220 uint64_t m_faceId;
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700221 std::string m_remoteUri;
222 std::string m_localUri;
223 uint64_t m_flags;
224 uint64_t m_nInInterests;
225 uint64_t m_nInDatas;
226 uint64_t m_nOutInterests;
227 uint64_t m_nOutDatas;
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700228
229 mutable Block m_wire;
230};
231
232inline
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700233FaceStatus::FaceStatus()
234 : m_faceId(0)
235 , m_flags(0)
236 , m_nInInterests(0)
237 , m_nInDatas(0)
238 , m_nOutInterests(0)
239 , m_nOutDatas(0)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700240{
241}
242
243inline
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700244FaceStatus::FaceStatus(const uint64_t faceId,
245 const std::string& uri,
246 uint64_t inInterest, uint64_t inData, uint64_t outInterest, uint64_t outData)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700247{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700248 (*this).setFaceId(faceId)
249 .setRemoteUri(uri)
250 .setNInInterests(inInterest)
251 .setNInDatas(inData)
252 .setNOutInterests(outInterest)
253 .setNOutDatas(outData);
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700254}
255
256template<bool T>
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700257inline size_t
258FaceStatus::wireEncode(EncodingImpl<T>& encoder) const
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700259{
260 size_t totalLength = 0;
261
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700262 totalLength += prependNonNegativeIntegerBlock(encoder,
263 tlv::nfd::NOutDatas, m_nOutDatas);
264 totalLength += prependNonNegativeIntegerBlock(encoder,
265 tlv::nfd::NOutInterests, m_nOutInterests);
266 totalLength += prependNonNegativeIntegerBlock(encoder,
267 tlv::nfd::NInDatas, m_nInDatas);
268 totalLength += prependNonNegativeIntegerBlock(encoder,
269 tlv::nfd::NInInterests, m_nInInterests);
270 totalLength += prependNonNegativeIntegerBlock(encoder,
271 tlv::nfd::FaceFlags, m_flags);
272 totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
273 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
274 totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
275 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
276 totalLength += prependNonNegativeIntegerBlock(encoder,
277 tlv::nfd::FaceId, m_faceId);
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700278
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700279 totalLength += encoder.prependVarNumber(totalLength);
280 totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700281 return totalLength;
282}
283
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700284inline const Block&
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700285FaceStatus::wireEncode() const
286{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700287 if (m_wire.hasWire())
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700288 return m_wire;
289
290 EncodingEstimator estimator;
291 size_t estimatedSize = wireEncode(estimator);
292
293 EncodingBuffer buffer(estimatedSize, 0);
294 wireEncode(buffer);
295
296 m_wire = buffer.block();
297 return m_wire;
298}
299
300inline void
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700301FaceStatus::wireDecode(const Block& block)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700302{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700303 if (block.type() != tlv::nfd::FaceStatus) {
304 throw Error("expecting FaceStatus block");
305 }
306 m_wire = block;
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700307 m_wire.parse();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700308 Block::element_const_iterator val = m_wire.elements_begin();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700309
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700310 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
311 m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
312 ++val;
313 }
314 else {
315 throw Error("missing required FaceId field");
316 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700317
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700318 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
319 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
320 ++val;
321 }
322 else {
323 throw Error("missing required Uri field");
324 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700325
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700326 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
327 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
328 ++val;
329 }
330 else {
331 throw Error("missing required LocalUri field");
332 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700333
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700334 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) {
335 m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
336 ++val;
337 }
338 else {
339 throw Error("missing required FaceFlags field");
340 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700341
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700342 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
343 m_nInInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
344 ++val;
345 }
346 else {
347 throw Error("missing required NInInterests field");
348 }
349
350 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
351 m_nInDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
352 ++val;
353 }
354 else {
355 throw Error("missing required NInDatas field");
356 }
357
358 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
359 m_nOutInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
360 ++val;
361 }
362 else {
363 throw Error("missing required NOutInterests field");
364 }
365
366 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
367 m_nOutDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
368 ++val;
369 }
370 else {
371 throw Error("missing required NOutDatas field");
372 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700373}
374
375inline std::ostream&
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700376operator<<(std::ostream& os, const FaceStatus& status)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700377{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700378 os << "FaceStatus("
379 << "FaceID: " << status.getFaceId() << ", "
380 << "RemoteUri: " << status.getRemoteUri() << ", "
381 << "LocalUri: " << status.getLocalUri() << ", "
382 << "Flags: " << status.getFlags() << ", "
383 << "Counters: " << status.getNInInterests() << "|" << status.getNInDatas()
384 << "|" << status.getNOutInterests() << "|" << status.getNOutDatas()
385 << ")";
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700386 return os;
387}
388
389} // namespace nfd
390} // namespace ndn
391
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700392#endif // NDN_MANAGEMENT_NFD_FACE_STATUS_HPP