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