NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
controller.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2018 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_MGMT_NFD_CONTROLLER_HPP
23 #define NDN_MGMT_NFD_CONTROLLER_HPP
24 
34 
35 namespace ndn {
36 
37 class Face;
38 
39 namespace nfd {
40 
51 class Controller : noncopyable
52 {
53 public:
56  using CommandSucceedCallback = function<void(const ControlParameters&)>;
57 
60  using CommandFailCallback = function<void(const ControlResponse&)>;
61 
64  using DatasetFailCallback = function<void(uint32_t code, const std::string& reason)>;
65 
69  Controller(Face& face, KeyChain& keyChain,
71 
72  ~Controller();
73 
76  template<typename Command>
77  void
78  start(const ControlParameters& parameters,
79  const CommandSucceedCallback& onSuccess,
80  const CommandFailCallback& onFailure,
81  const CommandOptions& options = CommandOptions())
82  {
83  startCommand(make_shared<Command>(), parameters, onSuccess, onFailure, options);
84  }
85 
88  template<typename Dataset>
89  std::enable_if_t<std::is_default_constructible<Dataset>::value>
90  fetch(const std::function<void(typename Dataset::ResultType)>& onSuccess,
91  const DatasetFailCallback& onFailure,
92  const CommandOptions& options = CommandOptions())
93  {
94  fetchDataset(make_shared<Dataset>(), onSuccess, onFailure, options);
95  }
96 
99  template<typename Dataset, typename ParamType = typename Dataset::ParamType>
100  void
101  fetch(const ParamType& param,
102  const std::function<void(typename Dataset::ResultType)>& onSuccess,
103  const DatasetFailCallback& onFailure,
104  const CommandOptions& options = CommandOptions())
105  {
106  fetchDataset(make_shared<Dataset>(param), onSuccess, onFailure, options);
107  }
108 
109 private:
110  void
111  startCommand(const shared_ptr<ControlCommand>& command,
112  const ControlParameters& parameters,
113  const CommandSucceedCallback& onSuccess,
114  const CommandFailCallback& onFailure,
115  const CommandOptions& options);
116 
117  void
118  processCommandResponse(const Data& data,
119  const shared_ptr<ControlCommand>& command,
120  const CommandSucceedCallback& onSuccess,
121  const CommandFailCallback& onFailure);
122 
123  void
124  processValidatedCommandResponse(const Data& data,
125  const shared_ptr<ControlCommand>& command,
126  const CommandSucceedCallback& onSuccess,
127  const CommandFailCallback& onFailure);
128 
129  template<typename Dataset>
130  void
131  fetchDataset(shared_ptr<Dataset> dataset,
132  const std::function<void(typename Dataset::ResultType)>& onSuccess,
133  const DatasetFailCallback& onFailure,
134  const CommandOptions& options);
135 
136  void
137  fetchDataset(const Name& prefix,
138  const std::function<void(ConstBufferPtr)>& processResponse,
139  const DatasetFailCallback& onFailure,
140  const CommandOptions& options);
141 
142  template<typename Dataset>
143  void
144  processDatasetResponse(shared_ptr<Dataset> dataset,
145  const std::function<void(typename Dataset::ResultType)>& onSuccess,
146  const DatasetFailCallback& onFailure,
147  ConstBufferPtr payload);
148 
149  void
150  processDatasetFetchError(const DatasetFailCallback& onFailure, uint32_t code, std::string msg);
151 
152 public:
155  static const uint32_t ERROR_TIMEOUT;
156 
159  static const uint32_t ERROR_NACK;
160 
163  static const uint32_t ERROR_VALIDATION;
164 
167  static const uint32_t ERROR_SERVER;
168 
171  static const uint32_t ERROR_LBOUND;
172 
173 protected:
178 
180  std::set<shared_ptr<util::SegmentFetcher>> m_fetchers;
181 };
182 
183 template<typename Dataset>
184 void
185 Controller::fetchDataset(shared_ptr<Dataset> dataset,
186  const std::function<void(typename Dataset::ResultType)>& onSuccess,
187  const DatasetFailCallback& onFailure,
188  const CommandOptions& options)
189 {
190  Name prefix = dataset->getDatasetPrefix(options.getPrefix());
191  fetchDataset(prefix,
192  [=, d = std::move(dataset)] (ConstBufferPtr p) {
193  processDatasetResponse(std::move(d), onSuccess, onFailure, std::move(p));
194  },
195  onFailure, options);
196 }
197 
198 template<typename Dataset>
199 void
200 Controller::processDatasetResponse(shared_ptr<Dataset> dataset,
201  const std::function<void(typename Dataset::ResultType)>& onSuccess,
202  const DatasetFailCallback& onFailure,
203  ConstBufferPtr payload)
204 {
205  typename Dataset::ResultType result;
206 
207  try {
208  result = dataset->parseResult(std::move(payload));
209  }
210  catch (const tlv::Error& e) {
211  if (onFailure)
212  onFailure(ERROR_SERVER, e.what());
213  return;
214  }
215 
216  if (onSuccess)
217  onSuccess(result);
218 }
219 
220 } // namespace nfd
221 } // namespace ndn
222 
223 #endif // NDN_MGMT_NFD_CONTROLLER_HPP
segment-fetcher.hpp
ndn::security::v2::Validator
Interface for validating data and interest packets.
Definition: validator.hpp:62
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
validator.hpp
ndn::security::CommandInterestSigner
Helper class to create command interests.
Definition: command-interest-signer.hpp:67
ndn::nfd::Controller::m_keyChain
KeyChain & m_keyChain
Definition: controller.hpp:175
ndn::nfd::Controller::ERROR_SERVER
static const uint32_t ERROR_SERVER
error code for server error
Definition: controller.hpp:167
control-command.hpp
ndn::nfd::Controller::CommandSucceedCallback
function< void(const ControlParameters &)> CommandSucceedCallback
a callback on command success
Definition: controller.hpp:56
NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:47
ndn::nfd::Controller
NFD Management protocol client.
Definition: controller.hpp:52
ndn::Face
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
ndn::nfd::Controller::m_signer
security::CommandInterestSigner m_signer
Definition: controller.hpp:177
ndn::nfd::CommandOptions::getPrefix
const Name & getPrefix() const
Definition: command-options.hpp:63
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
ndn::nfd::Controller::DatasetFailCallback
function< void(uint32_t code, const std::string &reason)> DatasetFailCallback
a callback on dataset retrieval failure
Definition: controller.hpp:64
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
ndn::security::v2::getAcceptAllValidator
security::v2::Validator & getAcceptAllValidator()
Definition: validator-null.cpp:36
ndn::security::v2::KeyChain
The interface of signing key management.
Definition: key-chain.hpp:47
validator-null.hpp
control-response.hpp
status-dataset.hpp
ndn::nfd::Controller::Controller
Controller(Face &face, KeyChain &keyChain, security::v2::Validator &validator=security::getAcceptAllValidator())
construct a Controller that uses face for transport, and uses the passed KeyChain to sign commands
Definition: controller.cpp:39
ndn::nfd::Controller::start
void start(const ControlParameters &parameters, const CommandSucceedCallback &onSuccess, const CommandFailCallback &onFailure, const CommandOptions &options=CommandOptions())
start command execution
Definition: controller.hpp:78
ndn::nfd::Controller::ERROR_TIMEOUT
static const uint32_t ERROR_TIMEOUT
error code for timeout
Definition: controller.hpp:155
ndn::mgmt::ControlResponse
ControlCommand response.
Definition: control-response.hpp:33
command-interest-signer.hpp
ndn::nfd::Controller::fetch
void fetch(const ParamType &param, const std::function< void(typename Dataset::ResultType)> &onSuccess, const DatasetFailCallback &onFailure, const CommandOptions &options=CommandOptions())
start dataset fetching
Definition: controller.hpp:101
ndn::Data
Represents a Data packet.
Definition: data.hpp:36
ndn::nfd::CommandOptions
contains options for ControlCommand execution
Definition: command-options.hpp:35
Face
ndn Face
Definition: face-impl.hpp:41
ndn::nfd::ControlParameters
represents parameters in a ControlCommand request or response
Definition: control-parameters.hpp:82
key-chain.hpp
ndn::nfd::Controller::~Controller
~Controller()
Definition: controller.cpp:47
ndn::nfd::Controller::m_validator
security::v2::Validator & m_validator
Definition: controller.hpp:176
ndn::nfd::Controller::ERROR_VALIDATION
static const uint32_t ERROR_VALIDATION
error code for response validation failure
Definition: controller.hpp:163
command-options.hpp
ndn::nfd::Controller::CommandFailCallback
function< void(const ControlResponse &)> CommandFailCallback
a callback on command failure
Definition: controller.hpp:60
ndn::ConstBufferPtr
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126
ndn::nfd::Controller::ERROR_LBOUND
static const uint32_t ERROR_LBOUND
inclusive lower bound of error codes
Definition: controller.hpp:171
ndn::nfd::Controller::fetch
std::enable_if_t< std::is_default_constructible< Dataset >::value > fetch(const std::function< void(typename Dataset::ResultType)> &onSuccess, const DatasetFailCallback &onFailure, const CommandOptions &options=CommandOptions())
start dataset fetching
Definition: controller.hpp:90
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::nfd::Controller::ERROR_NACK
static const uint32_t ERROR_NACK
error code for network Nack
Definition: controller.hpp:159
ndn::nfd::Controller::m_face
Face & m_face
Definition: controller.hpp:174