blob: 73d066c437ebc1e62679768ab0ef2f76a9701ab9 [file] [log] [blame]
Junxiao Shi3160a3f2018-01-09 21:25:15 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::tools::nfdc {
Junxiao Shi3160a3f2018-01-09 21:25:15 +000032
33void
Junxiao Shicdf78452018-03-02 23:14:15 +000034CsModule::registerCommands(CommandParser& parser)
35{
36 CommandDefinition defCsConfig("cs", "config");
37 defCsConfig
38 .setTitle("change CS configuration")
39 .addArg("capacity", ArgValueType::UNSIGNED, Required::NO, Positional::NO)
40 .addArg("admit", ArgValueType::BOOLEAN, Required::NO, Positional::NO)
41 .addArg("serve", ArgValueType::BOOLEAN, Required::NO, Positional::NO);
42 parser.addCommand(defCsConfig, &CsModule::config);
Junxiao Shia4d7fe02018-07-20 06:51:41 -060043
44 CommandDefinition defCsErase("cs", "erase");
45 defCsErase
46 .setTitle("erase cached Data")
47 .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES)
48 .addArg("count", ArgValueType::UNSIGNED, Required::NO, Positional::NO);
49 parser.addCommand(defCsErase, &CsModule::erase);
Junxiao Shicdf78452018-03-02 23:14:15 +000050}
51
52void
53CsModule::config(ExecuteContext& ctx)
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)) {
Davide Pesaventofa2aa502019-03-22 13:30:02 -040066 p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT, bool(enableAdmit));
Junxiao Shicdf78452018-03-02 23:14:15 +000067 }
68 if (!indeterminate(enableServe)) {
Davide Pesaventofa2aa502019-03-22 13:30:02 -040069 p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE, bool(enableServe));
Junxiao Shicdf78452018-03-02 23:14:15 +000070 }
71
72 ctx.controller.start<ndn::nfd::CsConfigCommand>(p,
73 [&] (const ControlParameters& resp) {
74 text::ItemAttributes ia;
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
87void
Junxiao Shia4d7fe02018-07-20 06:51:41 -060088CsModule::erase(ExecuteContext& ctx)
89{
90 auto prefix = ctx.args.get<Name>("prefix");
91 auto count = ctx.args.getOptional<uint64_t>("count");
92
Eric Newberry4995bf92020-04-18 16:59:19 -070093 uint64_t numErased = 0;
94 bool wasLimited = false;
95 bool wasSuccessful = true;
96
Junxiao Shia4d7fe02018-07-20 06:51:41 -060097 ControlParameters params;
98 params.setName(prefix);
Eric Newberry4995bf92020-04-18 16:59:19 -070099
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) {
123 text::ItemAttributes ia;
124 ctx.out << "cs-erased "
125 << ia("prefix") << prefix
126 << ia("count") << numErased
127 << '\n';
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600128 }
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600129}
130
131void
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000132CsModule::fetchStatus(Controller& controller,
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400133 const std::function<void()>& onSuccess,
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000134 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
145void
146CsModule::formatStatusXml(std::ostream& os) const
147{
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000148 formatItemXml(os, m_status);
149}
150
151void
152CsModule::formatItemXml(std::ostream& os, const CsInfo& item)
153{
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000154 os << "<cs>";
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000155 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>";
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000161 os << "</cs>";
162}
163
164void
165CsModule::formatStatusText(std::ostream& os) const
166{
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000167 os << "CS information:\n";
168 ndn::util::IndentedStream indented(os, " ");
169 formatItemText(indented, m_status);
170}
171
172void
173CsModule::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()
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000182 << ia.end();
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000183}
184
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400185} // namespace nfd::tools::nfdc