blob: 4571336383b771215e5cbbf79d8db6cfbc9b5c15 [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"
Junxiao Shi70911652014-08-12 10:14:24 -070027#include "../security/key-chain.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080028
29namespace ndn {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080030namespace nfd {
31
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040032/**
Alexander Afanasyev197e5652014-06-13 16:56:31 -070033 * \defgroup management Management
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040034 * \brief Classes and data structures to manage NDN forwarder
35 */
36/**
37 * \ingroup management
38 * \brief NFD Management protocol - ControlCommand client
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070039 */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070040class Controller : noncopyable
Alexander Afanasyeve289b532014-02-09 22:14:44 -080041{
42public:
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070043 /** \brief a callback on command success
Alexander Afanasyeve289b532014-02-09 22:14:44 -080044 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070045 typedef function<void(const ControlParameters&)> CommandSucceedCallback;
46
47 /** \brief a callback on command failure
48 */
49 typedef function<void(uint32_t/*code*/,const std::string&/*reason*/)> CommandFailCallback;
50
Junxiao Shi70911652014-08-12 10:14:24 -070051 /** \brief a function to sign the request Interest
Yingdi Yue66bf2a2014-04-28 17:07:36 -070052 */
53 typedef function<void(Interest&)> Sign;
54
Junxiao Shi70911652014-08-12 10:14:24 -070055 /** \brief construct a Controller that uses face for transport,
56 * and has an internal default KeyChain to sign commands
57 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070058 explicit
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080059 Controller(Face& face);
Alexander Afanasyeve289b532014-02-09 22:14:44 -080060
Junxiao Shi70911652014-08-12 10:14:24 -070061 /** \brief construct a Controller that uses face for transport,
62 * and uses the passed KeyChain to sign commands
63 */
64 Controller(Face& face, KeyChain& keyChain);
65
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070066 /** \brief start command execution
67 */
68 template<typename Command>
69 void
70 start(const ControlParameters& parameters,
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070071 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -070072 const CommandFailCallback& onFailure,
Yingdi Yue66bf2a2014-04-28 17:07:36 -070073 const time::milliseconds& timeout = getDefaultCommandTimeout())
74 {
75 start<Command>(parameters, onSuccess, onFailure,
Junxiao Shi70911652014-08-12 10:14:24 -070076 bind(&KeyChain::sign<Interest>, &m_keyChain, _1),
Yingdi Yue66bf2a2014-04-28 17:07:36 -070077 timeout);
78 }
79
Junxiao Shi70911652014-08-12 10:14:24 -070080 /** \brief start command execution
81 * \param certificate the certificate used to sign request Interests
82 * \deprecated passing IdentityCertificate() empty certificate as fourth argument
83 * is deprecated, use four-parameter overload instead
84 */
85 template<typename Command>
86 void
87 start(const ControlParameters& parameters,
88 const CommandSucceedCallback& onSuccess,
89 const CommandFailCallback& onFailure,
90 const IdentityCertificate& certificate,
91 const time::milliseconds& timeout = getDefaultCommandTimeout())
92 {
93 if (certificate.getName().empty()) { // deprecated usage
94 start<Command>(parameters, onSuccess, onFailure, timeout);
95 }
96 start<Command>(parameters, onSuccess, onFailure,
97 bind(static_cast<void(KeyChain::*)(Interest&,const Name&)>(&KeyChain::sign<Interest>),
98 &m_keyChain, _1, cref(certificate.getName())),
99 timeout);
100 }
101
102 /** \brief start command execution
103 * \param identity the identity used to sign request Interests
104 */
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700105 template<typename Command>
106 void
107 start(const ControlParameters& parameters,
108 const CommandSucceedCallback& onSuccess,
109 const CommandFailCallback& onFailure,
110 const Name& identity,
111 const time::milliseconds& timeout = getDefaultCommandTimeout())
112 {
113 start<Command>(parameters, onSuccess, onFailure,
Junxiao Shi70911652014-08-12 10:14:24 -0700114 bind(&KeyChain::signByIdentity<Interest>, &m_keyChain, _1, cref(identity)),
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700115 timeout);
116 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700117
Junxiao Shi70911652014-08-12 10:14:24 -0700118 /** \brief start command execution
119 * \param sign a function to sign request Interests
120 */
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700121 template<typename Command>
122 void
123 start(const ControlParameters& parameters,
124 const CommandSucceedCallback& onSuccess,
125 const CommandFailCallback& onFailure,
126 const Sign& sign,
Junxiao Shi70911652014-08-12 10:14:24 -0700127 const time::milliseconds& timeout = getDefaultCommandTimeout())
128 {
129 shared_ptr<ControlCommand> command = make_shared<Command>();
130 this->startCommand(command, parameters, onSuccess, onFailure, sign, timeout);
131 }
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700132
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800133private:
134 void
Junxiao Shi70911652014-08-12 10:14:24 -0700135 startCommand(const shared_ptr<ControlCommand>& command,
136 const ControlParameters& parameters,
137 const CommandSucceedCallback& onSuccess,
138 const CommandFailCallback& onFailure,
139 const Sign& sign,
140 const time::milliseconds& timeout);
141
142 void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700143 processCommandResponse(const Data& data,
144 const shared_ptr<ControlCommand>& command,
145 const CommandSucceedCallback& onSuccess,
146 const CommandFailCallback& onFailure);
hilata7d160f22014-03-13 19:51:42 -0500147
Junxiao Shi5c785d62014-04-20 18:10:20 -0700148public:
149 static time::milliseconds
150 getDefaultCommandTimeout()
151 {
152 return time::milliseconds(10000);
153 }
154
hilataa99e37e2014-02-15 23:52:46 -0600155protected:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800156 Face& m_face;
Junxiao Shi70911652014-08-12 10:14:24 -0700157 shared_ptr<KeyChain> m_internalKeyChain;
158 KeyChain& m_keyChain;
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800159};
160
161} // namespace nfd
162} // namespace ndn
163
Alexander Afanasyev26c24d22014-03-20 09:31:21 -0700164#endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP