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 | */ |
| 58 | Controller(Face& face, KeyChain& keyChain); |
| 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 | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 143 | /** \brief error code for server error |
| 144 | */ |
| 145 | static const uint32_t ERROR_SERVER; |
| 146 | |
| 147 | /** \brief inclusive lower bound of error codes |
| 148 | */ |
| 149 | static const uint32_t ERROR_LBOUND; |
| 150 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 151 | protected: |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 152 | Face& m_face; |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 153 | KeyChain& m_keyChain; |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame^] | 154 | Validator& m_validator; |
| 155 | |
| 156 | private: |
| 157 | static ValidatorNull s_validatorNull; |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 158 | }; |
| 159 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame^] | 160 | template<typename Dataset> |
| 161 | inline void |
| 162 | Controller::fetchDataset(shared_ptr<Dataset> dataset, |
| 163 | const std::function<void(typename Dataset::ResultType)>& onSuccess, |
| 164 | const CommandFailCallback& onFailure, |
| 165 | const CommandOptions& options) |
| 166 | { |
| 167 | Name prefix = dataset->getDatasetPrefix(options.getPrefix()); |
| 168 | this->fetchDataset(prefix, |
| 169 | bind(&Controller::processDatasetResponse<Dataset>, this, dataset, onSuccess, onFailure, _1), |
| 170 | onFailure, |
| 171 | options); |
| 172 | } |
| 173 | |
| 174 | template<typename Dataset> |
| 175 | inline void |
| 176 | Controller::processDatasetResponse(shared_ptr<Dataset> dataset, |
| 177 | const std::function<void(typename Dataset::ResultType)>& onSuccess, |
| 178 | const CommandFailCallback& onFailure, |
| 179 | ConstBufferPtr payload) |
| 180 | { |
| 181 | typename Dataset::ResultType result; |
| 182 | try { |
| 183 | result = dataset->parseResult(payload); |
| 184 | } |
| 185 | catch (const tlv::Error& ex) { |
| 186 | onFailure(ERROR_SERVER, ex.what()); |
| 187 | return; |
| 188 | } |
| 189 | onSuccess(result); |
| 190 | } |
| 191 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 192 | } // namespace nfd |
| 193 | } // namespace ndn |
| 194 | |
Alexander Afanasyev | 26c24d2 | 2014-03-20 09:31:21 -0700 | [diff] [blame] | 195 | #endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP |