blob: 5c6c45050220e47aee82419f833503f079275c15 [file] [log] [blame]
Junxiao Shif2bfb442018-01-05 12:34:57 +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-manager.hpp"
27#include <ndn-cxx/mgmt/nfd/cs-info.hpp>
28
29namespace nfd {
30
31CsManager::CsManager(Cs& cs, const ForwarderCounters& fwCnt,
32 Dispatcher& dispatcher, CommandAuthenticator& authenticator)
33 : NfdManagerBase(dispatcher, authenticator, "cs")
Junxiao Shic9b5e012018-02-07 15:04:18 +000034 , m_cs(cs)
Junxiao Shif2bfb442018-01-05 12:34:57 +000035 , m_fwCnt(fwCnt)
36{
Junxiao Shic9b5e012018-02-07 15:04:18 +000037 registerCommandHandler<ndn::nfd::CsConfigCommand>("config",
38 bind(&CsManager::changeConfig, this, _4, _5));
39
Junxiao Shif2bfb442018-01-05 12:34:57 +000040 registerStatusDatasetHandler("info", bind(&CsManager::serveInfo, this, _1, _2, _3));
41}
42
43void
Junxiao Shic9b5e012018-02-07 15:04:18 +000044CsManager::changeConfig(const ControlParameters& parameters,
45 const ndn::mgmt::CommandContinuation& done)
46{
47 using ndn::nfd::CsFlagBit;
48
49 if (parameters.hasCapacity()) {
50 m_cs.setLimit(parameters.getCapacity());
51 }
52
53 if (parameters.hasFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT)) {
54 m_cs.enableAdmit(parameters.getFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT));
55 }
56
57 if (parameters.hasFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE)) {
58 m_cs.enableServe(parameters.getFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE));
59 }
60
61 ControlParameters body;
62 body.setCapacity(m_cs.getLimit());
63 body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT, m_cs.shouldAdmit(), false);
64 body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE, m_cs.shouldServe(), false);
65 done(ControlResponse(200, "OK").setBody(body.wireEncode()));
66}
67
68void
Junxiao Shif2bfb442018-01-05 12:34:57 +000069CsManager::serveInfo(const Name& topPrefix, const Interest& interest,
70 ndn::mgmt::StatusDatasetContext& context) const
71{
72 ndn::nfd::CsInfo info;
73 info.setNHits(m_fwCnt.nCsHits);
74 info.setNMisses(m_fwCnt.nCsMisses);
75
76 context.append(info.wireEncode());
77 context.end();
78}
79
80} // namespace nfd