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-2018, 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, enableAdmit);
69  }
70  if (!indeterminate(enableServe)) {
71  p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE, 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  ControlParameters params;
96  params.setName(prefix);
97  if (count) {
98  params.setCount(*count);
99  }
100 
101  ctx.controller.start<ndn::nfd::CsEraseCommand>(
102  params,
103  [&] (const ControlParameters& resp) {
105  ctx.out << "cs-erased "
106  << ia("prefix") << resp.getName()
107  << ia("count") << resp.getCount()
108  << ia("has-more") << text::YesNo{resp.hasCapacity()}
109  << '\n';
110  },
111  ctx.makeCommandFailureHandler("erasing cached Data"),
112  ctx.makeCommandOptions());
113 
114  ctx.face.processEvents();
115 }
116 
117 void
118 CsModule::fetchStatus(Controller& controller,
119  const std::function<void()>& onSuccess,
120  const Controller::DatasetFailCallback& onFailure,
121  const CommandOptions& options)
122 {
123  controller.fetch<ndn::nfd::CsInfoDataset>(
124  [this, onSuccess] (const CsInfo& result) {
125  m_status = result;
126  onSuccess();
127  },
128  onFailure, options);
129 }
130 
131 void
132 CsModule::formatStatusXml(std::ostream& os) const
133 {
134  formatItemXml(os, m_status);
135 }
136 
137 void
138 CsModule::formatItemXml(std::ostream& os, const CsInfo& item)
139 {
140  os << "<cs>";
141  os << "<capacity>" << item.getCapacity() << "</capacity>";
142  os << xml::Flag{"admitEnabled", item.getEnableAdmit()};
143  os << xml::Flag{"serveEnabled", item.getEnableServe()};
144  os << "<nEntries>" << item.getNEntries() << "</nEntries>";
145  os << "<nHits>" << item.getNHits() << "</nHits>";
146  os << "<nMisses>" << item.getNMisses() << "</nMisses>";
147  os << "</cs>";
148 }
149 
150 void
151 CsModule::formatStatusText(std::ostream& os) const
152 {
153  os << "CS information:\n";
154  ndn::util::IndentedStream indented(os, " ");
155  formatItemText(indented, m_status);
156 }
157 
158 void
159 CsModule::formatItemText(std::ostream& os, const CsInfo& item)
160 {
161  text::ItemAttributes ia(true, 8);
162  os << ia("capacity") << item.getCapacity()
163  << ia("admit") << text::OnOff{item.getEnableAdmit()}
164  << ia("serve") << text::OnOff{item.getEnableServe()}
165  << ia("nEntries") << item.getNEntries()
166  << ia("nHits") << item.getNHits()
167  << ia("nMisses") << item.getNMisses()
168  << ia.end();
169 }
170 
171 } // namespace nfdc
172 } // namespace tools
173 } // namespace nfd
const CommandArguments & args
ndn::nfd::CommandOptions makeCommandOptions() const
static void formatItemXml(std::ostream &os, const CsInfo &item)
Definition: cs-module.cpp:138
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:118
void formatStatusText(std::ostream &os) const override
format collected status as text
Definition: cs-module.cpp:151
T get(const std::string &key, const T &defaultValue=T()) const
CommandDefinition & setTitle(const std::string &title)
set one-line description
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
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:132
static void formatItemText(std::ostream &os, const CsInfo &item)
Definition: cs-module.cpp:159
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
optional< T > getOptional(const std::string &key) const
CommandParser & addCommand(const CommandDefinition &def, const ExecuteCommand &execute, std::underlying_type< AvailableIn >::type modes=AVAILABLE_IN_ALL)
add an available command
boost::logic::tribool getTribool(const std::string &key) const
get an optional boolean argument as tribool
argument is optional
print boolean as &#39;yes&#39; or &#39;no&#39;
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;