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 | f03d479 | 2017-04-06 16:41:22 +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 | |
| 26 | #include "forwarder-general-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 | ForwarderGeneralModule::ForwarderGeneralModule() |
| 34 | : m_nfdIdCollector(nullptr) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | void |
| 39 | ForwarderGeneralModule::fetchStatus(Controller& controller, |
| 40 | const function<void()>& onSuccess, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 41 | const Controller::DatasetFailCallback& onFailure, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 42 | const CommandOptions& options) |
| 43 | { |
| 44 | controller.fetch<ndn::nfd::ForwarderGeneralStatusDataset>( |
| 45 | [this, onSuccess] (const ForwarderStatus& result) { |
| 46 | m_status = result; |
| 47 | onSuccess(); |
| 48 | }, |
| 49 | onFailure, options); |
| 50 | } |
| 51 | |
| 52 | static time::system_clock::Duration |
| 53 | calculateUptime(const ForwarderStatus& status) |
| 54 | { |
| 55 | return status.getCurrentTimestamp() - status.getStartTimestamp(); |
| 56 | } |
| 57 | |
| 58 | const Name& |
| 59 | ForwarderGeneralModule::getNfdId() const |
| 60 | { |
| 61 | if (m_nfdIdCollector != nullptr && m_nfdIdCollector->hasNfdId()) { |
| 62 | return m_nfdIdCollector->getNfdId(); |
| 63 | } |
| 64 | |
| 65 | static Name unavailable("/nfdId-unavailable"); |
| 66 | return unavailable; |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | ForwarderGeneralModule::formatStatusXml(std::ostream& os) const |
| 71 | { |
| 72 | this->formatItemXml(os, m_status, this->getNfdId()); |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | ForwarderGeneralModule::formatItemXml(std::ostream& os, const ForwarderStatus& item, |
| 77 | const Name& nfdId) const |
| 78 | { |
| 79 | os << "<generalStatus>"; |
| 80 | |
| 81 | os << "<nfdId>" << nfdId << "</nfdId>"; |
| 82 | os << "<version>" << xml::Text{item.getNfdVersion()} << "</version>"; |
| 83 | os << "<startTime>" << xml::formatTimestamp(item.getStartTimestamp()) << "</startTime>"; |
| 84 | os << "<currentTime>" << xml::formatTimestamp(item.getCurrentTimestamp()) << "</currentTime>"; |
| 85 | os << "<uptime>" << xml::formatDuration(calculateUptime(item)) << "</uptime>"; |
| 86 | |
| 87 | os << "<nNameTreeEntries>" << item.getNNameTreeEntries() << "</nNameTreeEntries>"; |
| 88 | os << "<nFibEntries>" << item.getNFibEntries() << "</nFibEntries>"; |
| 89 | os << "<nPitEntries>" << item.getNPitEntries() << "</nPitEntries>"; |
| 90 | os << "<nMeasurementsEntries>" << item.getNMeasurementsEntries() << "</nMeasurementsEntries>"; |
| 91 | os << "<nCsEntries>" << item.getNCsEntries() << "</nCsEntries>"; |
| 92 | |
| 93 | os << "<packetCounters>"; |
| 94 | os << "<incomingPackets>" |
| 95 | << "<nInterests>" << item.getNInInterests() << "</nInterests>" |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame^] | 96 | << "<nData>" << item.getNInData() << "</nData>" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 97 | << "<nNacks>" << item.getNInNacks() << "</nNacks>" |
| 98 | << "</incomingPackets>"; |
| 99 | os << "<outgoingPackets>" |
| 100 | << "<nInterests>" << item.getNOutInterests() << "</nInterests>" |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame^] | 101 | << "<nData>" << item.getNOutData() << "</nData>" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 102 | << "<nNacks>" << item.getNOutNacks() << "</nNacks>" |
| 103 | << "</outgoingPackets>"; |
| 104 | os << "</packetCounters>"; |
| 105 | |
| 106 | os << "</generalStatus>"; |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | ForwarderGeneralModule::formatStatusText(std::ostream& os) const |
| 111 | { |
| 112 | os << "General NFD status:\n"; |
| 113 | this->formatItemText(os, m_status, this->getNfdId()); |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | ForwarderGeneralModule::formatItemText(std::ostream& os, const ForwarderStatus& item, |
| 118 | const Name& nfdId) const |
| 119 | { |
| 120 | os << " nfdId=" << nfdId << "\n"; |
| 121 | os << " version=" << item.getNfdVersion() << "\n"; |
| 122 | os << " startTime=" << text::formatTimestamp(item.getStartTimestamp()) << "\n"; |
| 123 | os << " currentTime=" << text::formatTimestamp(item.getCurrentTimestamp()) << "\n"; |
| 124 | os << " uptime=" << text::formatDuration(calculateUptime(item), true) << "\n"; |
| 125 | |
| 126 | os << " nNameTreeEntries=" << item.getNNameTreeEntries() << "\n"; |
| 127 | os << " nFibEntries=" << item.getNFibEntries() << "\n"; |
| 128 | os << " nPitEntries=" << item.getNPitEntries() << "\n"; |
| 129 | os << " nMeasurementsEntries=" << item.getNMeasurementsEntries() << "\n"; |
| 130 | os << " nCsEntries=" << item.getNCsEntries() << "\n"; |
| 131 | |
| 132 | os << " nInInterests=" << item.getNInInterests() << "\n"; |
| 133 | os << " nOutInterests=" << item.getNOutInterests() << "\n"; |
Junxiao Shi | f03d479 | 2017-04-06 16:41:22 +0000 | [diff] [blame^] | 134 | os << " nInData=" << item.getNInData() << "\n"; |
| 135 | os << " nOutData=" << item.getNOutData() << "\n"; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 136 | os << " nInNacks=" << item.getNInNacks() << "\n"; |
| 137 | os << " nOutNacks=" << item.getNOutNacks() << "\n"; |
| 138 | } |
| 139 | |
| 140 | NfdIdCollector::NfdIdCollector(unique_ptr<ndn::Validator> inner) |
| 141 | : m_inner(std::move(inner)) |
| 142 | , m_hasNfdId(false) |
| 143 | { |
| 144 | BOOST_ASSERT(m_inner != nullptr); |
| 145 | } |
| 146 | |
| 147 | const Name& |
| 148 | NfdIdCollector::getNfdId() const |
| 149 | { |
| 150 | if (!m_hasNfdId) { |
| 151 | BOOST_THROW_EXCEPTION(std::runtime_error("NfdId is unavailable")); |
| 152 | } |
| 153 | |
| 154 | return m_nfdId; |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | NfdIdCollector::checkPolicy(const Data& data, int nSteps, |
| 159 | const ndn::OnDataValidated& accept, |
| 160 | const ndn::OnDataValidationFailed& reject, |
| 161 | std::vector<shared_ptr<ndn::ValidationRequest>>& nextSteps) |
| 162 | { |
| 163 | ndn::OnDataValidated accepted = [this, accept] (const shared_ptr<const Data>& data) { |
| 164 | accept(data); // pass the Data to Validator user's validated callback |
| 165 | |
| 166 | if (m_hasNfdId) { |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | const ndn::Signature& sig = data->getSignature(); |
| 171 | if (!sig.hasKeyLocator()) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | const ndn::KeyLocator& kl = sig.getKeyLocator(); |
| 176 | if (kl.getType() != ndn::KeyLocator::KeyLocator_Name) { |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | m_nfdId = kl.getName(); |
| 181 | m_hasNfdId = true; |
| 182 | }; |
| 183 | |
| 184 | BOOST_ASSERT(nSteps == 0); |
| 185 | m_inner->validate(data, accepted, reject); |
| 186 | } |
| 187 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 188 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 189 | } // namespace tools |
| 190 | } // namespace nfd |