blob: 6a3b0b5b39adde1a05e287d8e43f2b3362e52f7e [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);
45}
46
47void
48CsModule::config(ExecuteContext& ctx)
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) {
69 text::ItemAttributes ia;
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
82void
Junxiao Shi3160a3f2018-01-09 21:25:15 +000083CsModule::fetchStatus(Controller& controller,
Davide Pesavento87fc0f82018-04-11 23:43:51 -040084 const std::function<void()>& onSuccess,
Junxiao Shi3160a3f2018-01-09 21:25:15 +000085 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
96void
97CsModule::formatStatusXml(std::ostream& os) const
98{
Junxiao Shi7a36ac72018-03-21 15:23:22 +000099 formatItemXml(os, m_status);
100}
101
102void
103CsModule::formatItemXml(std::ostream& os, const CsInfo& item)
104{
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000105 os << "<cs>";
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000106 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>";
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000112 os << "</cs>";
113}
114
115void
116CsModule::formatStatusText(std::ostream& os) const
117{
Junxiao Shi7a36ac72018-03-21 15:23:22 +0000118 os << "CS information:\n";
119 ndn::util::IndentedStream indented(os, " ");
120 formatItemText(indented, m_status);
121}
122
123void
124CsModule::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()
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000133 << ia.end();
Junxiao Shi3160a3f2018-01-09 21:25:15 +0000134}
135
136} // namespace nfdc
137} // namespace tools
138} // namespace nfd