NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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-2017 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 
25 #include "control-command.hpp"
26 #include "control-response.hpp"
27 #include "status-dataset.hpp"
28 #include "command-options.hpp"
29 #include "../../security/command-interest-signer.hpp"
30 #include "../../security/validator-null.hpp"
31 #include "../../security/v2/key-chain.hpp"
32 #include "../../security/v2/validator.hpp"
33 
34 namespace ndn {
35 
36 class Face;
37 
38 namespace nfd {
39 
50 class Controller : noncopyable
51 {
52 public:
55  typedef function<void(const ControlParameters&)> CommandSucceedCallback;
56 
59  typedef function<void(const ControlResponse&)> CommandFailCallback;
60 
63  typedef function<void(uint32_t code, const std::string& reason)> DatasetFailCallback;
64 
69 
72  template<typename Command>
73  void
74  start(const ControlParameters& parameters,
75  const CommandSucceedCallback& onSuccess,
76  const CommandFailCallback& onFailure,
77  const CommandOptions& options = CommandOptions())
78  {
79  shared_ptr<ControlCommand> command = make_shared<Command>();
80  this->startCommand(command, parameters, onSuccess, onFailure, options);
81  }
82 
85  template<typename Dataset>
87  fetch(const std::function<void(typename Dataset::ResultType)>& onSuccess,
88  const DatasetFailCallback& onFailure,
89  const CommandOptions& options = CommandOptions())
90  {
91  this->fetchDataset(make_shared<Dataset>(), onSuccess, onFailure, options);
92  }
93 
96  template<typename Dataset, typename ParamType = typename Dataset::ParamType>
97  void
98  fetch(const ParamType& param,
99  const std::function<void(typename Dataset::ResultType)>& onSuccess,
100  const DatasetFailCallback& onFailure,
101  const CommandOptions& options = CommandOptions())
102  {
103  this->fetchDataset(make_shared<Dataset>(param), onSuccess, onFailure, options);
104  }
105 
106 private:
107  void
108  startCommand(const shared_ptr<ControlCommand>& command,
109  const ControlParameters& parameters,
110  const CommandSucceedCallback& onSuccess,
111  const CommandFailCallback& onFailure,
112  const CommandOptions& options);
113 
114  void
115  processCommandResponse(const Data& data,
116  const shared_ptr<ControlCommand>& command,
117  const CommandSucceedCallback& onSuccess,
118  const CommandFailCallback& onFailure);
119 
120  void
121  processValidatedCommandResponse(const Data& data,
122  const shared_ptr<ControlCommand>& command,
123  const CommandSucceedCallback& onSuccess,
124  const CommandFailCallback& onFailure);
125 
126  template<typename Dataset>
127  void
128  fetchDataset(shared_ptr<Dataset> dataset,
129  const std::function<void(typename Dataset::ResultType)>& onSuccess,
130  const DatasetFailCallback& onFailure,
131  const CommandOptions& options);
132 
133  void
134  fetchDataset(const Name& prefix,
135  const std::function<void(const ConstBufferPtr&)>& processResponse,
136  const DatasetFailCallback& onFailure,
137  const CommandOptions& options);
138 
139  template<typename Dataset>
140  void
141  processDatasetResponse(shared_ptr<Dataset> dataset,
142  const std::function<void(typename Dataset::ResultType)>& onSuccess,
143  const DatasetFailCallback& onFailure,
144  ConstBufferPtr payload);
145 
146  void
147  processDatasetFetchError(const DatasetFailCallback& onFailure, uint32_t code, std::string msg);
148 
149 public:
152  static const uint32_t ERROR_TIMEOUT;
153 
156  static const uint32_t ERROR_NACK;
157 
160  static const uint32_t ERROR_VALIDATION;
161 
164  static const uint32_t ERROR_SERVER;
165 
168  static const uint32_t ERROR_LBOUND;
169 
170 protected:
175 };
176 
177 template<typename Dataset>
178 inline void
179 Controller::fetchDataset(shared_ptr<Dataset> dataset,
180  const std::function<void(typename Dataset::ResultType)>& onSuccess1,
181  const DatasetFailCallback& onFailure1,
182  const CommandOptions& options)
183 {
184  const std::function<void(typename Dataset::ResultType)>& onSuccess = onSuccess1 ?
185  onSuccess1 : [] (const typename Dataset::ResultType&) {};
186  const DatasetFailCallback& onFailure = onFailure1 ?
187  onFailure1 : [] (uint32_t, const std::string&) {};
188 
189  Name prefix = dataset->getDatasetPrefix(options.getPrefix());
190  this->fetchDataset(prefix,
191  bind(&Controller::processDatasetResponse<Dataset>, this, dataset, onSuccess, onFailure, _1),
192  onFailure,
193  options);
194 }
195 
196 template<typename Dataset>
197 inline void
198 Controller::processDatasetResponse(shared_ptr<Dataset> dataset,
199  const std::function<void(typename Dataset::ResultType)>& onSuccess,
200  const DatasetFailCallback& onFailure,
201  ConstBufferPtr payload)
202 {
203  typename Dataset::ResultType result;
204  try {
205  result = dataset->parseResult(payload);
206  }
207  catch (const tlv::Error& e) {
208  onFailure(ERROR_SERVER, e.what());
209  return;
210  }
211 
212  onSuccess(result);
213 }
214 
215 } // namespace nfd
216 } // namespace ndn
217 
218 #endif // NDN_MGMT_NFD_CONTROLLER_HPP
Helper class to create command interests.
void start(const ControlParameters &parameters, const CommandSucceedCallback &onSuccess, const CommandFailCallback &onFailure, const CommandOptions &options=CommandOptions())
start command execution
Definition: controller.hpp:74
Copyright (c) 2011-2015 Regents of the University of California.
represents parameters in a ControlCommand request or response
security::CommandInterestSigner m_signer
Definition: controller.hpp:174
std::enable_if< std::is_default_constructible< Dataset >::value >::type fetch(const std::function< void(typename Dataset::ResultType)> &onSuccess, const DatasetFailCallback &onFailure, const CommandOptions &options=CommandOptions())
start dataset fetching
Definition: controller.hpp:87
function< void(const ControlResponse &)> CommandFailCallback
a callback on command failure
Definition: controller.hpp:59
const Name & getPrefix() const
static const uint32_t ERROR_TIMEOUT
error code for timeout
Definition: controller.hpp:152
function< void(const ControlParameters &)> CommandSucceedCallback
a callback on command success
Definition: controller.hpp:55
ndn Face
Definition: face-impl.hpp:41
static const uint32_t ERROR_LBOUND
inclusive lower bound of error codes
Definition: controller.hpp:168
contains options for ControlCommand execution
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
security::v2::Validator & getAcceptAllValidator()
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:95
NFD Management protocol client.
Definition: controller.hpp:50
static const uint32_t ERROR_VALIDATION
error code for response validation failure
Definition: controller.hpp:160
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:41
Represents an absolute name.
Definition: name.hpp:42
security::v2::Validator & m_validator
Definition: controller.hpp:173
Represents a Data packet.
Definition: data.hpp:35
ndn security v2 KeyChain
Definition: key-chain.cpp:76
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:98
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
Interface for validating data and interest packets.
Definition: validator.hpp:61
static const uint32_t ERROR_SERVER
error code for server error
Definition: controller.hpp:164
static const uint32_t ERROR_NACK
error code for network Nack
Definition: controller.hpp:156
function< void(uint32_t code, const std::string &reason)> DatasetFailCallback
a callback on dataset retrieval failure
Definition: controller.hpp:63
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:89