blob: 86b845c3315ebac0463b816654d2367e6b4fbb81 [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
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700172private:
173 uint64_t m_faceId;
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700174 std::string m_remoteUri;
175 std::string m_localUri;
176 uint64_t m_flags;
177 uint64_t m_nInInterests;
178 uint64_t m_nInDatas;
179 uint64_t m_nOutInterests;
180 uint64_t m_nOutDatas;
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700181
182 mutable Block m_wire;
183};
184
185inline
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700186FaceStatus::FaceStatus()
187 : m_faceId(0)
188 , m_flags(0)
189 , m_nInInterests(0)
190 , m_nInDatas(0)
191 , m_nOutInterests(0)
192 , m_nOutDatas(0)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700193{
194}
195
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700196template<bool T>
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700197inline size_t
198FaceStatus::wireEncode(EncodingImpl<T>& encoder) const
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700199{
200 size_t totalLength = 0;
201
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700202 totalLength += prependNonNegativeIntegerBlock(encoder,
203 tlv::nfd::NOutDatas, m_nOutDatas);
204 totalLength += prependNonNegativeIntegerBlock(encoder,
205 tlv::nfd::NOutInterests, m_nOutInterests);
206 totalLength += prependNonNegativeIntegerBlock(encoder,
207 tlv::nfd::NInDatas, m_nInDatas);
208 totalLength += prependNonNegativeIntegerBlock(encoder,
209 tlv::nfd::NInInterests, m_nInInterests);
210 totalLength += prependNonNegativeIntegerBlock(encoder,
211 tlv::nfd::FaceFlags, m_flags);
212 totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
213 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
214 totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
215 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
216 totalLength += prependNonNegativeIntegerBlock(encoder,
217 tlv::nfd::FaceId, m_faceId);
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700218
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700219 totalLength += encoder.prependVarNumber(totalLength);
220 totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700221 return totalLength;
222}
223
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700224inline const Block&
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700225FaceStatus::wireEncode() const
226{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700227 if (m_wire.hasWire())
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700228 return m_wire;
229
230 EncodingEstimator estimator;
231 size_t estimatedSize = wireEncode(estimator);
232
233 EncodingBuffer buffer(estimatedSize, 0);
234 wireEncode(buffer);
235
236 m_wire = buffer.block();
237 return m_wire;
238}
239
240inline void
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700241FaceStatus::wireDecode(const Block& block)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700242{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700243 if (block.type() != tlv::nfd::FaceStatus) {
244 throw Error("expecting FaceStatus block");
245 }
246 m_wire = block;
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700247 m_wire.parse();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700248 Block::element_const_iterator val = m_wire.elements_begin();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700249
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700250 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
251 m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
252 ++val;
253 }
254 else {
255 throw Error("missing required FaceId field");
256 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700257
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700258 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
259 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
260 ++val;
261 }
262 else {
263 throw Error("missing required Uri field");
264 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700265
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700266 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
267 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
268 ++val;
269 }
270 else {
271 throw Error("missing required LocalUri field");
272 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700273
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700274 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) {
275 m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
276 ++val;
277 }
278 else {
279 throw Error("missing required FaceFlags field");
280 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700281
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700282 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
283 m_nInInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
284 ++val;
285 }
286 else {
287 throw Error("missing required NInInterests field");
288 }
289
290 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
291 m_nInDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
292 ++val;
293 }
294 else {
295 throw Error("missing required NInDatas field");
296 }
297
298 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
299 m_nOutInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
300 ++val;
301 }
302 else {
303 throw Error("missing required NOutInterests field");
304 }
305
306 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
307 m_nOutDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
308 ++val;
309 }
310 else {
311 throw Error("missing required NOutDatas field");
312 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700313}
314
315inline std::ostream&
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700316operator<<(std::ostream& os, const FaceStatus& status)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700317{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700318 os << "FaceStatus("
319 << "FaceID: " << status.getFaceId() << ", "
320 << "RemoteUri: " << status.getRemoteUri() << ", "
321 << "LocalUri: " << status.getLocalUri() << ", "
322 << "Flags: " << status.getFlags() << ", "
323 << "Counters: " << status.getNInInterests() << "|" << status.getNInDatas()
324 << "|" << status.getNOutInterests() << "|" << status.getNOutDatas()
325 << ")";
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700326 return os;
327}
328
329} // namespace nfd
330} // namespace ndn
331
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700332#endif // NDN_MANAGEMENT_NFD_FACE_STATUS_HPP