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-2022 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_CXX_MGMT_NFD_CONTROLLER_HPP
23 #define NDN_CXX_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:
57  using CommandSucceedCallback = function<void(const ControlParameters&)>;
58 
62  using CommandFailCallback = function<void(const ControlResponse&)>;
63 
67  using DatasetFailCallback = function<void(uint32_t code, const std::string& reason)>;
68 
72  Controller(Face& face, KeyChain& keyChain,
74 
75  ~Controller();
76 
79  template<typename Command>
80  void
81  start(const ControlParameters& parameters,
82  const CommandSucceedCallback& onSuccess,
83  const CommandFailCallback& onFailure,
84  const CommandOptions& options = CommandOptions())
85  {
86  startCommand(make_shared<Command>(), parameters, onSuccess, onFailure, options);
87  }
88 
91  template<typename Dataset>
92  std::enable_if_t<std::is_default_constructible<Dataset>::value>
93  fetch(const std::function<void(typename Dataset::ResultType)>& onSuccess,
94  const DatasetFailCallback& onFailure,
95  const CommandOptions& options = CommandOptions())
96  {
97  fetchDataset(make_shared<Dataset>(), onSuccess, onFailure, options);
98  }
99 
102  template<typename Dataset, typename ParamType = typename Dataset::ParamType>
103  void
104  fetch(const ParamType& param,
105  const std::function<void(typename Dataset::ResultType)>& onSuccess,
106  const DatasetFailCallback& onFailure,
107  const CommandOptions& options = CommandOptions())
108  {
109  fetchDataset(make_shared<Dataset>(param), onSuccess, onFailure, options);
110  }
111 
112 private:
113  void
114  startCommand(const shared_ptr<ControlCommand>& command,
115  const ControlParameters& parameters,
116  const CommandSucceedCallback& onSuccess,
117  const CommandFailCallback& onFailure,
118  const CommandOptions& options);
119 
120  void
121  processCommandResponse(const Data& data,
122  const shared_ptr<ControlCommand>& command,
123  const CommandSucceedCallback& onSuccess,
124  const CommandFailCallback& onFailure);
125 
126  void
127  processValidatedCommandResponse(const Data& data,
128  const shared_ptr<ControlCommand>& command,
129  const CommandSucceedCallback& onSuccess,
130  const CommandFailCallback& onFailure);
131 
132  template<typename Dataset>
133  void
134  fetchDataset(shared_ptr<Dataset> dataset,
135  const std::function<void(typename Dataset::ResultType)>& onSuccess,
136  const DatasetFailCallback& onFailure,
137  const CommandOptions& options);
138 
139  void
140  fetchDataset(const Name& prefix,
141  const std::function<void(ConstBufferPtr)>& processResponse,
142  const DatasetFailCallback& onFailure,
143  const CommandOptions& options);
144 
145  template<typename Dataset>
146  void
147  processDatasetResponse(shared_ptr<Dataset> dataset,
148  const std::function<void(typename Dataset::ResultType)>& onSuccess,
149  const DatasetFailCallback& onFailure,
150  ConstBufferPtr payload);
151 
152  void
153  processDatasetFetchError(const DatasetFailCallback& onFailure, uint32_t code, std::string msg);
154 
155 public:
157  static const uint32_t ERROR_TIMEOUT;
158 
160  static const uint32_t ERROR_NACK;
161 
163  static const uint32_t ERROR_VALIDATION;
164 
166  static const uint32_t ERROR_SERVER;
167 
169  static const uint32_t ERROR_LBOUND;
170 
171 protected:
173  KeyChain& m_keyChain;
176 
178  std::set<shared_ptr<util::SegmentFetcher>> m_fetchers;
179 };
180 
181 template<typename Dataset>
182 void
183 Controller::fetchDataset(shared_ptr<Dataset> dataset,
184  const std::function<void(typename Dataset::ResultType)>& onSuccess,
185  const DatasetFailCallback& onFailure,
186  const CommandOptions& options)
187 {
188  Name prefix = dataset->getDatasetPrefix(options.getPrefix());
189  fetchDataset(prefix,
190  [=, d = std::move(dataset)] (ConstBufferPtr p) {
191  processDatasetResponse(std::move(d), onSuccess, onFailure, std::move(p));
192  },
193  onFailure, options);
194 }
195 
196 template<typename Dataset>
197 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 
205  try {
206  result = dataset->parseResult(std::move(payload));
207  }
208  catch (const tlv::Error& e) {
209  if (onFailure)
210  onFailure(ERROR_SERVER, e.what());
211  return;
212  }
213 
214  if (onSuccess)
215  onSuccess(result);
216 }
217 
218 } // namespace nfd
219 } // namespace ndn
220 
221 #endif // NDN_CXX_MGMT_NFD_CONTROLLER_HPP
Represents a Data packet.
Definition: data.hpp:39
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:91
Represents an absolute name.
Definition: name.hpp:44
ControlCommand response.
Contains options for ControlCommand execution.
const Name & getPrefix() const
Returns the command prefix.
Represents parameters in a ControlCommand request or response.
NFD Management protocol client.
Definition: controller.hpp:52
static const uint32_t ERROR_TIMEOUT
Error code for timeout.
Definition: controller.hpp:157
static const uint32_t ERROR_SERVER
Error code for server error.
Definition: controller.hpp:166
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:104
static const uint32_t ERROR_LBOUND
Inclusive lower bound of error codes.
Definition: controller.hpp:169
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:93
Controller(Face &face, KeyChain &keyChain, security::Validator &validator=security::getAcceptAllValidator())
Construct a Controller that uses face as transport and keyChain to sign commands.
Definition: controller.cpp:39
static const uint32_t ERROR_VALIDATION
Error code for response validation failure.
Definition: controller.hpp:163
void start(const ControlParameters &parameters, const CommandSucceedCallback &onSuccess, const CommandFailCallback &onFailure, const CommandOptions &options=CommandOptions())
Start command execution.
Definition: controller.hpp:81
security::InterestSigner m_signer
Definition: controller.hpp:175
function< void(const ControlResponse &)> CommandFailCallback
Callback on command failure.
Definition: controller.hpp:62
static const uint32_t ERROR_NACK
Error code for network Nack.
Definition: controller.hpp:160
std::set< shared_ptr< util::SegmentFetcher > > m_fetchers
Definition: controller.hpp:178
security::Validator & m_validator
Definition: controller.hpp:174
function< void(uint32_t code, const std::string &reason)> DatasetFailCallback
Callback on dataset retrieval failure.
Definition: controller.hpp:67
function< void(const ControlParameters &)> CommandSucceedCallback
Callback on command success.
Definition: controller.hpp:57
Helper class to create signed Interests.
Interface for validating data and interest packets.
Definition: validator.hpp:62
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:47
Validator & getAcceptAllValidator()
Definition: data.cpp:25
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139