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-2022, 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/util/indented-stream.hpp>
30 
31 namespace nfd::tools::nfdc {
32 
33 void
35 {
36  CommandDefinition defCsConfig("cs", "config");
37  defCsConfig
38  .setTitle("change CS configuration")
42  parser.addCommand(defCsConfig, &CsModule::config);
43 
44  CommandDefinition defCsErase("cs", "erase");
45  defCsErase
46  .setTitle("erase cached Data")
49  parser.addCommand(defCsErase, &CsModule::erase);
50 }
51 
52 void
54 {
55  using boost::logic::indeterminate;
56 
57  auto capacity = ctx.args.getOptional<uint64_t>("capacity");
58  auto enableAdmit = ctx.args.getTribool("admit");
59  auto enableServe = ctx.args.getTribool("serve");
60 
61  ControlParameters p;
62  if (capacity) {
63  p.setCapacity(*capacity);
64  }
65  if (!indeterminate(enableAdmit)) {
66  p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT, bool(enableAdmit));
67  }
68  if (!indeterminate(enableServe)) {
69  p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE, bool(enableServe));
70  }
71 
72  ctx.controller.start<ndn::nfd::CsConfigCommand>(p,
73  [&] (const ControlParameters& resp) {
75  ctx.out << "cs-config-updated "
76  << ia("capacity") << resp.getCapacity()
77  << ia("admit") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT)}
78  << ia("serve") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE)}
79  << '\n';
80  },
81  ctx.makeCommandFailureHandler("updating CS config"),
82  ctx.makeCommandOptions());
83 
84  ctx.face.processEvents();
85 }
86 
87 void
89 {
90  auto prefix = ctx.args.get<Name>("prefix");
91  auto count = ctx.args.getOptional<uint64_t>("count");
92 
93  uint64_t numErased = 0;
94  bool wasLimited = false;
95  bool wasSuccessful = true;
96 
97  ControlParameters params;
98  params.setName(prefix);
99 
100  // The cs/erase command can have a limit on the number of CS entries erased in a single operation.
101  // Therefore, we may need to run cs/erase multiple times to achieve the desired number of erases.
102  do {
103  if (count) {
104  params.setCount(*count - numErased);
105  }
106 
107  wasSuccessful = false;
108 
109  ctx.controller.start<ndn::nfd::CsEraseCommand>(
110  params,
111  [&] (const ControlParameters& resp) {
112  wasSuccessful = true;
113  numErased += resp.getCount();
114  wasLimited = resp.hasCapacity();
115  },
116  ctx.makeCommandFailureHandler("erasing cached Data"),
117  ctx.makeCommandOptions());
118 
119  ctx.face.processEvents();
120  } while (wasSuccessful && wasLimited);
121 
122  if (wasSuccessful) {
124  ctx.out << "cs-erased "
125  << ia("prefix") << prefix
126  << ia("count") << numErased
127  << '\n';
128  }
129 }
130 
131 void
132 CsModule::fetchStatus(Controller& controller,
133  const std::function<void()>& onSuccess,
134  const Controller::DatasetFailCallback& onFailure,
135  const CommandOptions& options)
136 {
137  controller.fetch<ndn::nfd::CsInfoDataset>(
138  [this, onSuccess] (const CsInfo& result) {
139  m_status = result;
140  onSuccess();
141  },
142  onFailure, options);
143 }
144 
145 void
146 CsModule::formatStatusXml(std::ostream& os) const
147 {
148  formatItemXml(os, m_status);
149 }
150 
151 void
152 CsModule::formatItemXml(std::ostream& os, const CsInfo& item)
153 {
154  os << "<cs>";
155  os << "<capacity>" << item.getCapacity() << "</capacity>";
156  os << xml::Flag{"admitEnabled", item.getEnableAdmit()};
157  os << xml::Flag{"serveEnabled", item.getEnableServe()};
158  os << "<nEntries>" << item.getNEntries() << "</nEntries>";
159  os << "<nHits>" << item.getNHits() << "</nHits>";
160  os << "<nMisses>" << item.getNMisses() << "</nMisses>";
161  os << "</cs>";
162 }
163 
164 void
165 CsModule::formatStatusText(std::ostream& os) const
166 {
167  os << "CS information:\n";
168  ndn::util::IndentedStream indented(os, " ");
169  formatItemText(indented, m_status);
170 }
171 
172 void
173 CsModule::formatItemText(std::ostream& os, const CsInfo& item)
174 {
175  text::ItemAttributes ia(true, 8);
176  os << ia("capacity") << item.getCapacity()
177  << ia("admit") << text::OnOff{item.getEnableAdmit()}
178  << ia("serve") << text::OnOff{item.getEnableServe()}
179  << ia("nEntries") << item.getNEntries()
180  << ia("nHits") << item.getNHits()
181  << ia("nMisses") << item.getNMisses()
182  << ia.end();
183 }
184 
185 } // 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:88
void formatStatusText(std::ostream &os) const override
Format collected status as text.
Definition: cs-module.cpp:165
static void formatItemText(std::ostream &os, const CsInfo &item)
Definition: cs-module.cpp:173
static void formatItemXml(std::ostream &os, const CsInfo &item)
Definition: cs-module.cpp:152
static void registerCommands(CommandParser &parser)
Register 'cs config' command.
Definition: cs-module.cpp:34
void formatStatusXml(std::ostream &os) const override
Format collected status as XML.
Definition: cs-module.cpp:146
static void config(ExecuteContext &ctx)
The 'cs config' command.
Definition: cs-module.cpp:53
void fetchStatus(Controller &controller, const std::function< void()> &onSuccess, const Controller::DatasetFailCallback &onFailure, const CommandOptions &options) override
Collect status from NFD.
Definition: cs-module.cpp:132
Context for command execution.
std::ostream & out
output stream
const CommandArguments & args
Controller::CommandFailCallback 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.