blob: 9d288556604a63eff7929a9070a4453a18e4d948 [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
29namespace nfd {
30namespace tools {
31namespace nfdc {
32
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);
43}
44
45void
46CsModule::config(ExecuteContext& ctx)
47{
48 using boost::logic::indeterminate;
49
50 auto capacity = ctx.args.getOptional<uint64_t>("capacity");
51 auto enableAdmit = ctx.args.getTribool("admit");
52 auto enableServe = ctx.args.getTribool("serve");
53
54 ControlParameters p;
55 if (capacity) {
56 p.setCapacity(*capacity);
57 }
58 if (!indeterminate(enableAdmit)) {
59 p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT, enableAdmit);
60 }
61 if (!indeterminate(enableServe)) {
62 p.setFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE, enableServe);
63 }
64
65 ctx.controller.start<ndn::nfd::CsConfigCommand>(p,
66 [&] (const ControlParameters& resp) {
67 text::ItemAttributes ia;
68 ctx.out << "cs-config-updated "
69 << ia("capacity") << resp.getCapacity()
70 << ia("admit") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_CS_ENABLE_ADMIT)}
71 << ia("serve") << text::OnOff{resp.getFlagBit(ndn::nfd::BIT_CS_ENABLE_SERVE)}
72 << '\n';
73 },
74 ctx.makeCommandFailureHandler("updating CS config"),
75 ctx.makeCommandOptions());
76
77 ctx.face.processEvents();
78}
79
80void
Junxiao Shi3160a3f2018-01-09 21:25:15 +000081CsModule::fetchStatus(Controller& controller,
82 const function<void()>& onSuccess,
83 const Controller::DatasetFailCallback& onFailure,
84 const CommandOptions& options)
85{
86 controller.fetch<ndn::nfd::CsInfoDataset>(
87 [this, onSuccess] (const CsInfo& result) {
88 m_status = result;
89 onSuccess();
90 },
91 onFailure, options);
92}
93
94void
95CsModule::formatStatusXml(std::ostream& os) const
96{
97 os << "<cs>";
98 os << "<nHits>" << m_status.getNHits() << "</nHits>";
99 os << "<nMisses>" << m_status.getNMisses() << "</nMisses>";
100 os << "</cs>";
101}
102
103void
104CsModule::formatStatusText(std::ostream& os) const
105{
106 os << "CS information:\n ";
107 text::ItemAttributes ia;
108 os << ia("nHits") << m_status.getNHits()
109 << ia("nMisses") << m_status.getNMisses()
110 << ia.end();
111 os << '\n';
112}
113
114} // namespace nfdc
115} // namespace tools
116} // namespace nfd