29 #include <ndn-cxx/mgmt/nfd/status-dataset.hpp>
31 #include <boost/lexical_cast/try_lexical_convert.hpp>
80 FaceQueryFilter filter;
82 filter.setRemoteUri(remoteUri->toString());
85 filter.setLocalUri(localUri->toString());
88 filter.setUriScheme(*uriScheme);
94 ctx.
exitCode =
static_cast<int>(res);
97 for (
const FaceStatus& item : findFace.
getResults()) {
108 BOOST_ASSERT_MSG(
false,
"unexpected FindFace result");
116 uint64_t faceId = ctx.
args.
get<uint64_t>(
"id");
121 ctx.
exitCode =
static_cast<int>(res);
128 ctx.
err << findFace.getErrorReason() <<
'\n';
131 BOOST_ASSERT_MSG(
false,
"unexpected FindFace result");
142 case FacePersistency::FACE_PERSISTENCY_NONE:
143 return y != FacePersistency::FACE_PERSISTENCY_NONE;
144 case FacePersistency::FACE_PERSISTENCY_ON_DEMAND:
145 return y == FacePersistency::FACE_PERSISTENCY_PERSISTENT ||
146 y == FacePersistency::FACE_PERSISTENCY_PERMANENT;
147 case FacePersistency::FACE_PERSISTENCY_PERSISTENT:
148 return y == FacePersistency::FACE_PERSISTENCY_PERMANENT;
149 case FacePersistency::FACE_PERSISTENCY_PERMANENT:
152 return static_cast<int>(x) <
static_cast<int>(y);
158 auto remoteUri = ctx.
args.
get<FaceUri>(
"remote");
160 auto persistency = ctx.
args.
get<FacePersistency>(
"persistency", FacePersistency::FACE_PERSISTENCY_PERSISTENT);
162 auto congestionMarking = ctx.
args.
getTribool(
"congestion-marking");
163 auto baseCongestionMarkingIntervalMs = ctx.
args.
getOptional<uint64_t>(
"congestion-marking-interval");
164 auto defaultCongestionThreshold = ctx.
args.
getOptional<uint64_t>(
"default-congestion-threshold");
168 std::optional<uint64_t> mtu;
169 if (mtuArg ==
"auto") {
170 mtu = std::numeric_limits<uint64_t>::max();
175 if (!boost::conversion::try_lexical_convert<int64_t>(*mtuArg, v) || v < 0) {
177 ctx.
err <<
"MTU must either be a non-negative integer or 'auto'\n";
181 mtu =
static_cast<uint64_t
>(v);
184 std::optional<FaceUri> canonicalRemote;
185 std::optional<FaceUri> canonicalLocal;
187 auto updateFace = [&] (ControlParameters respParams, ControlParameters resp) {
189 resp.setLocalUri(respParams.getLocalUri())
190 .setUri(respParams.getUri());
194 auto handle409 = [&] (
const ControlResponse& resp) {
195 ControlParameters respParams(resp.getBody());
196 if (respParams.getUri() != canonicalRemote->toString()) {
201 bool isChangingParams =
false;
202 ControlParameters params;
203 params.setFaceId(respParams.getFaceId());
205 if (mtu && (!respParams.hasMtu() || respParams.getMtu() != *mtu)) {
206 isChangingParams =
true;
211 isChangingParams =
true;
212 params.setFacePersistency(persistency);
215 if (!boost::logic::indeterminate(lpReliability) &&
216 lpReliability != respParams.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
217 isChangingParams =
true;
218 params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED,
bool(lpReliability));
221 if (!boost::logic::indeterminate(congestionMarking) &&
222 congestionMarking != respParams.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
223 isChangingParams =
true;
224 params.setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED,
bool(congestionMarking));
227 if (baseCongestionMarkingIntervalMs) {
228 isChangingParams =
true;
229 params.setBaseCongestionMarkingInterval(time::milliseconds(*baseCongestionMarkingIntervalMs));
232 if (defaultCongestionThreshold) {
233 isChangingParams =
true;
234 params.setDefaultCongestionThreshold(*defaultCongestionThreshold);
237 if (isChangingParams) {
238 ctx.
controller.start<ndn::nfd::FaceUpdateCommand>(
240 [=, &updateFace] (
const auto& cp) { updateFace(respParams, cp); },
252 auto doCreateFace = [&] {
253 ControlParameters params;
254 params.setUri(canonicalRemote->toString());
255 if (canonicalLocal) {
256 params.setLocalUri(canonicalLocal->toString());
258 params.setFacePersistency(persistency);
259 if (!boost::logic::indeterminate(lpReliability)) {
260 params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED,
bool(lpReliability));
262 if (!boost::logic::indeterminate(congestionMarking)) {
263 params.setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED,
bool(congestionMarking));
265 if (baseCongestionMarkingIntervalMs) {
266 params.setBaseCongestionMarkingInterval(time::milliseconds(*baseCongestionMarkingIntervalMs));
268 if (defaultCongestionThreshold) {
269 params.setDefaultCongestionThreshold(*defaultCongestionThreshold);
275 ctx.
controller.start<ndn::nfd::FaceCreateCommand>(
277 [&] (
const ControlParameters& resp) {
280 [&] (
const ControlResponse& resp) {
281 if (resp.getCode() == 409 && handle409(resp)) {
290 std::tie(canonicalRemote, error) =
canonize(ctx, remoteUri);
291 if (canonicalRemote) {
294 std::tie(canonicalLocal, error) =
canonize(ctx, *localUri);
295 if (canonicalLocal) {
302 ctx.
exitCode =
static_cast<int>(canonizationError.first);
303 ctx.
err << canonizationError.second <<
'\n';
313 ctx.
exitCode =
static_cast<int>(canonizationError.first);
314 ctx.
err << canonizationError.second <<
'\n';
317 ctx.
face.processEvents();
326 ctx.
exitCode =
static_cast<int>(res);
336 ctx.
err <<
"Multiple faces match specified remote FaceUri. Re-run the command with a FaceId:";
341 BOOST_ASSERT_MSG(
false,
"unexpected FindFace result");
347 ctx.
controller.start<ndn::nfd::FaceDestroyCommand>(
348 ControlParameters().setFaceId(face.getFaceId()),
349 [&] (
const ControlParameters& resp) {
351 ctx.
out <<
"face-destroyed ";
353 ctx.
out << ia(
"id") << face.getFaceId()
354 << ia(
"local") << face.getLocalUri()
355 << ia(
"remote") << face.getRemoteUri()
356 << ia(
"persistency") << face.getFacePersistency();
362 ctx.
face.processEvents();
367 const std::function<
void()>& onSuccess,
368 const ndn::nfd::DatasetFailureCallback& onFailure,
369 const CommandOptions& options)
371 controller.fetch<ndn::nfd::FaceDataset>(
372 [
this, onSuccess] (
const auto& result) {
383 for (
const FaceStatus& item : m_status) {
394 os <<
"<faceId>" << item.getFaceId() <<
"</faceId>";
395 os <<
"<remoteUri>" <<
xml::Text{item.getRemoteUri()} <<
"</remoteUri>";
396 os <<
"<localUri>" <<
xml::Text{item.getLocalUri()} <<
"</localUri>";
398 if (item.hasExpirationPeriod()) {
400 <<
"</expirationPeriod>";
402 os <<
"<faceScope>" << item.getFaceScope() <<
"</faceScope>";
403 os <<
"<facePersistency>" << item.getFacePersistency() <<
"</facePersistency>";
404 os <<
"<linkType>" << item.getLinkType() <<
"</linkType>";
406 if (!item.hasBaseCongestionMarkingInterval() && !item.hasDefaultCongestionThreshold()) {
407 os <<
"<congestion/>";
410 os <<
"<congestion>";
411 if (item.hasBaseCongestionMarkingInterval()) {
412 os <<
"<baseMarkingInterval>" <<
xml::formatDuration(item.getBaseCongestionMarkingInterval())
413 <<
"</baseMarkingInterval>";
415 if (item.hasDefaultCongestionThreshold()) {
416 os <<
"<defaultThreshold>" << item.getDefaultCongestionThreshold() <<
"</defaultThreshold>";
418 os <<
"</congestion>";
422 os <<
"<mtu>" << item.getMtu() <<
"</mtu>";
425 if (item.getFlags() == 0) {
430 os <<
xml::Flag{
"localFieldsEnabled", item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)};
431 os <<
xml::Flag{
"lpReliabilityEnabled", item.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)};
432 os <<
xml::Flag{
"congestionMarkingEnabled", item.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)};
436 os <<
"<packetCounters>";
437 os <<
"<incomingPackets>"
438 <<
"<nInterests>" << item.getNInInterests() <<
"</nInterests>"
439 <<
"<nData>" << item.getNInData() <<
"</nData>"
440 <<
"<nNacks>" << item.getNInNacks() <<
"</nNacks>"
441 <<
"</incomingPackets>";
442 os <<
"<outgoingPackets>"
443 <<
"<nInterests>" << item.getNOutInterests() <<
"</nInterests>"
444 <<
"<nData>" << item.getNOutData() <<
"</nData>"
445 <<
"<nNacks>" << item.getNOutNacks() <<
"</nNacks>"
446 <<
"</outgoingPackets>";
447 os <<
"</packetCounters>";
449 os <<
"<byteCounters>";
450 os <<
"<incomingBytes>" << item.getNInBytes() <<
"</incomingBytes>";
451 os <<
"<outgoingBytes>" << item.getNOutBytes() <<
"</outgoingBytes>";
452 os <<
"</byteCounters>";
461 for (
const FaceStatus& item : m_status) {
473 os << ia(
"faceid") << item.getFaceId();
474 os << ia(
"remote") << item.getRemoteUri();
475 os << ia(
"local") << item.getLocalUri();
477 if (item.hasExpirationPeriod()) {
478 os << ia(
"expires") << text::formatDuration<time::seconds>(item.getExpirationPeriod());
481 if (item.hasBaseCongestionMarkingInterval() || item.hasDefaultCongestionThreshold()) {
482 os << ia(
"congestion") <<
"{";
484 if (item.hasBaseCongestionMarkingInterval()) {
485 os << congestionSep <<
"base-marking-interval="
486 << text::formatDuration<time::milliseconds>(item.getBaseCongestionMarkingInterval());
488 if (item.hasDefaultCongestionThreshold()) {
489 os << congestionSep <<
"default-threshold=" << item.getDefaultCongestionThreshold() <<
"B";
495 os << ia(
"mtu") << item.getMtu();
500 << item.getNInInterests() <<
"i "
501 << item.getNInData() <<
"d "
502 << item.getNInNacks() <<
"n "
503 << item.getNInBytes() <<
"B} "
505 << item.getNOutInterests() <<
"i "
506 << item.getNOutData() <<
"d "
507 << item.getNOutNacks() <<
"n "
508 << item.getNOutBytes() <<
"B}}";
510 os << ia(
"flags") <<
'{';
512 os << flagSep << item.getFaceScope();
513 os << flagSep << item.getFacePersistency();
514 os << flagSep << item.getLinkType();
515 if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) {
516 os << flagSep <<
"local-fields";
518 if (item.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) {
519 os << flagSep <<
"lp-reliability";
521 if (item.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) {
522 os << flagSep <<
"congestion-marking";
531 const std::string& actionSummary,
532 const ControlParameters& resp)
535 os << actionSummary <<
' '
536 << ia(
"id") << resp.getFaceId()
537 << ia(
"local") << resp.getLocalUri()
538 << ia(
"remote") << resp.getUri()
539 << ia(
"persistency") << resp.getFacePersistency();
546 os << ia(
"reliability") <<
text::OnOff{resp.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)}
547 << ia(
"congestion-marking") <<
text::OnOff{resp.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)};
548 if (resp.hasBaseCongestionMarkingInterval()) {
549 os << ia(
"congestion-marking-interval")
550 << text::formatDuration<time::milliseconds>(resp.getBaseCongestionMarkingInterval());
552 if (resp.hasDefaultCongestionThreshold()) {
553 os << ia(
"default-congestion-threshold") << resp.getDefaultCongestionThreshold() <<
"B";
556 os << ia(
"mtu") << resp.getMtu();
Context for command execution.
std::ostream & out
output stream
ndn::nfd::Controller & controller
const CommandArguments & args
ndn::nfd::CommandFailureCallback makeCommandFailureHandler(const std::string &commandName)
int exitCode
program exit code
ndn::nfd::CommandOptions makeCommandOptions() const
std::ostream & err
error stream
Print attributes of an item.
Print different string on first and subsequent usage.
Print boolean as 'on' or 'off'.