blob: 17145bd35c26c9d3caaf9be21adf9b4373f598ba [file] [log] [blame]
Junxiao Shi13e637f2014-07-16 19:20:40 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * 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.
20 */
21
22#include "nfd-face-status.hpp"
23
24namespace ndn {
25namespace nfd {
26
27FaceStatus::FaceStatus()
Chengyu Fan36dca992014-09-25 13:42:03 -060028 : FaceTraits()
Junxiao Shi13e637f2014-07-16 19:20:40 -070029 , m_hasExpirationPeriod(false)
Junxiao Shi13e637f2014-07-16 19:20:40 -070030 , m_nInInterests(0)
31 , m_nInDatas(0)
32 , m_nOutInterests(0)
33 , m_nOutDatas(0)
34 , m_nInBytes(0)
35 , m_nOutBytes(0)
36{
37}
38
Chengyu Fan36dca992014-09-25 13:42:03 -060039FaceStatus::FaceStatus(const Block& block)
40{
41 this->wireDecode(block);
42}
43
Junxiao Shi13e637f2014-07-16 19:20:40 -070044template<bool T>
45size_t
46FaceStatus::wireEncode(EncodingImpl<T>& encoder) const
47{
48 size_t totalLength = 0;
49
50 totalLength += prependNonNegativeIntegerBlock(encoder,
51 tlv::nfd::NOutBytes, m_nOutBytes);
52 totalLength += prependNonNegativeIntegerBlock(encoder,
53 tlv::nfd::NInBytes, m_nInBytes);
54 totalLength += prependNonNegativeIntegerBlock(encoder,
55 tlv::nfd::NOutDatas, m_nOutDatas);
56 totalLength += prependNonNegativeIntegerBlock(encoder,
57 tlv::nfd::NOutInterests, m_nOutInterests);
58 totalLength += prependNonNegativeIntegerBlock(encoder,
59 tlv::nfd::NInDatas, m_nInDatas);
60 totalLength += prependNonNegativeIntegerBlock(encoder,
61 tlv::nfd::NInInterests, m_nInInterests);
62 totalLength += prependNonNegativeIntegerBlock(encoder,
Chengyu Fan36dca992014-09-25 13:42:03 -060063 tlv::nfd::LinkType, m_linkType);
64 totalLength += prependNonNegativeIntegerBlock(encoder,
65 tlv::nfd::FacePersistency, m_facePersistency);
66 totalLength += prependNonNegativeIntegerBlock(encoder,
67 tlv::nfd::FaceScope, m_faceScope);
Junxiao Shi13e637f2014-07-16 19:20:40 -070068 if (m_hasExpirationPeriod) {
69 totalLength += prependNonNegativeIntegerBlock(encoder,
70 tlv::nfd::ExpirationPeriod, m_expirationPeriod.count());
71 }
72 totalLength += prependByteArrayBlock(encoder, tlv::nfd::LocalUri,
73 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
74 totalLength += prependByteArrayBlock(encoder, tlv::nfd::Uri,
75 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
76 totalLength += prependNonNegativeIntegerBlock(encoder,
77 tlv::nfd::FaceId, m_faceId);
78
79 totalLength += encoder.prependVarNumber(totalLength);
80 totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
81 return totalLength;
82}
83
84template size_t
85FaceStatus::wireEncode<true>(EncodingImpl<true>& block) const;
86
87template size_t
88FaceStatus::wireEncode<false>(EncodingImpl<false>& block) const;
89
90const Block&
91FaceStatus::wireEncode() const
92{
93 if (m_wire.hasWire())
94 return m_wire;
95
96 EncodingEstimator estimator;
97 size_t estimatedSize = wireEncode(estimator);
98
99 EncodingBuffer buffer(estimatedSize, 0);
100 wireEncode(buffer);
101
102 m_wire = buffer.block();
103 return m_wire;
104}
105
106void
107FaceStatus::wireDecode(const Block& block)
108{
109 if (block.type() != tlv::nfd::FaceStatus) {
110 throw Error("expecting FaceStatus block");
111 }
112 m_wire = block;
113 m_wire.parse();
114 Block::element_const_iterator val = m_wire.elements_begin();
115
116 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
117 m_faceId = readNonNegativeInteger(*val);
118 ++val;
119 }
120 else {
121 throw Error("missing required FaceId field");
122 }
123
124 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
125 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
126 ++val;
127 }
128 else {
129 throw Error("missing required Uri field");
130 }
131
132 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
133 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
134 ++val;
135 }
136 else {
137 throw Error("missing required LocalUri field");
138 }
139
140 if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
141 m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
142 m_hasExpirationPeriod = true;
143 ++val;
144 }
145 else {
146 m_hasExpirationPeriod = false;
147 // ExpirationPeriod is optional
148 }
149
Chengyu Fan36dca992014-09-25 13:42:03 -0600150 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
151 m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700152 ++val;
153 }
154 else {
Chengyu Fan36dca992014-09-25 13:42:03 -0600155 throw Error("missing required FaceScope field");
156 }
157
158 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
159 m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
160 ++val;
161 }
162 else {
163 throw Error("missing required FacePersistency field");
164 }
165
166 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
167 m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
168 ++val;
169 }
170 else {
171 throw Error("missing required LinkType field");
Junxiao Shi13e637f2014-07-16 19:20:40 -0700172 }
173
174 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
175 m_nInInterests = readNonNegativeInteger(*val);
176 ++val;
177 }
178 else {
179 throw Error("missing required NInInterests field");
180 }
181
182 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
183 m_nInDatas = readNonNegativeInteger(*val);
184 ++val;
185 }
186 else {
187 throw Error("missing required NInDatas field");
188 }
189
190 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
191 m_nOutInterests = readNonNegativeInteger(*val);
192 ++val;
193 }
194 else {
195 throw Error("missing required NOutInterests field");
196 }
197
198 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
199 m_nOutDatas = readNonNegativeInteger(*val);
200 ++val;
201 }
202 else {
203 throw Error("missing required NOutDatas field");
204 }
205
206 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInBytes) {
207 m_nInBytes = readNonNegativeInteger(*val);
208 ++val;
209 }
210 else {
211 throw Error("missing required NInBytes field");
212 }
213
214 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutBytes) {
215 m_nOutBytes = readNonNegativeInteger(*val);
216 ++val;
217 }
218 else {
219 throw Error("missing required NOutBytes field");
220 }
221}
222
Chengyu Fan36dca992014-09-25 13:42:03 -0600223FaceStatus&
224FaceStatus::setExpirationPeriod(const time::milliseconds& expirationPeriod)
225{
226 m_wire.reset();
227 m_expirationPeriod = expirationPeriod;
228 m_hasExpirationPeriod = true;
229 return *this;
230}
231
232FaceStatus&
233FaceStatus::setNInInterests(uint64_t nInInterests)
234{
235 m_wire.reset();
236 m_nInInterests = nInInterests;
237 return *this;
238}
239
240FaceStatus&
241FaceStatus::setNInDatas(uint64_t nInDatas)
242{
243 m_wire.reset();
244 m_nInDatas = nInDatas;
245 return *this;
246}
247
248FaceStatus&
249FaceStatus::setNOutInterests(uint64_t nOutInterests)
250{
251 m_wire.reset();
252 m_nOutInterests = nOutInterests;
253 return *this;
254}
255
256FaceStatus&
257FaceStatus::setNOutDatas(uint64_t nOutDatas)
258{
259 m_wire.reset();
260 m_nOutDatas = nOutDatas;
261 return *this;
262}
263
264FaceStatus&
265FaceStatus::setNInBytes(uint64_t nInBytes)
266{
267 m_wire.reset();
268 m_nInBytes = nInBytes;
269 return *this;
270}
271
272FaceStatus&
273FaceStatus::setNOutBytes(uint64_t nOutBytes)
274{
275 m_wire.reset();
276 m_nOutBytes = nOutBytes;
277 return *this;
278}
279
280void
281FaceStatus::wireReset() const
282{
283 m_wire.reset();
284}
285
Junxiao Shi13e637f2014-07-16 19:20:40 -0700286std::ostream&
287operator<<(std::ostream& os, const FaceStatus& status)
288{
289 os << "FaceStatus("
290 << "FaceID: " << status.getFaceId() << ",\n"
291 << "RemoteUri: " << status.getRemoteUri() << ",\n"
292 << "LocalUri: " << status.getLocalUri() << ",\n";
293
294 if (status.hasExpirationPeriod()) {
295 os << "ExpirationPeriod: " << status.getExpirationPeriod() << ",\n";
296 }
297 else {
298 os << "ExpirationPeriod: infinite,\n";
299 }
300
Chengyu Fan36dca992014-09-25 13:42:03 -0600301 os << "FaceScope: " << status.getFaceScope() << ",\n"
302 << "FacePersistency: " << status.getFacePersistency() << ",\n"
303 << "LinkType: " << status.getLinkType() << ",\n"
Junxiao Shi13e637f2014-07-16 19:20:40 -0700304 << "Counters: { Interests: {in: " << status.getNInInterests() << ", "
305 << "out: " << status.getNOutInterests() << "},\n"
306 << " Data: {in: " << status.getNInDatas() << ", "
307 << "out: " << status.getNOutDatas() << "},\n"
308 << " bytes: {in: " << status.getNInBytes() << ", "
309 << "out: " << status.getNOutBytes() << "} }\n"
310 << ")";
311 return os;
312}
313
314} // namespace nfd
315} // namespace ndn
316