Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 1 | /* -*- 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 Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 29 | #include <ndn-cxx/util/indented-stream.hpp> |
| 30 | |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 31 | namespace nfd { |
| 32 | namespace tools { |
| 33 | namespace nfdc { |
| 34 | |
| 35 | void |
Junxiao Shi | cdf7845 | 2018-03-02 23:14:15 +0000 | [diff] [blame] | 36 | CsModule::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 | |
| 47 | void |
| 48 | CsModule::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 | |
| 82 | void |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 83 | CsModule::fetchStatus(Controller& controller, |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 84 | const std::function<void()>& onSuccess, |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 85 | 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 | |
| 96 | void |
| 97 | CsModule::formatStatusXml(std::ostream& os) const |
| 98 | { |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 99 | formatItemXml(os, m_status); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | CsModule::formatItemXml(std::ostream& os, const CsInfo& item) |
| 104 | { |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 105 | os << "<cs>"; |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 106 | 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 Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 112 | os << "</cs>"; |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | CsModule::formatStatusText(std::ostream& os) const |
| 117 | { |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 118 | os << "CS information:\n"; |
| 119 | ndn::util::IndentedStream indented(os, " "); |
| 120 | formatItemText(indented, m_status); |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | CsModule::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 Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 133 | << ia.end(); |
Junxiao Shi | 3160a3f | 2018-01-09 21:25:15 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | } // namespace nfdc |
| 137 | } // namespace tools |
| 138 | } // namespace nfd |