blob: 83c600697a6de7f8c6a7a57e9cfc08f3dfcca240 [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/*
Davide Pesaventofa2aa502019-03-22 13:30:02 -04003 * Copyright (c) 2014-2019, 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)
61 .addArg("mtu", ArgValueType::UNSIGNED, 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 Newberry4f8dd962018-06-17 21:32:07 -0700163 auto mtu = ctx.args.getOptional<uint64_t>("mtu");
Eric Newberry84d3adc2017-08-09 23:31:40 -0400164
Junxiao Shi0d976922017-04-01 14:35:21 +0000165 FaceUri canonicalRemote;
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400166 optional<FaceUri> canonicalLocal;
Junxiao Shi0d976922017-04-01 14:35:21 +0000167
168 auto handleCanonizeError = [&] (const FaceUri& faceUri, const std::string& error) {
169 ctx.exitCode = 4;
170 ctx.err << "Error when canonizing '" << faceUri << "': " << error << '\n';
171 };
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000172
173 auto printPositiveResult = [&] (const std::string& actionSummary, const ControlParameters& resp) {
174 text::ItemAttributes ia;
175 ctx.out << actionSummary << ' '
176 << ia("id") << resp.getFaceId()
Junxiao Shi1cce2a32017-05-02 02:39:55 +0000177 << ia("local") << resp.getLocalUri()
178 << ia("remote") << resp.getUri()
Eric Newberryde332452018-01-30 11:45:32 -0700179 << ia("persistency") << resp.getFacePersistency();
180 printFaceParams(ctx.out, ia, resp);
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000181 };
182
Eric Newberry84d3adc2017-08-09 23:31:40 -0400183 auto updateFace = [&printPositiveResult] (ControlParameters respParams, ControlParameters resp) {
184 // faces/update response does not have FaceUris, copy from faces/create response
185 resp.setLocalUri(respParams.getLocalUri())
186 .setUri(respParams.getUri());
187 printPositiveResult("face-updated", resp);
188 };
189
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000190 auto handle409 = [&] (const ControlResponse& resp) {
191 ControlParameters respParams(resp.getBody());
Junxiao Shi0d976922017-04-01 14:35:21 +0000192 if (respParams.getUri() != canonicalRemote.toString()) {
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000193 // we are conflicting with a different face, which is a general error
194 return false;
195 }
196
Eric Newberry4f8dd962018-06-17 21:32:07 -0700197 if (mtu && (!respParams.hasMtu() || respParams.getMtu() != *mtu)) {
198 // Mtu cannot be changed with faces/update
199 return false;
200 }
201 else if (persistencyLessThan(respParams.getFacePersistency(), persistency)) {
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000202 // need to upgrade persistency
Eric Newberry84d3adc2017-08-09 23:31:40 -0400203 ControlParameters params;
204 params.setFaceId(respParams.getFaceId()).setFacePersistency(persistency);
205 if (!boost::logic::indeterminate(lpReliability)) {
Davide Pesaventofa2aa502019-03-22 13:30:02 -0400206 params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, bool(lpReliability));
Eric Newberry84d3adc2017-08-09 23:31:40 -0400207 }
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000208 ctx.controller.start<ndn::nfd::FaceUpdateCommand>(
Eric Newberry84d3adc2017-08-09 23:31:40 -0400209 params,
210 bind(updateFace, respParams, _1),
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000211 ctx.makeCommandFailureHandler("upgrading face persistency"),
212 ctx.makeCommandOptions());
213 }
Eric Newberryde332452018-01-30 11:45:32 -0700214 else if ((!boost::logic::indeterminate(lpReliability) &&
215 lpReliability != respParams.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) ||
216 (!boost::logic::indeterminate(congestionMarking) &&
217 congestionMarking != respParams.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) ||
218 baseCongestionMarkingIntervalMs ||
219 defaultCongestionThreshold) {
Eric Newberry84d3adc2017-08-09 23:31:40 -0400220 ControlParameters params;
Eric Newberryde332452018-01-30 11:45:32 -0700221 params.setFaceId(respParams.getFaceId());
222
223 if (!boost::logic::indeterminate(lpReliability) &&
224 lpReliability != respParams.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
Davide Pesaventofa2aa502019-03-22 13:30:02 -0400225 params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, bool(lpReliability));
Eric Newberryde332452018-01-30 11:45:32 -0700226 }
227 if (!boost::logic::indeterminate(congestionMarking) &&
228 congestionMarking != respParams.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
Davide Pesaventofa2aa502019-03-22 13:30:02 -0400229 params.setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, bool(congestionMarking));
Eric Newberryde332452018-01-30 11:45:32 -0700230 }
231
232 if (baseCongestionMarkingIntervalMs) {
233 params.setBaseCongestionMarkingInterval(time::milliseconds(*baseCongestionMarkingIntervalMs));
234 }
235
236 if (defaultCongestionThreshold) {
237 params.setDefaultCongestionThreshold(*defaultCongestionThreshold);
238 }
239
Eric Newberry84d3adc2017-08-09 23:31:40 -0400240 ctx.controller.start<ndn::nfd::FaceUpdateCommand>(
241 params,
242 bind(updateFace, respParams, _1),
Eric Newberryde332452018-01-30 11:45:32 -0700243 ctx.makeCommandFailureHandler("updating face"),
Eric Newberry84d3adc2017-08-09 23:31:40 -0400244 ctx.makeCommandOptions());
245 }
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000246 else {
Eric Newberry84d3adc2017-08-09 23:31:40 -0400247 // don't do anything
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000248 printPositiveResult("face-exists", respParams);
249 }
250 return true;
251 };
Junxiao Shi1d7fef52017-02-02 05:33:14 +0000252
Junxiao Shi0d976922017-04-01 14:35:21 +0000253 auto doCreateFace = [&] {
254 ControlParameters params;
255 params.setUri(canonicalRemote.toString());
256 if (canonicalLocal) {
257 params.setLocalUri(canonicalLocal->toString());
258 }
259 params.setFacePersistency(persistency);
Eric Newberry84d3adc2017-08-09 23:31:40 -0400260 if (!boost::logic::indeterminate(lpReliability)) {
Davide Pesaventofa2aa502019-03-22 13:30:02 -0400261 params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, bool(lpReliability));
Eric Newberry84d3adc2017-08-09 23:31:40 -0400262 }
Eric Newberryde332452018-01-30 11:45:32 -0700263 if (!boost::logic::indeterminate(congestionMarking)) {
Davide Pesaventofa2aa502019-03-22 13:30:02 -0400264 params.setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, bool(congestionMarking));
Eric Newberryde332452018-01-30 11:45:32 -0700265 }
266 if (baseCongestionMarkingIntervalMs) {
267 params.setBaseCongestionMarkingInterval(time::milliseconds(*baseCongestionMarkingIntervalMs));
268 }
269 if (defaultCongestionThreshold) {
270 params.setDefaultCongestionThreshold(*defaultCongestionThreshold);
271 }
Eric Newberry4f8dd962018-06-17 21:32:07 -0700272 if (mtu) {
273 params.setMtu(*mtu);
274 }
Junxiao Shi0d976922017-04-01 14:35:21 +0000275
276 ctx.controller.start<ndn::nfd::FaceCreateCommand>(
277 params,
278 bind(printPositiveResult, "face-created", _1),
279 [&] (const ControlResponse& resp) {
280 if (resp.getCode() == 409 && handle409(resp)) {
281 return;
282 }
283 ctx.makeCommandFailureHandler("creating face")(resp); // invoke general error handler
284 },
285 ctx.makeCommandOptions());
286 };
287
288 remoteUri.canonize(
289 [&] (const FaceUri& canonicalUri) {
290 canonicalRemote = canonicalUri;
291 if (localUri) {
292 localUri->canonize(
293 [&] (const FaceUri& canonicalUri) {
294 canonicalLocal = canonicalUri;
295 doCreateFace();
296 },
297 bind(handleCanonizeError, *localUri, _1),
298 ctx.face.getIoService(), ctx.getTimeout());
299 }
300 else {
301 doCreateFace();
302 }
Junxiao Shi1d7fef52017-02-02 05:33:14 +0000303 },
Junxiao Shi0d976922017-04-01 14:35:21 +0000304 bind(handleCanonizeError, remoteUri, _1),
Junxiao Shi1d7fef52017-02-02 05:33:14 +0000305 ctx.face.getIoService(), ctx.getTimeout());
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000306
307 ctx.face.processEvents();
308}
309
310void
Junxiao Shi05dd4442017-02-06 22:50:07 +0000311FaceModule::destroy(ExecuteContext& ctx)
312{
Junxiao Shi05dd4442017-02-06 22:50:07 +0000313 FindFace findFace(ctx);
Davide Pesavento8b663a92018-11-21 22:57:20 -0500314 FindFace::Code res = findFace.execute(ctx.args.at("face"));
Junxiao Shi05dd4442017-02-06 22:50:07 +0000315
316 ctx.exitCode = static_cast<int>(res);
317 switch (res) {
318 case FindFace::Code::OK:
319 break;
320 case FindFace::Code::ERROR:
321 case FindFace::Code::CANONIZE_ERROR:
322 case FindFace::Code::NOT_FOUND:
323 ctx.err << findFace.getErrorReason() << '\n';
324 return;
325 case FindFace::Code::AMBIGUOUS:
326 ctx.err << "Multiple faces match specified remote FaceUri. Re-run the command with a FaceId:";
327 findFace.printDisambiguation(ctx.err, FindFace::DisambiguationStyle::LOCAL_URI);
328 ctx.err << '\n';
329 return;
330 default:
331 BOOST_ASSERT_MSG(false, "unexpected FindFace result");
332 return;
333 }
334
335 const FaceStatus& face = findFace.getFaceStatus();
336
337 ctx.controller.start<ndn::nfd::FaceDestroyCommand>(
338 ControlParameters().setFaceId(face.getFaceId()),
339 [&] (const ControlParameters& resp) {
340 ctx.out << "face-destroyed ";
341 text::ItemAttributes ia;
342 ctx.out << ia("id") << face.getFaceId()
343 << ia("local") << face.getLocalUri()
344 << ia("remote") << face.getRemoteUri()
Eric Newberryde332452018-01-30 11:45:32 -0700345 << ia("persistency") << face.getFacePersistency();
346 printFaceParams(ctx.out, ia, resp);
Junxiao Shi05dd4442017-02-06 22:50:07 +0000347 },
348 ctx.makeCommandFailureHandler("destroying face"),
349 ctx.makeCommandOptions());
350
351 ctx.face.processEvents();
352}
353
354void
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000355FaceModule::fetchStatus(Controller& controller,
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400356 const std::function<void()>& onSuccess,
Junxiao Shi29b41282016-08-22 03:47:02 +0000357 const Controller::DatasetFailCallback& onFailure,
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000358 const CommandOptions& options)
359{
360 controller.fetch<ndn::nfd::FaceDataset>(
361 [this, onSuccess] (const std::vector<FaceStatus>& result) {
362 m_status = result;
363 onSuccess();
364 },
365 onFailure, options);
366}
367
368void
369FaceModule::formatStatusXml(std::ostream& os) const
370{
371 os << "<faces>";
372 for (const FaceStatus& item : m_status) {
373 this->formatItemXml(os, item);
374 }
375 os << "</faces>";
376}
377
378void
379FaceModule::formatItemXml(std::ostream& os, const FaceStatus& item) const
380{
381 os << "<face>";
382
383 os << "<faceId>" << item.getFaceId() << "</faceId>";
384 os << "<remoteUri>" << xml::Text{item.getRemoteUri()} << "</remoteUri>";
385 os << "<localUri>" << xml::Text{item.getLocalUri()} << "</localUri>";
386
387 if (item.hasExpirationPeriod()) {
388 os << "<expirationPeriod>" << xml::formatDuration(item.getExpirationPeriod())
389 << "</expirationPeriod>";
390 }
391 os << "<faceScope>" << item.getFaceScope() << "</faceScope>";
392 os << "<facePersistency>" << item.getFacePersistency() << "</facePersistency>";
393 os << "<linkType>" << item.getLinkType() << "</linkType>";
394
Eric Newberryde332452018-01-30 11:45:32 -0700395 if (!item.hasBaseCongestionMarkingInterval() && !item.hasDefaultCongestionThreshold()) {
396 os << "<congestion/>";
397 }
398 else {
399 os << "<congestion>";
400 if (item.hasBaseCongestionMarkingInterval()) {
401 os << "<baseMarkingInterval>" << xml::formatDuration(item.getBaseCongestionMarkingInterval())
402 << "</baseMarkingInterval>";
403 }
404 if (item.hasDefaultCongestionThreshold()) {
405 os << "<defaultThreshold>" << item.getDefaultCongestionThreshold() << "</defaultThreshold>";
406 }
407 os << "</congestion>";
408 }
409
Eric Newberry4f8dd962018-06-17 21:32:07 -0700410 if (item.hasMtu()) {
411 os << "<mtu>" << item.getMtu() << "</mtu>";
412 }
413
Eric Newberry6d932e82016-11-24 05:05:43 +0000414 if (item.getFlags() == 0) {
415 os << "<flags/>";
416 }
417 else {
418 os << "<flags>";
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000419 os << xml::Flag{"localFieldsEnabled", item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)};
420 os << xml::Flag{"lpReliabilityEnabled", item.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)};
421 os << xml::Flag{"congestionMarkingEnabled", item.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)};
Eric Newberry6d932e82016-11-24 05:05:43 +0000422 os << "</flags>";
423 }
424
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000425 os << "<packetCounters>";
426 os << "<incomingPackets>"
427 << "<nInterests>" << item.getNInInterests() << "</nInterests>"
Junxiao Shif03d4792017-04-06 16:41:22 +0000428 << "<nData>" << item.getNInData() << "</nData>"
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000429 << "<nNacks>" << item.getNInNacks() << "</nNacks>"
430 << "</incomingPackets>";
431 os << "<outgoingPackets>"
432 << "<nInterests>" << item.getNOutInterests() << "</nInterests>"
Junxiao Shif03d4792017-04-06 16:41:22 +0000433 << "<nData>" << item.getNOutData() << "</nData>"
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000434 << "<nNacks>" << item.getNOutNacks() << "</nNacks>"
435 << "</outgoingPackets>";
436 os << "</packetCounters>";
437
438 os << "<byteCounters>";
439 os << "<incomingBytes>" << item.getNInBytes() << "</incomingBytes>";
440 os << "<outgoingBytes>" << item.getNOutBytes() << "</outgoingBytes>";
441 os << "</byteCounters>";
442
443 os << "</face>";
444}
445
446void
447FaceModule::formatStatusText(std::ostream& os) const
448{
449 os << "Faces:\n";
450 for (const FaceStatus& item : m_status) {
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000451 os << " ";
452 formatItemText(os, item, false);
453 os << '\n';
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000454 }
455}
456
457void
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000458FaceModule::formatItemText(std::ostream& os, const FaceStatus& item, bool wantMultiLine)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000459{
Eric Newberryde332452018-01-30 11:45:32 -0700460 text::ItemAttributes ia(wantMultiLine, 10);
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000461
462 os << ia("faceid") << item.getFaceId();
463 os << ia("remote") << item.getRemoteUri();
464 os << ia("local") << item.getLocalUri();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000465
466 if (item.hasExpirationPeriod()) {
Eric Newberryde332452018-01-30 11:45:32 -0700467 os << ia("expires") << text::formatDuration<time::seconds>(item.getExpirationPeriod());
468 }
469
470 if (item.hasBaseCongestionMarkingInterval() || item.hasDefaultCongestionThreshold()) {
471 os << ia("congestion") << "{";
472 text::Separator congestionSep("", " ");
473 if (item.hasBaseCongestionMarkingInterval()) {
474 os << congestionSep << "base-marking-interval="
475 << text::formatDuration<time::milliseconds>(item.getBaseCongestionMarkingInterval());
476 }
477 if (item.hasDefaultCongestionThreshold()) {
478 os << congestionSep << "default-threshold=" << item.getDefaultCongestionThreshold() << "B";
479 }
480 os << "}";
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000481 }
482
Eric Newberry4f8dd962018-06-17 21:32:07 -0700483 if (item.hasMtu()) {
484 os << ia("mtu") << item.getMtu();
485 }
486
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000487 os << ia("counters")
488 << "{in={"
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000489 << item.getNInInterests() << "i "
Junxiao Shif03d4792017-04-06 16:41:22 +0000490 << item.getNInData() << "d "
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000491 << item.getNInNacks() << "n "
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000492 << item.getNInBytes() << "B} "
493 << "out={"
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000494 << item.getNOutInterests() << "i "
Junxiao Shif03d4792017-04-06 16:41:22 +0000495 << item.getNOutData() << "d "
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000496 << item.getNOutNacks() << "n "
497 << item.getNOutBytes() << "B}}";
498
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000499 os << ia("flags") << '{';
500 text::Separator flagSep("", " ");
501 os << flagSep << item.getFaceScope();
502 os << flagSep << item.getFacePersistency();
503 os << flagSep << item.getLinkType();
Eric Newberry6d932e82016-11-24 05:05:43 +0000504 if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) {
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000505 os << flagSep << "local-fields";
Eric Newberry6d932e82016-11-24 05:05:43 +0000506 }
Eric Newberry84d3adc2017-08-09 23:31:40 -0400507 if (item.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
508 os << flagSep << "lp-reliability";
509 }
Eric Newberryde332452018-01-30 11:45:32 -0700510 if (item.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
511 os << flagSep << "congestion-marking";
512 }
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000513 os << '}';
Eric Newberry6d932e82016-11-24 05:05:43 +0000514
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000515 os << ia.end();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000516}
517
Eric Newberryde332452018-01-30 11:45:32 -0700518void
519FaceModule::printFaceParams(std::ostream& os, text::ItemAttributes& ia, const ControlParameters& resp)
520{
Junxiao Shi8dc473a2018-03-02 15:10:18 +0000521 os << ia("reliability") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)}
522 << ia("congestion-marking") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)};
Eric Newberryde332452018-01-30 11:45:32 -0700523 if (resp.hasBaseCongestionMarkingInterval()) {
524 os << ia("congestion-marking-interval")
525 << text::formatDuration<time::milliseconds>(resp.getBaseCongestionMarkingInterval());
526 }
527 if (resp.hasDefaultCongestionThreshold()) {
528 os << ia("default-congestion-threshold") << resp.getDefaultCongestionThreshold() << "B";
529 }
Eric Newberry4f8dd962018-06-17 21:32:07 -0700530 if (resp.hasMtu()) {
531 os << ia("mtu") << resp.getMtu();
532 }
Eric Newberryde332452018-01-30 11:45:32 -0700533 os << '\n';
534}
535
Junxiao Shi331ade72016-08-19 14:07:19 +0000536} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000537} // namespace tools
538} // namespace nfd