cs-module.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2023, 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 "cs-module.hpp"
27 #include "format-helpers.hpp"
28 
29 #include <ndn-cxx/mgmt/nfd/status-dataset.hpp>
30 #include <ndn-cxx/util/indented-stream.hpp>
31 
32 namespace nfd::tools::nfdc {
33 
34 void
36 {
37  CommandDefinition defCsConfig("cs", "config");
38  defCsConfig
39  .setTitle("change CS configuration")
43  parser.addCommand(defCsConfig, &CsModule::config);
44 
45  CommandDefinition defCsErase("cs", "erase");
46  defCsErase
47  .setTitle("erase cached Data")
50  parser.addCommand(defCsErase, &CsModule::erase);
51 }
52 
53 void
55 {
56  using boost::logic::indeterminate;
57 
58  auto capacity = ctx.args.getOptional<uint64_t>("capacity");
59  auto enableAdmit = ctx.args.getTribool("admit");
60  auto enableServe = ctx.args.getTribool("serve");
61 
62  ControlParameters p;
63  if (capacity) {
64  p.setCapacity(*capacity);
65  }
66  if (!indeterminate(enableAdmit)) {
67  p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT, bool(enableAdmit));
68  }
69  if (!indeterminate(enableServe)) {
70  p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE, bool(enableServe));
71  }
72 
73  ctx.controller.start<ndn::nfd::CsConfigCommand>(p,
74  [&] (const ControlParameters& resp) {
76  ctx.out << "cs-config-updated "
77  << ia("capacity") << resp.getCapacity()
78  << ia("admit") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT)}
79  << ia("serve") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE)}
80  << '\n';
81  },
82  ctx.makeCommandFailureHandler("updating CS config"),
83  ctx.makeCommandOptions());
84 
85  ctx.face.processEvents();
86 }
87 
88 void
90 {
91  auto prefix = ctx.args.get<Name>("prefix");
92  auto count = ctx.args.getOptional<uint64_t>("count");
93 
94  uint64_t numErased = 0;
95  bool wasLimited = false;
96  bool wasSuccessful = true;
97 
98  ControlParameters params;
99  params.setName(prefix);
100 
101  // The cs/erase command can have a limit on the number of CS entries erased in a single operation.
102  // Therefore, we may need to run cs/erase multiple times to achieve the desired number of erases.
103  do {
104  if (count) {
105  params.setCount(*count - numErased);
106  }
107 
108  wasSuccessful = false;
109 
110  ctx.controller.start<ndn::nfd::CsEraseCommand>(
111  params,
112  [&] (const ControlParameters& resp) {
113  wasSuccessful = true;
114  numErased += resp.getCount();
115  wasLimited = resp.hasCapacity();
116  },
117  ctx.makeCommandFailureHandler("erasing cached Data"),
118  ctx.makeCommandOptions());
119 
120  ctx.face.processEvents();
121  } while (wasSuccessful && wasLimited);
122 
123  if (wasSuccessful) {
125  ctx.out << "cs-erased "
126  << ia("prefix") << prefix
127  << ia("count") << numErased
128  << '\n';
129  }
130 }
131 
132 void
133 CsModule::fetchStatus(ndn::nfd::Controller& controller,
134  const std::function<void()>& onSuccess,
135  const ndn::nfd::DatasetFailureCallback& onFailure,
136  const CommandOptions& options)
137 {
138  controller.fetch<ndn::nfd::CsInfoDataset>(
139  [this, onSuccess] (const CsInfo& result) {
140  m_status = result;
141  onSuccess();
142  },
143  onFailure, options);
144 }
145 
146 void
147 CsModule::formatStatusXml(std::ostream& os) const
148 {
149  formatItemXml(os, m_status);
150 }
151 
152 void
153 CsModule::formatItemXml(std::ostream& os, const CsInfo& item)
154 {
155  os << "<cs>";
156  os << "<capacity>" << item.getCapacity() << "</capacity>";
157  os << xml::Flag{"admitEnabled", item.getEnableAdmit()};
158  os << xml::Flag{"serveEnabled", item.getEnableServe()};
159  os << "<nEntries>" << item.getNEntries() << "</nEntries>";
160  os << "<nHits>" << item.getNHits() << "</nHits>";
161  os << "<nMisses>" << item.getNMisses() << "</nMisses>";
162  os << "</cs>";
163 }
164 
165 void
166 CsModule::formatStatusText(std::ostream& os) const
167 {
168  os << "CS information:\n";
169  ndn::util::IndentedStream indented(os, " ");
170  formatItemText(indented, m_status);
171 }
172 
173 void
174 CsModule::formatItemText(std::ostream& os, const CsInfo& item)
175 {
176  text::ItemAttributes ia(true, 8);
177  os << ia("capacity") << item.getCapacity()
178  << ia("admit") << text::OnOff{item.getEnableAdmit()}
179  << ia("serve") << text::OnOff{item.getEnableServe()}
180  << ia("nEntries") << item.getNEntries()
181  << ia("nHits") << item.getNHits()
182  << ia("nMisses") << item.getNMisses()
183  << ia.end();
184 }
185 
186 } // namespace nfd::tools::nfdc
std::optional< T > getOptional(std::string_view key) const
T get(std::string_view key, const T &defaultValue=T()) const
boost::logic::tribool getTribool(std::string_view key) const
Get an optional boolean argument as tribool.
CommandDefinition & setTitle(std::string_view title)
Set one-line description.
CommandDefinition & addArg(const std::string &name, ArgValueType valueType, Required isRequired=Required::NO, Positional allowPositional=Positional::NO, const std::string &metavar="")
Declare an argument.
CommandParser & addCommand(const CommandDefinition &def, const ExecuteCommand &execute, std::underlying_type_t< AvailableIn > modes=AVAILABLE_IN_ALL)
Add an available command.
static void erase(ExecuteContext &ctx)
The 'cs erase' command.
Definition: cs-module.cpp:89
void formatStatusText(std::ostream &os) const override
Format collected status as text.
Definition: cs-module.cpp:166
static void formatItemText(std::ostream &os, const CsInfo &item)
Definition: cs-module.cpp:174
static void formatItemXml(std::ostream &os, const CsInfo &item)
Definition: cs-module.cpp:153
static void registerCommands(CommandParser &parser)
Register 'cs config' command.
Definition: cs-module.cpp:35
void formatStatusXml(std::ostream &os) const override
Format collected status as XML.
Definition: cs-module.cpp:147
static void config(ExecuteContext &ctx)
The 'cs config' command.
Definition: cs-module.cpp:54
void fetchStatus(ndn::nfd::Controller &controller, const std::function< void()> &onSuccess, const ndn::nfd::DatasetFailureCallback &onFailure, const CommandOptions &options) override
Collect status from NFD.
Definition: cs-module.cpp:133
Context for command execution.
std::ostream & out
output stream
ndn::nfd::Controller & controller
const CommandArguments & args
ndn::nfd::CommandFailureCallback makeCommandFailureHandler(const std::string &commandName)
ndn::nfd::CommandOptions makeCommandOptions() const
Print attributes of an item.
@ YES
argument is required
@ NO
argument is optional
@ YES
argument can be specified as positional
@ NO
argument must be named
@ UNSIGNED
Non-negative integer.
Print boolean as 'on' or 'off'.
Print true as an empty element and false as nothing.