blob: 3d3808c0399d6becb345a9f91ac040a0e047605e [file] [log] [blame]
Junxiao Shi7357ef22016-09-07 02:39:37 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 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
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
31namespace ndn {
32
33namespace security {
34class KeyChain;
35class Validator;
36} // namespace security
37class Face;
38
39namespace nfd {
40
41/**
42 * \defgroup management Management
43 * \brief Classes and data structures to manage NDN forwarder
44 */
45
46/**
47 * \ingroup management
48 * \brief NFD Management protocol client
49 * \sa https://redmine.named-data.net/projects/nfd/wiki/Management
50 */
51class Controller : noncopyable
52{
53public:
54 /** \brief a callback on command success
55 */
56 typedef function<void(const ControlParameters&)> CommandSucceedCallback;
57
58 /** \brief a callback on command failure
59 */
60 typedef function<void(const ControlResponse&)> CommandFailCallback;
61
62 /** \brief a callback on dataset retrieval failure
63 */
64 typedef function<void(uint32_t code, const std::string& reason)> DatasetFailCallback;
65
66 /** \brief construct a Controller that uses face for transport,
67 * and uses the passed KeyChain to sign commands
68 */
69 Controller(Face& face, security::KeyChain& keyChain, security::Validator& validator = s_validatorNull);
70
71 /** \brief start command execution
72 */
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
84 /** \brief start dataset fetching
85 */
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
95 /** \brief start dataset fetching
96 */
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
107private:
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
150public:
151 /** \brief error code for timeout
152 */
153 static const uint32_t ERROR_TIMEOUT;
154
155 /** \brief error code for network Nack
156 */
157 static const uint32_t ERROR_NACK;
158
159 /** \brief error code for response validation failure
160 */
161 static const uint32_t ERROR_VALIDATION;
162
163 /** \brief error code for server error
164 */
165 static const uint32_t ERROR_SERVER;
166
167 /** \brief inclusive lower bound of error codes
168 */
169 static const uint32_t ERROR_LBOUND;
170
171protected:
172 Face& m_face;
173 security::KeyChain& m_keyChain;
174 security::Validator& m_validator;
175
176private:
177 static ValidatorNull s_validatorNull;
178};
179
180template<typename Dataset>
181inline void
182Controller::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
199template<typename Dataset>
200inline void
201Controller::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