ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
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-2025 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
33namespace ndn {
34
35class Face;
36
40namespace nfd {
41
51using CommandSuccessCallback = std::function<void(const ControlParameters&)>;
52
57using CommandFailureCallback = std::function<void(const ControlResponse&)>;
58
63template<typename Dataset>
65 std::function<void(const std::invoke_result_t<decltype(&Dataset::parseResult), Dataset, ConstBufferPtr>&)>;
66
71using DatasetFailureCallback = std::function<void(uint32_t code, const std::string& reason)>;
72
78class Controller : noncopyable
79{
80public:
84 Controller(Face& face, KeyChain& keyChain,
86
88
92 template<typename Command,
93 typename CommandParameters = typename Command::RequestParameters>
94 void
95 start(const CommandParameters& parameters,
96 CommandSuccessCallback onSuccess,
97 const CommandFailureCallback& onFailure,
98 const CommandOptions& options = {})
99 {
100 auto request = Command::createRequest(options.getPrefix(), parameters);
101 request.setInterestLifetime(options.getTimeout());
102 sendCommandRequest(request, options.getSigningInfo(),
103 [] (const auto& responseParams) { Command::validateResponse(responseParams); },
104 std::move(onSuccess), onFailure);
105 }
106
110 template<typename Dataset>
111 std::enable_if_t<std::is_default_constructible_v<Dataset>>
113 const DatasetFailureCallback& onFailure,
114 const CommandOptions& options = {})
115 {
116 fetchDataset(Dataset(), onSuccess, onFailure, options);
117 }
118
122 template<typename Dataset, typename ParamType>
123 void
124 fetch(ParamType&& param,
125 const DatasetSuccessCallback<Dataset>& onSuccess,
126 const DatasetFailureCallback& onFailure,
127 const CommandOptions& options = {})
128 {
129 fetchDataset(Dataset(std::forward<ParamType>(param)), onSuccess, onFailure, options);
130 }
131
132private:
133 using ResponseParametersValidator = std::function<void(const ControlParameters&)>;
134
135 void
136 sendCommandRequest(Interest& interest,
137 const security::SigningInfo& signingInfo,
138 ResponseParametersValidator checkResponse,
139 CommandSuccessCallback onSuccess,
140 const CommandFailureCallback& onFailure);
141
142 void
143 processCommandResponse(const Data& data,
144 ResponseParametersValidator checkResponse,
145 CommandSuccessCallback onSuccess,
146 const CommandFailureCallback& onFailure);
147
148 static void
149 processValidatedCommandResponse(const Data& data,
150 const ResponseParametersValidator& checkResponse,
151 const CommandSuccessCallback& onSuccess,
152 const CommandFailureCallback& onFailure);
153
154 template<typename Dataset>
155 void
156 fetchDataset(Dataset&& dataset,
157 const DatasetSuccessCallback<Dataset>& onSuccess,
158 const DatasetFailureCallback& onFailure,
159 const CommandOptions& options);
160
161 void
162 fetchDataset(const Name& prefix,
163 const std::function<void(ConstBufferPtr)>& processResponse,
164 const DatasetFailureCallback& onFailure,
165 const CommandOptions& options);
166
167 static void
168 processDatasetFetchError(const DatasetFailureCallback& onFailure, uint32_t code, std::string msg);
169
170public:
171 enum : uint32_t {
179 ERROR_NACK = 10800,
182 };
183
184protected:
189
191 std::set<shared_ptr<SegmentFetcher>> m_fetchers;
192};
193
194template<typename Dataset>
195void
196Controller::fetchDataset(Dataset&& dataset,
197 const DatasetSuccessCallback<Dataset>& onSuccess,
198 const DatasetFailureCallback& onFailure,
199 const CommandOptions& options)
200{
201 Name prefix = dataset.getDatasetPrefix(options.getPrefix());
202 fetchDataset(prefix,
203 [=, dataset = std::forward<Dataset>(dataset)] (ConstBufferPtr payload) {
204 std::invoke_result_t<decltype(&Dataset::parseResult), Dataset, ConstBufferPtr> result;
205 try {
206 result = dataset.parseResult(std::move(payload));
207 }
208 catch (const tlv::Error& e) {
209 if (onFailure)
210 onFailure(ERROR_SERVER, "Dataset decoding failure: "s + e.what());
211 return;
212 }
213 if (onSuccess)
214 onSuccess(result);
215 },
216 onFailure, options);
217}
218
219} // namespace nfd
220} // namespace ndn
221
222#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 Interest packet.
Definition interest.hpp:50
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.
std::enable_if_t< std::is_default_constructible_v< Dataset > > fetch(const DatasetSuccessCallback< Dataset > &onSuccess, const DatasetFailureCallback &onFailure, const CommandOptions &options={})
Start dataset fetching.
void start(const CommandParameters &parameters, CommandSuccessCallback onSuccess, const CommandFailureCallback &onFailure, const CommandOptions &options={})
Start command execution.
@ ERROR_LBOUND
Inclusive lower bound of error codes.
@ ERROR_VALIDATION
Error code for response validation failure.
@ ERROR_SERVER
Error code for server error.
@ ERROR_NACK
Error code for network Nack.
@ ERROR_TIMEOUT
Error code for timeout.
void fetch(ParamType &&param, const DatasetSuccessCallback< Dataset > &onSuccess, const DatasetFailureCallback &onFailure, const CommandOptions &options={})
Start dataset fetching.
security::InterestSigner m_signer
security::Validator & m_validator
std::set< shared_ptr< SegmentFetcher > > m_fetchers
Helper class to create signed Interests.
The main interface for signing key management.
Definition key-chain.hpp:87
Signing parameters passed to KeyChain.
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.
std::function< void(uint32_t code, const std::string &reason)> DatasetFailureCallback
Callback on dataset retrieval failure.
std::function< void(const ControlParameters &)> CommandSuccessCallback
Callback on command success.
std::function< void(const std::invoke_result_t< decltype(&Dataset::parseResult), Dataset, ConstBufferPtr > &)> DatasetSuccessCallback
Callback on dataset retrieval success.
Validator & getAcceptAllValidator()
Definition data.cpp:25
std::shared_ptr< const Buffer > ConstBufferPtr
Definition buffer.hpp:140