blob: cd8db1dc748b31129e98eb528e8409eb2dceae79 [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
59 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070060 explicit
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080061 Controller(Face& face);
Alexander Afanasyeve289b532014-02-09 22:14:44 -080062
Junxiao Shi70911652014-08-12 10:14:24 -070063 /** \brief construct a Controller that uses face for transport,
64 * and uses the passed KeyChain to sign commands
65 */
66 Controller(Face& face, KeyChain& keyChain);
67
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070068 /** \brief start command execution
69 */
70 template<typename Command>
71 void
72 start(const ControlParameters& parameters,
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070073 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -070074 const CommandFailCallback& onFailure,
Junxiao Shi5de006b2014-10-26 20:20:52 -070075 const CommandOptions& options = CommandOptions())
Yingdi Yue66bf2a2014-04-28 17:07:36 -070076 {
Junxiao Shi5de006b2014-10-26 20:20:52 -070077 shared_ptr<ControlCommand> command = make_shared<Command>();
78 this->startCommand(command, parameters, onSuccess, onFailure, options);
79 }
80
81 /** \brief start command execution
82 * \deprecated use the overload taking CommandOptions
83 */
84 template<typename Command>
85 void
86 start(const ControlParameters& parameters,
87 const CommandSucceedCallback& onSuccess,
88 const CommandFailCallback& onFailure,
89 const time::milliseconds& timeout)
90 {
91 CommandOptions options;
92 options.setTimeout(timeout);
93
94 this->start<Command>(parameters, onSuccess, onFailure, options);
Yingdi Yue66bf2a2014-04-28 17:07:36 -070095 }
96
Junxiao Shi70911652014-08-12 10:14:24 -070097 /** \brief start command execution
Alexander Afanasyev7e6fefc2014-10-20 12:31:55 -040098 * \param certificate the certificate used to sign request Interests.
99 * If IdentityCertificate() is passed, the default signing certificate will be used.
100 *
101 * \note IdentityCertificate() creates a certificate with an empty name, which is an
102 * invalid certificate. A valid IdentityCertificate has at least 4 name components,
103 * as it follows `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model.
Junxiao Shi5de006b2014-10-26 20:20:52 -0700104 *
105 * \deprecated use the overload taking CommandOptions
Junxiao Shi70911652014-08-12 10:14:24 -0700106 */
107 template<typename Command>
108 void
109 start(const ControlParameters& parameters,
110 const CommandSucceedCallback& onSuccess,
111 const CommandFailCallback& onFailure,
112 const IdentityCertificate& certificate,
113 const time::milliseconds& timeout = getDefaultCommandTimeout())
114 {
Junxiao Shi5de006b2014-10-26 20:20:52 -0700115 CommandOptions options;
Alexander Afanasyev7e6fefc2014-10-20 12:31:55 -0400116 if (certificate.getName().empty()) {
Junxiao Shi5de006b2014-10-26 20:20:52 -0700117 options.setSigningDefault();
Alexander Afanasyev7e6fefc2014-10-20 12:31:55 -0400118 }
119 else {
Junxiao Shi5de006b2014-10-26 20:20:52 -0700120 options.setSigningCertificate(certificate);
Alexander Afanasyev7e6fefc2014-10-20 12:31:55 -0400121 }
Junxiao Shi5de006b2014-10-26 20:20:52 -0700122 options.setTimeout(timeout);
123
124 this->start<Command>(parameters, onSuccess, onFailure, options);
Junxiao Shi70911652014-08-12 10:14:24 -0700125 }
126
127 /** \brief start command execution
128 * \param identity the identity used to sign request Interests
Junxiao Shi5de006b2014-10-26 20:20:52 -0700129 * \deprecated use the overload taking CommandOptions
Junxiao Shi70911652014-08-12 10:14:24 -0700130 */
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 Name& identity,
137 const time::milliseconds& timeout = getDefaultCommandTimeout())
138 {
Junxiao Shi5de006b2014-10-26 20:20:52 -0700139 CommandOptions options;
140 options.setSigningIdentity(identity);
141 options.setTimeout(timeout);
142
143 this->start<Command>(parameters, onSuccess, onFailure, options);
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700144 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700145
Junxiao Shi70911652014-08-12 10:14:24 -0700146 /** \brief start command execution
147 * \param sign a function to sign request Interests
Junxiao Shi5de006b2014-10-26 20:20:52 -0700148 * \deprecated arbitrary signing function is no longer supported
Junxiao Shi70911652014-08-12 10:14:24 -0700149 */
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700150 template<typename Command>
151 void
152 start(const ControlParameters& parameters,
153 const CommandSucceedCallback& onSuccess,
154 const CommandFailCallback& onFailure,
155 const Sign& sign,
Junxiao Shi70911652014-08-12 10:14:24 -0700156 const time::milliseconds& timeout = getDefaultCommandTimeout())
157 {
158 shared_ptr<ControlCommand> command = make_shared<Command>();
159 this->startCommand(command, parameters, onSuccess, onFailure, sign, timeout);
160 }
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700161
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800162private:
163 void
Junxiao Shi70911652014-08-12 10:14:24 -0700164 startCommand(const shared_ptr<ControlCommand>& command,
165 const ControlParameters& parameters,
166 const CommandSucceedCallback& onSuccess,
167 const CommandFailCallback& onFailure,
Junxiao Shi5de006b2014-10-26 20:20:52 -0700168 const CommandOptions& options);
169
170 /** \deprecated This is to support arbitrary signing function.
171 */
172 void
173 startCommand(const shared_ptr<ControlCommand>& command,
174 const ControlParameters& parameters,
175 const CommandSucceedCallback& onSuccess,
176 const CommandFailCallback& onFailure,
Junxiao Shi70911652014-08-12 10:14:24 -0700177 const Sign& sign,
178 const time::milliseconds& timeout);
179
180 void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700181 processCommandResponse(const Data& data,
182 const shared_ptr<ControlCommand>& command,
183 const CommandSucceedCallback& onSuccess,
184 const CommandFailCallback& onFailure);
hilata7d160f22014-03-13 19:51:42 -0500185
Junxiao Shi5c785d62014-04-20 18:10:20 -0700186public:
Junxiao Shi5de006b2014-10-26 20:20:52 -0700187 /** \deprecated use CommandOptions::DEFAULT_TIMEOUT
188 */
Junxiao Shi5c785d62014-04-20 18:10:20 -0700189 static time::milliseconds
190 getDefaultCommandTimeout()
191 {
192 return time::milliseconds(10000);
193 }
194
Junxiao Shi5de006b2014-10-26 20:20:52 -0700195public:
196 /** \brief error code for timeout
197 * \note comes from http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668.aspx
198 */
199 static const uint32_t ERROR_TIMEOUT;
200
201 /** \brief error code for server error
202 */
203 static const uint32_t ERROR_SERVER;
204
205 /** \brief inclusive lower bound of error codes
206 */
207 static const uint32_t ERROR_LBOUND;
208
hilataa99e37e2014-02-15 23:52:46 -0600209protected:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800210 Face& m_face;
Junxiao Shi70911652014-08-12 10:14:24 -0700211 shared_ptr<KeyChain> m_internalKeyChain;
212 KeyChain& m_keyChain;
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800213};
214
215} // namespace nfd
216} // namespace ndn
217
Alexander Afanasyev26c24d22014-03-20 09:31:21 -0700218#endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP