blob: 1b86b3cc676526a1930c37951afd38928bce3233 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070020 */
21
22#ifndef NDN_MANAGEMENT_NFD_FACE_STATUS_HPP
23#define NDN_MANAGEMENT_NFD_FACE_STATUS_HPP
24
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070025// This include must be kept as the first one, to ensure nfd-face-flags.hpp compiles on its own.
26#include "nfd-face-flags.hpp"
27
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070028#include "../encoding/tlv-nfd.hpp"
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070029#include "../encoding/encoding-buffer.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070030#include "../encoding/block-helpers.hpp"
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070031
32namespace ndn {
33namespace nfd {
34
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040035/**
36 * \ingroup management
37 * \brief represents Face status
38 * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Dataset
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070039 */
40class FaceStatus : public FaceFlagsTraits<FaceStatus>
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070041{
42public:
43 class Error : public Tlv::Error
44 {
45 public:
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070046 explicit
47 Error(const std::string& what)
48 : Tlv::Error(what)
49 {
50 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070051 };
52
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070053 FaceStatus();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070054
55 explicit
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070056 FaceStatus(const Block& block)
57 {
58 this->wireDecode(block);
59 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070060
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070061 /** \brief prepend FaceStatus to the encoder
62 */
63 template<bool T>
64 size_t
65 wireEncode(EncodingImpl<T>& encoder) const;
66
67 /** \brief encode FaceStatus
68 */
69 const Block&
70 wireEncode() const;
71
72 /** \brief decode FaceStatus
73 */
74 void
75 wireDecode(const Block& wire);
76
77public: // getters & setters
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -070078 uint64_t
79 getFaceId() const
80 {
81 return m_faceId;
82 }
83
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -070084 FaceStatus&
85 setFaceId(uint64_t faceId)
86 {
87 m_wire.reset();
88 m_faceId = faceId;
89 return *this;
90 }
91
92 const std::string&
93 getRemoteUri() const
94 {
95 return m_remoteUri;
96 }
97
98 FaceStatus&
99 setRemoteUri(const std::string& remoteUri)
100 {
101 m_wire.reset();
102 m_remoteUri = remoteUri;
103 return *this;
104 }
105
106 const std::string&
107 getLocalUri() const
108 {
109 return m_localUri;
110 }
111
112 FaceStatus&
113 setLocalUri(const std::string& localUri)
114 {
115 m_wire.reset();
116 m_localUri = localUri;
117 return *this;
118 }
119
120 uint64_t
121 getFlags() const
122 {
123 return m_flags;
124 }
125
126 FaceStatus&
127 setFlags(uint64_t flags)
128 {
129 m_wire.reset();
130 m_flags = flags;
131 return *this;
132 }
133
134 uint64_t
135 getNInInterests() const
136 {
137 return m_nInInterests;
138 }
139
140 FaceStatus&
141 setNInInterests(uint64_t nInInterests)
142 {
143 m_wire.reset();
144 m_nInInterests = nInInterests;
145 return *this;
146 }
147
148 uint64_t
149 getNInDatas() const
150 {
151 return m_nInDatas;
152 }
153
154 FaceStatus&
155 setNInDatas(uint64_t nInDatas)
156 {
157 m_wire.reset();
158 m_nInDatas = nInDatas;
159 return *this;
160 }
161
162 uint64_t
163 getNOutInterests() const
164 {
165 return m_nOutInterests;
166 }
167
168 FaceStatus&
169 setNOutInterests(uint64_t nOutInterests)
170 {
171 m_wire.reset();
172 m_nOutInterests = nOutInterests;
173 return *this;
174 }
175
176 uint64_t
177 getNOutDatas() const
178 {
179 return m_nOutDatas;
180 }
181
182 FaceStatus&
183 setNOutDatas(uint64_t nOutDatas)
184 {
185 m_wire.reset();
186 m_nOutDatas = nOutDatas;
187 return *this;
188 }
189
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700190private:
191 uint64_t m_faceId;
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700192 std::string m_remoteUri;
193 std::string m_localUri;
194 uint64_t m_flags;
195 uint64_t m_nInInterests;
196 uint64_t m_nInDatas;
197 uint64_t m_nOutInterests;
198 uint64_t m_nOutDatas;
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700199
200 mutable Block m_wire;
201};
202
203inline
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700204FaceStatus::FaceStatus()
205 : m_faceId(0)
206 , m_flags(0)
207 , m_nInInterests(0)
208 , m_nInDatas(0)
209 , m_nOutInterests(0)
210 , m_nOutDatas(0)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700211{
212}
213
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700214template<bool T>
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700215inline size_t
216FaceStatus::wireEncode(EncodingImpl<T>& encoder) const
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700217{
218 size_t totalLength = 0;
219
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700220 totalLength += prependNonNegativeIntegerBlock(encoder,
221 tlv::nfd::NOutDatas, m_nOutDatas);
222 totalLength += prependNonNegativeIntegerBlock(encoder,
223 tlv::nfd::NOutInterests, m_nOutInterests);
224 totalLength += prependNonNegativeIntegerBlock(encoder,
225 tlv::nfd::NInDatas, m_nInDatas);
226 totalLength += prependNonNegativeIntegerBlock(encoder,
227 tlv::nfd::NInInterests, m_nInInterests);
228 totalLength += prependNonNegativeIntegerBlock(encoder,
229 tlv::nfd::FaceFlags, m_flags);
230 totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
231 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
232 totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
233 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
234 totalLength += prependNonNegativeIntegerBlock(encoder,
235 tlv::nfd::FaceId, m_faceId);
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700236
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700237 totalLength += encoder.prependVarNumber(totalLength);
238 totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700239 return totalLength;
240}
241
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700242inline const Block&
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700243FaceStatus::wireEncode() const
244{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700245 if (m_wire.hasWire())
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700246 return m_wire;
247
248 EncodingEstimator estimator;
249 size_t estimatedSize = wireEncode(estimator);
250
251 EncodingBuffer buffer(estimatedSize, 0);
252 wireEncode(buffer);
253
254 m_wire = buffer.block();
255 return m_wire;
256}
257
258inline void
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700259FaceStatus::wireDecode(const Block& block)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700260{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700261 if (block.type() != tlv::nfd::FaceStatus) {
262 throw Error("expecting FaceStatus block");
263 }
264 m_wire = block;
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700265 m_wire.parse();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700266 Block::element_const_iterator val = m_wire.elements_begin();
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700267
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700268 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
269 m_faceId = static_cast<uint64_t>(readNonNegativeInteger(*val));
270 ++val;
271 }
272 else {
273 throw Error("missing required FaceId field");
274 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700275
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700276 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
277 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
278 ++val;
279 }
280 else {
281 throw Error("missing required Uri field");
282 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700283
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700284 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
285 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
286 ++val;
287 }
288 else {
289 throw Error("missing required LocalUri field");
290 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700291
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700292 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceFlags) {
293 m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
294 ++val;
295 }
296 else {
297 throw Error("missing required FaceFlags field");
298 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700299
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700300 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
301 m_nInInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
302 ++val;
303 }
304 else {
305 throw Error("missing required NInInterests field");
306 }
307
308 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
309 m_nInDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
310 ++val;
311 }
312 else {
313 throw Error("missing required NInDatas field");
314 }
315
316 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
317 m_nOutInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
318 ++val;
319 }
320 else {
321 throw Error("missing required NOutInterests field");
322 }
323
324 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
325 m_nOutDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
326 ++val;
327 }
328 else {
329 throw Error("missing required NOutDatas field");
330 }
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700331}
332
333inline std::ostream&
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700334operator<<(std::ostream& os, const FaceStatus& status)
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700335{
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700336 os << "FaceStatus("
337 << "FaceID: " << status.getFaceId() << ", "
338 << "RemoteUri: " << status.getRemoteUri() << ", "
339 << "LocalUri: " << status.getLocalUri() << ", "
340 << "Flags: " << status.getFlags() << ", "
341 << "Counters: " << status.getNInInterests() << "|" << status.getNInDatas()
342 << "|" << status.getNOutInterests() << "|" << status.getNOutDatas()
343 << ")";
Alexander Afanasyev04fa37a2014-03-19 18:06:22 -0700344 return os;
345}
346
347} // namespace nfd
348} // namespace ndn
349
Junxiao Shi7b1ba1a2014-03-29 01:01:56 -0700350#endif // NDN_MANAGEMENT_NFD_FACE_STATUS_HPP