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