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 { |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 51 | namespace nfdc { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 52 | |
| 53 | void |
| 54 | FaceModule::fetchStatus(Controller& controller, |
| 55 | const function<void()>& onSuccess, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 56 | const Controller::DatasetFailCallback& onFailure, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 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 | |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame^] | 94 | if (item.getFlags() == 0) { |
| 95 | os << "<flags/>"; |
| 96 | } |
| 97 | else { |
| 98 | os << "<flags>"; |
| 99 | if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) { |
| 100 | os << "<localFieldsEnabled/>"; |
| 101 | } |
| 102 | os << "</flags>"; |
| 103 | } |
| 104 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 105 | os << "<packetCounters>"; |
| 106 | os << "<incomingPackets>" |
| 107 | << "<nInterests>" << item.getNInInterests() << "</nInterests>" |
| 108 | << "<nDatas>" << item.getNInDatas() << "</nDatas>" |
| 109 | << "<nNacks>" << item.getNInNacks() << "</nNacks>" |
| 110 | << "</incomingPackets>"; |
| 111 | os << "<outgoingPackets>" |
| 112 | << "<nInterests>" << item.getNOutInterests() << "</nInterests>" |
| 113 | << "<nDatas>" << item.getNOutDatas() << "</nDatas>" |
| 114 | << "<nNacks>" << item.getNOutNacks() << "</nNacks>" |
| 115 | << "</outgoingPackets>"; |
| 116 | os << "</packetCounters>"; |
| 117 | |
| 118 | os << "<byteCounters>"; |
| 119 | os << "<incomingBytes>" << item.getNInBytes() << "</incomingBytes>"; |
| 120 | os << "<outgoingBytes>" << item.getNOutBytes() << "</outgoingBytes>"; |
| 121 | os << "</byteCounters>"; |
| 122 | |
| 123 | os << "</face>"; |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | FaceModule::formatStatusText(std::ostream& os) const |
| 128 | { |
| 129 | os << "Faces:\n"; |
| 130 | for (const FaceStatus& item : m_status) { |
| 131 | this->formatItemText(os, item); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | FaceModule::formatItemText(std::ostream& os, const FaceStatus& item) const |
| 137 | { |
| 138 | os << " faceid=" << item.getFaceId(); |
| 139 | os << " remote=" << item.getRemoteUri(); |
| 140 | os << " local=" << item.getLocalUri(); |
| 141 | |
| 142 | if (item.hasExpirationPeriod()) { |
| 143 | os << " expires=" << text::formatDuration(item.getExpirationPeriod()); |
| 144 | } |
| 145 | |
| 146 | os << " counters={in={" |
| 147 | << item.getNInInterests() << "i " |
| 148 | << item.getNInDatas() << "d " |
| 149 | << item.getNInNacks() << "n " |
| 150 | << item.getNInBytes() << "B} "; |
| 151 | os << "out={" |
| 152 | << item.getNOutInterests() << "i " |
| 153 | << item.getNOutDatas() << "d " |
| 154 | << item.getNOutNacks() << "n " |
| 155 | << item.getNOutBytes() << "B}}"; |
| 156 | |
| 157 | os << " " << item.getFaceScope(); |
| 158 | os << " " << item.getFacePersistency(); |
| 159 | os << " " << item.getLinkType(); |
Eric Newberry | 6d932e8 | 2016-11-24 05:05:43 +0000 | [diff] [blame^] | 160 | |
| 161 | os << " flags={"; |
| 162 | if (item.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED)) { |
| 163 | os << "local-fields"; |
| 164 | } |
| 165 | os << "}"; |
| 166 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 167 | os << "\n"; |
| 168 | } |
| 169 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 170 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 171 | } // namespace tools |
| 172 | } // namespace nfd |