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 | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, 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) |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 61 | .addArg("mtu", ArgValueType::STRING, 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 | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 163 | auto mtuArg = ctx.args.getOptional<std::string>("mtu"); |
| 164 | |
| 165 | // MTU is nominally a uint64_t, but can be the string value 'auto' to unset an override MTU |
| 166 | optional<uint64_t> mtu; |
| 167 | if (mtuArg == "auto") { |
| 168 | mtu = std::numeric_limits<uint64_t>::max(); |
| 169 | } |
| 170 | else if (mtuArg) { |
| 171 | // boost::lexical_cast<uint64_t> will accept negative numbers |
| 172 | int64_t v = -1; |
| 173 | if (!boost::conversion::try_lexical_convert<int64_t>(*mtuArg, v) || v < 0) { |
| 174 | ctx.exitCode = 2; |
| 175 | ctx.err << "MTU must either be a non-negative integer or 'auto'\n"; |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | mtu = static_cast<uint64_t>(v); |
| 180 | } |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 181 | |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 182 | FaceUri canonicalRemote; |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 183 | optional<FaceUri> canonicalLocal; |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 184 | |
| 185 | auto handleCanonizeError = [&] (const FaceUri& faceUri, const std::string& error) { |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 186 | ctx.exitCode = static_cast<int>(FindFace::Code::CANONIZE_ERROR); |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 187 | ctx.err << "Error when canonizing '" << faceUri << "': " << error << '\n'; |
| 188 | }; |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 189 | |
| 190 | auto printPositiveResult = [&] (const std::string& actionSummary, const ControlParameters& resp) { |
| 191 | text::ItemAttributes ia; |
| 192 | ctx.out << actionSummary << ' ' |
| 193 | << ia("id") << resp.getFaceId() |
Junxiao Shi | 1cce2a3 | 2017-05-02 02:39:55 +0000 | [diff] [blame] | 194 | << ia("local") << resp.getLocalUri() |
| 195 | << ia("remote") << resp.getUri() |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 196 | << ia("persistency") << resp.getFacePersistency(); |
| 197 | printFaceParams(ctx.out, ia, resp); |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 200 | auto updateFace = [&printPositiveResult] (ControlParameters respParams, ControlParameters resp) { |
| 201 | // faces/update response does not have FaceUris, copy from faces/create response |
| 202 | resp.setLocalUri(respParams.getLocalUri()) |
| 203 | .setUri(respParams.getUri()); |
| 204 | printPositiveResult("face-updated", resp); |
| 205 | }; |
| 206 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 207 | auto handle409 = [&] (const ControlResponse& resp) { |
| 208 | ControlParameters respParams(resp.getBody()); |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 209 | if (respParams.getUri() != canonicalRemote.toString()) { |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 210 | // we are conflicting with a different face, which is a general error |
| 211 | return false; |
| 212 | } |
| 213 | |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 214 | bool isChangingParams = false; |
| 215 | ControlParameters params; |
| 216 | params.setFaceId(respParams.getFaceId()); |
| 217 | |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame] | 218 | if (mtu && (!respParams.hasMtu() || respParams.getMtu() != *mtu)) { |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 219 | isChangingParams = true; |
| 220 | params.setMtu(*mtu); |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame] | 221 | } |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 222 | |
| 223 | if (persistencyLessThan(respParams.getFacePersistency(), persistency)) { |
| 224 | isChangingParams = true; |
| 225 | params.setFacePersistency(persistency); |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 226 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 227 | |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 228 | if (!boost::logic::indeterminate(lpReliability) && |
| 229 | lpReliability != respParams.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) { |
| 230 | isChangingParams = true; |
| 231 | params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, bool(lpReliability)); |
| 232 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 233 | |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 234 | if (!boost::logic::indeterminate(congestionMarking) && |
| 235 | congestionMarking != respParams.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) { |
| 236 | isChangingParams = true; |
| 237 | params.setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, bool(congestionMarking)); |
| 238 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 239 | |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 240 | if (baseCongestionMarkingIntervalMs) { |
| 241 | isChangingParams = true; |
| 242 | params.setBaseCongestionMarkingInterval(time::milliseconds(*baseCongestionMarkingIntervalMs)); |
| 243 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 244 | |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 245 | if (defaultCongestionThreshold) { |
| 246 | isChangingParams = true; |
| 247 | params.setDefaultCongestionThreshold(*defaultCongestionThreshold); |
| 248 | } |
| 249 | |
| 250 | if (isChangingParams) { |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 251 | ctx.controller.start<ndn::nfd::FaceUpdateCommand>( |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 252 | params, |
| 253 | bind(updateFace, respParams, _1), |
| 254 | ctx.makeCommandFailureHandler("updating face"), |
| 255 | ctx.makeCommandOptions()); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 256 | } |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 257 | else { |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 258 | // don't do anything |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 259 | printPositiveResult("face-exists", respParams); |
| 260 | } |
Eric Newberry | 13ff259 | 2020-03-06 17:32:29 -0800 | [diff] [blame] | 261 | |
Yanbiao Li | 58ba3f9 | 2017-02-15 14:27:18 +0000 | [diff] [blame] | 262 | return true; |
| 263 | }; |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 264 | |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 265 | auto doCreateFace = [&] { |
| 266 | ControlParameters params; |
| 267 | params.setUri(canonicalRemote.toString()); |
| 268 | if (canonicalLocal) { |
| 269 | params.setLocalUri(canonicalLocal->toString()); |
| 270 | } |
| 271 | params.setFacePersistency(persistency); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 272 | if (!boost::logic::indeterminate(lpReliability)) { |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 273 | params.setFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED, bool(lpReliability)); |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 274 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 275 | if (!boost::logic::indeterminate(congestionMarking)) { |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 276 | params.setFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED, bool(congestionMarking)); |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 277 | } |
| 278 | if (baseCongestionMarkingIntervalMs) { |
| 279 | params.setBaseCongestionMarkingInterval(time::milliseconds(*baseCongestionMarkingIntervalMs)); |
| 280 | } |
| 281 | if (defaultCongestionThreshold) { |
| 282 | params.setDefaultCongestionThreshold(*defaultCongestionThreshold); |
| 283 | } |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame] | 284 | if (mtu) { |
| 285 | params.setMtu(*mtu); |
| 286 | } |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 287 | |
| 288 | ctx.controller.start<ndn::nfd::FaceCreateCommand>( |
| 289 | params, |
| 290 | bind(printPositiveResult, "face-created", _1), |
| 291 | [&] (const ControlResponse& resp) { |
| 292 | if (resp.getCode() == 409 && handle409(resp)) { |
| 293 | return; |
| 294 | } |
| 295 | ctx.makeCommandFailureHandler("creating face")(resp); // invoke general error handler |
| 296 | }, |
| 297 | ctx.makeCommandOptions()); |
| 298 | }; |
| 299 | |
| 300 | remoteUri.canonize( |
| 301 | [&] (const FaceUri& canonicalUri) { |
| 302 | canonicalRemote = canonicalUri; |
| 303 | if (localUri) { |
| 304 | localUri->canonize( |
| 305 | [&] (const FaceUri& canonicalUri) { |
| 306 | canonicalLocal = canonicalUri; |
| 307 | doCreateFace(); |
| 308 | }, |
| 309 | bind(handleCanonizeError, *localUri, _1), |
| 310 | ctx.face.getIoService(), ctx.getTimeout()); |
| 311 | } |
| 312 | else { |
| 313 | doCreateFace(); |
| 314 | } |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 315 | }, |
Junxiao Shi | 0d97692 | 2017-04-01 14:35:21 +0000 | [diff] [blame] | 316 | bind(handleCanonizeError, remoteUri, _1), |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 317 | ctx.face.getIoService(), ctx.getTimeout()); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 318 | |
| 319 | ctx.face.processEvents(); |
| 320 | } |
| 321 | |
| 322 | void |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 323 | FaceModule::destroy(ExecuteContext& ctx) |
| 324 | { |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 325 | FindFace findFace(ctx); |
Davide Pesavento | 8b663a9 | 2018-11-21 22:57:20 -0500 | [diff] [blame] | 326 | FindFace::Code res = findFace.execute(ctx.args.at("face")); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 327 | |
| 328 | ctx.exitCode = static_cast<int>(res); |
| 329 | switch (res) { |
| 330 | case FindFace::Code::OK: |
| 331 | break; |
| 332 | case FindFace::Code::ERROR: |
| 333 | case FindFace::Code::CANONIZE_ERROR: |
| 334 | case FindFace::Code::NOT_FOUND: |
| 335 | ctx.err << findFace.getErrorReason() << '\n'; |
| 336 | return; |
| 337 | case FindFace::Code::AMBIGUOUS: |
| 338 | ctx.err << "Multiple faces match specified remote FaceUri. Re-run the command with a FaceId:"; |
| 339 | findFace.printDisambiguation(ctx.err, FindFace::DisambiguationStyle::LOCAL_URI); |
| 340 | ctx.err << '\n'; |
| 341 | return; |
| 342 | default: |
| 343 | BOOST_ASSERT_MSG(false, "unexpected FindFace result"); |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | const FaceStatus& face = findFace.getFaceStatus(); |
| 348 | |
| 349 | ctx.controller.start<ndn::nfd::FaceDestroyCommand>( |
| 350 | ControlParameters().setFaceId(face.getFaceId()), |
| 351 | [&] (const ControlParameters& resp) { |
| 352 | ctx.out << "face-destroyed "; |
| 353 | text::ItemAttributes ia; |
| 354 | ctx.out << ia("id") << face.getFaceId() |
| 355 | << ia("local") << face.getLocalUri() |
| 356 | << ia("remote") << face.getRemoteUri() |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 357 | << ia("persistency") << face.getFacePersistency(); |
| 358 | printFaceParams(ctx.out, ia, resp); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 359 | }, |
| 360 | ctx.makeCommandFailureHandler("destroying face"), |
| 361 | ctx.makeCommandOptions()); |
| 362 | |
| 363 | ctx.face.processEvents(); |
| 364 | } |
| 365 | |
| 366 | void |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 367 | FaceModule::fetchStatus(Controller& controller, |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 368 | const std::function<void()>& onSuccess, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 369 | const Controller::DatasetFailCallback& onFailure, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 370 | const CommandOptions& options) |
| 371 | { |
| 372 | controller.fetch<ndn::nfd::FaceDataset>( |
| 373 | [this, onSuccess] (const std::vector<FaceStatus>& result) { |
| 374 | m_status = result; |
| 375 | onSuccess(); |
| 376 | }, |
| 377 | onFailure, options); |
| 378 | } |
| 379 | |
| 380 | void |
| 381 | FaceModule::formatStatusXml(std::ostream& os) const |
| 382 | { |
| 383 | os << "<faces>"; |
| 384 | for (const FaceStatus& item : m_status) { |
| 385 | this->formatItemXml(os, item); |
| 386 | } |
| 387 | os << "</faces>"; |
| 388 | } |
| 389 | |
| 390 | void |
| 391 | FaceModule::formatItemXml(std::ostream& os, const FaceStatus& item) const |
| 392 | { |
| 393 | os << "<face>"; |
| 394 | |
| 395 | os << "<faceId>" << item.getFaceId() << "</faceId>"; |
| 396 | os << "<remoteUri>" << xml::Text{item.getRemoteUri()} << "</remoteUri>"; |
| 397 | os << "<localUri>" << xml::Text{item.getLocalUri()} << "</localUri>"; |
| 398 | |
| 399 | if (item.hasExpirationPeriod()) { |
| 400 | os << "<expirationPeriod>" << xml::formatDuration(item.getExpirationPeriod()) |
| 401 | << "</expirationPeriod>"; |
| 402 | } |
| 403 | os << "<faceScope>" << item.getFaceScope() << "</faceScope>"; |
| 404 | os << "<facePersistency>" << item.getFacePersistency() << "</facePersistency>"; |
| 405 | os << "<linkType>" << item.getLinkType() << "</linkType>"; |
| 406 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 407 | if (!item.hasBaseCongestionMarkingInterval() && !item.hasDefaultCongestionThreshold()) { |
| 408 | os << "<congestion/>"; |
| 409 | } |
| 410 | else { |
| 411 | os << "<congestion>"; |
| 412 | if (item.hasBaseCongestionMarkingInterval()) { |
| 413 | os << "<baseMarkingInterval>" << xml::formatDuration(item.getBaseCongestionMarkingInterval()) |
| 414 | << "</baseMarkingInterval>"; |
| 415 | } |
| 416 | if (item.hasDefaultCongestionThreshold()) { |
| 417 | os << "<defaultThreshold>" << item.getDefaultCongestionThreshold() << "</defaultThreshold>"; |
| 418 | } |
| 419 | os << "</congestion>"; |
| 420 | } |
| 421 | |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame] | 422 | if (item.hasMtu()) { |
| 423 | os << "<mtu>" << item.getMtu() << "</mtu>"; |
| 424 | } |
| 425 | |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 426 | if (item.getFlags() == 0) { |
| 427 | os << "<flags/>"; |
| 428 | } |
| 429 | else { |
| 430 | os << "<flags>"; |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 431 | os << xml::Flag{"localFieldsEnabled", item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)}; |
| 432 | os << xml::Flag{"lpReliabilityEnabled", item.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)}; |
| 433 | os << xml::Flag{"congestionMarkingEnabled", item.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)}; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 434 | os << "</flags>"; |
| 435 | } |
| 436 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 437 | os << "<packetCounters>"; |
| 438 | os << "<incomingPackets>" |
| 439 | << "<nInterests>" << item.getNInInterests() << "</nInterests>" |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 440 | << "<nData>" << item.getNInData() << "</nData>" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 441 | << "<nNacks>" << item.getNInNacks() << "</nNacks>" |
| 442 | << "</incomingPackets>"; |
| 443 | os << "<outgoingPackets>" |
| 444 | << "<nInterests>" << item.getNOutInterests() << "</nInterests>" |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 445 | << "<nData>" << item.getNOutData() << "</nData>" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 446 | << "<nNacks>" << item.getNOutNacks() << "</nNacks>" |
| 447 | << "</outgoingPackets>"; |
| 448 | os << "</packetCounters>"; |
| 449 | |
| 450 | os << "<byteCounters>"; |
| 451 | os << "<incomingBytes>" << item.getNInBytes() << "</incomingBytes>"; |
| 452 | os << "<outgoingBytes>" << item.getNOutBytes() << "</outgoingBytes>"; |
| 453 | os << "</byteCounters>"; |
| 454 | |
| 455 | os << "</face>"; |
| 456 | } |
| 457 | |
| 458 | void |
| 459 | FaceModule::formatStatusText(std::ostream& os) const |
| 460 | { |
| 461 | os << "Faces:\n"; |
| 462 | for (const FaceStatus& item : m_status) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 463 | os << " "; |
| 464 | formatItemText(os, item, false); |
| 465 | os << '\n'; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | void |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 470 | FaceModule::formatItemText(std::ostream& os, const FaceStatus& item, bool wantMultiLine) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 471 | { |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 472 | text::ItemAttributes ia(wantMultiLine, 10); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 473 | |
| 474 | os << ia("faceid") << item.getFaceId(); |
| 475 | os << ia("remote") << item.getRemoteUri(); |
| 476 | os << ia("local") << item.getLocalUri(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 477 | |
| 478 | if (item.hasExpirationPeriod()) { |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 479 | os << ia("expires") << text::formatDuration<time::seconds>(item.getExpirationPeriod()); |
| 480 | } |
| 481 | |
| 482 | if (item.hasBaseCongestionMarkingInterval() || item.hasDefaultCongestionThreshold()) { |
| 483 | os << ia("congestion") << "{"; |
| 484 | text::Separator congestionSep("", " "); |
| 485 | if (item.hasBaseCongestionMarkingInterval()) { |
| 486 | os << congestionSep << "base-marking-interval=" |
| 487 | << text::formatDuration<time::milliseconds>(item.getBaseCongestionMarkingInterval()); |
| 488 | } |
| 489 | if (item.hasDefaultCongestionThreshold()) { |
| 490 | os << congestionSep << "default-threshold=" << item.getDefaultCongestionThreshold() << "B"; |
| 491 | } |
| 492 | os << "}"; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame] | 495 | if (item.hasMtu()) { |
| 496 | os << ia("mtu") << item.getMtu(); |
| 497 | } |
| 498 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 499 | os << ia("counters") |
| 500 | << "{in={" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 501 | << item.getNInInterests() << "i " |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 502 | << item.getNInData() << "d " |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 503 | << item.getNInNacks() << "n " |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 504 | << item.getNInBytes() << "B} " |
| 505 | << "out={" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 506 | << item.getNOutInterests() << "i " |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame] | 507 | << item.getNOutData() << "d " |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 508 | << item.getNOutNacks() << "n " |
| 509 | << item.getNOutBytes() << "B}}"; |
| 510 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 511 | os << ia("flags") << '{'; |
| 512 | text::Separator flagSep("", " "); |
| 513 | os << flagSep << item.getFaceScope(); |
| 514 | os << flagSep << item.getFacePersistency(); |
| 515 | os << flagSep << item.getLinkType(); |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 516 | if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 517 | os << flagSep << "local-fields"; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 518 | } |
Eric Newberry | 84d3adc | 2017-08-09 23:31:40 -0400 | [diff] [blame] | 519 | if (item.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)) { |
| 520 | os << flagSep << "lp-reliability"; |
| 521 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 522 | if (item.getFlagBit(ndn::nfd::BIT_CONGESTION_MARKING_ENABLED)) { |
| 523 | os << flagSep << "congestion-marking"; |
| 524 | } |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 525 | os << '}'; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 526 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 527 | os << ia.end(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 530 | void |
| 531 | FaceModule::printFaceParams(std::ostream& os, text::ItemAttributes& ia, const ControlParameters& resp) |
| 532 | { |
Junxiao Shi | 8dc473a | 2018-03-02 15:10:18 +0000 | [diff] [blame] | 533 | os << ia("reliability") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_LP_RELIABILITY_ENABLED)} |
| 534 | << 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] | 535 | if (resp.hasBaseCongestionMarkingInterval()) { |
| 536 | os << ia("congestion-marking-interval") |
| 537 | << text::formatDuration<time::milliseconds>(resp.getBaseCongestionMarkingInterval()); |
| 538 | } |
| 539 | if (resp.hasDefaultCongestionThreshold()) { |
| 540 | os << ia("default-congestion-threshold") << resp.getDefaultCongestionThreshold() << "B"; |
| 541 | } |
Eric Newberry | 4f8dd96 | 2018-06-17 21:32:07 -0700 | [diff] [blame] | 542 | if (resp.hasMtu()) { |
| 543 | os << ia("mtu") << resp.getMtu(); |
| 544 | } |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 545 | os << '\n'; |
| 546 | } |
| 547 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 548 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 549 | } // namespace tools |
| 550 | } // namespace nfd |