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 | /** |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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" |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame^] | 26 | #include "nfd-control-response.hpp" |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 27 | #include "nfd-status-dataset.hpp" |
| 28 | #include "nfd-command-options.hpp" |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 29 | #include "../security/validator-null.hpp" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 30 | |
| 31 | namespace ndn { |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame^] | 32 | |
| 33 | namespace security { |
| 34 | class KeyChain; |
| 35 | } // namespace security |
| 36 | class Face; |
| 37 | class Validator; |
| 38 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 39 | namespace nfd { |
| 40 | |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 41 | /** |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 42 | * \defgroup management Management |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 43 | * \brief Classes and data structures to manage NDN forwarder |
| 44 | */ |
Davide Pesavento | 18cf81b | 2015-09-12 23:36:43 +0200 | [diff] [blame] | 45 | |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 46 | /** |
| 47 | * \ingroup management |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame^] | 48 | * \brief NFD Management protocol client |
| 49 | * \sa https://redmine.named-data.net/projects/nfd/wiki/Management |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 50 | */ |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 51 | class Controller : noncopyable |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 52 | { |
| 53 | public: |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 54 | /** \brief a callback on command success |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 55 | */ |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 56 | typedef function<void(const ControlParameters&)> CommandSucceedCallback; |
| 57 | |
| 58 | /** \brief a callback on command failure |
| 59 | */ |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame^] | 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; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 65 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 66 | /** \brief construct a Controller that uses face for transport, |
| 67 | * and uses the passed KeyChain to sign commands |
| 68 | */ |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame^] | 69 | Controller(Face& face, security::KeyChain& keyChain, Validator& validator = s_validatorNull); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 70 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 71 | /** \brief start command execution |
| 72 | */ |
| 73 | template<typename Command> |
| 74 | void |
| 75 | start(const ControlParameters& parameters, |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 76 | const CommandSucceedCallback& onSuccess, |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 77 | const CommandFailCallback& onFailure, |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 78 | const CommandOptions& options = CommandOptions()) |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 79 | { |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 80 | shared_ptr<ControlCommand> command = make_shared<Command>(); |
| 81 | this->startCommand(command, parameters, onSuccess, onFailure, options); |
| 82 | } |
| 83 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 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, |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame^] | 89 | const DatasetFailCallback& onFailure, |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 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, |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame^] | 101 | const DatasetFailCallback& onFailure, |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 102 | const CommandOptions& options = CommandOptions()) |
| 103 | { |
| 104 | this->fetchDataset(make_shared<Dataset>(param), onSuccess, onFailure, options); |
| 105 | } |
| 106 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 107 | private: |
| 108 | void |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 109 | startCommand(const shared_ptr<ControlCommand>& command, |
| 110 | const ControlParameters& parameters, |
| 111 | const CommandSucceedCallback& onSuccess, |
| 112 | const CommandFailCallback& onFailure, |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 113 | const CommandOptions& options); |
| 114 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 115 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 116 | processCommandResponse(const Data& data, |
| 117 | const shared_ptr<ControlCommand>& command, |
| 118 | const CommandSucceedCallback& onSuccess, |
| 119 | const CommandFailCallback& onFailure); |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame] | 120 | |
Junxiao Shi | 54f727d | 2016-08-08 20:29:11 +0000 | [diff] [blame] | 121 | void |
| 122 | processValidatedCommandResponse(const Data& data, |
| 123 | const shared_ptr<ControlCommand>& command, |
| 124 | const CommandSucceedCallback& onSuccess, |
| 125 | const CommandFailCallback& onFailure); |
| 126 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 127 | template<typename Dataset> |
| 128 | void |
| 129 | fetchDataset(shared_ptr<Dataset> dataset, |
| 130 | const std::function<void(typename Dataset::ResultType)>& onSuccess, |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame^] | 131 | const DatasetFailCallback& onFailure, |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 132 | const CommandOptions& options); |
| 133 | |
| 134 | void |
| 135 | fetchDataset(const Name& prefix, |
| 136 | const std::function<void(const ConstBufferPtr&)>& processResponse, |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame^] | 137 | const DatasetFailCallback& onFailure, |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 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, |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame^] | 144 | const DatasetFailCallback& onFailure, |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 145 | ConstBufferPtr payload); |
| 146 | |
| 147 | void |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame^] | 148 | processDatasetFetchError(const DatasetFailCallback& onFailure, uint32_t code, std::string msg); |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 149 | |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 150 | public: |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 151 | /** \brief error code for timeout |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 152 | */ |
| 153 | static const uint32_t ERROR_TIMEOUT; |
| 154 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 155 | /** \brief error code for network Nack |
| 156 | */ |
| 157 | static const uint32_t ERROR_NACK; |
| 158 | |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 159 | /** \brief error code for response validation failure |
| 160 | */ |
| 161 | static const uint32_t ERROR_VALIDATION; |
| 162 | |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 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 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 171 | protected: |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 172 | Face& m_face; |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame^] | 173 | security::KeyChain& m_keyChain; |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 174 | Validator& m_validator; |
| 175 | |
| 176 | private: |
| 177 | static ValidatorNull s_validatorNull; |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 178 | }; |
| 179 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 180 | template<typename Dataset> |
| 181 | inline void |
| 182 | Controller::fetchDataset(shared_ptr<Dataset> dataset, |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 183 | const std::function<void(typename Dataset::ResultType)>& onSuccess1, |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame^] | 184 | const DatasetFailCallback& onFailure1, |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 185 | const CommandOptions& options) |
| 186 | { |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 187 | const std::function<void(typename Dataset::ResultType)>& onSuccess = onSuccess1 ? |
| 188 | onSuccess1 : [] (const typename Dataset::ResultType&) {}; |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame^] | 189 | const DatasetFailCallback& onFailure = onFailure1 ? |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 190 | onFailure1 : [] (uint32_t, const std::string&) {}; |
| 191 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 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 | |
| 199 | template<typename Dataset> |
| 200 | inline void |
| 201 | Controller::processDatasetResponse(shared_ptr<Dataset> dataset, |
| 202 | const std::function<void(typename Dataset::ResultType)>& onSuccess, |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame^] | 203 | const DatasetFailCallback& onFailure, |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 204 | ConstBufferPtr payload) |
| 205 | { |
| 206 | typename Dataset::ResultType result; |
| 207 | try { |
| 208 | result = dataset->parseResult(payload); |
| 209 | } |
Junxiao Shi | e7c7f15 | 2016-08-20 22:36:22 +0000 | [diff] [blame^] | 210 | catch (const tlv::Error& e) { |
| 211 | onFailure(ERROR_SERVER, e.what()); |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 212 | return; |
| 213 | } |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 214 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 215 | onSuccess(result); |
| 216 | } |
| 217 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 218 | } // namespace nfd |
| 219 | } // namespace ndn |
| 220 | |
Alexander Afanasyev | 26c24d2 | 2014-03-20 09:31:21 -0700 | [diff] [blame] | 221 | #endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP |