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" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 30 | namespace nfd { |
| 31 | |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 32 | /** |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 33 | * \defgroup management Management |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 34 | * \brief Classes and data structures to manage NDN forwarder |
| 35 | */ |
| 36 | /** |
| 37 | * \ingroup management |
| 38 | * \brief NFD Management protocol - ControlCommand client |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 39 | */ |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 40 | class Controller : noncopyable |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 41 | { |
| 42 | public: |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 43 | /** \brief a callback on command success |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 44 | */ |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 45 | typedef function<void(const ControlParameters&)> CommandSucceedCallback; |
| 46 | |
| 47 | /** \brief a callback on command failure |
| 48 | */ |
| 49 | typedef function<void(uint32_t/*code*/,const std::string&/*reason*/)> CommandFailCallback; |
| 50 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 51 | /** \brief a function to sign the request Interest |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 52 | */ |
| 53 | typedef function<void(Interest&)> Sign; |
| 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 has an internal default KeyChain to sign commands |
| 57 | */ |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 58 | explicit |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 59 | Controller(Face& face); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 60 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 61 | /** \brief construct a Controller that uses face for transport, |
| 62 | * and uses the passed KeyChain to sign commands |
| 63 | */ |
| 64 | Controller(Face& face, KeyChain& keyChain); |
| 65 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 66 | /** \brief start command execution |
| 67 | */ |
| 68 | template<typename Command> |
| 69 | void |
| 70 | start(const ControlParameters& parameters, |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 71 | const CommandSucceedCallback& onSuccess, |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 72 | const CommandFailCallback& onFailure, |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 73 | const time::milliseconds& timeout = getDefaultCommandTimeout()) |
| 74 | { |
| 75 | start<Command>(parameters, onSuccess, onFailure, |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 76 | bind(&KeyChain::sign<Interest>, &m_keyChain, _1), |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 77 | timeout); |
| 78 | } |
| 79 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 80 | /** \brief start command execution |
Alexander Afanasyev | 7e6fefc | 2014-10-20 12:31:55 -0400 | [diff] [blame^] | 81 | * \param certificate the certificate used to sign request Interests. |
| 82 | * If IdentityCertificate() is passed, the default signing certificate will be used. |
| 83 | * |
| 84 | * \note IdentityCertificate() creates a certificate with an empty name, which is an |
| 85 | * invalid certificate. A valid IdentityCertificate has at least 4 name components, |
| 86 | * as it follows `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model. |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 87 | */ |
| 88 | template<typename Command> |
| 89 | void |
| 90 | start(const ControlParameters& parameters, |
| 91 | const CommandSucceedCallback& onSuccess, |
| 92 | const CommandFailCallback& onFailure, |
| 93 | const IdentityCertificate& certificate, |
| 94 | const time::milliseconds& timeout = getDefaultCommandTimeout()) |
| 95 | { |
Alexander Afanasyev | 7e6fefc | 2014-10-20 12:31:55 -0400 | [diff] [blame^] | 96 | if (certificate.getName().empty()) { |
| 97 | start<Command>(parameters, onSuccess, onFailure, timeout); |
| 98 | } |
| 99 | else { |
| 100 | start<Command>(parameters, onSuccess, onFailure, |
| 101 | bind(static_cast<void(KeyChain::*)(Interest&,const Name&)>(&KeyChain::sign<Interest>), |
| 102 | &m_keyChain, _1, cref(certificate.getName())), |
| 103 | timeout); |
| 104 | } |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** \brief start command execution |
| 108 | * \param identity the identity used to sign request Interests |
| 109 | */ |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 110 | template<typename Command> |
| 111 | void |
| 112 | start(const ControlParameters& parameters, |
| 113 | const CommandSucceedCallback& onSuccess, |
| 114 | const CommandFailCallback& onFailure, |
| 115 | const Name& identity, |
| 116 | const time::milliseconds& timeout = getDefaultCommandTimeout()) |
| 117 | { |
| 118 | start<Command>(parameters, onSuccess, onFailure, |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 119 | bind(&KeyChain::signByIdentity<Interest>, &m_keyChain, _1, cref(identity)), |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 120 | timeout); |
| 121 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 122 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 123 | /** \brief start command execution |
| 124 | * \param sign a function to sign request Interests |
| 125 | */ |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 126 | template<typename Command> |
| 127 | void |
| 128 | start(const ControlParameters& parameters, |
| 129 | const CommandSucceedCallback& onSuccess, |
| 130 | const CommandFailCallback& onFailure, |
| 131 | const Sign& sign, |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 132 | const time::milliseconds& timeout = getDefaultCommandTimeout()) |
| 133 | { |
| 134 | shared_ptr<ControlCommand> command = make_shared<Command>(); |
| 135 | this->startCommand(command, parameters, onSuccess, onFailure, sign, timeout); |
| 136 | } |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 137 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 138 | private: |
| 139 | void |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 140 | startCommand(const shared_ptr<ControlCommand>& command, |
| 141 | const ControlParameters& parameters, |
| 142 | const CommandSucceedCallback& onSuccess, |
| 143 | const CommandFailCallback& onFailure, |
| 144 | const Sign& sign, |
| 145 | const time::milliseconds& timeout); |
| 146 | |
| 147 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 148 | processCommandResponse(const Data& data, |
| 149 | const shared_ptr<ControlCommand>& command, |
| 150 | const CommandSucceedCallback& onSuccess, |
| 151 | const CommandFailCallback& onFailure); |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame] | 152 | |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 153 | public: |
| 154 | static time::milliseconds |
| 155 | getDefaultCommandTimeout() |
| 156 | { |
| 157 | return time::milliseconds(10000); |
| 158 | } |
| 159 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 160 | protected: |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 161 | Face& m_face; |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 162 | shared_ptr<KeyChain> m_internalKeyChain; |
| 163 | KeyChain& m_keyChain; |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | } // namespace nfd |
| 167 | } // namespace ndn |
| 168 | |
Alexander Afanasyev | 26c24d2 | 2014-03-20 09:31:21 -0700 | [diff] [blame] | 169 | #endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP |