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 
47 void
49 {
50  using boost::logic::indeterminate;
51 
52  auto capacity = ctx.args.getOptional<uint64_t>("capacity");
53  auto enableAdmit = ctx.args.getTribool("admit");
54  auto enableServe = ctx.args.getTribool("serve");
55 
56  ControlParameters p;
57  if (capacity) {
58  p.setCapacity(*capacity);
59  }
60  if (!indeterminate(enableAdmit)) {
61  p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT, enableAdmit);
62  }
63  if (!indeterminate(enableServe)) {
64  p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE, enableServe);
65  }
66 
67  ctx.controller.start<ndn::nfd::CsConfigCommand>(p,
68  [&] (const ControlParameters& resp) {
70  ctx.out << "cs-config-updated "
71  << ia("capacity") << resp.getCapacity()
72  << ia("admit") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT)}
73  << ia("serve") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE)}
74  << '\n';
75  },
76  ctx.makeCommandFailureHandler("updating CS config"),
77  ctx.makeCommandOptions());
78 
79  ctx.face.processEvents();
80 }
81 
82 void
83 CsModule::fetchStatus(Controller& controller,
84  const std::function<void()>& onSuccess,
85  const Controller::DatasetFailCallback& onFailure,
86  const CommandOptions& options)
87 {
88  controller.fetch<ndn::nfd::CsInfoDataset>(
89  [this, onSuccess] (const CsInfo& result) {
90  m_status = result;
91  onSuccess();
92  },
93  onFailure, options);
94 }
95 
96 void
97 CsModule::formatStatusXml(std::ostream& os) const
98 {
99  formatItemXml(os, m_status);
100 }
101 
102 void
103 CsModule::formatItemXml(std::ostream& os, const CsInfo& item)
104 {
105  os << "<cs>";
106  os << "<capacity>" << item.getCapacity() << "</capacity>";
107  os << xml::Flag{"admitEnabled", item.getEnableAdmit()};
108  os << xml::Flag{"serveEnabled", item.getEnableServe()};
109  os << "<nEntries>" << item.getNEntries() << "</nEntries>";
110  os << "<nHits>" << item.getNHits() << "</nHits>";
111  os << "<nMisses>" << item.getNMisses() << "</nMisses>";
112  os << "</cs>";
113 }
114 
115 void
116 CsModule::formatStatusText(std::ostream& os) const
117 {
118  os << "CS information:\n";
119  ndn::util::IndentedStream indented(os, " ");
120  formatItemText(indented, m_status);
121 }
122 
123 void
124 CsModule::formatItemText(std::ostream& os, const CsInfo& item)
125 {
126  text::ItemAttributes ia(true, 8);
127  os << ia("capacity") << item.getCapacity()
128  << ia("admit") << text::OnOff{item.getEnableAdmit()}
129  << ia("serve") << text::OnOff{item.getEnableServe()}
130  << ia("nEntries") << item.getNEntries()
131  << ia("nHits") << item.getNHits()
132  << ia("nMisses") << item.getNMisses()
133  << ia.end();
134 }
135 
136 } // namespace nfdc
137 } // namespace tools
138 } // namespace nfd
const CommandArguments & args
ndn::nfd::CommandOptions makeCommandOptions() const
static void formatItemXml(std::ostream &os, const CsInfo &item)
Definition: cs-module.cpp:103
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:83
void formatStatusText(std::ostream &os) const override
format collected status as text
Definition: cs-module.cpp:116
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:48
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:97
static void formatItemText(std::ostream &os, const CsInfo &item)
Definition: cs-module.cpp:124
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
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
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;