blob: b4ae417db6baa9de6bc590d904eb313099003950 [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/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070011 */
12
13#ifndef NDN_MANAGEMENT_NFD_FACE_STATUS_HPP
14#define NDN_MANAGEMENT_NFD_FACE_STATUS_HPP
15
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070016// This include must be kept as the first one, to ensure nfd-face-flags.hpp compiles on its own.
17#include "nfd-face-flags.hpp"
18
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070019#include "../encoding/tlv-nfd.hpp"
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070020#include "../encoding/encoding-buffer.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070021#include "../encoding/block-helpers.hpp"
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070022
23namespace ndn {
24namespace nfd {
25
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040026/**
27 * \ingroup management
28 * \brief represents Face status
29 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Dataset
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070030 */
31class FaceStatus : public FaceFlagsTraits<FaceStatus>
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070032{
33public:
34 class Error : public Tlv::Error
35 {
36 public:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070037 explicit
38 Error(const std::string& what)
39 : Tlv::Error(what)
40 {
41 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070042 };
43
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070044 FaceStatus();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070045
46 explicit
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070047 FaceStatus(const Block& block)
48 {
49 this->wireDecode(block);
50 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070051
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070052 /** \brief prepend FaceStatus to the encoder
53 */
54 template<bool T>
55 size_t
56 wireEncode(EncodingImpl<T>& encoder) const;
57
58 /** \brief encode FaceStatus
59 */
60 const Block&
61 wireEncode() const;
62
63 /** \brief decode FaceStatus
64 */
65 void
66 wireDecode(const Block& wire);
67
68public: // getters & setters
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070069 uint64_t
70 getFaceId() const
71 {
72 return m_faceId;
73 }
74
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070075 FaceStatus&
76 setFaceId(uint64_t faceId)
77 {
78 m_wire.reset();
79 m_faceId = faceId;
80 return *this;
81 }
82
83 const std::string&
84 getRemoteUri() const
85 {
86 return m_remoteUri;
87 }
88
89 FaceStatus&
90 setRemoteUri(const std::string& remoteUri)
91 {
92 m_wire.reset();
93 m_remoteUri = remoteUri;
94 return *this;
95 }
96
97 const std::string&
98 getLocalUri() const
99 {
100 return m_localUri;
101 }
102
103 FaceStatus&
104 setLocalUri(const std::string& localUri)
105 {
106 m_wire.reset();
107 m_localUri = localUri;
108 return *this;
109 }
110
111 uint64_t
112 getFlags() const
113 {
114 return m_flags;
115 }
116
117 FaceStatus&
118 setFlags(uint64_t flags)
119 {
120 m_wire.reset();
121 m_flags = flags;
122 return *this;
123 }
124
125 uint64_t
126 getNInInterests() const
127 {
128 return m_nInInterests;
129 }
130
131 FaceStatus&
132 setNInInterests(uint64_t nInInterests)
133 {
134 m_wire.reset();
135 m_nInInterests = nInInterests;
136 return *this;
137 }
138
139 uint64_t
140 getNInDatas() const
141 {
142 return m_nInDatas;
143 }
144
145 FaceStatus&
146 setNInDatas(uint64_t nInDatas)
147 {
148 m_wire.reset();
149 m_nInDatas = nInDatas;
150 return *this;
151 }
152
153 uint64_t
154 getNOutInterests() const
155 {
156 return m_nOutInterests;
157 }
158
159 FaceStatus&
160 setNOutInterests(uint64_t nOutInterests)
161 {
162 m_wire.reset();
163 m_nOutInterests = nOutInterests;
164 return *this;
165 }
166
167 uint64_t
168 getNOutDatas() const
169 {
170 return m_nOutDatas;
171 }
172
173 FaceStatus&
174 setNOutDatas(uint64_t nOutDatas)
175 {
176 m_wire.reset();
177 m_nOutDatas = nOutDatas;
178 return *this;
179 }
180
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700181private:
182 uint64_t m_faceId;
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700183 std::string m_remoteUri;
184 std::string m_localUri;
185 uint64_t m_flags;
186 uint64_t m_nInInterests;
187 uint64_t m_nInDatas;
188 uint64_t m_nOutInterests;
189 uint64_t m_nOutDatas;
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700190
191 mutable Block m_wire;
192};
193
194inline
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700195FaceStatus::FaceStatus()
196 : m_faceId(0)
197 , m_flags(0)
198 , m_nInInterests(0)
199 , m_nInDatas(0)
200 , m_nOutInterests(0)
201 , m_nOutDatas(0)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700202{
203}
204
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700205template<bool T>
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700206inline size_t
207FaceStatus::wireEncode(EncodingImpl<T>& encoder) const
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700208{
209 size_t totalLength = 0;
210
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700211 totalLength += prependNonNegativeIntegerBlock(encoder,
212 tlv::nfd::NOutDatas, m_nOutDatas);
213 totalLength += prependNonNegativeIntegerBlock(encoder,
214 tlv::nfd::NOutInterests, m_nOutInterests);
215 totalLength += prependNonNegativeIntegerBlock(encoder,
216 tlv::nfd::NInDatas, m_nInDatas);
217 totalLength += prependNonNegativeIntegerBlock(encoder,
218 tlv::nfd::NInInterests, m_nInInterests);
219 totalLength += prependNonNegativeIntegerBlock(encoder,
220 tlv::nfd::FaceFlags, m_flags);
221 totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
222 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
223 totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
224 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
225 totalLength += prependNonNegativeIntegerBlock(encoder,
226 tlv::nfd::FaceId, m_faceId);
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700227
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700228 totalLength += encoder.prependVarNumber(totalLength);
229 totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700230 return totalLength;
231}
232
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700233inline const Block&
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700234FaceStatus::wireEncode() const
235{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700236 if (m_wire.hasWire())
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700237 return m_wire;
238
239 EncodingEstimator estimator;
240 size_t estimatedSize = wireEncode(estimator);
241
242 EncodingBuffer buffer(estimatedSize, 0);
243 wireEncode(buffer);
244
245 m_wire = buffer.block();
246 return m_wire;
247}
248
249inline void
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700250FaceStatus::wireDecode(const Block& block)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700251{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700252 if (block.type() != tlv::nfd::FaceStatus) {
253 throw Error("expecting FaceStatus block");
254 }
255 m_wire = block;
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700256 m_wire.parse();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700257 Block::element_const_iterator val = m_wire.elements_begin();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700258
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700259 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
260 m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
261 ++val;
262 }
263 else {
264 throw Error("missing required FaceId field");
265 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700266
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700267 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
268 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
269 ++val;
270 }
271 else {
272 throw Error("missing required Uri field");
273 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700274
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700275 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
276 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
277 ++val;
278 }
279 else {
280 throw Error("missing required LocalUri field");
281 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700282
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700283 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) {
284 m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
285 ++val;
286 }
287 else {
288 throw Error("missing required FaceFlags field");
289 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700290
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700291 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
292 m_nInInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
293 ++val;
294 }
295 else {
296 throw Error("missing required NInInterests field");
297 }
298
299 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
300 m_nInDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
301 ++val;
302 }
303 else {
304 throw Error("missing required NInDatas field");
305 }
306
307 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
308 m_nOutInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
309 ++val;
310 }
311 else {
312 throw Error("missing required NOutInterests field");
313 }
314
315 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
316 m_nOutDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
317 ++val;
318 }
319 else {
320 throw Error("missing required NOutDatas field");
321 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700322}
323
324inline std::ostream&
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700325operator<<(std::ostream& os, const FaceStatus& status)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700326{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700327 os << "FaceStatus("
328 << "FaceID: " << status.getFaceId() << ", "
329 << "RemoteUri: " << status.getRemoteUri() << ", "
330 << "LocalUri: " << status.getLocalUri() << ", "
331 << "Flags: " << status.getFlags() << ", "
332 << "Counters: " << status.getNInInterests() << "|" << status.getNInDatas()
333 << "|" << status.getNOutInterests() << "|" << status.getNOutDatas()
334 << ")";
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700335 return os;
336}
337
338} // namespace nfd
339} // namespace ndn
340
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700341#endif // NDN_MANAGEMENT_NFD_FACE_STATUS_HPP