blob: 528666932a90819ee073e3d104b5762229af6cf2 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyeve289b532014-02-09 22:14:44 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyeve289b532014-02-09 22:14:44 -080020 */
21
Alexander Afanasyev26c24d22014-03-20 09:31:21 -070022#ifndef NDN_MANAGEMENT_NFD_CONTROLLER_HPP
23#define NDN_MANAGEMENT_NFD_CONTROLLER_HPP
Alexander Afanasyeve289b532014-02-09 22:14:44 -080024
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070025#include "nfd-control-command.hpp"
26#include "../face.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080027
28namespace ndn {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080029namespace nfd {
30
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040031/**
32 * \defgroup management
33 * \brief Classes and data structures to manage NDN forwarder
34 */
35/**
36 * \ingroup management
37 * \brief NFD Management protocol - ControlCommand client
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070038 */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070039class Controller : noncopyable
Alexander Afanasyeve289b532014-02-09 22:14:44 -080040{
41public:
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070042 /** \brief a callback on command success
Alexander Afanasyeve289b532014-02-09 22:14:44 -080043 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070044 typedef function<void(const ControlParameters&)> CommandSucceedCallback;
45
46 /** \brief a callback on command failure
47 */
48 typedef function<void(uint32_t/*code*/,const std::string&/*reason*/)> CommandFailCallback;
49
Yingdi Yue66bf2a2014-04-28 17:07:36 -070050 /** \brief a callback on signing command interest
51 */
52 typedef function<void(Interest&)> Sign;
53
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070054 explicit
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080055 Controller(Face& face);
Alexander Afanasyeve289b532014-02-09 22:14:44 -080056
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070057 /** \brief start command execution
58 */
59 template<typename Command>
60 void
61 start(const ControlParameters& parameters,
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070062 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -070063 const CommandFailCallback& onFailure,
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070064 const IdentityCertificate& certificate = IdentityCertificate(),
Yingdi Yue66bf2a2014-04-28 17:07:36 -070065 const time::milliseconds& timeout = getDefaultCommandTimeout())
66 {
67 start<Command>(parameters, onSuccess, onFailure,
68 bind(&CommandInterestGenerator::generate,
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070069 &m_commandInterestGenerator, _1, cref(certificate.getName())),
Yingdi Yue66bf2a2014-04-28 17:07:36 -070070 timeout);
71 }
72
73 template<typename Command>
74 void
75 start(const ControlParameters& parameters,
76 const CommandSucceedCallback& onSuccess,
77 const CommandFailCallback& onFailure,
78 const Name& identity,
79 const time::milliseconds& timeout = getDefaultCommandTimeout())
80 {
81 start<Command>(parameters, onSuccess, onFailure,
82 bind(&CommandInterestGenerator::generateWithIdentity,
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070083 &m_commandInterestGenerator, _1, cref(identity)),
Yingdi Yue66bf2a2014-04-28 17:07:36 -070084 timeout);
85 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070086
Yingdi Yue66bf2a2014-04-28 17:07:36 -070087 template<typename Command>
88 void
89 start(const ControlParameters& parameters,
90 const CommandSucceedCallback& onSuccess,
91 const CommandFailCallback& onFailure,
92 const Sign& sign,
93 const time::milliseconds& timeout = getDefaultCommandTimeout());
94
Alexander Afanasyeve289b532014-02-09 22:14:44 -080095private:
96 void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070097 processCommandResponse(const Data& data,
98 const shared_ptr<ControlCommand>& command,
99 const CommandSucceedCallback& onSuccess,
100 const CommandFailCallback& onFailure);
hilata7d160f22014-03-13 19:51:42 -0500101
Junxiao Shi5c785d62014-04-20 18:10:20 -0700102public:
103 static time::milliseconds
104 getDefaultCommandTimeout()
105 {
106 return time::milliseconds(10000);
107 }
108
hilataa99e37e2014-02-15 23:52:46 -0600109protected:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800110 Face& m_face;
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000111 CommandInterestGenerator m_commandInterestGenerator;
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800112};
113
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700114template<typename Command>
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700115inline void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700116Controller::start(const ControlParameters& parameters,
117 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -0700118 const CommandFailCallback& onFailure,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700119 const Sign& sign,
120 const time::milliseconds& timeout)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700121{
Junxiao Shi5c785d62014-04-20 18:10:20 -0700122 BOOST_ASSERT(timeout > time::milliseconds::zero());
123
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700124 shared_ptr<ControlCommand> command = make_shared<Command>();
125
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700126 Interest commandInterest = command->makeCommandInterest(parameters, sign);
127
Junxiao Shi5c785d62014-04-20 18:10:20 -0700128 commandInterest.setInterestLifetime(timeout);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700129
130 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668.aspx
131 const uint32_t timeoutCode = 10060;
132 m_face.expressInterest(commandInterest,
133 bind(&Controller::processCommandResponse, this, _2,
134 command, onSuccess, onFailure),
135 bind(onFailure, timeoutCode, "Command Interest timed out"));
136}
137
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800138} // namespace nfd
139} // namespace ndn
140
Alexander Afanasyev26c24d22014-03-20 09:31:21 -0700141#endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP