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