blob: b3cf120544e4ba7496a971e6818377fd2335242d [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 Afanasyev04fa37a2014-03-19 18:06:22 -070021
22namespace ndn {
23namespace nfd {
24
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070025/** \brief represents Face status
26 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Dataset
27 */
28class FaceStatus : public FaceFlagsTraits<FaceStatus>
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070029{
30public:
31 class Error : public Tlv::Error
32 {
33 public:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070034 explicit
35 Error(const std::string& what)
36 : Tlv::Error(what)
37 {
38 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070039 };
40
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070041 FaceStatus();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070042
43 explicit
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070044 FaceStatus(const Block& block)
45 {
46 this->wireDecode(block);
47 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070048
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070049 /** \brief prepend FaceStatus to the encoder
50 */
51 template<bool T>
52 size_t
53 wireEncode(EncodingImpl<T>& encoder) const;
54
55 /** \brief encode FaceStatus
56 */
57 const Block&
58 wireEncode() const;
59
60 /** \brief decode FaceStatus
61 */
62 void
63 wireDecode(const Block& wire);
64
65public: // getters & setters
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070066 uint64_t
67 getFaceId() const
68 {
69 return m_faceId;
70 }
71
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070072 FaceStatus&
73 setFaceId(uint64_t faceId)
74 {
75 m_wire.reset();
76 m_faceId = faceId;
77 return *this;
78 }
79
80 const std::string&
81 getRemoteUri() const
82 {
83 return m_remoteUri;
84 }
85
86 FaceStatus&
87 setRemoteUri(const std::string& remoteUri)
88 {
89 m_wire.reset();
90 m_remoteUri = remoteUri;
91 return *this;
92 }
93
94 const std::string&
95 getLocalUri() const
96 {
97 return m_localUri;
98 }
99
100 FaceStatus&
101 setLocalUri(const std::string& localUri)
102 {
103 m_wire.reset();
104 m_localUri = localUri;
105 return *this;
106 }
107
108 uint64_t
109 getFlags() const
110 {
111 return m_flags;
112 }
113
114 FaceStatus&
115 setFlags(uint64_t flags)
116 {
117 m_wire.reset();
118 m_flags = flags;
119 return *this;
120 }
121
122 uint64_t
123 getNInInterests() const
124 {
125 return m_nInInterests;
126 }
127
128 FaceStatus&
129 setNInInterests(uint64_t nInInterests)
130 {
131 m_wire.reset();
132 m_nInInterests = nInInterests;
133 return *this;
134 }
135
136 uint64_t
137 getNInDatas() const
138 {
139 return m_nInDatas;
140 }
141
142 FaceStatus&
143 setNInDatas(uint64_t nInDatas)
144 {
145 m_wire.reset();
146 m_nInDatas = nInDatas;
147 return *this;
148 }
149
150 uint64_t
151 getNOutInterests() const
152 {
153 return m_nOutInterests;
154 }
155
156 FaceStatus&
157 setNOutInterests(uint64_t nOutInterests)
158 {
159 m_wire.reset();
160 m_nOutInterests = nOutInterests;
161 return *this;
162 }
163
164 uint64_t
165 getNOutDatas() const
166 {
167 return m_nOutDatas;
168 }
169
170 FaceStatus&
171 setNOutDatas(uint64_t nOutDatas)
172 {
173 m_wire.reset();
174 m_nOutDatas = nOutDatas;
175 return *this;
176 }
177
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700178private:
179 uint64_t m_faceId;
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700180 std::string m_remoteUri;
181 std::string m_localUri;
182 uint64_t m_flags;
183 uint64_t m_nInInterests;
184 uint64_t m_nInDatas;
185 uint64_t m_nOutInterests;
186 uint64_t m_nOutDatas;
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700187
188 mutable Block m_wire;
189};
190
191inline
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700192FaceStatus::FaceStatus()
193 : m_faceId(0)
194 , m_flags(0)
195 , m_nInInterests(0)
196 , m_nInDatas(0)
197 , m_nOutInterests(0)
198 , m_nOutDatas(0)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700199{
200}
201
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700202template<bool T>
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700203inline size_t
204FaceStatus::wireEncode(EncodingImpl<T>& encoder) const
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700205{
206 size_t totalLength = 0;
207
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700208 totalLength += prependNonNegativeIntegerBlock(encoder,
209 tlv::nfd::NOutDatas, m_nOutDatas);
210 totalLength += prependNonNegativeIntegerBlock(encoder,
211 tlv::nfd::NOutInterests, m_nOutInterests);
212 totalLength += prependNonNegativeIntegerBlock(encoder,
213 tlv::nfd::NInDatas, m_nInDatas);
214 totalLength += prependNonNegativeIntegerBlock(encoder,
215 tlv::nfd::NInInterests, m_nInInterests);
216 totalLength += prependNonNegativeIntegerBlock(encoder,
217 tlv::nfd::FaceFlags, m_flags);
218 totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
219 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
220 totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
221 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
222 totalLength += prependNonNegativeIntegerBlock(encoder,
223 tlv::nfd::FaceId, m_faceId);
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700224
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700225 totalLength += encoder.prependVarNumber(totalLength);
226 totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700227 return totalLength;
228}
229
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700230inline const Block&
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700231FaceStatus::wireEncode() const
232{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700233 if (m_wire.hasWire())
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700234 return m_wire;
235
236 EncodingEstimator estimator;
237 size_t estimatedSize = wireEncode(estimator);
238
239 EncodingBuffer buffer(estimatedSize, 0);
240 wireEncode(buffer);
241
242 m_wire = buffer.block();
243 return m_wire;
244}
245
246inline void
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700247FaceStatus::wireDecode(const Block& block)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700248{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700249 if (block.type() != tlv::nfd::FaceStatus) {
250 throw Error("expecting FaceStatus block");
251 }
252 m_wire = block;
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700253 m_wire.parse();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700254 Block::element_const_iterator val = m_wire.elements_begin();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700255
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700256 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
257 m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
258 ++val;
259 }
260 else {
261 throw Error("missing required FaceId field");
262 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700263
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700264 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
265 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
266 ++val;
267 }
268 else {
269 throw Error("missing required Uri field");
270 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700271
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700272 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
273 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
274 ++val;
275 }
276 else {
277 throw Error("missing required LocalUri field");
278 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700279
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700280 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) {
281 m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
282 ++val;
283 }
284 else {
285 throw Error("missing required FaceFlags field");
286 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700287
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700288 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
289 m_nInInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
290 ++val;
291 }
292 else {
293 throw Error("missing required NInInterests field");
294 }
295
296 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
297 m_nInDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
298 ++val;
299 }
300 else {
301 throw Error("missing required NInDatas field");
302 }
303
304 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
305 m_nOutInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
306 ++val;
307 }
308 else {
309 throw Error("missing required NOutInterests field");
310 }
311
312 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
313 m_nOutDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
314 ++val;
315 }
316 else {
317 throw Error("missing required NOutDatas field");
318 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700319}
320
321inline std::ostream&
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700322operator<<(std::ostream& os, const FaceStatus& status)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700323{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700324 os << "FaceStatus("
325 << "FaceID: " << status.getFaceId() << ", "
326 << "RemoteUri: " << status.getRemoteUri() << ", "
327 << "LocalUri: " << status.getLocalUri() << ", "
328 << "Flags: " << status.getFlags() << ", "
329 << "Counters: " << status.getNInInterests() << "|" << status.getNInDatas()
330 << "|" << status.getNOutInterests() << "|" << status.getNOutDatas()
331 << ")";
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700332 return os;
333}
334
335} // namespace nfd
336} // namespace ndn
337
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700338#endif // NDN_MANAGEMENT_NFD_FACE_STATUS_HPP