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