forwarder-general-module.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
27 #include "format-helpers.hpp"
28 
29 namespace nfd {
30 namespace tools {
31 namespace nfdc {
32 
34  : m_nfdIdCollector(nullptr)
35 {
36 }
37 
38 void
39 ForwarderGeneralModule::fetchStatus(Controller& controller,
40  const function<void()>& onSuccess,
41  const Controller::DatasetFailCallback& onFailure,
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
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>"
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 
109 void
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";
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 
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&
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 
188 } // namespace nfdc
189 } // namespace tools
190 } // namespace nfd
virtual void formatStatusXml(std::ostream &os) const override
format collected status as XML
void formatItemText(std::ostream &os, const ForwarderStatus &item, const Name &nfdId) const
format a single status item as text
virtual void fetchStatus(Controller &controller, const function< void()> &onSuccess, const Controller::DatasetFailCallback &onFailure, const CommandOptions &options) override
collect status from NFD
NfdIdCollector(unique_ptr< ndn::Validator > inner)
STL namespace.
std::string formatTimestamp(time::system_clock::TimePoint t)
std::string formatDuration(DURATION d)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
std::string formatTimestamp(time::system_clock::TimePoint t)
static time::system_clock::Duration calculateUptime(const ForwarderStatus &status)
std::string formatDuration(DURATION d, bool isLong=false)
void formatItemXml(std::ostream &os, const ForwarderStatus &item, const Name &nfdId) const
format a single status item as XML
virtual void formatStatusText(std::ostream &os) const override
format collected status as text
virtual void checkPolicy(const Interest &interest, int nSteps, const ndn::OnInterestValidated &accept, const ndn::OnInterestValidationFailed &reject, std::vector< shared_ptr< ndn::ValidationRequest >> &nextSteps) override