blob: 02e9b9a3181ac9121f7712c73cddefc7b72cdc7a [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
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070026/** \brief represents Face status
27 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Dataset
28 */
29class FaceStatus : public FaceFlagsTraits<FaceStatus>
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070030{
31public:
32 class Error : public Tlv::Error
33 {
34 public:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070035 explicit
36 Error(const std::string& what)
37 : Tlv::Error(what)
38 {
39 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070040 };
41
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070042 FaceStatus();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070043
44 explicit
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070045 FaceStatus(const Block& block)
46 {
47 this->wireDecode(block);
48 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070049
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070050 /** \brief prepend FaceStatus to the encoder
51 */
52 template<bool T>
53 size_t
54 wireEncode(EncodingImpl<T>& encoder) const;
55
56 /** \brief encode FaceStatus
57 */
58 const Block&
59 wireEncode() const;
60
61 /** \brief decode FaceStatus
62 */
63 void
64 wireDecode(const Block& wire);
65
66public: // getters & setters
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070067 uint64_t
68 getFaceId() const
69 {
70 return m_faceId;
71 }
72
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070073 FaceStatus&
74 setFaceId(uint64_t faceId)
75 {
76 m_wire.reset();
77 m_faceId = faceId;
78 return *this;
79 }
80
81 const std::string&
82 getRemoteUri() const
83 {
84 return m_remoteUri;
85 }
86
87 FaceStatus&
88 setRemoteUri(const std::string& remoteUri)
89 {
90 m_wire.reset();
91 m_remoteUri = remoteUri;
92 return *this;
93 }
94
95 const std::string&
96 getLocalUri() const
97 {
98 return m_localUri;
99 }
100
101 FaceStatus&
102 setLocalUri(const std::string& localUri)
103 {
104 m_wire.reset();
105 m_localUri = localUri;
106 return *this;
107 }
108
109 uint64_t
110 getFlags() const
111 {
112 return m_flags;
113 }
114
115 FaceStatus&
116 setFlags(uint64_t flags)
117 {
118 m_wire.reset();
119 m_flags = flags;
120 return *this;
121 }
122
123 uint64_t
124 getNInInterests() const
125 {
126 return m_nInInterests;
127 }
128
129 FaceStatus&
130 setNInInterests(uint64_t nInInterests)
131 {
132 m_wire.reset();
133 m_nInInterests = nInInterests;
134 return *this;
135 }
136
137 uint64_t
138 getNInDatas() const
139 {
140 return m_nInDatas;
141 }
142
143 FaceStatus&
144 setNInDatas(uint64_t nInDatas)
145 {
146 m_wire.reset();
147 m_nInDatas = nInDatas;
148 return *this;
149 }
150
151 uint64_t
152 getNOutInterests() const
153 {
154 return m_nOutInterests;
155 }
156
157 FaceStatus&
158 setNOutInterests(uint64_t nOutInterests)
159 {
160 m_wire.reset();
161 m_nOutInterests = nOutInterests;
162 return *this;
163 }
164
165 uint64_t
166 getNOutDatas() const
167 {
168 return m_nOutDatas;
169 }
170
171 FaceStatus&
172 setNOutDatas(uint64_t nOutDatas)
173 {
174 m_wire.reset();
175 m_nOutDatas = nOutDatas;
176 return *this;
177 }
178
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700179private:
180 uint64_t m_faceId;
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700181 std::string m_remoteUri;
182 std::string m_localUri;
183 uint64_t m_flags;
184 uint64_t m_nInInterests;
185 uint64_t m_nInDatas;
186 uint64_t m_nOutInterests;
187 uint64_t m_nOutDatas;
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700188
189 mutable Block m_wire;
190};
191
192inline
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700193FaceStatus::FaceStatus()
194 : m_faceId(0)
195 , m_flags(0)
196 , m_nInInterests(0)
197 , m_nInDatas(0)
198 , m_nOutInterests(0)
199 , m_nOutDatas(0)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700200{
201}
202
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700203template<bool T>
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700204inline size_t
205FaceStatus::wireEncode(EncodingImpl<T>& encoder) const
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700206{
207 size_t totalLength = 0;
208
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700209 totalLength += prependNonNegativeIntegerBlock(encoder,
210 tlv::nfd::NOutDatas, m_nOutDatas);
211 totalLength += prependNonNegativeIntegerBlock(encoder,
212 tlv::nfd::NOutInterests, m_nOutInterests);
213 totalLength += prependNonNegativeIntegerBlock(encoder,
214 tlv::nfd::NInDatas, m_nInDatas);
215 totalLength += prependNonNegativeIntegerBlock(encoder,
216 tlv::nfd::NInInterests, m_nInInterests);
217 totalLength += prependNonNegativeIntegerBlock(encoder,
218 tlv::nfd::FaceFlags, m_flags);
219 totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
220 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
221 totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
222 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
223 totalLength += prependNonNegativeIntegerBlock(encoder,
224 tlv::nfd::FaceId, m_faceId);
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700225
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700226 totalLength += encoder.prependVarNumber(totalLength);
227 totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700228 return totalLength;
229}
230
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700231inline const Block&
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700232FaceStatus::wireEncode() const
233{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700234 if (m_wire.hasWire())
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700235 return m_wire;
236
237 EncodingEstimator estimator;
238 size_t estimatedSize = wireEncode(estimator);
239
240 EncodingBuffer buffer(estimatedSize, 0);
241 wireEncode(buffer);
242
243 m_wire = buffer.block();
244 return m_wire;
245}
246
247inline void
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700248FaceStatus::wireDecode(const Block& block)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700249{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700250 if (block.type() != tlv::nfd::FaceStatus) {
251 throw Error("expecting FaceStatus block");
252 }
253 m_wire = block;
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700254 m_wire.parse();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700255 Block::element_const_iterator val = m_wire.elements_begin();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700256
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700257 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
258 m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
259 ++val;
260 }
261 else {
262 throw Error("missing required FaceId field");
263 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700264
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700265 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
266 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
267 ++val;
268 }
269 else {
270 throw Error("missing required Uri field");
271 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700272
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700273 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
274 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
275 ++val;
276 }
277 else {
278 throw Error("missing required LocalUri field");
279 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700280
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700281 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) {
282 m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
283 ++val;
284 }
285 else {
286 throw Error("missing required FaceFlags field");
287 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700288
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700289 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
290 m_nInInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
291 ++val;
292 }
293 else {
294 throw Error("missing required NInInterests field");
295 }
296
297 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
298 m_nInDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
299 ++val;
300 }
301 else {
302 throw Error("missing required NInDatas field");
303 }
304
305 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
306 m_nOutInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
307 ++val;
308 }
309 else {
310 throw Error("missing required NOutInterests field");
311 }
312
313 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
314 m_nOutDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
315 ++val;
316 }
317 else {
318 throw Error("missing required NOutDatas field");
319 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700320}
321
322inline std::ostream&
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700323operator<<(std::ostream& os, const FaceStatus& status)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700324{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700325 os << "FaceStatus("
326 << "FaceID: " << status.getFaceId() << ", "
327 << "RemoteUri: " << status.getRemoteUri() << ", "
328 << "LocalUri: " << status.getLocalUri() << ", "
329 << "Flags: " << status.getFlags() << ", "
330 << "Counters: " << status.getNInInterests() << "|" << status.getNInDatas()
331 << "|" << status.getNOutInterests() << "|" << status.getNOutDatas()
332 << ")";
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700333 return os;
334}
335
336} // namespace nfd
337} // namespace ndn
338
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700339#endif // NDN_MANAGEMENT_NFD_FACE_STATUS_HPP