Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 4064127 | 2023-03-16 13:31:12 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 4 | * 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 Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 26 | #include "face-module.hpp" |
Davide Pesavento | 990620a | 2022-06-05 00:25:53 -0400 | [diff] [blame] | 27 | #include "face-helpers.hpp" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 28 | |
Davide Pesavento | 4064127 | 2023-03-16 13:31:12 -0400 | [diff] [blame] | 29 | #include <ndn-cxx/mgmt/nfd/status-dataset.hpp> |
| 30 | |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 31 | #include <boost/lexical_cast/try_lexical_convert.hpp> |
| 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | namespace nfd::tools::nfdc { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 34 | |
| 35 | void |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 36 | FaceModule::registerCommands(CommandParser& parser) |
| 37 | { |
Junxiao Shi | 36e5429 | 2017-02-17 18:43:16 +0000 | [diff] [blame] | 38 | CommandDefinition defFaceList("face", "list"); |
| 39 | defFaceList |
| 40 | .setTitle("print face list") |
| 41 | .addArg("remote", ArgValueType::FACE_URI, Required::NO, Positional::YES) |
| 42 | .addArg("local", ArgValueType::FACE_URI, Required::NO, Positional::NO) |
| 43 | .addArg("scheme", ArgValueType::STRING, Required::NO, Positional::NO, "scheme"); |
| 44 | parser.addCommand(defFaceList, &FaceModule::list); |
Davide Pesavento | d214744 | 2018-02-19 23:58:17 -0500 | [diff] [blame] | 45 | parser.addAlias("face", "list", ""); |
Junxiao Shi | 36e5429 | 2017-02-17 18:43:16 +0000 | [diff] [blame] | 46 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 47 | CommandDefinition defFaceShow("face", "show"); |
| 48 | defFaceShow |
| 49 | .setTitle("show face information") |
| 50 | .addArg("id", ArgValueType::UNSIGNED, Required::YES, Positional::YES); |
| 51 | parser.addCommand(defFaceShow, &FaceModule::show); |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 52 | |
| 53 | CommandDefinition defFaceCreate("face", "create"); |
| 54 | defFaceCreate |
| 55 | .setTitle("create a face") |
| 56 | .addArg("remote", ArgValueType::FACE_URI, Required::YES, Positional::YES) |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 57 | .addArg("persistency", ArgValueType::FACE_PERSISTENCY, Required::NO, Positional::YES) |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 58 | .addArg("local", ArgValueType::FACE_URI, Required::NO, Positional::NO) |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 59 | .addArg("reliability", ArgValueType::BOOLEAN, Required::NO, Positional::NO) |
| 60 | .addArg("congestion-marking", ArgValueType::BOOLEAN, Required::NO, Positional::NO) |
| 61 | .addArg("congestion-marking-interval", ArgValueType::UNSIGNED, Required::NO, Positional::NO) |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame] | 62 | .addArg("default-congestion-threshold", ArgValueType::UNSIGNED, Required::NO, Positional::NO) |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 63 | .addArg("mtu", ArgValueType::STRING, Required::NO, Positional::NO); |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 64 | parser.addCommand(defFaceCreate, &FaceModule::create); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 65 | |
| 66 | CommandDefinition defFaceDestroy("face", "destroy"); |
| 67 | defFaceDestroy |
| 68 | .setTitle("destroy a face") |
| 69 | .addArg("face", ArgValueType::FACE_ID_OR_URI, Required::YES, Positional::YES); |
| 70 | parser.addCommand(defFaceDestroy, &FaceModule::destroy); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
Junxiao Shi | 36e5429 | 2017-02-17 18:43:16 +0000 | [diff] [blame] | 74 | FaceModule::list(ExecuteContext& ctx) |
| 75 | { |
| 76 | auto remoteUri = ctx.args.getOptional<FaceUri>("remote"); |
| 77 | auto localUri = ctx.args.getOptional<FaceUri>("local"); |
| 78 | auto uriScheme = ctx.args.getOptional<std::string>("scheme"); |
| 79 | |
| 80 | FaceQueryFilter filter; |
| 81 | if (remoteUri) { |
| 82 | filter.setRemoteUri(remoteUri->toString()); |
| 83 | } |
| 84 | if (localUri) { |
| 85 | filter.setLocalUri(localUri->toString()); |
| 86 | } |
| 87 | if (uriScheme) { |
| 88 | filter.setUriScheme(*uriScheme); |
| 89 | } |
| 90 | |
| 91 | FindFace findFace(ctx); |
| 92 | FindFace::Code res = findFace.execute(filter, true); |
| 93 | |
| 94 | ctx.exitCode = static_cast<int>(res); |
| 95 | switch (res) { |
| 96 | case FindFace::Code::OK: |
| 97 | for (const FaceStatus& item : findFace.getResults()) { |
| 98 | formatItemText(ctx.out, item, false); |
| 99 | ctx.out << '\n'; |
| 100 | } |
| 101 | break; |
| 102 | case FindFace::Code::ERROR: |
| 103 | case FindFace::Code::NOT_FOUND: |
| 104 | case FindFace::Code::CANONIZE_ERROR: |
| 105 | ctx.err << findFace.getErrorReason() << '\n'; |
| 106 | break; |
| 107 | default: |
| 108 | BOOST_ASSERT_MSG(false, "unexpected FindFace result"); |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 114 | FaceModule::show(ExecuteContext& ctx) |
| 115 | { |
| 116 | uint64_t faceId = ctx.args.get<uint64_t>("id"); |
| 117 | |
Junxiao Shi | 8f803f2 | 2017-02-10 03:04:28 +0000 | [diff] [blame] | 118 | FindFace findFace(ctx); |
| 119 | FindFace::Code res = findFace.execute(faceId); |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 120 | |
Junxiao Shi | 8f803f2 | 2017-02-10 03:04:28 +0000 | [diff] [blame] | 121 | ctx.exitCode = static_cast<int>(res); |
| 122 | switch (res) { |
| 123 | case FindFace::Code::OK: |
| 124 | formatItemText(ctx.out, findFace.getFaceStatus(), true); |
| 125 | break; |
| 126 | case FindFace::Code::ERROR: |
| 127 | case FindFace::Code::NOT_FOUND: |
| 128 | ctx.err << findFace.getErrorReason() << '\n'; |
| 129 | break; |
| 130 | default: |
| 131 | BOOST_ASSERT_MSG(false, "unexpected FindFace result"); |
| 132 | break; |
| 133 | } |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 136 | /** \brief Order persistency in NONE < ON_DEMAND < PERSISTENCY < PERMANENT. |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 137 | */ |
| 138 | static bool |
| 139 | persistencyLessThan(FacePersistency x, FacePersistency y) |
| 140 | { |
| 141 | switch (x) { |
| 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: |
| 150 | return false; |
| 151 | } |
| 152 | return static_cast<int>(x) < static_cast<int>(y); |
| 153 | } |
| 154 | |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 155 | void |
| 156 | FaceModule::create(ExecuteContext& ctx) |
| 157 | { |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 158 | auto remoteUri = ctx.args.get<FaceUri>("remote"); |
| 159 | auto localUri = ctx.args.getOptional<FaceUri>("local"); |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 160 | auto persistency = ctx.args.get<FacePersistency>("persistency", FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 161 | auto lpReliability = ctx.args.getTribool("reliability"); |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 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"); |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 165 | auto mtuArg = ctx.args.getOptional<std::string>("mtu"); |
| 166 | |
| 167 | // MTU is nominally a uint64_t, but can be the string value 'auto' to unset an override MTU |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 168 | std::optional<uint64_t> mtu; |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 169 | if (mtuArg == "auto") { |
| 170 | mtu = std::numeric_limits<uint64_t>::max(); |
| 171 | } |
| 172 | else if (mtuArg) { |
| 173 | // boost::lexical_cast<uint64_t> will accept negative numbers |
| 174 | int64_t v = -1; |
| 175 | if (!boost::conversion::try_lexical_convert<int64_t>(*mtuArg, v) || v < 0) { |
| 176 | ctx.exitCode = 2; |
| 177 | ctx.err << "MTU must either be a non-negative integer or 'auto'\n"; |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | mtu = static_cast<uint64_t>(v); |
| 182 | } |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 183 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 184 | std::optional<FaceUri> canonicalRemote; |
| 185 | std::optional<FaceUri> canonicalLocal; |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 186 | |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 187 | auto updateFace = [&] (ControlParameters respParams, ControlParameters resp) { |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 188 | // faces/update response does not have FaceUris, copy from faces/create response |
| 189 | resp.setLocalUri(respParams.getLocalUri()) |
| 190 | .setUri(respParams.getUri()); |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 191 | printSuccess(ctx.out, "face-updated", resp); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 192 | }; |
| 193 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 194 | auto handle409 = [&] (const ControlResponse& resp) { |
| 195 | ControlParameters respParams(resp.getBody()); |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 196 | if (respParams.getUri() != canonicalRemote->toString()) { |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 197 | // we are conflicting with a different face, which is a general error |
| 198 | return false; |
| 199 | } |
| 200 | |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 201 | bool isChangingParams = false; |
| 202 | ControlParameters params; |
| 203 | params.setFaceId(respParams.getFaceId()); |
| 204 | |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame] | 205 | if (mtu && (!respParams.hasMtu() || respParams.getMtu() != *mtu)) { |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 206 | isChangingParams = true; |
| 207 | params.setMtu(*mtu); |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame] | 208 | } |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 209 | |
| 210 | if (persistencyLessThan(respParams.getFacePersistency(), persistency)) { |
| 211 | isChangingParams = true; |
| 212 | params.setFacePersistency(persistency); |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 213 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 214 | |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 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)); |
| 219 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 220 | |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 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)); |
| 225 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 226 | |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 227 | if (baseCongestionMarkingIntervalMs) { |
| 228 | isChangingParams = true; |
| 229 | params.setBaseCongestionMarkingInterval(time::milliseconds(*baseCongestionMarkingIntervalMs)); |
| 230 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 231 | |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 232 | if (defaultCongestionThreshold) { |
| 233 | isChangingParams = true; |
| 234 | params.setDefaultCongestionThreshold(*defaultCongestionThreshold); |
| 235 | } |
| 236 | |
| 237 | if (isChangingParams) { |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 238 | ctx.controller.start<ndn::nfd::FaceUpdateCommand>( |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 239 | params, |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 240 | [=, &updateFace] (const auto& cp) { updateFace(respParams, cp); }, |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 241 | ctx.makeCommandFailureHandler("updating face"), |
| 242 | ctx.makeCommandOptions()); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 243 | } |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 244 | else { |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 245 | // don't do anything |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 246 | printSuccess(ctx.out, "face-exists", respParams); |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 247 | } |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 248 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 249 | return true; |
| 250 | }; |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 251 | |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 252 | auto doCreateFace = [&] { |
| 253 | ControlParameters params; |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 254 | params.setUri(canonicalRemote->toString()); |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 255 | if (canonicalLocal) { |
| 256 | params.setLocalUri(canonicalLocal->toString()); |
| 257 | } |
| 258 | params.setFacePersistency(persistency); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 259 | if (!boost::logic::indeterminate(lpReliability)) { |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 260 | params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, bool(lpReliability)); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 261 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 262 | if (!boost::logic::indeterminate(congestionMarking)) { |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 263 | params.setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, bool(congestionMarking)); |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 264 | } |
| 265 | if (baseCongestionMarkingIntervalMs) { |
| 266 | params.setBaseCongestionMarkingInterval(time::milliseconds(*baseCongestionMarkingIntervalMs)); |
| 267 | } |
| 268 | if (defaultCongestionThreshold) { |
| 269 | params.setDefaultCongestionThreshold(*defaultCongestionThreshold); |
| 270 | } |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame] | 271 | if (mtu) { |
| 272 | params.setMtu(*mtu); |
| 273 | } |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 274 | |
| 275 | ctx.controller.start<ndn::nfd::FaceCreateCommand>( |
| 276 | params, |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 277 | [&] (const ControlParameters& resp) { |
| 278 | printSuccess(ctx.out, "face-created", resp); |
| 279 | }, |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 280 | [&] (const ControlResponse& resp) { |
| 281 | if (resp.getCode() == 409 && handle409(resp)) { |
| 282 | return; |
| 283 | } |
| 284 | ctx.makeCommandFailureHandler("creating face")(resp); // invoke general error handler |
| 285 | }, |
| 286 | ctx.makeCommandOptions()); |
| 287 | }; |
| 288 | |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 289 | std::string error; |
| 290 | std::tie(canonicalRemote, error) = canonize(ctx, remoteUri); |
| 291 | if (canonicalRemote) { |
| 292 | // RemoteUri canonization successful |
| 293 | if (localUri) { |
| 294 | std::tie(canonicalLocal, error) = canonize(ctx, *localUri); |
| 295 | if (canonicalLocal) { |
| 296 | // LocalUri canonization successful |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 297 | doCreateFace(); |
| 298 | } |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 299 | else { |
| 300 | // LocalUri canonization failure |
| 301 | auto canonizationError = canonizeErrorHelper(*localUri, error, "local FaceUri"); |
| 302 | ctx.exitCode = static_cast<int>(canonizationError.first); |
| 303 | ctx.err << canonizationError.second << '\n'; |
| 304 | } |
| 305 | } |
| 306 | else { |
| 307 | doCreateFace(); |
| 308 | } |
| 309 | } |
| 310 | else { |
| 311 | // RemoteUri canonization failure |
| 312 | auto canonizationError = canonizeErrorHelper(remoteUri, error, "remote FaceUri"); |
| 313 | ctx.exitCode = static_cast<int>(canonizationError.first); |
| 314 | ctx.err << canonizationError.second << '\n'; |
| 315 | } |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 316 | |
| 317 | ctx.face.processEvents(); |
| 318 | } |
| 319 | |
| 320 | void |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 321 | FaceModule::destroy(ExecuteContext& ctx) |
| 322 | { |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 323 | FindFace findFace(ctx); |
Davide Pesavento | 8b663a9 | 2018-11-21 22:57:20 -0500 | [diff] [blame] | 324 | FindFace::Code res = findFace.execute(ctx.args.at("face")); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 325 | |
| 326 | ctx.exitCode = static_cast<int>(res); |
| 327 | switch (res) { |
| 328 | case FindFace::Code::OK: |
| 329 | break; |
| 330 | case FindFace::Code::ERROR: |
| 331 | case FindFace::Code::CANONIZE_ERROR: |
| 332 | case FindFace::Code::NOT_FOUND: |
| 333 | ctx.err << findFace.getErrorReason() << '\n'; |
| 334 | return; |
| 335 | case FindFace::Code::AMBIGUOUS: |
| 336 | ctx.err << "Multiple faces match specified remote FaceUri. Re-run the command with a FaceId:"; |
| 337 | findFace.printDisambiguation(ctx.err, FindFace::DisambiguationStyle::LOCAL_URI); |
| 338 | ctx.err << '\n'; |
| 339 | return; |
| 340 | default: |
| 341 | BOOST_ASSERT_MSG(false, "unexpected FindFace result"); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | const FaceStatus& face = findFace.getFaceStatus(); |
| 346 | |
| 347 | ctx.controller.start<ndn::nfd::FaceDestroyCommand>( |
| 348 | ControlParameters().setFaceId(face.getFaceId()), |
| 349 | [&] (const ControlParameters& resp) { |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 350 | // We can't use printSuccess because some face attributes come from FaceStatus not ControlResponse |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 351 | ctx.out << "face-destroyed "; |
| 352 | text::ItemAttributes ia; |
| 353 | ctx.out << ia("id") << face.getFaceId() |
| 354 | << ia("local") << face.getLocalUri() |
| 355 | << ia("remote") << face.getRemoteUri() |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 356 | << ia("persistency") << face.getFacePersistency(); |
| 357 | printFaceParams(ctx.out, ia, resp); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 358 | }, |
| 359 | ctx.makeCommandFailureHandler("destroying face"), |
| 360 | ctx.makeCommandOptions()); |
| 361 | |
| 362 | ctx.face.processEvents(); |
| 363 | } |
| 364 | |
| 365 | void |
Davide Pesavento | 63b3ae8 | 2023-03-24 23:53:24 -0400 | [diff] [blame] | 366 | FaceModule::fetchStatus(ndn::nfd::Controller& controller, |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 367 | const std::function<void()>& onSuccess, |
Davide Pesavento | 63b3ae8 | 2023-03-24 23:53:24 -0400 | [diff] [blame] | 368 | const ndn::nfd::DatasetFailureCallback& onFailure, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 369 | const CommandOptions& options) |
| 370 | { |
| 371 | controller.fetch<ndn::nfd::FaceDataset>( |
Davide Pesavento | 990620a | 2022-06-05 00:25:53 -0400 | [diff] [blame] | 372 | [this, onSuccess] (const auto& result) { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 373 | m_status = result; |
| 374 | onSuccess(); |
| 375 | }, |
| 376 | onFailure, options); |
| 377 | } |
| 378 | |
| 379 | void |
| 380 | FaceModule::formatStatusXml(std::ostream& os) const |
| 381 | { |
| 382 | os << "<faces>"; |
| 383 | for (const FaceStatus& item : m_status) { |
| 384 | this->formatItemXml(os, item); |
| 385 | } |
| 386 | os << "</faces>"; |
| 387 | } |
| 388 | |
| 389 | void |
| 390 | FaceModule::formatItemXml(std::ostream& os, const FaceStatus& item) const |
| 391 | { |
| 392 | os << "<face>"; |
| 393 | |
| 394 | os << "<faceId>" << item.getFaceId() << "</faceId>"; |
| 395 | os << "<remoteUri>" << xml::Text{item.getRemoteUri()} << "</remoteUri>"; |
| 396 | os << "<localUri>" << xml::Text{item.getLocalUri()} << "</localUri>"; |
| 397 | |
| 398 | if (item.hasExpirationPeriod()) { |
| 399 | os << "<expirationPeriod>" << xml::formatDuration(item.getExpirationPeriod()) |
| 400 | << "</expirationPeriod>"; |
| 401 | } |
| 402 | os << "<faceScope>" << item.getFaceScope() << "</faceScope>"; |
| 403 | os << "<facePersistency>" << item.getFacePersistency() << "</facePersistency>"; |
| 404 | os << "<linkType>" << item.getLinkType() << "</linkType>"; |
| 405 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 406 | if (!item.hasBaseCongestionMarkingInterval() && !item.hasDefaultCongestionThreshold()) { |
| 407 | os << "<congestion/>"; |
| 408 | } |
| 409 | else { |
| 410 | os << "<congestion>"; |
| 411 | if (item.hasBaseCongestionMarkingInterval()) { |
| 412 | os << "<baseMarkingInterval>" << xml::formatDuration(item.getBaseCongestionMarkingInterval()) |
| 413 | << "</baseMarkingInterval>"; |
| 414 | } |
| 415 | if (item.hasDefaultCongestionThreshold()) { |
| 416 | os << "<defaultThreshold>" << item.getDefaultCongestionThreshold() << "</defaultThreshold>"; |
| 417 | } |
| 418 | os << "</congestion>"; |
| 419 | } |
| 420 | |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame] | 421 | if (item.hasMtu()) { |
| 422 | os << "<mtu>" << item.getMtu() << "</mtu>"; |
| 423 | } |
| 424 | |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 425 | if (item.getFlags() == 0) { |
| 426 | os << "<flags/>"; |
| 427 | } |
| 428 | else { |
| 429 | os << "<flags>"; |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 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)}; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 433 | os << "</flags>"; |
| 434 | } |
| 435 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 436 | os << "<packetCounters>"; |
| 437 | os << "<incomingPackets>" |
| 438 | << "<nInterests>" << item.getNInInterests() << "</nInterests>" |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 439 | << "<nData>" << item.getNInData() << "</nData>" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 440 | << "<nNacks>" << item.getNInNacks() << "</nNacks>" |
| 441 | << "</incomingPackets>"; |
| 442 | os << "<outgoingPackets>" |
| 443 | << "<nInterests>" << item.getNOutInterests() << "</nInterests>" |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 444 | << "<nData>" << item.getNOutData() << "</nData>" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 445 | << "<nNacks>" << item.getNOutNacks() << "</nNacks>" |
| 446 | << "</outgoingPackets>"; |
| 447 | os << "</packetCounters>"; |
| 448 | |
| 449 | os << "<byteCounters>"; |
| 450 | os << "<incomingBytes>" << item.getNInBytes() << "</incomingBytes>"; |
| 451 | os << "<outgoingBytes>" << item.getNOutBytes() << "</outgoingBytes>"; |
| 452 | os << "</byteCounters>"; |
| 453 | |
| 454 | os << "</face>"; |
| 455 | } |
| 456 | |
| 457 | void |
| 458 | FaceModule::formatStatusText(std::ostream& os) const |
| 459 | { |
| 460 | os << "Faces:\n"; |
| 461 | for (const FaceStatus& item : m_status) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 462 | os << " "; |
| 463 | formatItemText(os, item, false); |
| 464 | os << '\n'; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | void |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 469 | FaceModule::formatItemText(std::ostream& os, const FaceStatus& item, bool wantMultiLine) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 470 | { |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 471 | text::ItemAttributes ia(wantMultiLine, 10); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 472 | |
| 473 | os << ia("faceid") << item.getFaceId(); |
| 474 | os << ia("remote") << item.getRemoteUri(); |
| 475 | os << ia("local") << item.getLocalUri(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 476 | |
| 477 | if (item.hasExpirationPeriod()) { |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 478 | os << ia("expires") << text::formatDuration<time::seconds>(item.getExpirationPeriod()); |
| 479 | } |
| 480 | |
| 481 | if (item.hasBaseCongestionMarkingInterval() || item.hasDefaultCongestionThreshold()) { |
| 482 | os << ia("congestion") << "{"; |
| 483 | text::Separator congestionSep("", " "); |
| 484 | if (item.hasBaseCongestionMarkingInterval()) { |
| 485 | os << congestionSep << "base-marking-interval=" |
| 486 | << text::formatDuration<time::milliseconds>(item.getBaseCongestionMarkingInterval()); |
| 487 | } |
| 488 | if (item.hasDefaultCongestionThreshold()) { |
| 489 | os << congestionSep << "default-threshold=" << item.getDefaultCongestionThreshold() << "B"; |
| 490 | } |
| 491 | os << "}"; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame] | 494 | if (item.hasMtu()) { |
| 495 | os << ia("mtu") << item.getMtu(); |
| 496 | } |
| 497 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 498 | os << ia("counters") |
| 499 | << "{in={" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 500 | << item.getNInInterests() << "i " |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 501 | << item.getNInData() << "d " |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 502 | << item.getNInNacks() << "n " |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 503 | << item.getNInBytes() << "B} " |
| 504 | << "out={" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 505 | << item.getNOutInterests() << "i " |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 506 | << item.getNOutData() << "d " |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 507 | << item.getNOutNacks() << "n " |
| 508 | << item.getNOutBytes() << "B}}"; |
| 509 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 510 | os << ia("flags") << '{'; |
| 511 | text::Separator flagSep("", " "); |
| 512 | os << flagSep << item.getFaceScope(); |
| 513 | os << flagSep << item.getFacePersistency(); |
| 514 | os << flagSep << item.getLinkType(); |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 515 | if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 516 | os << flagSep << "local-fields"; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 517 | } |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 518 | if (item.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) { |
| 519 | os << flagSep << "lp-reliability"; |
| 520 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 521 | if (item.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) { |
| 522 | os << flagSep << "congestion-marking"; |
| 523 | } |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 524 | os << '}'; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 525 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 526 | os << ia.end(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 529 | void |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 530 | FaceModule::printSuccess(std::ostream& os, |
| 531 | const std::string& actionSummary, |
| 532 | const ControlParameters& resp) |
| 533 | { |
| 534 | text::ItemAttributes ia; |
| 535 | os << actionSummary << ' ' |
| 536 | << ia("id") << resp.getFaceId() |
| 537 | << ia("local") << resp.getLocalUri() |
| 538 | << ia("remote") << resp.getUri() |
| 539 | << ia("persistency") << resp.getFacePersistency(); |
| 540 | printFaceParams(os, ia, resp); |
| 541 | } |
| 542 | |
| 543 | void |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 544 | FaceModule::printFaceParams(std::ostream& os, text::ItemAttributes& ia, const ControlParameters& resp) |
| 545 | { |
Junxiao Shi | 8dc473a | 2018-03-02 15:10:18 +0000 | [diff] [blame] | 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)}; |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 548 | if (resp.hasBaseCongestionMarkingInterval()) { |
| 549 | os << ia("congestion-marking-interval") |
| 550 | << text::formatDuration<time::milliseconds>(resp.getBaseCongestionMarkingInterval()); |
| 551 | } |
| 552 | if (resp.hasDefaultCongestionThreshold()) { |
| 553 | os << ia("default-congestion-threshold") << resp.getDefaultCongestionThreshold() << "B"; |
| 554 | } |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame] | 555 | if (resp.hasMtu()) { |
| 556 | os << ia("mtu") << resp.getMtu(); |
| 557 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 558 | os << '\n'; |
| 559 | } |
| 560 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 561 | } // namespace nfd::tools::nfdc |