blob: dc12e96b182cf7c3508513b4bd5c0e727412d6ca [file] [log] [blame]
Junxiao Shi38f4ce92016-08-04 10:01:52 +00001/* -*- 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#include "forwarder-general-module.hpp"
27#include "format-helpers.hpp"
28
29namespace nfd {
30namespace tools {
Junxiao Shi331ade72016-08-19 14:07:19 +000031namespace nfdc {
Junxiao Shi38f4ce92016-08-04 10:01:52 +000032
33ForwarderGeneralModule::ForwarderGeneralModule()
34 : m_nfdIdCollector(nullptr)
35{
36}
37
38void
39ForwarderGeneralModule::fetchStatus(Controller& controller,
40 const function<void()>& onSuccess,
Junxiao Shi29b41282016-08-22 03:47:02 +000041 const Controller::DatasetFailCallback& onFailure,
Junxiao Shi38f4ce92016-08-04 10:01:52 +000042 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
52static time::system_clock::Duration
53calculateUptime(const ForwarderStatus& status)
54{
55 return status.getCurrentTimestamp() - status.getStartTimestamp();
56}
57
58const Name&
59ForwarderGeneralModule::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
69void
70ForwarderGeneralModule::formatStatusXml(std::ostream& os) const
71{
72 this->formatItemXml(os, m_status, this->getNfdId());
73}
74
75void
76ForwarderGeneralModule::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>"
96 << "<nDatas>" << item.getNInDatas() << "</nDatas>"
97 << "<nNacks>" << item.getNInNacks() << "</nNacks>"
98 << "</incomingPackets>";
99 os << "<outgoingPackets>"
100 << "<nInterests>" << item.getNOutInterests() << "</nInterests>"
101 << "<nDatas>" << item.getNOutDatas() << "</nDatas>"
102 << "<nNacks>" << item.getNOutNacks() << "</nNacks>"
103 << "</outgoingPackets>";
104 os << "</packetCounters>";
105
106 os << "</generalStatus>";
107}
108
109void
110ForwarderGeneralModule::formatStatusText(std::ostream& os) const
111{
112 os << "General NFD status:\n";
113 this->formatItemText(os, m_status, this->getNfdId());
114}
115
116void
117ForwarderGeneralModule::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";
134 os << " nInDatas=" << item.getNInDatas() << "\n";
135 os << " nOutDatas=" << item.getNOutDatas() << "\n";
136 os << " nInNacks=" << item.getNInNacks() << "\n";
137 os << " nOutNacks=" << item.getNOutNacks() << "\n";
138}
139
140NfdIdCollector::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
147const Name&
148NfdIdCollector::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
157void
158NfdIdCollector::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 Shi331ade72016-08-19 14:07:19 +0000188} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000189} // namespace tools
190} // namespace nfd