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 | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 26 | #include "nfd-status-dataset.hpp" |
| 27 | #include "nfd-command-options.hpp" |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 28 | #include "../face.hpp" |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 29 | #include "../security/key-chain.hpp" |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 30 | #include "../security/validator-null.hpp" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 33 | namespace nfd { |
| 34 | |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 35 | /** |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 36 | * \defgroup management Management |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 37 | * \brief Classes and data structures to manage NDN forwarder |
| 38 | */ |
Davide Pesavento | 18cf81b | 2015-09-12 23:36:43 +0200 | [diff] [blame] | 39 | |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 40 | /** |
| 41 | * \ingroup management |
| 42 | * \brief NFD Management protocol - ControlCommand client |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 43 | */ |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 44 | class Controller : noncopyable |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 45 | { |
| 46 | public: |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 47 | /** \brief a callback on command success |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 48 | */ |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 49 | typedef function<void(const ControlParameters&)> CommandSucceedCallback; |
| 50 | |
| 51 | /** \brief a callback on command failure |
| 52 | */ |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 53 | typedef function<void(uint32_t code, const std::string& reason)> CommandFailCallback; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 54 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 55 | /** \brief construct a Controller that uses face for transport, |
| 56 | * and uses the passed KeyChain to sign commands |
| 57 | */ |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 58 | Controller(Face& face, KeyChain& keyChain, Validator& validator = s_validatorNull); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 59 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 60 | /** \brief start command execution |
| 61 | */ |
| 62 | template<typename Command> |
| 63 | void |
| 64 | start(const ControlParameters& parameters, |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 65 | const CommandSucceedCallback& onSuccess, |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 66 | const CommandFailCallback& onFailure, |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 67 | const CommandOptions& options = CommandOptions()) |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 68 | { |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 69 | shared_ptr<ControlCommand> command = make_shared<Command>(); |
| 70 | this->startCommand(command, parameters, onSuccess, onFailure, options); |
| 71 | } |
| 72 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 73 | /** \brief start dataset fetching |
| 74 | */ |
| 75 | template<typename Dataset> |
| 76 | typename std::enable_if<std::is_default_constructible<Dataset>::value>::type |
| 77 | fetch(const std::function<void(typename Dataset::ResultType)>& onSuccess, |
| 78 | const CommandFailCallback& onFailure, |
| 79 | const CommandOptions& options = CommandOptions()) |
| 80 | { |
| 81 | this->fetchDataset(make_shared<Dataset>(), onSuccess, onFailure, options); |
| 82 | } |
| 83 | |
| 84 | /** \brief start dataset fetching |
| 85 | */ |
| 86 | template<typename Dataset, typename ParamType = typename Dataset::ParamType> |
| 87 | void |
| 88 | fetch(const ParamType& param, |
| 89 | const std::function<void(typename Dataset::ResultType)>& onSuccess, |
| 90 | const CommandFailCallback& onFailure, |
| 91 | const CommandOptions& options = CommandOptions()) |
| 92 | { |
| 93 | this->fetchDataset(make_shared<Dataset>(param), onSuccess, onFailure, options); |
| 94 | } |
| 95 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 96 | private: |
| 97 | void |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 98 | startCommand(const shared_ptr<ControlCommand>& command, |
| 99 | const ControlParameters& parameters, |
| 100 | const CommandSucceedCallback& onSuccess, |
| 101 | const CommandFailCallback& onFailure, |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 102 | const CommandOptions& options); |
| 103 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 104 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 105 | processCommandResponse(const Data& data, |
| 106 | const shared_ptr<ControlCommand>& command, |
| 107 | const CommandSucceedCallback& onSuccess, |
| 108 | const CommandFailCallback& onFailure); |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame] | 109 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 110 | template<typename Dataset> |
| 111 | void |
| 112 | fetchDataset(shared_ptr<Dataset> dataset, |
| 113 | const std::function<void(typename Dataset::ResultType)>& onSuccess, |
| 114 | const CommandFailCallback& onFailure, |
| 115 | const CommandOptions& options); |
| 116 | |
| 117 | void |
| 118 | fetchDataset(const Name& prefix, |
| 119 | const std::function<void(const ConstBufferPtr&)>& processResponse, |
| 120 | const CommandFailCallback& onFailure, |
| 121 | const CommandOptions& options); |
| 122 | |
| 123 | template<typename Dataset> |
| 124 | void |
| 125 | processDatasetResponse(shared_ptr<Dataset> dataset, |
| 126 | const std::function<void(typename Dataset::ResultType)>& onSuccess, |
| 127 | const CommandFailCallback& onFailure, |
| 128 | ConstBufferPtr payload); |
| 129 | |
| 130 | void |
| 131 | processDatasetFetchError(const CommandFailCallback& onFailure, uint32_t code, std::string msg); |
| 132 | |
| 133 | |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 134 | public: |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 135 | /** \brief error code for timeout |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 136 | */ |
| 137 | static const uint32_t ERROR_TIMEOUT; |
| 138 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 139 | /** \brief error code for network Nack |
| 140 | */ |
| 141 | static const uint32_t ERROR_NACK; |
| 142 | |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 143 | /** \brief error code for response validation failure |
| 144 | */ |
| 145 | static const uint32_t ERROR_VALIDATION; |
| 146 | |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 147 | /** \brief error code for server error |
| 148 | */ |
| 149 | static const uint32_t ERROR_SERVER; |
| 150 | |
| 151 | /** \brief inclusive lower bound of error codes |
| 152 | */ |
| 153 | static const uint32_t ERROR_LBOUND; |
| 154 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 155 | protected: |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 156 | Face& m_face; |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 157 | KeyChain& m_keyChain; |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 158 | Validator& m_validator; |
| 159 | |
| 160 | private: |
| 161 | static ValidatorNull s_validatorNull; |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 162 | }; |
| 163 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 164 | template<typename Dataset> |
| 165 | inline void |
| 166 | Controller::fetchDataset(shared_ptr<Dataset> dataset, |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame^] | 167 | const std::function<void(typename Dataset::ResultType)>& onSuccess1, |
| 168 | const CommandFailCallback& onFailure1, |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 169 | const CommandOptions& options) |
| 170 | { |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame^] | 171 | const std::function<void(typename Dataset::ResultType)>& onSuccess = onSuccess1 ? |
| 172 | onSuccess1 : [] (const typename Dataset::ResultType&) {}; |
| 173 | const CommandFailCallback& onFailure = onFailure1 ? |
| 174 | onFailure1 : [] (uint32_t, const std::string&) {}; |
| 175 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 176 | Name prefix = dataset->getDatasetPrefix(options.getPrefix()); |
| 177 | this->fetchDataset(prefix, |
| 178 | bind(&Controller::processDatasetResponse<Dataset>, this, dataset, onSuccess, onFailure, _1), |
| 179 | onFailure, |
| 180 | options); |
| 181 | } |
| 182 | |
| 183 | template<typename Dataset> |
| 184 | inline void |
| 185 | Controller::processDatasetResponse(shared_ptr<Dataset> dataset, |
| 186 | const std::function<void(typename Dataset::ResultType)>& onSuccess, |
| 187 | const CommandFailCallback& onFailure, |
| 188 | ConstBufferPtr payload) |
| 189 | { |
| 190 | typename Dataset::ResultType result; |
| 191 | try { |
| 192 | result = dataset->parseResult(payload); |
| 193 | } |
| 194 | catch (const tlv::Error& ex) { |
| 195 | onFailure(ERROR_SERVER, ex.what()); |
| 196 | return; |
| 197 | } |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame^] | 198 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 199 | onSuccess(result); |
| 200 | } |
| 201 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 202 | } // namespace nfd |
| 203 | } // namespace ndn |
| 204 | |
Alexander Afanasyev | 26c24d2 | 2014-03-20 09:31:21 -0700 | [diff] [blame] | 205 | #endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP |