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