blob: fafd0956d68f94d5f2232ad671b0b61cbfcbf81e [file] [log] [blame]
Junxiao Shi3160a3f2018-01-09 21:25:15 +00001/* -*- 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
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)) {
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) {
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
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) {
104 text::ItemAttributes ia;
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
117void
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000118CsModule::fetchStatus(Controller& controller,
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400119 const std::function<void()>& onSuccess,
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000120 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
131void
132CsModule::formatStatusXml(std::ostream& os) const
133{
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000134 formatItemXml(os, m_status);
135}
136
137void
138CsModule::formatItemXml(std::ostream& os, const CsInfo& item)
139{
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000140 os << "<cs>";
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000141 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>";
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000147 os << "</cs>";
148}
149
150void
151CsModule::formatStatusText(std::ostream& os) const
152{
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000153 os << "CS information:\n";
154 ndn::util::IndentedStream indented(os, " ");
155 formatItemText(indented, m_status);
156}
157
158void
159CsModule::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()
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000168 << ia.end();
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000169}
170
171} // namespace nfdc
172} // namespace tools
173} // namespace nfd