NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
cs-manager.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, 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"
28 #include "table/cs.hpp"
29 
31 
32 namespace nfd {
33 
34 constexpr size_t CsManager::ERASE_LIMIT;
35 
36 CsManager::CsManager(Cs& cs, const ForwarderCounters& fwCounters,
37  Dispatcher& dispatcher, CommandAuthenticator& authenticator)
38  : ManagerBase("cs", dispatcher, authenticator)
39  , m_cs(cs)
40  , m_fwCounters(fwCounters)
41 {
42  registerCommandHandler<ndn::nfd::CsConfigCommand>("config",
43  bind(&CsManager::changeConfig, this, _4, _5));
44  registerCommandHandler<ndn::nfd::CsEraseCommand>("erase",
45  bind(&CsManager::erase, this, _4, _5));
46 
47  registerStatusDatasetHandler("info", bind(&CsManager::serveInfo, this, _1, _2, _3));
48 }
49 
50 void
51 CsManager::changeConfig(const ControlParameters& parameters,
53 {
54  using ndn::nfd::CsFlagBit;
55 
56  if (parameters.hasCapacity()) {
57  m_cs.setLimit(parameters.getCapacity());
58  }
59 
62  }
63 
66  }
67 
68  ControlParameters body;
69  body.setCapacity(m_cs.getLimit());
70  body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_ADMIT, m_cs.shouldAdmit(), false);
71  body.setFlagBit(CsFlagBit::BIT_CS_ENABLE_SERVE, m_cs.shouldServe(), false);
72  done(ControlResponse(200, "OK").setBody(body.wireEncode()));
73 }
74 
75 void
76 CsManager::erase(const ControlParameters& parameters,
78 {
79  size_t count = parameters.hasCount() ?
80  parameters.getCount() :
81  std::numeric_limits<size_t>::max();
82  m_cs.erase(parameters.getName(), std::min(count, ERASE_LIMIT),
83  [=] (size_t nErased) {
84  ControlParameters body;
85  body.setName(parameters.getName());
86  body.setCount(nErased);
87  if (nErased == ERASE_LIMIT && count > ERASE_LIMIT) {
88  m_cs.find(Interest(parameters.getName()).setCanBePrefix(true),
89  [=] (const Interest&, const Data&) mutable {
90  body.setCapacity(ERASE_LIMIT);
91  done(ControlResponse(200, "OK").setBody(body.wireEncode()));
92  },
93  [=] (const Interest&) {
94  done(ControlResponse(200, "OK").setBody(body.wireEncode()));
95  });
96  }
97  else {
98  done(ControlResponse(200, "OK").setBody(body.wireEncode()));
99  }
100  });
101 }
102 
103 void
104 CsManager::serveInfo(const Name& topPrefix, const Interest& interest,
105  ndn::mgmt::StatusDatasetContext& context) const
106 {
107  ndn::nfd::CsInfo info;
108  info.setCapacity(m_cs.getLimit());
109  info.setEnableAdmit(m_cs.shouldAdmit());
110  info.setEnableServe(m_cs.shouldServe());
111  info.setNEntries(m_cs.size());
112  info.setNHits(m_fwCounters.nCsHits);
113  info.setNMisses(m_fwCounters.nCsMisses);
114 
115  context.append(info.wireEncode());
116  context.end();
117 }
118 
119 } // namespace nfd
nfd::cs::Cs::getLimit
size_t getLimit() const
get capacity (in number of packets)
Definition: cs.hpp:103
nfd::cs::Cs::shouldAdmit
bool shouldAdmit() const
get CS_ENABLE_ADMIT flag
Definition: cs.hpp:134
nfd::cs::Cs::enableAdmit
void enableAdmit(bool shouldAdmit)
set CS_ENABLE_ADMIT flag
Definition: cs.cpp:165
ndn::mgmt::StatusDatasetContext::append
void append(const Block &block)
append a Block to the response
Definition: status-dataset-context.cpp:69
ndn::tlv::Interest
@ Interest
Definition: tlv.hpp:65
ndn::nfd::CsInfo::setNMisses
CsInfo & setNMisses(uint64_t nMisses)
Definition: cs-info.cpp:173
nfd::cs::Cs
implements the Content Store
Definition: cs.hpp:45
ndn::nfd::CsInfo::wireEncode
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Definition: cs-info.cpp:48
ndn::mgmt::CommandContinuation
std::function< void(const ControlResponse &resp)> CommandContinuation
a function to be called after ControlCommandHandler completes
Definition: dispatcher.hpp:95
ndn::nfd::ControlResponse
mgmt::ControlResponse ControlResponse
Definition: control-response.hpp:30
nfd::ManagerBase::registerStatusDatasetHandler
void registerStatusDatasetHandler(const std::string &verb, const ndn::mgmt::StatusDatasetHandler &handler)
Definition: manager-base.cpp:47
nfd::cs::Cs::enableServe
void enableServe(bool shouldServe)
set CS_ENABLE_SERVE flag
Definition: cs.cpp:175
ndn::nfd::CsInfo::setNHits
CsInfo & setNHits(uint64_t nHits)
Definition: cs-info.cpp:165
ndn::nfd::ControlParameters::hasFlagBit
bool hasFlagBit(size_t bit) const
Definition: control-parameters.cpp:244
ndn::nfd::CsInfo
represents the CS Information dataset
Definition: cs-info.hpp:38
nfd::ForwarderCounters
counters provided by Forwarder
Definition: forwarder-counters.hpp:36
ndn::nfd::CsInfo::setEnableServe
CsInfo & setEnableServe(bool enableServe)
Definition: cs-info.cpp:149
ns3::ndn::Name
Name
Definition: ndn-common.cpp:25
ndn::nfd::CsInfo::setNEntries
CsInfo & setNEntries(uint64_t nEntries)
Definition: cs-info.cpp:157
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
nfd::cs::Cs::erase
void erase(const Name &prefix, size_t limit, AfterEraseCallback &&cb)
asynchronously erases entries under prefix
Definition: cs.hpp:64
ndn::mgmt::StatusDatasetContext
provides a context for generating response to a StatusDataset request
Definition: status-dataset-context.hpp:37
ndn::tlv::nfd::ControlParameters
@ ControlParameters
Definition: tlv-nfd.hpp:35
nfd::CsManager::ERASE_LIMIT
static constexpr size_t ERASE_LIMIT
Definition: cs-manager.hpp:69
ndn::nfd::CsFlagBit
CsFlagBit
CS enablement flags.
Definition: nfd-constants.hpp:92
ndn::nfd::CsInfo::setEnableAdmit
CsInfo & setEnableAdmit(bool enableAdmit)
Definition: cs-info.cpp:141
ndn::nfd::BIT_CS_ENABLE_SERVE
@ BIT_CS_ENABLE_SERVE
enables the CS to satisfy Interests using cached Data
Definition: nfd-constants.hpp:94
nfd::CsManager::CsManager
CsManager(cs::Cs &cs, const ForwarderCounters &fwCounters, Dispatcher &dispatcher, CommandAuthenticator &authenticator)
Definition: cs-manager.cpp:36
cs.hpp
cs-info.hpp
ndn::nfd::ControlParameters::hasCapacity
bool hasCapacity() const
Definition: control-parameters.hpp:287
ndn::nfd::ControlParameters::getFlagBit
bool getFlagBit(size_t bit) const
Definition: control-parameters.cpp:258
ndn::nfd::BIT_CS_ENABLE_ADMIT
@ BIT_CS_ENABLE_ADMIT
enables the CS to admit new Data
Definition: nfd-constants.hpp:93
nfd::ManagerBase
A collection of common functions shared by all NFD managers, such as communicating with the dispatche...
Definition: manager-base.hpp:48
ndn::nfd::ControlParameters
represents parameters in a ControlCommand request or response
Definition: control-parameters.hpp:82
ndn::mgmt::Dispatcher
represents a dispatcher on server side of NFD Management protocol
Definition: dispatcher.hpp:131
nfd::cs::Cs::setLimit
void setLimit(size_t nMaxPackets)
change capacity (in number of packets)
Definition: cs.hpp:111
ndn::mgmt::StatusDatasetContext::end
void end()
end the response successfully after appending zero or more blocks
Definition: status-dataset-context.cpp:95
forwarder-counters.hpp
nfd::cs::Cs::shouldServe
bool shouldServe() const
get CS_ENABLE_SERVE flag
Definition: cs.hpp:149
ndn::nfd::ControlParameters::getCapacity
uint64_t getCapacity() const
Definition: control-parameters.hpp:293
nfd::CommandAuthenticator
Provides ControlCommand authorization according to NFD configuration file.
Definition: command-authenticator.hpp:46
cs-manager.hpp
ndn::nfd::CsInfo::setCapacity
CsInfo & setCapacity(uint64_t capacity)
Definition: cs-info.cpp:133