Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Alexander Afanasyev | 26c24d2 | 2014-03-20 09:31:21 -0700 | [diff] [blame] | 7 | #ifndef NDN_MANAGEMENT_NFD_CONTROLLER_HPP |
| 8 | #define NDN_MANAGEMENT_NFD_CONTROLLER_HPP |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 9 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 10 | #include "controller.hpp" |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 11 | #include "nfd-control-command.hpp" |
| 12 | #include "../face.hpp" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 13 | |
| 14 | namespace ndn { |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 15 | namespace nfd { |
| 16 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 17 | /** \brief NFD Management protocol - ControlCommand client |
| 18 | */ |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 19 | class Controller : public ndn::Controller |
| 20 | { |
| 21 | public: |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 22 | /** \brief a callback on command success |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 23 | */ |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 24 | typedef function<void(const ControlParameters&)> CommandSucceedCallback; |
| 25 | |
| 26 | /** \brief a callback on command failure |
| 27 | */ |
| 28 | typedef function<void(uint32_t/*code*/,const std::string&/*reason*/)> CommandFailCallback; |
| 29 | |
| 30 | explicit |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 31 | Controller(Face& face); |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 32 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 33 | /** \brief start command execution |
| 34 | */ |
| 35 | template<typename Command> |
| 36 | void |
| 37 | start(const ControlParameters& parameters, |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 38 | const CommandSucceedCallback& onSuccess, |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 39 | const CommandFailCallback& onFailure, |
| 40 | time::milliseconds timeout = getDefaultCommandTimeout()); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 41 | |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 42 | public: // selfreg using FIB Management commands |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 43 | virtual void |
| 44 | selfRegisterPrefix(const Name& prefixToRegister, |
| 45 | const SuccessCallback& onSuccess, |
| 46 | const FailCallback& onFail); |
| 47 | |
| 48 | virtual void |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 49 | selfDeregisterPrefix(const Name& prefixToDeRegister, |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 50 | const SuccessCallback& onSuccess, |
| 51 | const FailCallback& onFail); |
| 52 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 53 | private: |
| 54 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 55 | processCommandResponse(const Data& data, |
| 56 | const shared_ptr<ControlCommand>& command, |
| 57 | const CommandSucceedCallback& onSuccess, |
| 58 | const CommandFailCallback& onFailure); |
hilata | 7d160f2 | 2014-03-13 19:51:42 -0500 | [diff] [blame] | 59 | |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 60 | public: |
| 61 | static time::milliseconds |
| 62 | getDefaultCommandTimeout() |
| 63 | { |
| 64 | return time::milliseconds(10000); |
| 65 | } |
| 66 | |
hilata | a99e37e | 2014-02-15 23:52:46 -0600 | [diff] [blame] | 67 | protected: |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 68 | Face& m_face; |
Alexander Afanasyev | 884280c | 2014-03-07 09:40:39 +0000 | [diff] [blame] | 69 | CommandInterestGenerator m_commandInterestGenerator; |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 72 | |
| 73 | template<typename Command> |
| 74 | void |
| 75 | Controller::start(const ControlParameters& parameters, |
| 76 | const CommandSucceedCallback& onSuccess, |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 77 | const CommandFailCallback& onFailure, |
| 78 | time::milliseconds timeout) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 79 | { |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 80 | BOOST_ASSERT(timeout > time::milliseconds::zero()); |
| 81 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 82 | shared_ptr<ControlCommand> command = make_shared<Command>(); |
| 83 | |
| 84 | Interest commandInterest = command->makeCommandInterest(parameters, m_commandInterestGenerator); |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame] | 85 | commandInterest.setInterestLifetime(timeout); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 86 | |
| 87 | // http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668.aspx |
| 88 | const uint32_t timeoutCode = 10060; |
| 89 | m_face.expressInterest(commandInterest, |
| 90 | bind(&Controller::processCommandResponse, this, _2, |
| 91 | command, onSuccess, onFailure), |
| 92 | bind(onFailure, timeoutCode, "Command Interest timed out")); |
| 93 | } |
| 94 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 95 | } // namespace nfd |
| 96 | } // namespace ndn |
| 97 | |
Alexander Afanasyev | 26c24d2 | 2014-03-20 09:31:21 -0700 | [diff] [blame] | 98 | #endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP |