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" |
| 27 | #include "format-helpers.hpp" |
| 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 | { |
| 36 | CommandDefinition defFaceShow("face", "show"); |
| 37 | defFaceShow |
| 38 | .setTitle("show face information") |
| 39 | .addArg("id", ArgValueType::UNSIGNED, Required::YES, Positional::YES); |
| 40 | parser.addCommand(defFaceShow, &FaceModule::show); |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame^] | 41 | |
| 42 | CommandDefinition defFaceCreate("face", "create"); |
| 43 | defFaceCreate |
| 44 | .setTitle("create a face") |
| 45 | .addArg("remote", ArgValueType::FACE_URI, Required::YES, Positional::YES) |
| 46 | .addArg("persistency", ArgValueType::FACE_PERSISTENCY, Required::NO, Positional::YES); |
| 47 | parser.addCommand(defFaceCreate, &FaceModule::create); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
| 51 | FaceModule::show(ExecuteContext& ctx) |
| 52 | { |
| 53 | uint64_t faceId = ctx.args.get<uint64_t>("id"); |
| 54 | |
| 55 | ndn::nfd::FaceQueryFilter filter; |
| 56 | filter.setFaceId(faceId); |
| 57 | ctx.controller.fetch<ndn::nfd::FaceQueryDataset>( |
| 58 | filter, |
| 59 | [faceId, &ctx] (const std::vector<FaceStatus>& result) { |
| 60 | if (result.size() != 1) { |
| 61 | ctx.exitCode = 3; |
| 62 | ctx.err << "Face " << faceId << " not found.\n"; |
| 63 | return; |
| 64 | } |
| 65 | formatItemText(ctx.out, result.front(), true); |
| 66 | }, |
Junxiao Shi | 1d7fef5 | 2017-02-02 05:33:14 +0000 | [diff] [blame^] | 67 | ctx.makeDatasetFailureHandler("face information"), |
| 68 | ctx.makeCommandOptions()); |
| 69 | |
| 70 | ctx.face.processEvents(); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | FaceModule::create(ExecuteContext& ctx) |
| 75 | { |
| 76 | auto faceUri = ctx.args.get<FaceUri>("remote"); |
| 77 | auto persistency = ctx.args.get<FacePersistency>("persistency", FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
| 78 | |
| 79 | faceUri.canonize( |
| 80 | [&] (const FaceUri& canonicalUri) { |
| 81 | ctx.controller.start<ndn::nfd::FaceCreateCommand>( |
| 82 | ControlParameters().setUri(canonicalUri.toString()).setFacePersistency(persistency), |
| 83 | [&] (const ControlParameters& resp) { |
| 84 | ctx.out << "face-created "; |
| 85 | text::ItemAttributes ia; |
| 86 | ctx.out << ia("id") << resp.getFaceId() |
| 87 | << ia("remote") << resp.getUri() |
| 88 | << ia("persistency") << resp.getFacePersistency() << '\n'; |
| 89 | ///\todo #3864 display localUri |
| 90 | }, |
| 91 | ctx.makeCommandFailureHandler("creating face"), ///\todo #3232 update persistency upon 409 |
| 92 | ctx.makeCommandOptions()); |
| 93 | }, |
| 94 | [&] (const std::string& canonizeError) { |
| 95 | ctx.exitCode = 4; |
| 96 | ctx.err << "Error when canonizing FaceUri: " << canonizeError << '\n'; |
| 97 | }, |
| 98 | ctx.face.getIoService(), ctx.getTimeout()); |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 99 | |
| 100 | ctx.face.processEvents(); |
| 101 | } |
| 102 | |
| 103 | void |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 104 | FaceModule::fetchStatus(Controller& controller, |
| 105 | const function<void()>& onSuccess, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 106 | const Controller::DatasetFailCallback& onFailure, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 107 | const CommandOptions& options) |
| 108 | { |
| 109 | controller.fetch<ndn::nfd::FaceDataset>( |
| 110 | [this, onSuccess] (const std::vector<FaceStatus>& result) { |
| 111 | m_status = result; |
| 112 | onSuccess(); |
| 113 | }, |
| 114 | onFailure, options); |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | FaceModule::formatStatusXml(std::ostream& os) const |
| 119 | { |
| 120 | os << "<faces>"; |
| 121 | for (const FaceStatus& item : m_status) { |
| 122 | this->formatItemXml(os, item); |
| 123 | } |
| 124 | os << "</faces>"; |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | FaceModule::formatItemXml(std::ostream& os, const FaceStatus& item) const |
| 129 | { |
| 130 | os << "<face>"; |
| 131 | |
| 132 | os << "<faceId>" << item.getFaceId() << "</faceId>"; |
| 133 | os << "<remoteUri>" << xml::Text{item.getRemoteUri()} << "</remoteUri>"; |
| 134 | os << "<localUri>" << xml::Text{item.getLocalUri()} << "</localUri>"; |
| 135 | |
| 136 | if (item.hasExpirationPeriod()) { |
| 137 | os << "<expirationPeriod>" << xml::formatDuration(item.getExpirationPeriod()) |
| 138 | << "</expirationPeriod>"; |
| 139 | } |
| 140 | os << "<faceScope>" << item.getFaceScope() << "</faceScope>"; |
| 141 | os << "<facePersistency>" << item.getFacePersistency() << "</facePersistency>"; |
| 142 | os << "<linkType>" << item.getLinkType() << "</linkType>"; |
| 143 | |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 144 | if (item.getFlags() == 0) { |
| 145 | os << "<flags/>"; |
| 146 | } |
| 147 | else { |
| 148 | os << "<flags>"; |
| 149 | if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) { |
| 150 | os << "<localFieldsEnabled/>"; |
| 151 | } |
| 152 | os << "</flags>"; |
| 153 | } |
| 154 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 155 | os << "<packetCounters>"; |
| 156 | os << "<incomingPackets>" |
| 157 | << "<nInterests>" << item.getNInInterests() << "</nInterests>" |
| 158 | << "<nDatas>" << item.getNInDatas() << "</nDatas>" |
| 159 | << "<nNacks>" << item.getNInNacks() << "</nNacks>" |
| 160 | << "</incomingPackets>"; |
| 161 | os << "<outgoingPackets>" |
| 162 | << "<nInterests>" << item.getNOutInterests() << "</nInterests>" |
| 163 | << "<nDatas>" << item.getNOutDatas() << "</nDatas>" |
| 164 | << "<nNacks>" << item.getNOutNacks() << "</nNacks>" |
| 165 | << "</outgoingPackets>"; |
| 166 | os << "</packetCounters>"; |
| 167 | |
| 168 | os << "<byteCounters>"; |
| 169 | os << "<incomingBytes>" << item.getNInBytes() << "</incomingBytes>"; |
| 170 | os << "<outgoingBytes>" << item.getNOutBytes() << "</outgoingBytes>"; |
| 171 | os << "</byteCounters>"; |
| 172 | |
| 173 | os << "</face>"; |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | FaceModule::formatStatusText(std::ostream& os) const |
| 178 | { |
| 179 | os << "Faces:\n"; |
| 180 | for (const FaceStatus& item : m_status) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 181 | os << " "; |
| 182 | formatItemText(os, item, false); |
| 183 | os << '\n'; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | void |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 188 | FaceModule::formatItemText(std::ostream& os, const FaceStatus& item, bool wantMultiLine) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 189 | { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 190 | text::ItemAttributes ia(wantMultiLine, 8); |
| 191 | |
| 192 | os << ia("faceid") << item.getFaceId(); |
| 193 | os << ia("remote") << item.getRemoteUri(); |
| 194 | os << ia("local") << item.getLocalUri(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 195 | |
| 196 | if (item.hasExpirationPeriod()) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 197 | os << ia("expires") << text::formatDuration(item.getExpirationPeriod()); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 200 | os << ia("counters") |
| 201 | << "{in={" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 202 | << item.getNInInterests() << "i " |
| 203 | << item.getNInDatas() << "d " |
| 204 | << item.getNInNacks() << "n " |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 205 | << item.getNInBytes() << "B} " |
| 206 | << "out={" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 207 | << item.getNOutInterests() << "i " |
| 208 | << item.getNOutDatas() << "d " |
| 209 | << item.getNOutNacks() << "n " |
| 210 | << item.getNOutBytes() << "B}}"; |
| 211 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 212 | os << ia("flags") << '{'; |
| 213 | text::Separator flagSep("", " "); |
| 214 | os << flagSep << item.getFaceScope(); |
| 215 | os << flagSep << item.getFacePersistency(); |
| 216 | os << flagSep << item.getLinkType(); |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 217 | if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 218 | os << flagSep << "local-fields"; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 219 | } |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 220 | os << '}'; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 221 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 222 | os << ia.end(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 225 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 226 | } // namespace tools |
| 227 | } // namespace nfd |