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) |
| 60 | .addArg("default-congestion-threshold", ArgValueType::UNSIGNED, Required::NO, Positional::NO); |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 61 | parser.addCommand(defFaceCreate, &FaceModule::create); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 62 | |
| 63 | CommandDefinition defFaceDestroy("face", "destroy"); |
| 64 | defFaceDestroy |
| 65 | .setTitle("destroy a face") |
| 66 | .addArg("face", ArgValueType::FACE_ID_OR_URI, Required::YES, Positional::YES); |
| 67 | parser.addCommand(defFaceDestroy, &FaceModule::destroy); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void |
Junxiao Shi | 36e5429 | 2017-02-17 18:43:16 +0000 | [diff] [blame] | 71 | FaceModule::list(ExecuteContext& ctx) |
| 72 | { |
| 73 | auto remoteUri = ctx.args.getOptional<FaceUri>("remote"); |
| 74 | auto localUri = ctx.args.getOptional<FaceUri>("local"); |
| 75 | auto uriScheme = ctx.args.getOptional<std::string>("scheme"); |
| 76 | |
| 77 | FaceQueryFilter filter; |
| 78 | if (remoteUri) { |
| 79 | filter.setRemoteUri(remoteUri->toString()); |
| 80 | } |
| 81 | if (localUri) { |
| 82 | filter.setLocalUri(localUri->toString()); |
| 83 | } |
| 84 | if (uriScheme) { |
| 85 | filter.setUriScheme(*uriScheme); |
| 86 | } |
| 87 | |
| 88 | FindFace findFace(ctx); |
| 89 | FindFace::Code res = findFace.execute(filter, true); |
| 90 | |
| 91 | ctx.exitCode = static_cast<int>(res); |
| 92 | switch (res) { |
| 93 | case FindFace::Code::OK: |
| 94 | for (const FaceStatus& item : findFace.getResults()) { |
| 95 | formatItemText(ctx.out, item, false); |
| 96 | ctx.out << '\n'; |
| 97 | } |
| 98 | break; |
| 99 | case FindFace::Code::ERROR: |
| 100 | case FindFace::Code::NOT_FOUND: |
| 101 | case FindFace::Code::CANONIZE_ERROR: |
| 102 | ctx.err << findFace.getErrorReason() << '\n'; |
| 103 | break; |
| 104 | default: |
| 105 | BOOST_ASSERT_MSG(false, "unexpected FindFace result"); |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 111 | FaceModule::show(ExecuteContext& ctx) |
| 112 | { |
| 113 | uint64_t faceId = ctx.args.get<uint64_t>("id"); |
| 114 | |
Junxiao Shi | 8f803f2 | 2017-02-10 03:04:28 +0000 | [diff] [blame] | 115 | FindFace findFace(ctx); |
| 116 | FindFace::Code res = findFace.execute(faceId); |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 117 | |
Junxiao Shi | 8f803f2 | 2017-02-10 03:04:28 +0000 | [diff] [blame] | 118 | ctx.exitCode = static_cast<int>(res); |
| 119 | switch (res) { |
| 120 | case FindFace::Code::OK: |
| 121 | formatItemText(ctx.out, findFace.getFaceStatus(), true); |
| 122 | break; |
| 123 | case FindFace::Code::ERROR: |
| 124 | case FindFace::Code::NOT_FOUND: |
| 125 | ctx.err << findFace.getErrorReason() << '\n'; |
| 126 | break; |
| 127 | default: |
| 128 | BOOST_ASSERT_MSG(false, "unexpected FindFace result"); |
| 129 | break; |
| 130 | } |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 133 | /** \brief order persistency in NONE < ON_DEMAND < PERSISTENCY < PERMANENT |
| 134 | */ |
| 135 | static bool |
| 136 | persistencyLessThan(FacePersistency x, FacePersistency y) |
| 137 | { |
| 138 | switch (x) { |
| 139 | case FacePersistency::FACE_PERSISTENCY_NONE: |
| 140 | return y != FacePersistency::FACE_PERSISTENCY_NONE; |
| 141 | case FacePersistency::FACE_PERSISTENCY_ON_DEMAND: |
| 142 | return y == FacePersistency::FACE_PERSISTENCY_PERSISTENT || |
| 143 | y == FacePersistency::FACE_PERSISTENCY_PERMANENT; |
| 144 | case FacePersistency::FACE_PERSISTENCY_PERSISTENT: |
| 145 | return y == FacePersistency::FACE_PERSISTENCY_PERMANENT; |
| 146 | case FacePersistency::FACE_PERSISTENCY_PERMANENT: |
| 147 | return false; |
| 148 | } |
| 149 | return static_cast<int>(x) < static_cast<int>(y); |
| 150 | } |
| 151 | |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 152 | void |
| 153 | FaceModule::create(ExecuteContext& ctx) |
| 154 | { |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 155 | auto remoteUri = ctx.args.get<FaceUri>("remote"); |
| 156 | auto localUri = ctx.args.getOptional<FaceUri>("local"); |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 157 | auto persistency = ctx.args.get<FacePersistency>("persistency", FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 158 | auto lpReliability = ctx.args.getTribool("reliability"); |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 159 | auto congestionMarking = ctx.args.getTribool("congestion-marking"); |
| 160 | auto baseCongestionMarkingIntervalMs = ctx.args.getOptional<uint64_t>("congestion-marking-interval"); |
| 161 | auto defaultCongestionThreshold = ctx.args.getOptional<uint64_t>("default-congestion-threshold"); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 162 | |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 163 | FaceUri canonicalRemote; |
| 164 | ndn::optional<FaceUri> canonicalLocal; |
| 165 | |
| 166 | auto handleCanonizeError = [&] (const FaceUri& faceUri, const std::string& error) { |
| 167 | ctx.exitCode = 4; |
| 168 | ctx.err << "Error when canonizing '" << faceUri << "': " << error << '\n'; |
| 169 | }; |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 170 | |
| 171 | auto printPositiveResult = [&] (const std::string& actionSummary, const ControlParameters& resp) { |
| 172 | text::ItemAttributes ia; |
| 173 | ctx.out << actionSummary << ' ' |
| 174 | << ia("id") << resp.getFaceId() |
Junxiao Shi | 1cce2a3 | 2017-05-02 02:39:55 +0000 | [diff] [blame] | 175 | << ia("local") << resp.getLocalUri() |
| 176 | << ia("remote") << resp.getUri() |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 177 | << ia("persistency") << resp.getFacePersistency(); |
| 178 | printFaceParams(ctx.out, ia, resp); |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 181 | auto updateFace = [&printPositiveResult] (ControlParameters respParams, ControlParameters resp) { |
| 182 | // faces/update response does not have FaceUris, copy from faces/create response |
| 183 | resp.setLocalUri(respParams.getLocalUri()) |
| 184 | .setUri(respParams.getUri()); |
| 185 | printPositiveResult("face-updated", resp); |
| 186 | }; |
| 187 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 188 | auto handle409 = [&] (const ControlResponse& resp) { |
| 189 | ControlParameters respParams(resp.getBody()); |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 190 | if (respParams.getUri() != canonicalRemote.toString()) { |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 191 | // we are conflicting with a different face, which is a general error |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | if (persistencyLessThan(respParams.getFacePersistency(), persistency)) { |
| 196 | // need to upgrade persistency |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 197 | ControlParameters params; |
| 198 | params.setFaceId(respParams.getFaceId()).setFacePersistency(persistency); |
| 199 | if (!boost::logic::indeterminate(lpReliability)) { |
| 200 | params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, lpReliability); |
| 201 | } |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 202 | ctx.controller.start<ndn::nfd::FaceUpdateCommand>( |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 203 | params, |
| 204 | bind(updateFace, respParams, _1), |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 205 | ctx.makeCommandFailureHandler("upgrading face persistency"), |
| 206 | ctx.makeCommandOptions()); |
| 207 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 208 | else if ((!boost::logic::indeterminate(lpReliability) && |
| 209 | lpReliability != respParams.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) || |
| 210 | (!boost::logic::indeterminate(congestionMarking) && |
| 211 | congestionMarking != respParams.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) || |
| 212 | baseCongestionMarkingIntervalMs || |
| 213 | defaultCongestionThreshold) { |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 214 | ControlParameters params; |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 215 | params.setFaceId(respParams.getFaceId()); |
| 216 | |
| 217 | if (!boost::logic::indeterminate(lpReliability) && |
| 218 | lpReliability != respParams.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) { |
| 219 | params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, lpReliability); |
| 220 | } |
| 221 | if (!boost::logic::indeterminate(congestionMarking) && |
| 222 | congestionMarking != respParams.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) { |
| 223 | params.setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, congestionMarking); |
| 224 | } |
| 225 | |
| 226 | if (baseCongestionMarkingIntervalMs) { |
| 227 | params.setBaseCongestionMarkingInterval(time::milliseconds(*baseCongestionMarkingIntervalMs)); |
| 228 | } |
| 229 | |
| 230 | if (defaultCongestionThreshold) { |
| 231 | params.setDefaultCongestionThreshold(*defaultCongestionThreshold); |
| 232 | } |
| 233 | |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 234 | ctx.controller.start<ndn::nfd::FaceUpdateCommand>( |
| 235 | params, |
| 236 | bind(updateFace, respParams, _1), |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 237 | ctx.makeCommandFailureHandler("updating face"), |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 238 | ctx.makeCommandOptions()); |
| 239 | } |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 240 | else { |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 241 | // don't do anything |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 242 | printPositiveResult("face-exists", respParams); |
| 243 | } |
| 244 | return true; |
| 245 | }; |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 246 | |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 247 | auto doCreateFace = [&] { |
| 248 | ControlParameters params; |
| 249 | params.setUri(canonicalRemote.toString()); |
| 250 | if (canonicalLocal) { |
| 251 | params.setLocalUri(canonicalLocal->toString()); |
| 252 | } |
| 253 | params.setFacePersistency(persistency); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 254 | if (!boost::logic::indeterminate(lpReliability)) { |
| 255 | params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, lpReliability); |
| 256 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 257 | if (!boost::logic::indeterminate(congestionMarking)) { |
| 258 | params.setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, congestionMarking); |
| 259 | } |
| 260 | if (baseCongestionMarkingIntervalMs) { |
| 261 | params.setBaseCongestionMarkingInterval(time::milliseconds(*baseCongestionMarkingIntervalMs)); |
| 262 | } |
| 263 | if (defaultCongestionThreshold) { |
| 264 | params.setDefaultCongestionThreshold(*defaultCongestionThreshold); |
| 265 | } |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 266 | |
| 267 | ctx.controller.start<ndn::nfd::FaceCreateCommand>( |
| 268 | params, |
| 269 | bind(printPositiveResult, "face-created", _1), |
| 270 | [&] (const ControlResponse& resp) { |
| 271 | if (resp.getCode() == 409 && handle409(resp)) { |
| 272 | return; |
| 273 | } |
| 274 | ctx.makeCommandFailureHandler("creating face")(resp); // invoke general error handler |
| 275 | }, |
| 276 | ctx.makeCommandOptions()); |
| 277 | }; |
| 278 | |
| 279 | remoteUri.canonize( |
| 280 | [&] (const FaceUri& canonicalUri) { |
| 281 | canonicalRemote = canonicalUri; |
| 282 | if (localUri) { |
| 283 | localUri->canonize( |
| 284 | [&] (const FaceUri& canonicalUri) { |
| 285 | canonicalLocal = canonicalUri; |
| 286 | doCreateFace(); |
| 287 | }, |
| 288 | bind(handleCanonizeError, *localUri, _1), |
| 289 | ctx.face.getIoService(), ctx.getTimeout()); |
| 290 | } |
| 291 | else { |
| 292 | doCreateFace(); |
| 293 | } |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 294 | }, |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 295 | bind(handleCanonizeError, remoteUri, _1), |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 296 | ctx.face.getIoService(), ctx.getTimeout()); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 297 | |
| 298 | ctx.face.processEvents(); |
| 299 | } |
| 300 | |
| 301 | void |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 302 | FaceModule::destroy(ExecuteContext& ctx) |
| 303 | { |
Junxiao Shi | 918e5d4 | 2017-02-25 03:58:21 +0000 | [diff] [blame] | 304 | const boost::any& faceIdOrUri = ctx.args.at("face"); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 305 | |
| 306 | FindFace findFace(ctx); |
Junxiao Shi | 918e5d4 | 2017-02-25 03:58:21 +0000 | [diff] [blame] | 307 | FindFace::Code res = findFace.execute(faceIdOrUri); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 308 | |
| 309 | ctx.exitCode = static_cast<int>(res); |
| 310 | switch (res) { |
| 311 | case FindFace::Code::OK: |
| 312 | break; |
| 313 | case FindFace::Code::ERROR: |
| 314 | case FindFace::Code::CANONIZE_ERROR: |
| 315 | case FindFace::Code::NOT_FOUND: |
| 316 | ctx.err << findFace.getErrorReason() << '\n'; |
| 317 | return; |
| 318 | case FindFace::Code::AMBIGUOUS: |
| 319 | ctx.err << "Multiple faces match specified remote FaceUri. Re-run the command with a FaceId:"; |
| 320 | findFace.printDisambiguation(ctx.err, FindFace::DisambiguationStyle::LOCAL_URI); |
| 321 | ctx.err << '\n'; |
| 322 | return; |
| 323 | default: |
| 324 | BOOST_ASSERT_MSG(false, "unexpected FindFace result"); |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | const FaceStatus& face = findFace.getFaceStatus(); |
| 329 | |
| 330 | ctx.controller.start<ndn::nfd::FaceDestroyCommand>( |
| 331 | ControlParameters().setFaceId(face.getFaceId()), |
| 332 | [&] (const ControlParameters& resp) { |
| 333 | ctx.out << "face-destroyed "; |
| 334 | text::ItemAttributes ia; |
| 335 | ctx.out << ia("id") << face.getFaceId() |
| 336 | << ia("local") << face.getLocalUri() |
| 337 | << ia("remote") << face.getRemoteUri() |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 338 | << ia("persistency") << face.getFacePersistency(); |
| 339 | printFaceParams(ctx.out, ia, resp); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 340 | }, |
| 341 | ctx.makeCommandFailureHandler("destroying face"), |
| 342 | ctx.makeCommandOptions()); |
| 343 | |
| 344 | ctx.face.processEvents(); |
| 345 | } |
| 346 | |
| 347 | void |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 348 | FaceModule::fetchStatus(Controller& controller, |
| 349 | const function<void()>& onSuccess, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 350 | const Controller::DatasetFailCallback& onFailure, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 351 | const CommandOptions& options) |
| 352 | { |
| 353 | controller.fetch<ndn::nfd::FaceDataset>( |
| 354 | [this, onSuccess] (const std::vector<FaceStatus>& result) { |
| 355 | m_status = result; |
| 356 | onSuccess(); |
| 357 | }, |
| 358 | onFailure, options); |
| 359 | } |
| 360 | |
| 361 | void |
| 362 | FaceModule::formatStatusXml(std::ostream& os) const |
| 363 | { |
| 364 | os << "<faces>"; |
| 365 | for (const FaceStatus& item : m_status) { |
| 366 | this->formatItemXml(os, item); |
| 367 | } |
| 368 | os << "</faces>"; |
| 369 | } |
| 370 | |
| 371 | void |
| 372 | FaceModule::formatItemXml(std::ostream& os, const FaceStatus& item) const |
| 373 | { |
| 374 | os << "<face>"; |
| 375 | |
| 376 | os << "<faceId>" << item.getFaceId() << "</faceId>"; |
| 377 | os << "<remoteUri>" << xml::Text{item.getRemoteUri()} << "</remoteUri>"; |
| 378 | os << "<localUri>" << xml::Text{item.getLocalUri()} << "</localUri>"; |
| 379 | |
| 380 | if (item.hasExpirationPeriod()) { |
| 381 | os << "<expirationPeriod>" << xml::formatDuration(item.getExpirationPeriod()) |
| 382 | << "</expirationPeriod>"; |
| 383 | } |
| 384 | os << "<faceScope>" << item.getFaceScope() << "</faceScope>"; |
| 385 | os << "<facePersistency>" << item.getFacePersistency() << "</facePersistency>"; |
| 386 | os << "<linkType>" << item.getLinkType() << "</linkType>"; |
| 387 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 388 | if (!item.hasBaseCongestionMarkingInterval() && !item.hasDefaultCongestionThreshold()) { |
| 389 | os << "<congestion/>"; |
| 390 | } |
| 391 | else { |
| 392 | os << "<congestion>"; |
| 393 | if (item.hasBaseCongestionMarkingInterval()) { |
| 394 | os << "<baseMarkingInterval>" << xml::formatDuration(item.getBaseCongestionMarkingInterval()) |
| 395 | << "</baseMarkingInterval>"; |
| 396 | } |
| 397 | if (item.hasDefaultCongestionThreshold()) { |
| 398 | os << "<defaultThreshold>" << item.getDefaultCongestionThreshold() << "</defaultThreshold>"; |
| 399 | } |
| 400 | os << "</congestion>"; |
| 401 | } |
| 402 | |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 403 | if (item.getFlags() == 0) { |
| 404 | os << "<flags/>"; |
| 405 | } |
| 406 | else { |
| 407 | os << "<flags>"; |
| 408 | if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) { |
| 409 | os << "<localFieldsEnabled/>"; |
| 410 | } |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 411 | if (item.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) { |
| 412 | os << "<lpReliabilityEnabled/>"; |
| 413 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 414 | if (item.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) { |
| 415 | os << "<congestionMarkingEnabled/>"; |
| 416 | } |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 417 | os << "</flags>"; |
| 418 | } |
| 419 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 420 | os << "<packetCounters>"; |
| 421 | os << "<incomingPackets>" |
| 422 | << "<nInterests>" << item.getNInInterests() << "</nInterests>" |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 423 | << "<nData>" << item.getNInData() << "</nData>" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 424 | << "<nNacks>" << item.getNInNacks() << "</nNacks>" |
| 425 | << "</incomingPackets>"; |
| 426 | os << "<outgoingPackets>" |
| 427 | << "<nInterests>" << item.getNOutInterests() << "</nInterests>" |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 428 | << "<nData>" << item.getNOutData() << "</nData>" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 429 | << "<nNacks>" << item.getNOutNacks() << "</nNacks>" |
| 430 | << "</outgoingPackets>"; |
| 431 | os << "</packetCounters>"; |
| 432 | |
| 433 | os << "<byteCounters>"; |
| 434 | os << "<incomingBytes>" << item.getNInBytes() << "</incomingBytes>"; |
| 435 | os << "<outgoingBytes>" << item.getNOutBytes() << "</outgoingBytes>"; |
| 436 | os << "</byteCounters>"; |
| 437 | |
| 438 | os << "</face>"; |
| 439 | } |
| 440 | |
| 441 | void |
| 442 | FaceModule::formatStatusText(std::ostream& os) const |
| 443 | { |
| 444 | os << "Faces:\n"; |
| 445 | for (const FaceStatus& item : m_status) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 446 | os << " "; |
| 447 | formatItemText(os, item, false); |
| 448 | os << '\n'; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
| 452 | void |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 453 | FaceModule::formatItemText(std::ostream& os, const FaceStatus& item, bool wantMultiLine) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 454 | { |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 455 | text::ItemAttributes ia(wantMultiLine, 10); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 456 | |
| 457 | os << ia("faceid") << item.getFaceId(); |
| 458 | os << ia("remote") << item.getRemoteUri(); |
| 459 | os << ia("local") << item.getLocalUri(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 460 | |
| 461 | if (item.hasExpirationPeriod()) { |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 462 | os << ia("expires") << text::formatDuration<time::seconds>(item.getExpirationPeriod()); |
| 463 | } |
| 464 | |
| 465 | if (item.hasBaseCongestionMarkingInterval() || item.hasDefaultCongestionThreshold()) { |
| 466 | os << ia("congestion") << "{"; |
| 467 | text::Separator congestionSep("", " "); |
| 468 | if (item.hasBaseCongestionMarkingInterval()) { |
| 469 | os << congestionSep << "base-marking-interval=" |
| 470 | << text::formatDuration<time::milliseconds>(item.getBaseCongestionMarkingInterval()); |
| 471 | } |
| 472 | if (item.hasDefaultCongestionThreshold()) { |
| 473 | os << congestionSep << "default-threshold=" << item.getDefaultCongestionThreshold() << "B"; |
| 474 | } |
| 475 | os << "}"; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 478 | os << ia("counters") |
| 479 | << "{in={" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 480 | << item.getNInInterests() << "i " |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 481 | << item.getNInData() << "d " |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 482 | << item.getNInNacks() << "n " |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 483 | << item.getNInBytes() << "B} " |
| 484 | << "out={" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 485 | << item.getNOutInterests() << "i " |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 486 | << item.getNOutData() << "d " |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 487 | << item.getNOutNacks() << "n " |
| 488 | << item.getNOutBytes() << "B}}"; |
| 489 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 490 | os << ia("flags") << '{'; |
| 491 | text::Separator flagSep("", " "); |
| 492 | os << flagSep << item.getFaceScope(); |
| 493 | os << flagSep << item.getFacePersistency(); |
| 494 | os << flagSep << item.getLinkType(); |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 495 | if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 496 | os << flagSep << "local-fields"; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 497 | } |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 498 | if (item.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) { |
| 499 | os << flagSep << "lp-reliability"; |
| 500 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 501 | if (item.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) { |
| 502 | os << flagSep << "congestion-marking"; |
| 503 | } |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 504 | os << '}'; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 505 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 506 | os << ia.end(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 509 | void |
| 510 | FaceModule::printFaceParams(std::ostream& os, text::ItemAttributes& ia, const ControlParameters& resp) |
| 511 | { |
Junxiao Shi | 8dc473a | 2018-03-02 15:10:18 +0000 | [diff] [blame^] | 512 | os << ia("reliability") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)} |
| 513 | << 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] | 514 | if (resp.hasBaseCongestionMarkingInterval()) { |
| 515 | os << ia("congestion-marking-interval") |
| 516 | << text::formatDuration<time::milliseconds>(resp.getBaseCongestionMarkingInterval()); |
| 517 | } |
| 518 | if (resp.hasDefaultCongestionThreshold()) { |
| 519 | os << ia("default-congestion-threshold") << resp.getDefaultCongestionThreshold() << "B"; |
| 520 | } |
| 521 | os << '\n'; |
| 522 | } |
| 523 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 524 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 525 | } // namespace tools |
| 526 | } // namespace nfd |