Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 26c24d2 | 2014-03-20 09:31:21 -0700 | [diff] [blame] | 22 | #ifndef NDN_MANAGEMENT_NFD_CONTROLLER_HPP |
| 23 | #define NDN_MANAGEMENT_NFD_CONTROLLER_HPP |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 24 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 25 | #include "nfd-control-command.hpp" |
| 26 | #include "../face.hpp" |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 27 | #include "../security/key-chain.hpp" |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 28 | #include "nfd-command-options.hpp" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 31 | namespace nfd { |
| 32 | |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 33 | /** |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 34 | * \defgroup management Management |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 35 | * \brief Classes and data structures to manage NDN forwarder |
| 36 | */ |
| 37 | /** |
| 38 | * \ingroup management |
| 39 | * \brief NFD Management protocol - ControlCommand client |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 40 | */ |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 41 | class Controller : noncopyable |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 42 | { |
| 43 | public: |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 44 | /** \brief a callback on command success |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 45 | */ |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 46 | 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 Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 52 | /** \brief a function to sign the request Interest |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 53 | * \deprecated arbitrary signing function is no longer supported |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 54 | */ |
| 55 | typedef function<void(Interest&)> Sign; |
| 56 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 57 | /** \brief construct a Controller that uses face for transport, |
| 58 | * and has an internal default KeyChain to sign commands |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 59 | * \deprecated use two-parameter overload |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 60 | */ |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 61 | explicit |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 62 | Controller(Face& face); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 63 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 64 | /** \brief construct a Controller that uses face for transport, |
| 65 | * and uses the passed KeyChain to sign commands |
| 66 | */ |
| 67 | Controller(Face& face, KeyChain& keyChain); |
| 68 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 69 | /** \brief start command execution |
| 70 | */ |
| 71 | template<typename Command> |
| 72 | void |
| 73 | start(const ControlParameters& parameters, |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 74 | const CommandSucceedCallback& onSuccess, |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 75 | const CommandFailCallback& onFailure, |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 76 | const CommandOptions& options = CommandOptions()) |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 77 | { |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 78 | shared_ptr<ControlCommand> command = make_shared<Command>(); |
| 79 | this->startCommand(command, parameters, onSuccess, onFailure, options); |
| 80 | } |
| 81 | |
| 82 | /** \brief start command execution |
| 83 | * \deprecated use the overload taking CommandOptions |
| 84 | */ |
| 85 | template<typename Command> |
| 86 | void |
| 87 | start(const ControlParameters& parameters, |
| 88 | const CommandSucceedCallback& onSuccess, |
| 89 | const CommandFailCallback& onFailure, |
| 90 | const time::milliseconds& timeout) |
| 91 | { |
| 92 | CommandOptions options; |
| 93 | options.setTimeout(timeout); |
| 94 | |
| 95 | this->start<Command>(parameters, onSuccess, onFailure, options); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 98 | /** \brief start command execution |
Alexander Afanasyev | 7e6fefc | 2014-10-20 12:31:55 -0400 | [diff] [blame] | 99 | * \param certificate the certificate used to sign request Interests. |
| 100 | * If IdentityCertificate() is passed, the default signing certificate will be used. |
| 101 | * |
| 102 | * \note IdentityCertificate() creates a certificate with an empty name, which is an |
| 103 | * invalid certificate. A valid IdentityCertificate has at least 4 name components, |
| 104 | * as it follows `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model. |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 105 | * |
| 106 | * \deprecated use the overload taking CommandOptions |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 107 | */ |
| 108 | template<typename Command> |
| 109 | void |
| 110 | start(const ControlParameters& parameters, |
| 111 | const CommandSucceedCallback& onSuccess, |
| 112 | const CommandFailCallback& onFailure, |
| 113 | const IdentityCertificate& certificate, |
| 114 | const time::milliseconds& timeout = getDefaultCommandTimeout()) |
| 115 | { |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 116 | CommandOptions options; |
Alexander Afanasyev | 7e6fefc | 2014-10-20 12:31:55 -0400 | [diff] [blame] | 117 | if (certificate.getName().empty()) { |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 118 | options.setSigningDefault(); |
Alexander Afanasyev | 7e6fefc | 2014-10-20 12:31:55 -0400 | [diff] [blame] | 119 | } |
| 120 | else { |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 121 | options.setSigningCertificate(certificate); |
Alexander Afanasyev | 7e6fefc | 2014-10-20 12:31:55 -0400 | [diff] [blame] | 122 | } |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 123 | options.setTimeout(timeout); |
| 124 | |
| 125 | this->start<Command>(parameters, onSuccess, onFailure, options); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** \brief start command execution |
| 129 | * \param identity the identity used to sign request Interests |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 130 | * \deprecated use the overload taking CommandOptions |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 131 | */ |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 132 | template<typename Command> |
| 133 | void |
| 134 | start(const ControlParameters& parameters, |
| 135 | const CommandSucceedCallback& onSuccess, |
| 136 | const CommandFailCallback& onFailure, |
| 137 | const Name& identity, |
| 138 | const time::milliseconds& timeout = getDefaultCommandTimeout()) |
| 139 | { |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 140 | CommandOptions options; |
| 141 | options.setSigningIdentity(identity); |
| 142 | options.setTimeout(timeout); |
| 143 | |
| 144 | this->start<Command>(parameters, onSuccess, onFailure, options); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 145 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 146 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 147 | /** \brief start command execution |
| 148 | * \param sign a function to sign request Interests |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 149 | * \deprecated arbitrary signing function is no longer supported |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 150 | */ |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 151 | template<typename Command> |
| 152 | void |
| 153 | start(const ControlParameters& parameters, |
| 154 | const CommandSucceedCallback& onSuccess, |
| 155 | const CommandFailCallback& onFailure, |
| 156 | const Sign& sign, |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 157 | const time::milliseconds& timeout = getDefaultCommandTimeout()) |
| 158 | { |
| 159 | shared_ptr<ControlCommand> command = make_shared<Command>(); |
| 160 | this->startCommand(command, parameters, onSuccess, onFailure, sign, timeout); |
| 161 | } |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 162 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 163 | private: |
| 164 | void |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 165 | startCommand(const shared_ptr<ControlCommand>& command, |
| 166 | const ControlParameters& parameters, |
| 167 | const CommandSucceedCallback& onSuccess, |
| 168 | const CommandFailCallback& onFailure, |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 169 | const CommandOptions& options); |
| 170 | |
| 171 | /** \deprecated This is to support arbitrary signing function. |
| 172 | */ |
| 173 | void |
| 174 | startCommand(const shared_ptr<ControlCommand>& command, |
| 175 | const ControlParameters& parameters, |
| 176 | const CommandSucceedCallback& onSuccess, |
| 177 | const CommandFailCallback& onFailure, |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 178 | const Sign& sign, |
| 179 | const time::milliseconds& timeout); |
| 180 | |
| 181 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 182 | processCommandResponse(const Data& data, |
| 183 | const shared_ptr<ControlCommand>& command, |
| 184 | const CommandSucceedCallback& onSuccess, |
| 185 | const CommandFailCallback& onFailure); |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame] | 186 | |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 187 | public: |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 188 | /** \deprecated use CommandOptions::DEFAULT_TIMEOUT |
| 189 | */ |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 190 | static time::milliseconds |
| 191 | getDefaultCommandTimeout() |
| 192 | { |
| 193 | return time::milliseconds(10000); |
| 194 | } |
| 195 | |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 196 | public: |
| 197 | /** \brief error code for timeout |
| 198 | * \note comes from http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668.aspx |
| 199 | */ |
| 200 | static const uint32_t ERROR_TIMEOUT; |
| 201 | |
| 202 | /** \brief error code for server error |
| 203 | */ |
| 204 | static const uint32_t ERROR_SERVER; |
| 205 | |
| 206 | /** \brief inclusive lower bound of error codes |
| 207 | */ |
| 208 | static const uint32_t ERROR_LBOUND; |
| 209 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 210 | protected: |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 211 | Face& m_face; |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 212 | |
| 213 | /** \deprecated |
| 214 | */ |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 215 | shared_ptr<KeyChain> m_internalKeyChain; |
Junxiao Shi | edd834e | 2014-10-28 20:28:58 -0700 | [diff] [blame] | 216 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 217 | KeyChain& m_keyChain; |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | } // namespace nfd |
| 221 | } // namespace ndn |
| 222 | |
Alexander Afanasyev | 26c24d2 | 2014-03-20 09:31:21 -0700 | [diff] [blame] | 223 | #endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP |