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" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 29 | namespace nfd { |
| 30 | |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 31 | /** |
| 32 | * \defgroup management |
| 33 | * \brief Classes and data structures to manage NDN forwarder |
| 34 | */ |
| 35 | /** |
| 36 | * \ingroup management |
| 37 | * \brief NFD Management protocol - ControlCommand client |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 38 | */ |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 39 | class Controller : noncopyable |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 40 | { |
| 41 | public: |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 42 | /** \brief a callback on command success |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 43 | */ |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 44 | typedef function<void(const ControlParameters&)> CommandSucceedCallback; |
| 45 | |
| 46 | /** \brief a callback on command failure |
| 47 | */ |
| 48 | typedef function<void(uint32_t/*code*/,const std::string&/*reason*/)> CommandFailCallback; |
| 49 | |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 50 | /** \brief a callback on signing command interest |
| 51 | */ |
| 52 | typedef function<void(Interest&)> Sign; |
| 53 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 54 | explicit |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 55 | Controller(Face& face); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 56 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 57 | /** \brief start command execution |
| 58 | */ |
| 59 | template<typename Command> |
| 60 | void |
| 61 | start(const ControlParameters& parameters, |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 62 | const CommandSucceedCallback& onSuccess, |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 63 | const CommandFailCallback& onFailure, |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 64 | const IdentityCertificate& certificate = IdentityCertificate(), |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 65 | const time::milliseconds& timeout = getDefaultCommandTimeout()) |
| 66 | { |
| 67 | start<Command>(parameters, onSuccess, onFailure, |
| 68 | bind(&CommandInterestGenerator::generate, |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 69 | &m_commandInterestGenerator, _1, cref(certificate.getName())), |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 70 | timeout); |
| 71 | } |
| 72 | |
| 73 | template<typename Command> |
| 74 | void |
| 75 | start(const ControlParameters& parameters, |
| 76 | const CommandSucceedCallback& onSuccess, |
| 77 | const CommandFailCallback& onFailure, |
| 78 | const Name& identity, |
| 79 | const time::milliseconds& timeout = getDefaultCommandTimeout()) |
| 80 | { |
| 81 | start<Command>(parameters, onSuccess, onFailure, |
| 82 | bind(&CommandInterestGenerator::generateWithIdentity, |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 83 | &m_commandInterestGenerator, _1, cref(identity)), |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 84 | timeout); |
| 85 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 86 | |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 87 | template<typename Command> |
| 88 | void |
| 89 | start(const ControlParameters& parameters, |
| 90 | const CommandSucceedCallback& onSuccess, |
| 91 | const CommandFailCallback& onFailure, |
| 92 | const Sign& sign, |
| 93 | const time::milliseconds& timeout = getDefaultCommandTimeout()); |
| 94 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 95 | private: |
| 96 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 97 | processCommandResponse(const Data& data, |
| 98 | const shared_ptr<ControlCommand>& command, |
| 99 | const CommandSucceedCallback& onSuccess, |
| 100 | const CommandFailCallback& onFailure); |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame] | 101 | |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 102 | public: |
| 103 | static time::milliseconds |
| 104 | getDefaultCommandTimeout() |
| 105 | { |
| 106 | return time::milliseconds(10000); |
| 107 | } |
| 108 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 109 | protected: |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 110 | Face& m_face; |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 111 | CommandInterestGenerator m_commandInterestGenerator; |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 114 | template<typename Command> |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 115 | inline void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 116 | Controller::start(const ControlParameters& parameters, |
| 117 | const CommandSucceedCallback& onSuccess, |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 118 | const CommandFailCallback& onFailure, |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 119 | const Sign& sign, |
| 120 | const time::milliseconds& timeout) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 121 | { |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 122 | BOOST_ASSERT(timeout > time::milliseconds::zero()); |
| 123 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 124 | shared_ptr<ControlCommand> command = make_shared<Command>(); |
| 125 | |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 126 | Interest commandInterest = command->makeCommandInterest(parameters, sign); |
| 127 | |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 128 | commandInterest.setInterestLifetime(timeout); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 129 | |
| 130 | // http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668.aspx |
| 131 | const uint32_t timeoutCode = 10060; |
| 132 | m_face.expressInterest(commandInterest, |
| 133 | bind(&Controller::processCommandResponse, this, _2, |
| 134 | command, onSuccess, onFailure), |
| 135 | bind(onFailure, timeoutCode, "Command Interest timed out")); |
| 136 | } |
| 137 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 138 | } // namespace nfd |
| 139 | } // namespace ndn |
| 140 | |
Alexander Afanasyev | 26c24d2 | 2014-03-20 09:31:21 -0700 | [diff] [blame] | 141 | #endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP |