Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Alexander Afanasyev | 26c24d2 | 2014-03-20 09:31:21 -0700 | [diff] [blame] | 13 | #ifndef NDN_MANAGEMENT_NFD_CONTROLLER_HPP |
| 14 | #define NDN_MANAGEMENT_NFD_CONTROLLER_HPP |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 15 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 16 | #include "controller.hpp" |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 17 | #include "nfd-control-command.hpp" |
| 18 | #include "../face.hpp" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 19 | |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame^] | 20 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 21 | namespace ndn { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 22 | namespace nfd { |
| 23 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 24 | /** \brief NFD Management protocol - ControlCommand client |
| 25 | */ |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 26 | class Controller : public ndn::Controller |
| 27 | { |
| 28 | public: |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 29 | /** \brief a callback on command success |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 30 | */ |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 31 | typedef function<void(const ControlParameters&)> CommandSucceedCallback; |
| 32 | |
| 33 | /** \brief a callback on command failure |
| 34 | */ |
| 35 | typedef function<void(uint32_t/*code*/,const std::string&/*reason*/)> CommandFailCallback; |
| 36 | |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame^] | 37 | /** \brief a callback on signing command interest |
| 38 | */ |
| 39 | typedef function<void(Interest&)> Sign; |
| 40 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 41 | explicit |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 42 | Controller(Face& face); |
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 | /** \brief start command execution |
| 45 | */ |
| 46 | template<typename Command> |
| 47 | void |
| 48 | start(const ControlParameters& parameters, |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 49 | const CommandSucceedCallback& onSuccess, |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 50 | const CommandFailCallback& onFailure, |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame^] | 51 | const time::milliseconds& timeout = getDefaultCommandTimeout()) |
| 52 | { |
| 53 | start<Command>(parameters, onSuccess, onFailure, |
| 54 | bind(&CommandInterestGenerator::generate, |
| 55 | &m_commandInterestGenerator, _1, |
| 56 | boost::cref(Name())), |
| 57 | timeout); |
| 58 | } |
| 59 | |
| 60 | template<typename Command> |
| 61 | void |
| 62 | start(const ControlParameters& parameters, |
| 63 | const CommandSucceedCallback& onSuccess, |
| 64 | const CommandFailCallback& onFailure, |
| 65 | const IdentityCertificate& certificate, |
| 66 | const time::milliseconds& timeout = getDefaultCommandTimeout()) |
| 67 | { |
| 68 | start<Command>(parameters, onSuccess, onFailure, |
| 69 | bind(&CommandInterestGenerator::generate, |
| 70 | &m_commandInterestGenerator, _1, |
| 71 | boost::cref(certificate.getName())), |
| 72 | timeout); |
| 73 | } |
| 74 | |
| 75 | template<typename Command> |
| 76 | void |
| 77 | start(const ControlParameters& parameters, |
| 78 | const CommandSucceedCallback& onSuccess, |
| 79 | const CommandFailCallback& onFailure, |
| 80 | const Name& identity, |
| 81 | const time::milliseconds& timeout = getDefaultCommandTimeout()) |
| 82 | { |
| 83 | start<Command>(parameters, onSuccess, onFailure, |
| 84 | bind(&CommandInterestGenerator::generateWithIdentity, |
| 85 | &m_commandInterestGenerator, _1, |
| 86 | boost::cref(identity)), |
| 87 | timeout); |
| 88 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 89 | |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 90 | public: // selfreg using FIB Management commands |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 91 | virtual void |
| 92 | selfRegisterPrefix(const Name& prefixToRegister, |
| 93 | const SuccessCallback& onSuccess, |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame^] | 94 | const FailCallback& onFail) |
| 95 | { |
| 96 | this->selfRegisterPrefix(prefixToRegister, onSuccess, onFail, |
| 97 | bind(&CommandInterestGenerator::generate, |
| 98 | &m_commandInterestGenerator, _1, |
| 99 | boost::cref(Name()))); |
| 100 | } |
| 101 | |
| 102 | virtual void |
| 103 | selfRegisterPrefix(const Name& prefixToRegister, |
| 104 | const SuccessCallback& onSuccess, |
| 105 | const FailCallback& onFail, |
| 106 | const IdentityCertificate& certificate) |
| 107 | { |
| 108 | this->selfRegisterPrefix(prefixToRegister, onSuccess, onFail, |
| 109 | bind(&CommandInterestGenerator::generate, |
| 110 | &m_commandInterestGenerator, _1, |
| 111 | boost::cref(certificate.getName()))); |
| 112 | } |
| 113 | |
| 114 | virtual void |
| 115 | selfRegisterPrefix(const Name& prefixToRegister, |
| 116 | const SuccessCallback& onSuccess, |
| 117 | const FailCallback& onFail, |
| 118 | const Name& identity) |
| 119 | { |
| 120 | this->selfRegisterPrefix(prefixToRegister, onSuccess, onFail, |
| 121 | bind(&CommandInterestGenerator::generateWithIdentity, |
| 122 | &m_commandInterestGenerator, _1, |
| 123 | boost::cref(identity))); |
| 124 | } |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 125 | |
| 126 | virtual void |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 127 | selfDeregisterPrefix(const Name& prefixToDeRegister, |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 128 | const SuccessCallback& onSuccess, |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame^] | 129 | const FailCallback& onFail) |
| 130 | { |
| 131 | this->selfDeregisterPrefix(prefixToDeRegister, onSuccess, onFail, |
| 132 | bind(&CommandInterestGenerator::generate, |
| 133 | &m_commandInterestGenerator, _1, |
| 134 | boost::cref(Name()))); |
| 135 | } |
| 136 | |
| 137 | virtual void |
| 138 | selfDeregisterPrefix(const Name& prefixToDeRegister, |
| 139 | const SuccessCallback& onSuccess, |
| 140 | const FailCallback& onFail, |
| 141 | const IdentityCertificate& certificate) |
| 142 | { |
| 143 | this->selfDeregisterPrefix(prefixToDeRegister, onSuccess, onFail, |
| 144 | bind(&CommandInterestGenerator::generate, |
| 145 | &m_commandInterestGenerator, _1, |
| 146 | boost::cref(certificate.getName()))); |
| 147 | } |
| 148 | |
| 149 | virtual void |
| 150 | selfDeregisterPrefix(const Name& prefixToDeRegister, |
| 151 | const SuccessCallback& onSuccess, |
| 152 | const FailCallback& onFail, |
| 153 | const Name& identity) |
| 154 | { |
| 155 | this->selfDeregisterPrefix(prefixToDeRegister, onSuccess, onFail, |
| 156 | bind(&CommandInterestGenerator::generateWithIdentity, |
| 157 | &m_commandInterestGenerator, _1, |
| 158 | boost::cref(identity))); |
| 159 | } |
| 160 | |
| 161 | protected: |
| 162 | template<typename Command> |
| 163 | void |
| 164 | start(const ControlParameters& parameters, |
| 165 | const CommandSucceedCallback& onSuccess, |
| 166 | const CommandFailCallback& onFailure, |
| 167 | const Sign& sign, |
| 168 | const time::milliseconds& timeout = getDefaultCommandTimeout()); |
| 169 | |
| 170 | virtual void |
| 171 | selfRegisterPrefix(const Name& prefixToRegister, |
| 172 | const SuccessCallback& onSuccess, |
| 173 | const FailCallback& onFail, |
| 174 | const Sign& sign); |
| 175 | |
| 176 | virtual void |
| 177 | selfDeregisterPrefix(const Name& prefixToDeRegister, |
| 178 | const SuccessCallback& onSuccess, |
| 179 | const FailCallback& onFail, |
| 180 | const Sign& sign); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 181 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 182 | private: |
| 183 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 184 | processCommandResponse(const Data& data, |
| 185 | const shared_ptr<ControlCommand>& command, |
| 186 | const CommandSucceedCallback& onSuccess, |
| 187 | const CommandFailCallback& onFailure); |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame] | 188 | |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 189 | public: |
| 190 | static time::milliseconds |
| 191 | getDefaultCommandTimeout() |
| 192 | { |
| 193 | return time::milliseconds(10000); |
| 194 | } |
| 195 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 196 | protected: |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 197 | Face& m_face; |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 198 | CommandInterestGenerator m_commandInterestGenerator; |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 199 | }; |
| 200 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 201 | |
| 202 | template<typename Command> |
| 203 | void |
| 204 | Controller::start(const ControlParameters& parameters, |
| 205 | const CommandSucceedCallback& onSuccess, |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 206 | const CommandFailCallback& onFailure, |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame^] | 207 | const Sign& sign, |
| 208 | const time::milliseconds& timeout) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 209 | { |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 210 | BOOST_ASSERT(timeout > time::milliseconds::zero()); |
| 211 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 212 | shared_ptr<ControlCommand> command = make_shared<Command>(); |
| 213 | |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame^] | 214 | Interest commandInterest = command->makeCommandInterest(parameters, sign); |
| 215 | |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 216 | commandInterest.setInterestLifetime(timeout); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 217 | |
| 218 | // http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668.aspx |
| 219 | const uint32_t timeoutCode = 10060; |
| 220 | m_face.expressInterest(commandInterest, |
| 221 | bind(&Controller::processCommandResponse, this, _2, |
| 222 | command, onSuccess, onFailure), |
| 223 | bind(onFailure, timeoutCode, "Command Interest timed out")); |
| 224 | } |
| 225 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 226 | } // namespace nfd |
| 227 | } // namespace ndn |
| 228 | |
Alexander Afanasyev | 26c24d2 | 2014-03-20 09:31:21 -0700 | [diff] [blame] | 229 | #endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP |