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-2020 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
void start(const ControlParameters &parameters, const CommandSucceedCallback &onSuccess, const CommandFailCallback &onFailure, const CommandOptions &options=CommandOptions())
start command execution
Definition: controller.hpp:78
Definition: data.cpp:26
The interface of signing key management.
Definition: key-chain.hpp:45
represents parameters in a ControlCommand request or response
security::CommandInterestSigner m_signer
Definition: controller.hpp:177
function< void(const ControlResponse &)> CommandFailCallback
a callback on command failure
Definition: controller.hpp:60
function< void(uint32_t code, const std::string &reason)> DatasetFailCallback
a callback on dataset retrieval failure
Definition: controller.hpp:64
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
static const uint32_t ERROR_TIMEOUT
error code for timeout
Definition: controller.hpp:155
Helper class to create signed Interests.
static const uint32_t ERROR_LBOUND
inclusive lower bound of error codes
Definition: controller.hpp:171
contains options for ControlCommand execution
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
NFD Management protocol client.
Definition: controller.hpp:51
static const uint32_t ERROR_VALIDATION
error code for response validation failure
Definition: controller.hpp:163
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
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:47
Represents an absolute name.
Definition: name.hpp:44
std::set< shared_ptr< util::SegmentFetcher > > m_fetchers
Definition: controller.hpp:180
function< void(const ControlParameters &)> CommandSucceedCallback
a callback on command success
Definition: controller.hpp:56
const Name & getPrefix() const
security::v2::Validator & m_validator
Definition: controller.hpp:176
Validator & getAcceptAllValidator()
Represents a Data packet.
Definition: data.hpp:39
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
represents an error in TLV encoding or decoding
Definition: tlv.hpp:51
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:167
static const uint32_t ERROR_NACK
error code for network Nack
Definition: controller.hpp:159
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126