Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 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 | |
| 26 | /** \todo |
| 27 | * #2542 plans to merge nfd-status with nfdc. |
| 28 | * FaceModule class should be changed as follows: |
| 29 | * (1) move into ndn::nfd::nfdc namespace |
| 30 | * (2) assuming command syntax is similar to Windows NT \p netsh , |
| 31 | * this class can handle command argument parsing as soon as |
| 32 | * 'face' sub-command is detected |
| 33 | * (3) introduce methods to create and destroy faces, and update face attributes |
| 34 | * |
| 35 | * \todo |
| 36 | * #3444 aims at improving output texts of nfdc. |
| 37 | * Assuming it's worked with or after #2542: |
| 38 | * (1) introduce an \p style parameter on formatItemText method to specify desired text style, |
| 39 | * such as human friendly style vs. porcelain style for script consumption |
| 40 | * (2) upon successful command execute, convert the command result into FaceStatus type, |
| 41 | * and use formatItemText to render the result, so that output from status retrieval |
| 42 | * and command execution have consistent styles |
| 43 | * } |
| 44 | */ |
| 45 | |
| 46 | #include "face-module.hpp" |
| 47 | #include "format-helpers.hpp" |
| 48 | |
| 49 | namespace nfd { |
| 50 | namespace tools { |
| 51 | namespace nfd_status { |
| 52 | |
| 53 | void |
| 54 | FaceModule::fetchStatus(Controller& controller, |
| 55 | const function<void()>& onSuccess, |
| 56 | const Controller::CommandFailCallback& onFailure, |
| 57 | const CommandOptions& options) |
| 58 | { |
| 59 | controller.fetch<ndn::nfd::FaceDataset>( |
| 60 | [this, onSuccess] (const std::vector<FaceStatus>& result) { |
| 61 | m_status = result; |
| 62 | onSuccess(); |
| 63 | }, |
| 64 | onFailure, options); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | FaceModule::formatStatusXml(std::ostream& os) const |
| 69 | { |
| 70 | os << "<faces>"; |
| 71 | for (const FaceStatus& item : m_status) { |
| 72 | this->formatItemXml(os, item); |
| 73 | } |
| 74 | os << "</faces>"; |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | FaceModule::formatItemXml(std::ostream& os, const FaceStatus& item) const |
| 79 | { |
| 80 | os << "<face>"; |
| 81 | |
| 82 | os << "<faceId>" << item.getFaceId() << "</faceId>"; |
| 83 | os << "<remoteUri>" << xml::Text{item.getRemoteUri()} << "</remoteUri>"; |
| 84 | os << "<localUri>" << xml::Text{item.getLocalUri()} << "</localUri>"; |
| 85 | |
| 86 | if (item.hasExpirationPeriod()) { |
| 87 | os << "<expirationPeriod>" << xml::formatDuration(item.getExpirationPeriod()) |
| 88 | << "</expirationPeriod>"; |
| 89 | } |
| 90 | os << "<faceScope>" << item.getFaceScope() << "</faceScope>"; |
| 91 | os << "<facePersistency>" << item.getFacePersistency() << "</facePersistency>"; |
| 92 | os << "<linkType>" << item.getLinkType() << "</linkType>"; |
| 93 | |
| 94 | os << "<packetCounters>"; |
| 95 | os << "<incomingPackets>" |
| 96 | << "<nInterests>" << item.getNInInterests() << "</nInterests>" |
| 97 | << "<nDatas>" << item.getNInDatas() << "</nDatas>" |
| 98 | << "<nNacks>" << item.getNInNacks() << "</nNacks>" |
| 99 | << "</incomingPackets>"; |
| 100 | os << "<outgoingPackets>" |
| 101 | << "<nInterests>" << item.getNOutInterests() << "</nInterests>" |
| 102 | << "<nDatas>" << item.getNOutDatas() << "</nDatas>" |
| 103 | << "<nNacks>" << item.getNOutNacks() << "</nNacks>" |
| 104 | << "</outgoingPackets>"; |
| 105 | os << "</packetCounters>"; |
| 106 | |
| 107 | os << "<byteCounters>"; |
| 108 | os << "<incomingBytes>" << item.getNInBytes() << "</incomingBytes>"; |
| 109 | os << "<outgoingBytes>" << item.getNOutBytes() << "</outgoingBytes>"; |
| 110 | os << "</byteCounters>"; |
| 111 | |
| 112 | os << "</face>"; |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | FaceModule::formatStatusText(std::ostream& os) const |
| 117 | { |
| 118 | os << "Faces:\n"; |
| 119 | for (const FaceStatus& item : m_status) { |
| 120 | this->formatItemText(os, item); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | FaceModule::formatItemText(std::ostream& os, const FaceStatus& item) const |
| 126 | { |
| 127 | os << " faceid=" << item.getFaceId(); |
| 128 | os << " remote=" << item.getRemoteUri(); |
| 129 | os << " local=" << item.getLocalUri(); |
| 130 | |
| 131 | if (item.hasExpirationPeriod()) { |
| 132 | os << " expires=" << text::formatDuration(item.getExpirationPeriod()); |
| 133 | } |
| 134 | |
| 135 | os << " counters={in={" |
| 136 | << item.getNInInterests() << "i " |
| 137 | << item.getNInDatas() << "d " |
| 138 | << item.getNInNacks() << "n " |
| 139 | << item.getNInBytes() << "B} "; |
| 140 | os << "out={" |
| 141 | << item.getNOutInterests() << "i " |
| 142 | << item.getNOutDatas() << "d " |
| 143 | << item.getNOutNacks() << "n " |
| 144 | << item.getNOutBytes() << "B}}"; |
| 145 | |
| 146 | os << " " << item.getFaceScope(); |
| 147 | os << " " << item.getFacePersistency(); |
| 148 | os << " " << item.getLinkType(); |
| 149 | os << "\n"; |
| 150 | } |
| 151 | |
| 152 | } // namespace nfd_status |
| 153 | } // namespace tools |
| 154 | } // namespace nfd |