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-2023 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 
32 
33 namespace ndn {
34 
35 class Face;
36 
40 namespace nfd {
41 
51 using CommandSuccessCallback = std::function<void(const ControlParameters&)>;
52 
57 using CommandFailureCallback = std::function<void(const ControlResponse&)>;
58 
63 template<typename Dataset>
65  std::function<void(const std::invoke_result_t<decltype(&Dataset::parseResult), Dataset, ConstBufferPtr>&)>;
66 
71 using DatasetFailureCallback = std::function<void(uint32_t code, const std::string& reason)>;
72 
78 class Controller : noncopyable
79 {
80 public:
84  Controller(Face& face, KeyChain& keyChain,
86 
87  ~Controller();
88 
92  template<typename Command>
93  void
94  start(const ControlParameters& parameters,
95  const CommandSuccessCallback& onSuccess,
96  const CommandFailureCallback& onFailure,
97  const CommandOptions& options = {})
98  {
99  startCommand(std::make_shared<Command>(), parameters, onSuccess, onFailure, options);
100  }
101 
105  template<typename Dataset>
106  std::enable_if_t<std::is_default_constructible_v<Dataset>>
108  const DatasetFailureCallback& onFailure,
109  const CommandOptions& options = {})
110  {
111  fetchDataset(Dataset(), onSuccess, onFailure, options);
112  }
113 
117  template<typename Dataset, typename ParamType>
118  void
119  fetch(ParamType&& param,
120  const DatasetSuccessCallback<Dataset>& onSuccess,
121  const DatasetFailureCallback& onFailure,
122  const CommandOptions& options = {})
123  {
124  fetchDataset(Dataset(std::forward<ParamType>(param)), onSuccess, onFailure, options);
125  }
126 
127 private:
128  void
129  startCommand(const shared_ptr<ControlCommand>& command,
130  const ControlParameters& parameters,
131  const CommandSuccessCallback& onSuccess,
132  const CommandFailureCallback& onFailure,
133  const CommandOptions& options);
134 
135  void
136  processCommandResponse(const Data& data,
137  const shared_ptr<ControlCommand>& command,
138  const CommandSuccessCallback& onSuccess,
139  const CommandFailureCallback& onFailure);
140 
141  static void
142  processValidatedCommandResponse(const Data& data,
143  const shared_ptr<ControlCommand>& command,
144  const CommandSuccessCallback& onSuccess,
145  const CommandFailureCallback& onFailure);
146 
147  template<typename Dataset>
148  void
149  fetchDataset(Dataset&& dataset,
150  const DatasetSuccessCallback<Dataset>& onSuccess,
151  const DatasetFailureCallback& onFailure,
152  const CommandOptions& options);
153 
154  void
155  fetchDataset(const Name& prefix,
156  const std::function<void(ConstBufferPtr)>& processResponse,
157  const DatasetFailureCallback& onFailure,
158  const CommandOptions& options);
159 
160  static void
161  processDatasetFetchError(const DatasetFailureCallback& onFailure, uint32_t code, std::string msg);
162 
163 public:
164  enum : uint32_t {
170  ERROR_TIMEOUT = 10060,
172  ERROR_NACK = 10800,
175  };
176 
177 protected:
182 
184  std::set<shared_ptr<SegmentFetcher>> m_fetchers;
185 };
186 
187 template<typename Dataset>
188 void
189 Controller::fetchDataset(Dataset&& dataset,
190  const DatasetSuccessCallback<Dataset>& onSuccess,
191  const DatasetFailureCallback& onFailure,
192  const CommandOptions& options)
193 {
194  Name prefix = dataset.getDatasetPrefix(options.getPrefix());
195  fetchDataset(prefix,
196  [=, dataset = std::forward<Dataset>(dataset)] (ConstBufferPtr payload) {
197  std::invoke_result_t<decltype(&Dataset::parseResult), Dataset, ConstBufferPtr> result;
198  try {
199  result = dataset.parseResult(std::move(payload));
200  }
201  catch (const tlv::Error& e) {
202  if (onFailure)
203  onFailure(ERROR_SERVER, "Dataset decoding failure: "s + e.what());
204  return;
205  }
206  if (onSuccess)
207  onSuccess(result);
208  },
209  onFailure, options);
210 }
211 
212 } // namespace nfd
213 } // namespace ndn
214 
215 #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:45
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:79
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:30
@ ERROR_LBOUND
Inclusive lower bound of error codes.
Definition: controller.hpp:166
@ ERROR_VALIDATION
Error code for response validation failure.
Definition: controller.hpp:174
@ ERROR_SERVER
Error code for server error.
Definition: controller.hpp:168
@ ERROR_NACK
Error code for network Nack.
Definition: controller.hpp:172
@ ERROR_TIMEOUT
Error code for timeout.
Definition: controller.hpp:170
void start(const ControlParameters &parameters, const CommandSuccessCallback &onSuccess, const CommandFailureCallback &onFailure, const CommandOptions &options={})
Start command execution.
Definition: controller.hpp:94
std::enable_if_t< std::is_default_constructible_v< Dataset > > fetch(const DatasetSuccessCallback< Dataset > &onSuccess, const DatasetFailureCallback &onFailure, const CommandOptions &options={})
Start dataset fetching.
Definition: controller.hpp:107
void fetch(ParamType &&param, const DatasetSuccessCallback< Dataset > &onSuccess, const DatasetFailureCallback &onFailure, const CommandOptions &options={})
Start dataset fetching.
Definition: controller.hpp:119
security::InterestSigner m_signer
Definition: controller.hpp:181
security::Validator & m_validator
Definition: controller.hpp:180
std::set< shared_ptr< SegmentFetcher > > m_fetchers
Definition: controller.hpp:184
Helper class to create signed Interests.
The main interface for signing key management.
Definition: key-chain.hpp:87
Interface for validating data and interest packets.
Definition: validator.hpp:61
Represents an error in TLV encoding or decoding.
Definition: tlv.hpp:54
#define NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED
Definition: common.hpp:48
std::function< void(const ControlResponse &)> CommandFailureCallback
Callback on command failure.
Definition: controller.hpp:57
std::function< void(uint32_t code, const std::string &reason)> DatasetFailureCallback
Callback on dataset retrieval failure.
Definition: controller.hpp:71
std::function< void(const ControlParameters &)> CommandSuccessCallback
Callback on command success.
Definition: controller.hpp:51
std::function< void(const std::invoke_result_t< decltype(&Dataset::parseResult), Dataset, ConstBufferPtr > &)> DatasetSuccessCallback
Callback on dataset retrieval success.
Definition: controller.hpp:65
Validator & getAcceptAllValidator()
@ ControlParameters
Definition: tlv-nfd.hpp:35
Definition: data.cpp:25
std::shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:140