blob: cdaad5a34af259aa9cab554a7181277940bcaf12 [file] [log] [blame]
Junxiao Shi3160a3f2018-01-09 21:25:15 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Eric Newberry4995bf92020-04-18 16:59:19 -07003 * Copyright (c) 2014-2020, Regents of the University of California,
Junxiao Shi3160a3f2018-01-09 21:25:15 +00004 * 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
Junxiao Shi7a36ac72018-03-21 15:23:22 +000029#include <ndn-cxx/util/indented-stream.hpp>
30
Junxiao Shi3160a3f2018-01-09 21:25:15 +000031namespace nfd {
32namespace tools {
33namespace nfdc {
34
35void
Junxiao Shicdf78452018-03-02 23:14:15 +000036CsModule::registerCommands(CommandParser& parser)
37{
38 CommandDefinition defCsConfig("cs", "config");
39 defCsConfig
40 .setTitle("change CS configuration")
41 .addArg("capacity", ArgValueType::UNSIGNED, Required::NO, Positional::NO)
42 .addArg("admit", ArgValueType::BOOLEAN, Required::NO, Positional::NO)
43 .addArg("serve", ArgValueType::BOOLEAN, Required::NO, Positional::NO);
44 parser.addCommand(defCsConfig, &CsModule::config);
Junxiao Shia4d7fe02018-07-20 06:51:41 -060045
46 CommandDefinition defCsErase("cs", "erase");
47 defCsErase
48 .setTitle("erase cached Data")
49 .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES)
50 .addArg("count", ArgValueType::UNSIGNED, Required::NO, Positional::NO);
51 parser.addCommand(defCsErase, &CsModule::erase);
Junxiao Shicdf78452018-03-02 23:14:15 +000052}
53
54void
55CsModule::config(ExecuteContext& ctx)
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)) {
Davide Pesaventofa2aa502019-03-22 13:30:02 -040068 p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT, bool(enableAdmit));
Junxiao Shicdf78452018-03-02 23:14:15 +000069 }
70 if (!indeterminate(enableServe)) {
Davide Pesaventofa2aa502019-03-22 13:30:02 -040071 p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE, bool(enableServe));
Junxiao Shicdf78452018-03-02 23:14:15 +000072 }
73
74 ctx.controller.start<ndn::nfd::CsConfigCommand>(p,
75 [&] (const ControlParameters& resp) {
76 text::ItemAttributes ia;
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
89void
Junxiao Shia4d7fe02018-07-20 06:51:41 -060090CsModule::erase(ExecuteContext& ctx)
91{
92 auto prefix = ctx.args.get<Name>("prefix");
93 auto count = ctx.args.getOptional<uint64_t>("count");
94
Eric Newberry4995bf92020-04-18 16:59:19 -070095 uint64_t numErased = 0;
96 bool wasLimited = false;
97 bool wasSuccessful = true;
98
Junxiao Shia4d7fe02018-07-20 06:51:41 -060099 ControlParameters params;
100 params.setName(prefix);
Eric Newberry4995bf92020-04-18 16:59:19 -0700101
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) {
125 text::ItemAttributes ia;
126 ctx.out << "cs-erased "
127 << ia("prefix") << prefix
128 << ia("count") << numErased
129 << '\n';
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600130 }
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600131}
132
133void
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000134CsModule::fetchStatus(Controller& controller,
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400135 const std::function<void()>& onSuccess,
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000136 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
147void
148CsModule::formatStatusXml(std::ostream& os) const
149{
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000150 formatItemXml(os, m_status);
151}
152
153void
154CsModule::formatItemXml(std::ostream& os, const CsInfo& item)
155{
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000156 os << "<cs>";
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000157 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>";
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000163 os << "</cs>";
164}
165
166void
167CsModule::formatStatusText(std::ostream& os) const
168{
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000169 os << "CS information:\n";
170 ndn::util::IndentedStream indented(os, " ");
171 formatItemText(indented, m_status);
172}
173
174void
175CsModule::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()
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000184 << ia.end();
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000185}
186
187} // namespace nfdc
188} // namespace tools
189} // namespace nfd