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 | /* |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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" |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 27 | #include "find-face.hpp" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | namespace tools { |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 31 | namespace nfdc { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 32 | |
| 33 | void |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 34 | FaceModule::registerCommands(CommandParser& parser) |
| 35 | { |
Junxiao Shi | 36e5429 | 2017-02-17 18:43:16 +0000 | [diff] [blame] | 36 | 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 Pesavento | d214744 | 2018-02-19 23:58:17 -0500 | [diff] [blame] | 43 | parser.addAlias("face", "list", ""); |
Junxiao Shi | 36e5429 | 2017-02-17 18:43:16 +0000 | [diff] [blame] | 44 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 45 | 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 Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 50 | |
| 51 | CommandDefinition defFaceCreate("face", "create"); |
| 52 | defFaceCreate |
| 53 | .setTitle("create a face") |
| 54 | .addArg("remote", ArgValueType::FACE_URI, Required::YES, Positional::YES) |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 55 | .addArg("persistency", ArgValueType::FACE_PERSISTENCY, Required::NO, Positional::YES) |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 56 | .addArg("local", ArgValueType::FACE_URI, Required::NO, Positional::NO) |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 57 | .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 Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame^] | 60 | .addArg("default-congestion-threshold", ArgValueType::UNSIGNED, Required::NO, Positional::NO) |
| 61 | .addArg("mtu", ArgValueType::UNSIGNED, Required::NO, Positional::NO); |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 62 | parser.addCommand(defFaceCreate, &FaceModule::create); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 63 | |
| 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 Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void |
Junxiao Shi | 36e5429 | 2017-02-17 18:43:16 +0000 | [diff] [blame] | 72 | FaceModule::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 | |
| 111 | void |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 112 | FaceModule::show(ExecuteContext& ctx) |
| 113 | { |
| 114 | uint64_t faceId = ctx.args.get<uint64_t>("id"); |
| 115 | |
Junxiao Shi | 8f803f2 | 2017-02-10 03:04:28 +0000 | [diff] [blame] | 116 | FindFace findFace(ctx); |
| 117 | FindFace::Code res = findFace.execute(faceId); |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 118 | |
Junxiao Shi | 8f803f2 | 2017-02-10 03:04:28 +0000 | [diff] [blame] | 119 | 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 Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 134 | /** \brief order persistency in NONE < ON_DEMAND < PERSISTENCY < PERMANENT |
| 135 | */ |
| 136 | static bool |
| 137 | persistencyLessThan(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 Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 153 | void |
| 154 | FaceModule::create(ExecuteContext& ctx) |
| 155 | { |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 156 | auto remoteUri = ctx.args.get<FaceUri>("remote"); |
| 157 | auto localUri = ctx.args.getOptional<FaceUri>("local"); |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 158 | auto persistency = ctx.args.get<FacePersistency>("persistency", FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 159 | auto lpReliability = ctx.args.getTribool("reliability"); |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 160 | 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 Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame^] | 163 | auto mtu = ctx.args.getOptional<uint64_t>("mtu"); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 164 | |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 165 | FaceUri canonicalRemote; |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 166 | optional<FaceUri> canonicalLocal; |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 167 | |
| 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 Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 172 | |
| 173 | auto printPositiveResult = [&] (const std::string& actionSummary, const ControlParameters& resp) { |
| 174 | text::ItemAttributes ia; |
| 175 | ctx.out << actionSummary << ' ' |
| 176 | << ia("id") << resp.getFaceId() |
Junxiao Shi | 1cce2a3 | 2017-05-02 02:39:55 +0000 | [diff] [blame] | 177 | << ia("local") << resp.getLocalUri() |
| 178 | << ia("remote") << resp.getUri() |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 179 | << ia("persistency") << resp.getFacePersistency(); |
| 180 | printFaceParams(ctx.out, ia, resp); |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 181 | }; |
| 182 | |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 183 | 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 Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 190 | auto handle409 = [&] (const ControlResponse& resp) { |
| 191 | ControlParameters respParams(resp.getBody()); |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 192 | if (respParams.getUri() != canonicalRemote.toString()) { |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 193 | // we are conflicting with a different face, which is a general error |
| 194 | return false; |
| 195 | } |
| 196 | |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame^] | 197 | 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 Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 202 | // need to upgrade persistency |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 203 | ControlParameters params; |
| 204 | params.setFaceId(respParams.getFaceId()).setFacePersistency(persistency); |
| 205 | if (!boost::logic::indeterminate(lpReliability)) { |
| 206 | params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, lpReliability); |
| 207 | } |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 208 | ctx.controller.start<ndn::nfd::FaceUpdateCommand>( |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 209 | params, |
| 210 | bind(updateFace, respParams, _1), |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 211 | ctx.makeCommandFailureHandler("upgrading face persistency"), |
| 212 | ctx.makeCommandOptions()); |
| 213 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 214 | 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 Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 220 | ControlParameters params; |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 221 | params.setFaceId(respParams.getFaceId()); |
| 222 | |
| 223 | if (!boost::logic::indeterminate(lpReliability) && |
| 224 | lpReliability != respParams.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) { |
| 225 | params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, lpReliability); |
| 226 | } |
| 227 | if (!boost::logic::indeterminate(congestionMarking) && |
| 228 | congestionMarking != respParams.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) { |
| 229 | params.setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, congestionMarking); |
| 230 | } |
| 231 | |
| 232 | if (baseCongestionMarkingIntervalMs) { |
| 233 | params.setBaseCongestionMarkingInterval(time::milliseconds(*baseCongestionMarkingIntervalMs)); |
| 234 | } |
| 235 | |
| 236 | if (defaultCongestionThreshold) { |
| 237 | params.setDefaultCongestionThreshold(*defaultCongestionThreshold); |
| 238 | } |
| 239 | |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 240 | ctx.controller.start<ndn::nfd::FaceUpdateCommand>( |
| 241 | params, |
| 242 | bind(updateFace, respParams, _1), |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 243 | ctx.makeCommandFailureHandler("updating face"), |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 244 | ctx.makeCommandOptions()); |
| 245 | } |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 246 | else { |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 247 | // don't do anything |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 248 | printPositiveResult("face-exists", respParams); |
| 249 | } |
| 250 | return true; |
| 251 | }; |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 252 | |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 253 | auto doCreateFace = [&] { |
| 254 | ControlParameters params; |
| 255 | params.setUri(canonicalRemote.toString()); |
| 256 | if (canonicalLocal) { |
| 257 | params.setLocalUri(canonicalLocal->toString()); |
| 258 | } |
| 259 | params.setFacePersistency(persistency); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 260 | if (!boost::logic::indeterminate(lpReliability)) { |
| 261 | params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, lpReliability); |
| 262 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 263 | if (!boost::logic::indeterminate(congestionMarking)) { |
| 264 | params.setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, congestionMarking); |
| 265 | } |
| 266 | if (baseCongestionMarkingIntervalMs) { |
| 267 | params.setBaseCongestionMarkingInterval(time::milliseconds(*baseCongestionMarkingIntervalMs)); |
| 268 | } |
| 269 | if (defaultCongestionThreshold) { |
| 270 | params.setDefaultCongestionThreshold(*defaultCongestionThreshold); |
| 271 | } |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame^] | 272 | if (mtu) { |
| 273 | params.setMtu(*mtu); |
| 274 | } |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 275 | |
| 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 Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 303 | }, |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 304 | bind(handleCanonizeError, remoteUri, _1), |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 305 | ctx.face.getIoService(), ctx.getTimeout()); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 306 | |
| 307 | ctx.face.processEvents(); |
| 308 | } |
| 309 | |
| 310 | void |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 311 | FaceModule::destroy(ExecuteContext& ctx) |
| 312 | { |
Junxiao Shi | 918e5d4 | 2017-02-25 03:58:21 +0000 | [diff] [blame] | 313 | const boost::any& faceIdOrUri = ctx.args.at("face"); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 314 | |
| 315 | FindFace findFace(ctx); |
Junxiao Shi | 918e5d4 | 2017-02-25 03:58:21 +0000 | [diff] [blame] | 316 | FindFace::Code res = findFace.execute(faceIdOrUri); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 317 | |
| 318 | ctx.exitCode = static_cast<int>(res); |
| 319 | switch (res) { |
| 320 | case FindFace::Code::OK: |
| 321 | break; |
| 322 | case FindFace::Code::ERROR: |
| 323 | case FindFace::Code::CANONIZE_ERROR: |
| 324 | case FindFace::Code::NOT_FOUND: |
| 325 | ctx.err << findFace.getErrorReason() << '\n'; |
| 326 | return; |
| 327 | case FindFace::Code::AMBIGUOUS: |
| 328 | ctx.err << "Multiple faces match specified remote FaceUri. Re-run the command with a FaceId:"; |
| 329 | findFace.printDisambiguation(ctx.err, FindFace::DisambiguationStyle::LOCAL_URI); |
| 330 | ctx.err << '\n'; |
| 331 | return; |
| 332 | default: |
| 333 | BOOST_ASSERT_MSG(false, "unexpected FindFace result"); |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | const FaceStatus& face = findFace.getFaceStatus(); |
| 338 | |
| 339 | ctx.controller.start<ndn::nfd::FaceDestroyCommand>( |
| 340 | ControlParameters().setFaceId(face.getFaceId()), |
| 341 | [&] (const ControlParameters& resp) { |
| 342 | ctx.out << "face-destroyed "; |
| 343 | text::ItemAttributes ia; |
| 344 | ctx.out << ia("id") << face.getFaceId() |
| 345 | << ia("local") << face.getLocalUri() |
| 346 | << ia("remote") << face.getRemoteUri() |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 347 | << ia("persistency") << face.getFacePersistency(); |
| 348 | printFaceParams(ctx.out, ia, resp); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 349 | }, |
| 350 | ctx.makeCommandFailureHandler("destroying face"), |
| 351 | ctx.makeCommandOptions()); |
| 352 | |
| 353 | ctx.face.processEvents(); |
| 354 | } |
| 355 | |
| 356 | void |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 357 | FaceModule::fetchStatus(Controller& controller, |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 358 | const std::function<void()>& onSuccess, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 359 | const Controller::DatasetFailCallback& onFailure, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 360 | const CommandOptions& options) |
| 361 | { |
| 362 | controller.fetch<ndn::nfd::FaceDataset>( |
| 363 | [this, onSuccess] (const std::vector<FaceStatus>& result) { |
| 364 | m_status = result; |
| 365 | onSuccess(); |
| 366 | }, |
| 367 | onFailure, options); |
| 368 | } |
| 369 | |
| 370 | void |
| 371 | FaceModule::formatStatusXml(std::ostream& os) const |
| 372 | { |
| 373 | os << "<faces>"; |
| 374 | for (const FaceStatus& item : m_status) { |
| 375 | this->formatItemXml(os, item); |
| 376 | } |
| 377 | os << "</faces>"; |
| 378 | } |
| 379 | |
| 380 | void |
| 381 | FaceModule::formatItemXml(std::ostream& os, const FaceStatus& item) const |
| 382 | { |
| 383 | os << "<face>"; |
| 384 | |
| 385 | os << "<faceId>" << item.getFaceId() << "</faceId>"; |
| 386 | os << "<remoteUri>" << xml::Text{item.getRemoteUri()} << "</remoteUri>"; |
| 387 | os << "<localUri>" << xml::Text{item.getLocalUri()} << "</localUri>"; |
| 388 | |
| 389 | if (item.hasExpirationPeriod()) { |
| 390 | os << "<expirationPeriod>" << xml::formatDuration(item.getExpirationPeriod()) |
| 391 | << "</expirationPeriod>"; |
| 392 | } |
| 393 | os << "<faceScope>" << item.getFaceScope() << "</faceScope>"; |
| 394 | os << "<facePersistency>" << item.getFacePersistency() << "</facePersistency>"; |
| 395 | os << "<linkType>" << item.getLinkType() << "</linkType>"; |
| 396 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 397 | if (!item.hasBaseCongestionMarkingInterval() && !item.hasDefaultCongestionThreshold()) { |
| 398 | os << "<congestion/>"; |
| 399 | } |
| 400 | else { |
| 401 | os << "<congestion>"; |
| 402 | if (item.hasBaseCongestionMarkingInterval()) { |
| 403 | os << "<baseMarkingInterval>" << xml::formatDuration(item.getBaseCongestionMarkingInterval()) |
| 404 | << "</baseMarkingInterval>"; |
| 405 | } |
| 406 | if (item.hasDefaultCongestionThreshold()) { |
| 407 | os << "<defaultThreshold>" << item.getDefaultCongestionThreshold() << "</defaultThreshold>"; |
| 408 | } |
| 409 | os << "</congestion>"; |
| 410 | } |
| 411 | |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame^] | 412 | if (item.hasMtu()) { |
| 413 | os << "<mtu>" << item.getMtu() << "</mtu>"; |
| 414 | } |
| 415 | |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 416 | if (item.getFlags() == 0) { |
| 417 | os << "<flags/>"; |
| 418 | } |
| 419 | else { |
| 420 | os << "<flags>"; |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 421 | os << xml::Flag{"localFieldsEnabled", item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)}; |
| 422 | os << xml::Flag{"lpReliabilityEnabled", item.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)}; |
| 423 | os << xml::Flag{"congestionMarkingEnabled", item.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)}; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 424 | os << "</flags>"; |
| 425 | } |
| 426 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 427 | os << "<packetCounters>"; |
| 428 | os << "<incomingPackets>" |
| 429 | << "<nInterests>" << item.getNInInterests() << "</nInterests>" |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 430 | << "<nData>" << item.getNInData() << "</nData>" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 431 | << "<nNacks>" << item.getNInNacks() << "</nNacks>" |
| 432 | << "</incomingPackets>"; |
| 433 | os << "<outgoingPackets>" |
| 434 | << "<nInterests>" << item.getNOutInterests() << "</nInterests>" |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 435 | << "<nData>" << item.getNOutData() << "</nData>" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 436 | << "<nNacks>" << item.getNOutNacks() << "</nNacks>" |
| 437 | << "</outgoingPackets>"; |
| 438 | os << "</packetCounters>"; |
| 439 | |
| 440 | os << "<byteCounters>"; |
| 441 | os << "<incomingBytes>" << item.getNInBytes() << "</incomingBytes>"; |
| 442 | os << "<outgoingBytes>" << item.getNOutBytes() << "</outgoingBytes>"; |
| 443 | os << "</byteCounters>"; |
| 444 | |
| 445 | os << "</face>"; |
| 446 | } |
| 447 | |
| 448 | void |
| 449 | FaceModule::formatStatusText(std::ostream& os) const |
| 450 | { |
| 451 | os << "Faces:\n"; |
| 452 | for (const FaceStatus& item : m_status) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 453 | os << " "; |
| 454 | formatItemText(os, item, false); |
| 455 | os << '\n'; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
| 459 | void |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 460 | FaceModule::formatItemText(std::ostream& os, const FaceStatus& item, bool wantMultiLine) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 461 | { |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 462 | text::ItemAttributes ia(wantMultiLine, 10); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 463 | |
| 464 | os << ia("faceid") << item.getFaceId(); |
| 465 | os << ia("remote") << item.getRemoteUri(); |
| 466 | os << ia("local") << item.getLocalUri(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 467 | |
| 468 | if (item.hasExpirationPeriod()) { |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 469 | os << ia("expires") << text::formatDuration<time::seconds>(item.getExpirationPeriod()); |
| 470 | } |
| 471 | |
| 472 | if (item.hasBaseCongestionMarkingInterval() || item.hasDefaultCongestionThreshold()) { |
| 473 | os << ia("congestion") << "{"; |
| 474 | text::Separator congestionSep("", " "); |
| 475 | if (item.hasBaseCongestionMarkingInterval()) { |
| 476 | os << congestionSep << "base-marking-interval=" |
| 477 | << text::formatDuration<time::milliseconds>(item.getBaseCongestionMarkingInterval()); |
| 478 | } |
| 479 | if (item.hasDefaultCongestionThreshold()) { |
| 480 | os << congestionSep << "default-threshold=" << item.getDefaultCongestionThreshold() << "B"; |
| 481 | } |
| 482 | os << "}"; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame^] | 485 | if (item.hasMtu()) { |
| 486 | os << ia("mtu") << item.getMtu(); |
| 487 | } |
| 488 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 489 | os << ia("counters") |
| 490 | << "{in={" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 491 | << item.getNInInterests() << "i " |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 492 | << item.getNInData() << "d " |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 493 | << item.getNInNacks() << "n " |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 494 | << item.getNInBytes() << "B} " |
| 495 | << "out={" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 496 | << item.getNOutInterests() << "i " |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 497 | << item.getNOutData() << "d " |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 498 | << item.getNOutNacks() << "n " |
| 499 | << item.getNOutBytes() << "B}}"; |
| 500 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 501 | os << ia("flags") << '{'; |
| 502 | text::Separator flagSep("", " "); |
| 503 | os << flagSep << item.getFaceScope(); |
| 504 | os << flagSep << item.getFacePersistency(); |
| 505 | os << flagSep << item.getLinkType(); |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 506 | if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 507 | os << flagSep << "local-fields"; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 508 | } |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 509 | if (item.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) { |
| 510 | os << flagSep << "lp-reliability"; |
| 511 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 512 | if (item.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) { |
| 513 | os << flagSep << "congestion-marking"; |
| 514 | } |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 515 | os << '}'; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 516 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 517 | os << ia.end(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 520 | void |
| 521 | FaceModule::printFaceParams(std::ostream& os, text::ItemAttributes& ia, const ControlParameters& resp) |
| 522 | { |
Junxiao Shi | 8dc473a | 2018-03-02 15:10:18 +0000 | [diff] [blame] | 523 | os << ia("reliability") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)} |
| 524 | << 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] | 525 | if (resp.hasBaseCongestionMarkingInterval()) { |
| 526 | os << ia("congestion-marking-interval") |
| 527 | << text::formatDuration<time::milliseconds>(resp.getBaseCongestionMarkingInterval()); |
| 528 | } |
| 529 | if (resp.hasDefaultCongestionThreshold()) { |
| 530 | os << ia("default-congestion-threshold") << resp.getDefaultCongestionThreshold() << "B"; |
| 531 | } |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame^] | 532 | if (resp.hasMtu()) { |
| 533 | os << ia("mtu") << resp.getMtu(); |
| 534 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 535 | os << '\n'; |
| 536 | } |
| 537 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 538 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 539 | } // namespace tools |
| 540 | } // namespace nfd |