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); |
| 41 | } |
| 42 | |
| 43 | void |
| 44 | FaceModule::show(ExecuteContext& ctx) |
| 45 | { |
| 46 | uint64_t faceId = ctx.args.get<uint64_t>("id"); |
| 47 | |
| 48 | ndn::nfd::FaceQueryFilter filter; |
| 49 | filter.setFaceId(faceId); |
| 50 | ctx.controller.fetch<ndn::nfd::FaceQueryDataset>( |
| 51 | filter, |
| 52 | [faceId, &ctx] (const std::vector<FaceStatus>& result) { |
| 53 | if (result.size() != 1) { |
| 54 | ctx.exitCode = 3; |
| 55 | ctx.err << "Face " << faceId << " not found.\n"; |
| 56 | return; |
| 57 | } |
| 58 | formatItemText(ctx.out, result.front(), true); |
| 59 | }, |
| 60 | ctx.makeDatasetFailureHandler("face information")); |
| 61 | |
| 62 | ctx.face.processEvents(); |
| 63 | } |
| 64 | |
| 65 | void |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 66 | FaceModule::fetchStatus(Controller& controller, |
| 67 | const function<void()>& onSuccess, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 68 | const Controller::DatasetFailCallback& onFailure, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 69 | const CommandOptions& options) |
| 70 | { |
| 71 | controller.fetch<ndn::nfd::FaceDataset>( |
| 72 | [this, onSuccess] (const std::vector<FaceStatus>& result) { |
| 73 | m_status = result; |
| 74 | onSuccess(); |
| 75 | }, |
| 76 | onFailure, options); |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | FaceModule::formatStatusXml(std::ostream& os) const |
| 81 | { |
| 82 | os << "<faces>"; |
| 83 | for (const FaceStatus& item : m_status) { |
| 84 | this->formatItemXml(os, item); |
| 85 | } |
| 86 | os << "</faces>"; |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | FaceModule::formatItemXml(std::ostream& os, const FaceStatus& item) const |
| 91 | { |
| 92 | os << "<face>"; |
| 93 | |
| 94 | os << "<faceId>" << item.getFaceId() << "</faceId>"; |
| 95 | os << "<remoteUri>" << xml::Text{item.getRemoteUri()} << "</remoteUri>"; |
| 96 | os << "<localUri>" << xml::Text{item.getLocalUri()} << "</localUri>"; |
| 97 | |
| 98 | if (item.hasExpirationPeriod()) { |
| 99 | os << "<expirationPeriod>" << xml::formatDuration(item.getExpirationPeriod()) |
| 100 | << "</expirationPeriod>"; |
| 101 | } |
| 102 | os << "<faceScope>" << item.getFaceScope() << "</faceScope>"; |
| 103 | os << "<facePersistency>" << item.getFacePersistency() << "</facePersistency>"; |
| 104 | os << "<linkType>" << item.getLinkType() << "</linkType>"; |
| 105 | |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 106 | if (item.getFlags() == 0) { |
| 107 | os << "<flags/>"; |
| 108 | } |
| 109 | else { |
| 110 | os << "<flags>"; |
| 111 | if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) { |
| 112 | os << "<localFieldsEnabled/>"; |
| 113 | } |
| 114 | os << "</flags>"; |
| 115 | } |
| 116 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 117 | os << "<packetCounters>"; |
| 118 | os << "<incomingPackets>" |
| 119 | << "<nInterests>" << item.getNInInterests() << "</nInterests>" |
| 120 | << "<nDatas>" << item.getNInDatas() << "</nDatas>" |
| 121 | << "<nNacks>" << item.getNInNacks() << "</nNacks>" |
| 122 | << "</incomingPackets>"; |
| 123 | os << "<outgoingPackets>" |
| 124 | << "<nInterests>" << item.getNOutInterests() << "</nInterests>" |
| 125 | << "<nDatas>" << item.getNOutDatas() << "</nDatas>" |
| 126 | << "<nNacks>" << item.getNOutNacks() << "</nNacks>" |
| 127 | << "</outgoingPackets>"; |
| 128 | os << "</packetCounters>"; |
| 129 | |
| 130 | os << "<byteCounters>"; |
| 131 | os << "<incomingBytes>" << item.getNInBytes() << "</incomingBytes>"; |
| 132 | os << "<outgoingBytes>" << item.getNOutBytes() << "</outgoingBytes>"; |
| 133 | os << "</byteCounters>"; |
| 134 | |
| 135 | os << "</face>"; |
| 136 | } |
| 137 | |
| 138 | void |
| 139 | FaceModule::formatStatusText(std::ostream& os) const |
| 140 | { |
| 141 | os << "Faces:\n"; |
| 142 | for (const FaceStatus& item : m_status) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 143 | os << " "; |
| 144 | formatItemText(os, item, false); |
| 145 | os << '\n'; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | void |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 150 | FaceModule::formatItemText(std::ostream& os, const FaceStatus& item, bool wantMultiLine) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 151 | { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 152 | text::ItemAttributes ia(wantMultiLine, 8); |
| 153 | |
| 154 | os << ia("faceid") << item.getFaceId(); |
| 155 | os << ia("remote") << item.getRemoteUri(); |
| 156 | os << ia("local") << item.getLocalUri(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 157 | |
| 158 | if (item.hasExpirationPeriod()) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 159 | os << ia("expires") << text::formatDuration(item.getExpirationPeriod()); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 162 | os << ia("counters") |
| 163 | << "{in={" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 164 | << item.getNInInterests() << "i " |
| 165 | << item.getNInDatas() << "d " |
| 166 | << item.getNInNacks() << "n " |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 167 | << item.getNInBytes() << "B} " |
| 168 | << "out={" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 169 | << item.getNOutInterests() << "i " |
| 170 | << item.getNOutDatas() << "d " |
| 171 | << item.getNOutNacks() << "n " |
| 172 | << item.getNOutBytes() << "B}}"; |
| 173 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 174 | os << ia("flags") << '{'; |
| 175 | text::Separator flagSep("", " "); |
| 176 | os << flagSep << item.getFaceScope(); |
| 177 | os << flagSep << item.getFacePersistency(); |
| 178 | os << flagSep << item.getLinkType(); |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 179 | if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 180 | os << flagSep << "local-fields"; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 181 | } |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 182 | os << '}'; |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame] | 183 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 184 | os << ia.end(); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 187 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 188 | } // namespace tools |
| 189 | } // namespace nfd |