blob: 37f5bd9355c12b7aaaba448a5017ff282ffe43c2 [file] [log] [blame]
Junxiao Shi38f4ce92016-08-04 10:01:52 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry84d3adc2017-08-09 23:31:40 -04002/*
Eric Newberry13ff2592020-03-06 17:32:29 -08003 * Copyright (c) 2014-2020, Regents of the University of California,
Junxiao Shi38f4ce92016-08-04 10:01:52 +00004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
Junxiao Shi38f4ce92016-08-04 10:01:52 +000026#include "face-module.hpp"
Junxiao Shi05dd4442017-02-06 22:50:07 +000027#include "find-face.hpp"
Junxiao Shi38f4ce92016-08-04 10:01:52 +000028
29namespace nfd {
30namespace tools {
Junxiao Shi331ade72016-08-19 14:07:19 +000031namespace nfdc {
Junxiao Shi38f4ce92016-08-04 10:01:52 +000032
33void
Junxiao Shi1f481fa2017-01-26 15:14:43 +000034FaceModule::registerCommands(CommandParser& parser)
35{
Junxiao Shi36e54292017-02-17 18:43:16 +000036 CommandDefinition defFaceList("face", "list");
37 defFaceList
38 .setTitle("print face list")
39 .addArg("remote", ArgValueType::FACE_URI, Required::NO, Positional::YES)
40 .addArg("local", ArgValueType::FACE_URI, Required::NO, Positional::NO)
41 .addArg("scheme", ArgValueType::STRING, Required::NO, Positional::NO, "scheme");
42 parser.addCommand(defFaceList, &FaceModule::list);
Davide Pesaventod2147442018-02-19 23:58:17 -050043 parser.addAlias("face", "list", "");
Junxiao Shi36e54292017-02-17 18:43:16 +000044
Junxiao Shi1f481fa2017-01-26 15:14:43 +000045 CommandDefinition defFaceShow("face", "show");
46 defFaceShow
47 .setTitle("show face information")
48 .addArg("id", ArgValueType::UNSIGNED, Required::YES, Positional::YES);
49 parser.addCommand(defFaceShow, &FaceModule::show);
Junxiao Shi1d7fef52017-02-02 05:33:14 +000050
51 CommandDefinition defFaceCreate("face", "create");
52 defFaceCreate
53 .setTitle("create a face")
54 .addArg("remote", ArgValueType::FACE_URI, Required::YES, Positional::YES)
Junxiao Shi0d976922017-04-01 14:35:21 +000055 .addArg("persistency", ArgValueType::FACE_PERSISTENCY, Required::NO, Positional::YES)
Eric Newberry84d3adc2017-08-09 23:31:40 -040056 .addArg("local", ArgValueType::FACE_URI, Required::NO, Positional::NO)
Eric Newberryde332452018-01-30 11:45:32 -070057 .addArg("reliability", ArgValueType::BOOLEAN, Required::NO, Positional::NO)
58 .addArg("congestion-marking", ArgValueType::BOOLEAN, Required::NO, Positional::NO)
59 .addArg("congestion-marking-interval", ArgValueType::UNSIGNED, Required::NO, Positional::NO)
Eric Newberry4f8dd962018-06-17 21:32:07 -070060 .addArg("default-congestion-threshold", ArgValueType::UNSIGNED, Required::NO, Positional::NO)
Eric Newberry13ff2592020-03-06 17:32:29 -080061 .addArg("mtu", ArgValueType::STRING, Required::NO, Positional::NO);
Junxiao Shi1d7fef52017-02-02 05:33:14 +000062 parser.addCommand(defFaceCreate, &FaceModule::create);
Junxiao Shi05dd4442017-02-06 22:50:07 +000063
64 CommandDefinition defFaceDestroy("face", "destroy");
65 defFaceDestroy
66 .setTitle("destroy a face")
67 .addArg("face", ArgValueType::FACE_ID_OR_URI, Required::YES, Positional::YES);
68 parser.addCommand(defFaceDestroy, &FaceModule::destroy);
Junxiao Shi1f481fa2017-01-26 15:14:43 +000069}
70
71void
Junxiao Shi36e54292017-02-17 18:43:16 +000072FaceModule::list(ExecuteContext& ctx)
73{
74 auto remoteUri = ctx.args.getOptional<FaceUri>("remote");
75 auto localUri = ctx.args.getOptional<FaceUri>("local");
76 auto uriScheme = ctx.args.getOptional<std::string>("scheme");
77
78 FaceQueryFilter filter;
79 if (remoteUri) {
80 filter.setRemoteUri(remoteUri->toString());
81 }
82 if (localUri) {
83 filter.setLocalUri(localUri->toString());
84 }
85 if (uriScheme) {
86 filter.setUriScheme(*uriScheme);
87 }
88
89 FindFace findFace(ctx);
90 FindFace::Code res = findFace.execute(filter, true);
91
92 ctx.exitCode = static_cast<int>(res);
93 switch (res) {
94 case FindFace::Code::OK:
95 for (const FaceStatus& item : findFace.getResults()) {
96 formatItemText(ctx.out, item, false);
97 ctx.out << '\n';
98 }
99 break;
100 case FindFace::Code::ERROR:
101 case FindFace::Code::NOT_FOUND:
102 case FindFace::Code::CANONIZE_ERROR:
103 ctx.err << findFace.getErrorReason() << '\n';
104 break;
105 default:
106 BOOST_ASSERT_MSG(false, "unexpected FindFace result");
107 break;
108 }
109}
110
111void
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000112FaceModule::show(ExecuteContext& ctx)
113{
114 uint64_t faceId = ctx.args.get<uint64_t>("id");
115
Junxiao Shi8f803f22017-02-10 03:04:28 +0000116 FindFace findFace(ctx);
117 FindFace::Code res = findFace.execute(faceId);
Junxiao Shi1d7fef52017-02-02 05:33:14 +0000118
Junxiao Shi8f803f22017-02-10 03:04:28 +0000119 ctx.exitCode = static_cast<int>(res);
120 switch (res) {
121 case FindFace::Code::OK:
122 formatItemText(ctx.out, findFace.getFaceStatus(), true);
123 break;
124 case FindFace::Code::ERROR:
125 case FindFace::Code::NOT_FOUND:
126 ctx.err << findFace.getErrorReason() << '\n';
127 break;
128 default:
129 BOOST_ASSERT_MSG(false, "unexpected FindFace result");
130 break;
131 }
Junxiao Shi1d7fef52017-02-02 05:33:14 +0000132}
133
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000134/** \brief order persistency in NONE < ON_DEMAND < PERSISTENCY < PERMANENT
135 */
136static bool
137persistencyLessThan(FacePersistency x, FacePersistency y)
138{
139 switch (x) {
140 case FacePersistency::FACE_PERSISTENCY_NONE:
141 return y != FacePersistency::FACE_PERSISTENCY_NONE;
142 case FacePersistency::FACE_PERSISTENCY_ON_DEMAND:
143 return y == FacePersistency::FACE_PERSISTENCY_PERSISTENT ||
144 y == FacePersistency::FACE_PERSISTENCY_PERMANENT;
145 case FacePersistency::FACE_PERSISTENCY_PERSISTENT:
146 return y == FacePersistency::FACE_PERSISTENCY_PERMANENT;
147 case FacePersistency::FACE_PERSISTENCY_PERMANENT:
148 return false;
149 }
150 return static_cast<int>(x) < static_cast<int>(y);
151}
152
Junxiao Shi1d7fef52017-02-02 05:33:14 +0000153void
154FaceModule::create(ExecuteContext& ctx)
155{
Junxiao Shi0d976922017-04-01 14:35:21 +0000156 auto remoteUri = ctx.args.get<FaceUri>("remote");
157 auto localUri = ctx.args.getOptional<FaceUri>("local");
Junxiao Shi1d7fef52017-02-02 05:33:14 +0000158 auto persistency = ctx.args.get<FacePersistency>("persistency", FacePersistency::FACE_PERSISTENCY_PERSISTENT);
Eric Newberry84d3adc2017-08-09 23:31:40 -0400159 auto lpReliability = ctx.args.getTribool("reliability");
Eric Newberryde332452018-01-30 11:45:32 -0700160 auto congestionMarking = ctx.args.getTribool("congestion-marking");
161 auto baseCongestionMarkingIntervalMs = ctx.args.getOptional<uint64_t>("congestion-marking-interval");
162 auto defaultCongestionThreshold = ctx.args.getOptional<uint64_t>("default-congestion-threshold");
Eric Newberry13ff2592020-03-06 17:32:29 -0800163 auto mtuArg = ctx.args.getOptional<std::string>("mtu");
164
165 // MTU is nominally a uint64_t, but can be the string value 'auto' to unset an override MTU
166 optional<uint64_t> mtu;
167 if (mtuArg == "auto") {
168 mtu = std::numeric_limits<uint64_t>::max();
169 }
170 else if (mtuArg) {
171 // boost::lexical_cast<uint64_t> will accept negative numbers
172 int64_t v = -1;
173 if (!boost::conversion::try_lexical_convert<int64_t>(*mtuArg, v) || v < 0) {
174 ctx.exitCode = 2;
175 ctx.err << "MTU must either be a non-negative integer or 'auto'\n";
176 return;
177 }
178
179 mtu = static_cast<uint64_t>(v);
180 }
Eric Newberry84d3adc2017-08-09 23:31:40 -0400181
Junxiao Shi0d976922017-04-01 14:35:21 +0000182 FaceUri canonicalRemote;
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400183 optional<FaceUri> canonicalLocal;
Junxiao Shi0d976922017-04-01 14:35:21 +0000184
185 auto handleCanonizeError = [&] (const FaceUri& faceUri, const std::string& error) {
Eric Newberry13ff2592020-03-06 17:32:29 -0800186 ctx.exitCode = static_cast<int>(FindFace::Code::CANONIZE_ERROR);
Junxiao Shi0d976922017-04-01 14:35:21 +0000187 ctx.err << "Error when canonizing '" << faceUri << "': " << error << '\n';
188 };
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000189
190 auto printPositiveResult = [&] (const std::string& actionSummary, const ControlParameters& resp) {
191 text::ItemAttributes ia;
192 ctx.out << actionSummary << ' '
193 << ia("id") << resp.getFaceId()
Junxiao Shi1cce2a32017-05-02 02:39:55 +0000194 << ia("local") << resp.getLocalUri()
195 << ia("remote") << resp.getUri()
Eric Newberryde332452018-01-30 11:45:32 -0700196 << ia("persistency") << resp.getFacePersistency();
197 printFaceParams(ctx.out, ia, resp);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000198 };
199
Eric Newberry84d3adc2017-08-09 23:31:40 -0400200 auto updateFace = [&printPositiveResult] (ControlParameters respParams, ControlParameters resp) {
201 // faces/update response does not have FaceUris, copy from faces/create response
202 resp.setLocalUri(respParams.getLocalUri())
203 .setUri(respParams.getUri());
204 printPositiveResult("face-updated", resp);
205 };
206
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000207 auto handle409 = [&] (const ControlResponse& resp) {
208 ControlParameters respParams(resp.getBody());
Junxiao Shi0d976922017-04-01 14:35:21 +0000209 if (respParams.getUri() != canonicalRemote.toString()) {
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000210 // we are conflicting with a different face, which is a general error
211 return false;
212 }
213
Eric Newberry13ff2592020-03-06 17:32:29 -0800214 bool isChangingParams = false;
215 ControlParameters params;
216 params.setFaceId(respParams.getFaceId());
217
Eric Newberry4f8dd962018-06-17 21:32:07 -0700218 if (mtu && (!respParams.hasMtu() || respParams.getMtu() != *mtu)) {
Eric Newberry13ff2592020-03-06 17:32:29 -0800219 isChangingParams = true;
220 params.setMtu(*mtu);
Eric Newberry4f8dd962018-06-17 21:32:07 -0700221 }
Eric Newberry13ff2592020-03-06 17:32:29 -0800222
223 if (persistencyLessThan(respParams.getFacePersistency(), persistency)) {
224 isChangingParams = true;
225 params.setFacePersistency(persistency);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000226 }
Eric Newberryde332452018-01-30 11:45:32 -0700227
Eric Newberry13ff2592020-03-06 17:32:29 -0800228 if (!boost::logic::indeterminate(lpReliability) &&
229 lpReliability != respParams.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
230 isChangingParams = true;
231 params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, bool(lpReliability));
232 }
Eric Newberryde332452018-01-30 11:45:32 -0700233
Eric Newberry13ff2592020-03-06 17:32:29 -0800234 if (!boost::logic::indeterminate(congestionMarking) &&
235 congestionMarking != respParams.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
236 isChangingParams = true;
237 params.setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, bool(congestionMarking));
238 }
Eric Newberryde332452018-01-30 11:45:32 -0700239
Eric Newberry13ff2592020-03-06 17:32:29 -0800240 if (baseCongestionMarkingIntervalMs) {
241 isChangingParams = true;
242 params.setBaseCongestionMarkingInterval(time::milliseconds(*baseCongestionMarkingIntervalMs));
243 }
Eric Newberryde332452018-01-30 11:45:32 -0700244
Eric Newberry13ff2592020-03-06 17:32:29 -0800245 if (defaultCongestionThreshold) {
246 isChangingParams = true;
247 params.setDefaultCongestionThreshold(*defaultCongestionThreshold);
248 }
249
250 if (isChangingParams) {
Eric Newberry84d3adc2017-08-09 23:31:40 -0400251 ctx.controller.start<ndn::nfd::FaceUpdateCommand>(
Eric Newberry13ff2592020-03-06 17:32:29 -0800252 params,
253 bind(updateFace, respParams, _1),
254 ctx.makeCommandFailureHandler("updating face"),
255 ctx.makeCommandOptions());
Eric Newberry84d3adc2017-08-09 23:31:40 -0400256 }
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000257 else {
Eric Newberry84d3adc2017-08-09 23:31:40 -0400258 // don't do anything
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000259 printPositiveResult("face-exists", respParams);
260 }
Eric Newberry13ff2592020-03-06 17:32:29 -0800261
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000262 return true;
263 };
Junxiao Shi1d7fef52017-02-02 05:33:14 +0000264
Junxiao Shi0d976922017-04-01 14:35:21 +0000265 auto doCreateFace = [&] {
266 ControlParameters params;
267 params.setUri(canonicalRemote.toString());
268 if (canonicalLocal) {
269 params.setLocalUri(canonicalLocal->toString());
270 }
271 params.setFacePersistency(persistency);
Eric Newberry84d3adc2017-08-09 23:31:40 -0400272 if (!boost::logic::indeterminate(lpReliability)) {
Davide Pesaventofa2aa502019-03-22 13:30:02 -0400273 params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, bool(lpReliability));
Eric Newberry84d3adc2017-08-09 23:31:40 -0400274 }
Eric Newberryde332452018-01-30 11:45:32 -0700275 if (!boost::logic::indeterminate(congestionMarking)) {
Davide Pesaventofa2aa502019-03-22 13:30:02 -0400276 params.setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, bool(congestionMarking));
Eric Newberryde332452018-01-30 11:45:32 -0700277 }
278 if (baseCongestionMarkingIntervalMs) {
279 params.setBaseCongestionMarkingInterval(time::milliseconds(*baseCongestionMarkingIntervalMs));
280 }
281 if (defaultCongestionThreshold) {
282 params.setDefaultCongestionThreshold(*defaultCongestionThreshold);
283 }
Eric Newberry4f8dd962018-06-17 21:32:07 -0700284 if (mtu) {
285 params.setMtu(*mtu);
286 }
Junxiao Shi0d976922017-04-01 14:35:21 +0000287
288 ctx.controller.start<ndn::nfd::FaceCreateCommand>(
289 params,
290 bind(printPositiveResult, "face-created", _1),
291 [&] (const ControlResponse& resp) {
292 if (resp.getCode() == 409 && handle409(resp)) {
293 return;
294 }
295 ctx.makeCommandFailureHandler("creating face")(resp); // invoke general error handler
296 },
297 ctx.makeCommandOptions());
298 };
299
300 remoteUri.canonize(
301 [&] (const FaceUri& canonicalUri) {
302 canonicalRemote = canonicalUri;
303 if (localUri) {
304 localUri->canonize(
305 [&] (const FaceUri& canonicalUri) {
306 canonicalLocal = canonicalUri;
307 doCreateFace();
308 },
309 bind(handleCanonizeError, *localUri, _1),
310 ctx.face.getIoService(), ctx.getTimeout());
311 }
312 else {
313 doCreateFace();
314 }
Junxiao Shi1d7fef52017-02-02 05:33:14 +0000315 },
Junxiao Shi0d976922017-04-01 14:35:21 +0000316 bind(handleCanonizeError, remoteUri, _1),
Junxiao Shi1d7fef52017-02-02 05:33:14 +0000317 ctx.face.getIoService(), ctx.getTimeout());
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000318
319 ctx.face.processEvents();
320}
321
322void
Junxiao Shi05dd4442017-02-06 22:50:07 +0000323FaceModule::destroy(ExecuteContext& ctx)
324{
Junxiao Shi05dd4442017-02-06 22:50:07 +0000325 FindFace findFace(ctx);
Davide Pesavento8b663a92018-11-21 22:57:20 -0500326 FindFace::Code res = findFace.execute(ctx.args.at("face"));
Junxiao Shi05dd4442017-02-06 22:50:07 +0000327
328 ctx.exitCode = static_cast<int>(res);
329 switch (res) {
330 case FindFace::Code::OK:
331 break;
332 case FindFace::Code::ERROR:
333 case FindFace::Code::CANONIZE_ERROR:
334 case FindFace::Code::NOT_FOUND:
335 ctx.err << findFace.getErrorReason() << '\n';
336 return;
337 case FindFace::Code::AMBIGUOUS:
338 ctx.err << "Multiple faces match specified remote FaceUri. Re-run the command with a FaceId:";
339 findFace.printDisambiguation(ctx.err, FindFace::DisambiguationStyle::LOCAL_URI);
340 ctx.err << '\n';
341 return;
342 default:
343 BOOST_ASSERT_MSG(false, "unexpected FindFace result");
344 return;
345 }
346
347 const FaceStatus& face = findFace.getFaceStatus();
348
349 ctx.controller.start<ndn::nfd::FaceDestroyCommand>(
350 ControlParameters().setFaceId(face.getFaceId()),
351 [&] (const ControlParameters& resp) {
352 ctx.out << "face-destroyed ";
353 text::ItemAttributes ia;
354 ctx.out << ia("id") << face.getFaceId()
355 << ia("local") << face.getLocalUri()
356 << ia("remote") << face.getRemoteUri()
Eric Newberryde332452018-01-30 11:45:32 -0700357 << ia("persistency") << face.getFacePersistency();
358 printFaceParams(ctx.out, ia, resp);
Junxiao Shi05dd4442017-02-06 22:50:07 +0000359 },
360 ctx.makeCommandFailureHandler("destroying face"),
361 ctx.makeCommandOptions());
362
363 ctx.face.processEvents();
364}
365
366void
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000367FaceModule::fetchStatus(Controller& controller,
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400368 const std::function<void()>& onSuccess,
Junxiao Shi29b41282016-08-22 03:47:02 +0000369 const Controller::DatasetFailCallback& onFailure,
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000370 const CommandOptions& options)
371{
372 controller.fetch<ndn::nfd::FaceDataset>(
373 [this, onSuccess] (const std::vector<FaceStatus>& result) {
374 m_status = result;
375 onSuccess();
376 },
377 onFailure, options);
378}
379
380void
381FaceModule::formatStatusXml(std::ostream& os) const
382{
383 os << "<faces>";
384 for (const FaceStatus& item : m_status) {
385 this->formatItemXml(os, item);
386 }
387 os << "</faces>";
388}
389
390void
391FaceModule::formatItemXml(std::ostream& os, const FaceStatus& item) const
392{
393 os << "<face>";
394
395 os << "<faceId>" << item.getFaceId() << "</faceId>";
396 os << "<remoteUri>" << xml::Text{item.getRemoteUri()} << "</remoteUri>";
397 os << "<localUri>" << xml::Text{item.getLocalUri()} << "</localUri>";
398
399 if (item.hasExpirationPeriod()) {
400 os << "<expirationPeriod>" << xml::formatDuration(item.getExpirationPeriod())
401 << "</expirationPeriod>";
402 }
403 os << "<faceScope>" << item.getFaceScope() << "</faceScope>";
404 os << "<facePersistency>" << item.getFacePersistency() << "</facePersistency>";
405 os << "<linkType>" << item.getLinkType() << "</linkType>";
406
Eric Newberryde332452018-01-30 11:45:32 -0700407 if (!item.hasBaseCongestionMarkingInterval() && !item.hasDefaultCongestionThreshold()) {
408 os << "<congestion/>";
409 }
410 else {
411 os << "<congestion>";
412 if (item.hasBaseCongestionMarkingInterval()) {
413 os << "<baseMarkingInterval>" << xml::formatDuration(item.getBaseCongestionMarkingInterval())
414 << "</baseMarkingInterval>";
415 }
416 if (item.hasDefaultCongestionThreshold()) {
417 os << "<defaultThreshold>" << item.getDefaultCongestionThreshold() << "</defaultThreshold>";
418 }
419 os << "</congestion>";
420 }
421
Eric Newberry4f8dd962018-06-17 21:32:07 -0700422 if (item.hasMtu()) {
423 os << "<mtu>" << item.getMtu() << "</mtu>";
424 }
425
Eric Newberry6d932e82016-11-24 05:05:43 +0000426 if (item.getFlags() == 0) {
427 os << "<flags/>";
428 }
429 else {
430 os << "<flags>";
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000431 os << xml::Flag{"localFieldsEnabled", item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)};
432 os << xml::Flag{"lpReliabilityEnabled", item.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)};
433 os << xml::Flag{"congestionMarkingEnabled", item.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)};
Eric Newberry6d932e82016-11-24 05:05:43 +0000434 os << "</flags>";
435 }
436
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000437 os << "<packetCounters>";
438 os << "<incomingPackets>"
439 << "<nInterests>" << item.getNInInterests() << "</nInterests>"
Junxiao Shif03d4792017-04-06 16:41:22 +0000440 << "<nData>" << item.getNInData() << "</nData>"
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000441 << "<nNacks>" << item.getNInNacks() << "</nNacks>"
442 << "</incomingPackets>";
443 os << "<outgoingPackets>"
444 << "<nInterests>" << item.getNOutInterests() << "</nInterests>"
Junxiao Shif03d4792017-04-06 16:41:22 +0000445 << "<nData>" << item.getNOutData() << "</nData>"
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000446 << "<nNacks>" << item.getNOutNacks() << "</nNacks>"
447 << "</outgoingPackets>";
448 os << "</packetCounters>";
449
450 os << "<byteCounters>";
451 os << "<incomingBytes>" << item.getNInBytes() << "</incomingBytes>";
452 os << "<outgoingBytes>" << item.getNOutBytes() << "</outgoingBytes>";
453 os << "</byteCounters>";
454
455 os << "</face>";
456}
457
458void
459FaceModule::formatStatusText(std::ostream& os) const
460{
461 os << "Faces:\n";
462 for (const FaceStatus& item : m_status) {
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000463 os << " ";
464 formatItemText(os, item, false);
465 os << '\n';
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000466 }
467}
468
469void
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000470FaceModule::formatItemText(std::ostream& os, const FaceStatus& item, bool wantMultiLine)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000471{
Eric Newberryde332452018-01-30 11:45:32 -0700472 text::ItemAttributes ia(wantMultiLine, 10);
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000473
474 os << ia("faceid") << item.getFaceId();
475 os << ia("remote") << item.getRemoteUri();
476 os << ia("local") << item.getLocalUri();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000477
478 if (item.hasExpirationPeriod()) {
Eric Newberryde332452018-01-30 11:45:32 -0700479 os << ia("expires") << text::formatDuration<time::seconds>(item.getExpirationPeriod());
480 }
481
482 if (item.hasBaseCongestionMarkingInterval() || item.hasDefaultCongestionThreshold()) {
483 os << ia("congestion") << "{";
484 text::Separator congestionSep("", " ");
485 if (item.hasBaseCongestionMarkingInterval()) {
486 os << congestionSep << "base-marking-interval="
487 << text::formatDuration<time::milliseconds>(item.getBaseCongestionMarkingInterval());
488 }
489 if (item.hasDefaultCongestionThreshold()) {
490 os << congestionSep << "default-threshold=" << item.getDefaultCongestionThreshold() << "B";
491 }
492 os << "}";
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000493 }
494
Eric Newberry4f8dd962018-06-17 21:32:07 -0700495 if (item.hasMtu()) {
496 os << ia("mtu") << item.getMtu();
497 }
498
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000499 os << ia("counters")
500 << "{in={"
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000501 << item.getNInInterests() << "i "
Junxiao Shif03d4792017-04-06 16:41:22 +0000502 << item.getNInData() << "d "
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000503 << item.getNInNacks() << "n "
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000504 << item.getNInBytes() << "B} "
505 << "out={"
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000506 << item.getNOutInterests() << "i "
Junxiao Shif03d4792017-04-06 16:41:22 +0000507 << item.getNOutData() << "d "
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000508 << item.getNOutNacks() << "n "
509 << item.getNOutBytes() << "B}}";
510
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000511 os << ia("flags") << '{';
512 text::Separator flagSep("", " ");
513 os << flagSep << item.getFaceScope();
514 os << flagSep << item.getFacePersistency();
515 os << flagSep << item.getLinkType();
Eric Newberry6d932e82016-11-24 05:05:43 +0000516 if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) {
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000517 os << flagSep << "local-fields";
Eric Newberry6d932e82016-11-24 05:05:43 +0000518 }
Eric Newberry84d3adc2017-08-09 23:31:40 -0400519 if (item.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
520 os << flagSep << "lp-reliability";
521 }
Eric Newberryde332452018-01-30 11:45:32 -0700522 if (item.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
523 os << flagSep << "congestion-marking";
524 }
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000525 os << '}';
Eric Newberry6d932e82016-11-24 05:05:43 +0000526
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000527 os << ia.end();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000528}
529
Eric Newberryde332452018-01-30 11:45:32 -0700530void
531FaceModule::printFaceParams(std::ostream& os, text::ItemAttributes& ia, const ControlParameters& resp)
532{
Junxiao Shi8dc473a2018-03-02 15:10:18 +0000533 os << ia("reliability") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)}
534 << ia("congestion-marking") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)};
Eric Newberryde332452018-01-30 11:45:32 -0700535 if (resp.hasBaseCongestionMarkingInterval()) {
536 os << ia("congestion-marking-interval")
537 << text::formatDuration<time::milliseconds>(resp.getBaseCongestionMarkingInterval());
538 }
539 if (resp.hasDefaultCongestionThreshold()) {
540 os << ia("default-congestion-threshold") << resp.getDefaultCongestionThreshold() << "B";
541 }
Eric Newberry4f8dd962018-06-17 21:32:07 -0700542 if (resp.hasMtu()) {
543 os << ia("mtu") << resp.getMtu();
544 }
Eric Newberryde332452018-01-30 11:45:32 -0700545 os << '\n';
546}
547
Junxiao Shi331ade72016-08-19 14:07:19 +0000548} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000549} // namespace tools
550} // namespace nfd