Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | #include "format-helpers.hpp" |
| 29 | |
| 30 | namespace nfd { |
| 31 | namespace tools { |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 32 | namespace nfdc { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 33 | |
| 34 | void |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 35 | FaceModule::registerCommands(CommandParser& parser) |
| 36 | { |
| 37 | CommandDefinition defFaceShow("face", "show"); |
| 38 | defFaceShow |
| 39 | .setTitle("show face information") |
| 40 | .addArg("id", ArgValueType::UNSIGNED, Required::YES, Positional::YES); |
| 41 | parser.addCommand(defFaceShow, &FaceModule::show); |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 42 | |
| 43 | CommandDefinition defFaceCreate("face", "create"); |
| 44 | defFaceCreate |
| 45 | .setTitle("create a face") |
| 46 | .addArg("remote", ArgValueType::FACE_URI, Required::YES, Positional::YES) |
| 47 | .addArg("persistency", ArgValueType::FACE_PERSISTENCY, Required::NO, Positional::YES); |
| 48 | parser.addCommand(defFaceCreate, &FaceModule::create); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame^] | 49 | |
| 50 | CommandDefinition defFaceDestroy("face", "destroy"); |
| 51 | defFaceDestroy |
| 52 | .setTitle("destroy a face") |
| 53 | .addArg("face", ArgValueType::FACE_ID_OR_URI, Required::YES, Positional::YES); |
| 54 | parser.addCommand(defFaceDestroy, &FaceModule::destroy); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void |
| 58 | FaceModule::show(ExecuteContext& ctx) |
| 59 | { |
| 60 | uint64_t faceId = ctx.args.get<uint64_t>("id"); |
| 61 | |
| 62 | ndn::nfd::FaceQueryFilter filter; |
| 63 | filter.setFaceId(faceId); |
| 64 | ctx.controller.fetch<ndn::nfd::FaceQueryDataset>( |
| 65 | filter, |
| 66 | [faceId, &ctx] (const std::vector<FaceStatus>& result) { |
| 67 | if (result.size() != 1) { |
| 68 | ctx.exitCode = 3; |
| 69 | ctx.err << "Face " << faceId << " not found.\n"; |
| 70 | return; |
| 71 | } |
| 72 | formatItemText(ctx.out, result.front(), true); |
| 73 | }, |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame] | 74 | ctx.makeDatasetFailureHandler("face information"), |
| 75 | ctx.makeCommandOptions()); |
| 76 | |
| 77 | ctx.face.processEvents(); |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | FaceModule::create(ExecuteContext& ctx) |
| 82 | { |
| 83 | auto faceUri = ctx.args.get<FaceUri>("remote"); |
| 84 | auto persistency = ctx.args.get<FacePersistency>("persistency", FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
| 85 | |
| 86 | faceUri.canonize( |
| 87 | [&] (const FaceUri& canonicalUri) { |
| 88 | ctx.controller.start<ndn::nfd::FaceCreateCommand>( |
| 89 | ControlParameters().setUri(canonicalUri.toString()).setFacePersistency(persistency), |
| 90 | [&] (const ControlParameters& resp) { |
| 91 | ctx.out << "face-created "; |
| 92 | text::ItemAttributes ia; |
| 93 | ctx.out << ia("id") << resp.getFaceId() |
| 94 | << ia("remote") << resp.getUri() |
| 95 | << ia("persistency") << resp.getFacePersistency() << '\n'; |
| 96 | ///\todo #3864 display localUri |
| 97 | }, |
| 98 | ctx.makeCommandFailureHandler("creating face"), ///\todo #3232 update persistency upon 409 |
| 99 | ctx.makeCommandOptions()); |
| 100 | }, |
| 101 | [&] (const std::string& canonizeError) { |
| 102 | ctx.exitCode = 4; |
| 103 | ctx.err << "Error when canonizing FaceUri: " << canonizeError << '\n'; |
| 104 | }, |
| 105 | ctx.face.getIoService(), ctx.getTimeout()); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 106 | |
| 107 | ctx.face.processEvents(); |
| 108 | } |
| 109 | |
| 110 | void |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame^] | 111 | FaceModule::destroy(ExecuteContext& ctx) |
| 112 | { |
| 113 | const boost::any faceIdOrUri = ctx.args.at("face"); |
| 114 | |
| 115 | FindFace findFace(ctx); |
| 116 | FindFace::Code res = FindFace::Code::ERROR; |
| 117 | const uint64_t* faceId = boost::any_cast<uint64_t>(&faceIdOrUri); |
| 118 | if (faceId != nullptr) { |
| 119 | res = findFace.execute(*faceId); |
| 120 | } |
| 121 | else { |
| 122 | res = findFace.execute(boost::any_cast<FaceUri>(faceIdOrUri)); |
| 123 | } |
| 124 | |
| 125 | ctx.exitCode = static_cast<int>(res); |
| 126 | switch (res) { |
| 127 | case FindFace::Code::OK: |
| 128 | break; |
| 129 | case FindFace::Code::ERROR: |
| 130 | case FindFace::Code::CANONIZE_ERROR: |
| 131 | case FindFace::Code::NOT_FOUND: |
| 132 | ctx.err << findFace.getErrorReason() << '\n'; |
| 133 | return; |
| 134 | case FindFace::Code::AMBIGUOUS: |
| 135 | ctx.err << "Multiple faces match specified remote FaceUri. Re-run the command with a FaceId:"; |
| 136 | findFace.printDisambiguation(ctx.err, FindFace::DisambiguationStyle::LOCAL_URI); |
| 137 | ctx.err << '\n'; |
| 138 | return; |
| 139 | default: |
| 140 | BOOST_ASSERT_MSG(false, "unexpected FindFace result"); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | const FaceStatus& face = findFace.getFaceStatus(); |
| 145 | |
| 146 | ctx.controller.start<ndn::nfd::FaceDestroyCommand>( |
| 147 | ControlParameters().setFaceId(face.getFaceId()), |
| 148 | [&] (const ControlParameters& resp) { |
| 149 | ctx.out << "face-destroyed "; |
| 150 | text::ItemAttributes ia; |
| 151 | ctx.out << ia("id") << face.getFaceId() |
| 152 | << ia("local") << face.getLocalUri() |
| 153 | << ia("remote") << face.getRemoteUri() |
| 154 | << ia("persistency") << face.getFacePersistency() << '\n'; |
| 155 | }, |
| 156 | ctx.makeCommandFailureHandler("destroying face"), |
| 157 | ctx.makeCommandOptions()); |
| 158 | |
| 159 | ctx.face.processEvents(); |
| 160 | } |
| 161 | |
| 162 | void |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 163 | FaceModule::fetchStatus(Controller& controller, |
| 164 | const function<void()>& onSuccess, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 165 | const Controller::DatasetFailCallback& onFailure, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 166 | const CommandOptions& options) |
| 167 | { |
| 168 | controller.fetch<ndn::nfd::FaceDataset>( |
| 169 | [this, onSuccess] (const std::vector<FaceStatus>& result) { |
| 170 | m_status = result; |
| 171 | onSuccess(); |
| 172 | }, |
| 173 | onFailure, options); |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | FaceModule::formatStatusXml(std::ostream& os) const |
| 178 | { |
| 179 | os << "<faces>"; |
| 180 | for (const FaceStatus& item : m_status) { |
| 181 | this->formatItemXml(os, item); |
| 182 | } |
| 183 | os << "</faces>"; |
| 184 | } |
| 185 | |
| 186 | void |
| 187 | FaceModule::formatItemXml(std::ostream& os, const FaceStatus& item) const |
| 188 | { |
| 189 | os << "<face>"; |
| 190 | |
| 191 | os << "<faceId>" << item.getFaceId() << "</faceId>"; |
| 192 | os << "<remoteUri>" << xml::Text{item.getRemoteUri()} << "</remoteUri>"; |
| 193 | os << "<localUri>" << xml::Text{item.getLocalUri()} << "</localUri>"; |
| 194 | |
| 195 | if (item.hasExpirationPeriod()) { |
| 196 | os << "<expirationPeriod>" << xml::formatDuration(item.getExpirationPeriod()) |
| 197 | << "</expirationPeriod>"; |
| 198 | } |
| 199 | os << "<faceScope>" << item.getFaceScope() << "</faceScope>"; |
| 200 | os << "<facePersistency>" << item.getFacePersistency() << "</facePersistency>"; |
| 201 | os << "<linkType>" << item.getLinkType() << "</linkType>"; |
| 202 | |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 203 | if (item.getFlags() == 0) { |
| 204 | os << "<flags/>"; |
| 205 | } |
| 206 | else { |
| 207 | os << "<flags>"; |
| 208 | if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) { |
| 209 | os << "<localFieldsEnabled/>"; |
| 210 | } |
| 211 | os << "</flags>"; |
| 212 | } |
| 213 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 214 | os << "<packetCounters>"; |
| 215 | os << "<incomingPackets>" |
| 216 | << "<nInterests>" << item.getNInInterests() << "</nInterests>" |
| 217 | << "<nDatas>" << item.getNInDatas() << "</nDatas>" |
| 218 | << "<nNacks>" << item.getNInNacks() << "</nNacks>" |
| 219 | << "</incomingPackets>"; |
| 220 | os << "<outgoingPackets>" |
| 221 | << "<nInterests>" << item.getNOutInterests() << "</nInterests>" |
| 222 | << "<nDatas>" << item.getNOutDatas() << "</nDatas>" |
| 223 | << "<nNacks>" << item.getNOutNacks() << "</nNacks>" |
| 224 | << "</outgoingPackets>"; |
| 225 | os << "</packetCounters>"; |
| 226 | |
| 227 | os << "<byteCounters>"; |
| 228 | os << "<incomingBytes>" << item.getNInBytes() << "</incomingBytes>"; |
| 229 | os << "<outgoingBytes>" << item.getNOutBytes() << "</outgoingBytes>"; |
| 230 | os << "</byteCounters>"; |
| 231 | |
| 232 | os << "</face>"; |
| 233 | } |
| 234 | |
| 235 | void |
| 236 | FaceModule::formatStatusText(std::ostream& os) const |
| 237 | { |
| 238 | os << "Faces:\n"; |
| 239 | for (const FaceStatus& item : m_status) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 240 | os << " "; |
| 241 | formatItemText(os, item, false); |
| 242 | os << '\n'; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | void |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 247 | FaceModule::formatItemText(std::ostream& os, const FaceStatus& item, bool wantMultiLine) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 248 | { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 249 | text::ItemAttributes ia(wantMultiLine, 8); |
| 250 | |
| 251 | os << ia("faceid") << item.getFaceId(); |
| 252 | os << ia("remote") << item.getRemoteUri(); |
| 253 | os << ia("local") << item.getLocalUri(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 254 | |
| 255 | if (item.hasExpirationPeriod()) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 256 | os << ia("expires") << text::formatDuration(item.getExpirationPeriod()); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 259 | os << ia("counters") |
| 260 | << "{in={" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 261 | << item.getNInInterests() << "i " |
| 262 | << item.getNInDatas() << "d " |
| 263 | << item.getNInNacks() << "n " |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 264 | << item.getNInBytes() << "B} " |
| 265 | << "out={" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 266 | << item.getNOutInterests() << "i " |
| 267 | << item.getNOutDatas() << "d " |
| 268 | << item.getNOutNacks() << "n " |
| 269 | << item.getNOutBytes() << "B}}"; |
| 270 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 271 | os << ia("flags") << '{'; |
| 272 | text::Separator flagSep("", " "); |
| 273 | os << flagSep << item.getFaceScope(); |
| 274 | os << flagSep << item.getFacePersistency(); |
| 275 | os << flagSep << item.getLinkType(); |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 276 | if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 277 | os << flagSep << "local-fields"; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 278 | } |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 279 | os << '}'; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 280 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 281 | os << ia.end(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 284 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 285 | } // namespace tools |
| 286 | } // namespace nfd |