blob: a7d1290d84d4e1e6425c3ba9ef9beffa1019ab3a [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyeve289b532014-02-09 22:14:44 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyeve289b532014-02-09 22:14:44 -080020 */
21
Alexander Afanasyev26c24d22014-03-20 09:31:21 -070022#ifndef NDN_MANAGEMENT_NFD_CONTROLLER_HPP
23#define NDN_MANAGEMENT_NFD_CONTROLLER_HPP
Alexander Afanasyeve289b532014-02-09 22:14:44 -080024
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070025#include "nfd-control-command.hpp"
26#include "../face.hpp"
Junxiao Shi70911652014-08-12 10:14:24 -070027#include "../security/key-chain.hpp"
Junxiao Shi5de006b2014-10-26 20:20:52 -070028#include "nfd-command-options.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080029
30namespace ndn {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080031namespace nfd {
32
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040033/**
Alexander Afanasyev197e5652014-06-13 16:56:31 -070034 * \defgroup management Management
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040035 * \brief Classes and data structures to manage NDN forwarder
36 */
37/**
38 * \ingroup management
39 * \brief NFD Management protocol - ControlCommand client
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070040 */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070041class Controller : noncopyable
Alexander Afanasyeve289b532014-02-09 22:14:44 -080042{
43public:
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070044 /** \brief a callback on command success
Alexander Afanasyeve289b532014-02-09 22:14:44 -080045 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070046 typedef function<void(const ControlParameters&)> CommandSucceedCallback;
47
48 /** \brief a callback on command failure
49 */
50 typedef function<void(uint32_t/*code*/,const std::string&/*reason*/)> CommandFailCallback;
51
Junxiao Shi70911652014-08-12 10:14:24 -070052 /** \brief a function to sign the request Interest
Junxiao Shi5de006b2014-10-26 20:20:52 -070053 * \deprecated arbitrary signing function is no longer supported
Yingdi Yue66bf2a2014-04-28 17:07:36 -070054 */
55 typedef function<void(Interest&)> Sign;
56
Junxiao Shi70911652014-08-12 10:14:24 -070057 /** \brief construct a Controller that uses face for transport,
58 * and has an internal default KeyChain to sign commands
Junxiao Shiedd834e2014-10-28 20:28:58 -070059 * \deprecated use two-parameter overload
Junxiao Shi70911652014-08-12 10:14:24 -070060 */
Junxiao Shi415b17c2014-11-12 00:43:25 -070061 DEPRECATED(
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070062 explicit
Junxiao Shi415b17c2014-11-12 00:43:25 -070063 Controller(Face& face));
Alexander Afanasyeve289b532014-02-09 22:14:44 -080064
Junxiao Shi70911652014-08-12 10:14:24 -070065 /** \brief construct a Controller that uses face for transport,
66 * and uses the passed KeyChain to sign commands
67 */
68 Controller(Face& face, KeyChain& keyChain);
69
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070070 /** \brief start command execution
71 */
72 template<typename Command>
73 void
74 start(const ControlParameters& parameters,
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070075 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -070076 const CommandFailCallback& onFailure,
Junxiao Shi5de006b2014-10-26 20:20:52 -070077 const CommandOptions& options = CommandOptions())
Yingdi Yue66bf2a2014-04-28 17:07:36 -070078 {
Junxiao Shi5de006b2014-10-26 20:20:52 -070079 shared_ptr<ControlCommand> command = make_shared<Command>();
80 this->startCommand(command, parameters, onSuccess, onFailure, options);
81 }
82
83 /** \brief start command execution
84 * \deprecated use the overload taking CommandOptions
85 */
Junxiao Shi415b17c2014-11-12 00:43:25 -070086 DEPRECATED(
Junxiao Shi5de006b2014-10-26 20:20:52 -070087 template<typename Command>
88 void
89 start(const ControlParameters& parameters,
90 const CommandSucceedCallback& onSuccess,
91 const CommandFailCallback& onFailure,
Junxiao Shi415b17c2014-11-12 00:43:25 -070092 const time::milliseconds& timeout));
Yingdi Yue66bf2a2014-04-28 17:07:36 -070093
Junxiao Shi70911652014-08-12 10:14:24 -070094 /** \brief start command execution
Alexander Afanasyev7e6fefc2014-10-20 12:31:55 -040095 * \param certificate the certificate used to sign request Interests.
96 * If IdentityCertificate() is passed, the default signing certificate will be used.
97 *
98 * \note IdentityCertificate() creates a certificate with an empty name, which is an
99 * invalid certificate. A valid IdentityCertificate has at least 4 name components,
100 * as it follows `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model.
Junxiao Shi5de006b2014-10-26 20:20:52 -0700101 *
102 * \deprecated use the overload taking CommandOptions
Junxiao Shi70911652014-08-12 10:14:24 -0700103 */
Junxiao Shi415b17c2014-11-12 00:43:25 -0700104 DEPRECATED(
Junxiao Shi70911652014-08-12 10:14:24 -0700105 template<typename Command>
106 void
107 start(const ControlParameters& parameters,
108 const CommandSucceedCallback& onSuccess,
109 const CommandFailCallback& onFailure,
110 const IdentityCertificate& certificate,
Junxiao Shi415b17c2014-11-12 00:43:25 -0700111 const time::milliseconds& timeout = CommandOptions::DEFAULT_TIMEOUT));
Junxiao Shi70911652014-08-12 10:14:24 -0700112
113 /** \brief start command execution
114 * \param identity the identity used to sign request Interests
Junxiao Shi5de006b2014-10-26 20:20:52 -0700115 * \deprecated use the overload taking CommandOptions
Junxiao Shi70911652014-08-12 10:14:24 -0700116 */
Junxiao Shi415b17c2014-11-12 00:43:25 -0700117 DEPRECATED(
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700118 template<typename Command>
119 void
120 start(const ControlParameters& parameters,
121 const CommandSucceedCallback& onSuccess,
122 const CommandFailCallback& onFailure,
123 const Name& identity,
Junxiao Shi415b17c2014-11-12 00:43:25 -0700124 const time::milliseconds& timeout = CommandOptions::DEFAULT_TIMEOUT));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700125
Junxiao Shi70911652014-08-12 10:14:24 -0700126 /** \brief start command execution
127 * \param sign a function to sign request Interests
Junxiao Shi5de006b2014-10-26 20:20:52 -0700128 * \deprecated arbitrary signing function is no longer supported
Junxiao Shi70911652014-08-12 10:14:24 -0700129 */
Junxiao Shi415b17c2014-11-12 00:43:25 -0700130 DEPRECATED(
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700131 template<typename Command>
132 void
133 start(const ControlParameters& parameters,
134 const CommandSucceedCallback& onSuccess,
135 const CommandFailCallback& onFailure,
136 const Sign& sign,
Junxiao Shi415b17c2014-11-12 00:43:25 -0700137 const time::milliseconds& timeout = CommandOptions::DEFAULT_TIMEOUT));
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700138
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800139private:
140 void
Junxiao Shi70911652014-08-12 10:14:24 -0700141 startCommand(const shared_ptr<ControlCommand>& command,
142 const ControlParameters& parameters,
143 const CommandSucceedCallback& onSuccess,
144 const CommandFailCallback& onFailure,
Junxiao Shi5de006b2014-10-26 20:20:52 -0700145 const CommandOptions& options);
146
147 /** \deprecated This is to support arbitrary signing function.
148 */
149 void
150 startCommand(const shared_ptr<ControlCommand>& command,
151 const ControlParameters& parameters,
152 const CommandSucceedCallback& onSuccess,
153 const CommandFailCallback& onFailure,
Junxiao Shi70911652014-08-12 10:14:24 -0700154 const Sign& sign,
155 const time::milliseconds& timeout);
156
157 void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700158 processCommandResponse(const Data& data,
159 const shared_ptr<ControlCommand>& command,
160 const CommandSucceedCallback& onSuccess,
161 const CommandFailCallback& onFailure);
hilata7d160f22014-03-13 19:51:42 -0500162
Junxiao Shi5c785d62014-04-20 18:10:20 -0700163public:
Junxiao Shi5de006b2014-10-26 20:20:52 -0700164 /** \deprecated use CommandOptions::DEFAULT_TIMEOUT
165 */
Junxiao Shi415b17c2014-11-12 00:43:25 -0700166 DEPRECATED(
Junxiao Shi5c785d62014-04-20 18:10:20 -0700167 static time::milliseconds
Junxiao Shi415b17c2014-11-12 00:43:25 -0700168 getDefaultCommandTimeout());
Junxiao Shi5c785d62014-04-20 18:10:20 -0700169
Junxiao Shi5de006b2014-10-26 20:20:52 -0700170public:
171 /** \brief error code for timeout
172 * \note comes from http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668.aspx
173 */
174 static const uint32_t ERROR_TIMEOUT;
175
176 /** \brief error code for server error
177 */
178 static const uint32_t ERROR_SERVER;
179
180 /** \brief inclusive lower bound of error codes
181 */
182 static const uint32_t ERROR_LBOUND;
183
hilataa99e37e2014-02-15 23:52:46 -0600184protected:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800185 Face& m_face;
Junxiao Shiedd834e2014-10-28 20:28:58 -0700186
187 /** \deprecated
188 */
Junxiao Shi70911652014-08-12 10:14:24 -0700189 shared_ptr<KeyChain> m_internalKeyChain;
Junxiao Shiedd834e2014-10-28 20:28:58 -0700190
Junxiao Shi70911652014-08-12 10:14:24 -0700191 KeyChain& m_keyChain;
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800192};
193
Junxiao Shi415b17c2014-11-12 00:43:25 -0700194
195// deprecated methods
196
197template<typename Command>
198inline void
199Controller::start(const ControlParameters& parameters,
200 const CommandSucceedCallback& onSuccess,
201 const CommandFailCallback& onFailure,
202 const time::milliseconds& timeout)
203{
204 CommandOptions options;
205 options.setTimeout(timeout);
206
207 this->start<Command>(parameters, onSuccess, onFailure, options);
208}
209
210template<typename Command>
211inline void
212Controller::start(const ControlParameters& parameters,
213 const CommandSucceedCallback& onSuccess,
214 const CommandFailCallback& onFailure,
215 const IdentityCertificate& certificate,
216 const time::milliseconds& timeout)
217{
218 CommandOptions options;
219 if (certificate.getName().empty()) {
220 options.setSigningDefault();
221 }
222 else {
223 options.setSigningCertificate(certificate);
224 }
225 options.setTimeout(timeout);
226
227 this->start<Command>(parameters, onSuccess, onFailure, options);
228}
229
230template<typename Command>
231inline void
232Controller::start(const ControlParameters& parameters,
233 const CommandSucceedCallback& onSuccess,
234 const CommandFailCallback& onFailure,
235 const Name& identity,
236 const time::milliseconds& timeout)
237{
238 CommandOptions options;
239 options.setSigningIdentity(identity);
240 options.setTimeout(timeout);
241
242 this->start<Command>(parameters, onSuccess, onFailure, options);
243}
244
245template<typename Command>
246inline void
247Controller::start(const ControlParameters& parameters,
248 const CommandSucceedCallback& onSuccess,
249 const CommandFailCallback& onFailure,
250 const Sign& sign,
251 const time::milliseconds& timeout)
252{
253 shared_ptr<ControlCommand> command = make_shared<Command>();
254 this->startCommand(command, parameters, onSuccess, onFailure, sign, timeout);
255}
256
257inline time::milliseconds
258Controller::getDefaultCommandTimeout()
259{
260 return CommandOptions::DEFAULT_TIMEOUT;
261}
262
263
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800264} // namespace nfd
265} // namespace ndn
266
Alexander Afanasyev26c24d22014-03-20 09:31:21 -0700267#endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP