blob: 583be71b0bd6e16cbd1b76fd338f11b2632ff075 [file] [log] [blame]
Alexander Afanasyeve289b532014-02-09 22:14:44 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Afanasyeve289b532014-02-09 22:14:44 -080011 */
12
Alexander Afanasyev26c24d22014-03-20 09:31:21 -070013#ifndef NDN_MANAGEMENT_NFD_CONTROLLER_HPP
14#define NDN_MANAGEMENT_NFD_CONTROLLER_HPP
Alexander Afanasyeve289b532014-02-09 22:14:44 -080015
Alexander Afanasyeve289b532014-02-09 22:14:44 -080016#include "controller.hpp"
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070017#include "nfd-control-command.hpp"
18#include "../face.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080019
Yingdi Yue66bf2a2014-04-28 17:07:36 -070020
Alexander Afanasyeve289b532014-02-09 22:14:44 -080021namespace ndn {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080022namespace nfd {
23
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070024/** \brief NFD Management protocol - ControlCommand client
25 */
Alexander Afanasyeve289b532014-02-09 22:14:44 -080026class Controller : public ndn::Controller
27{
28public:
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070029 /** \brief a callback on command success
Alexander Afanasyeve289b532014-02-09 22:14:44 -080030 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070031 typedef function<void(const ControlParameters&)> CommandSucceedCallback;
32
33 /** \brief a callback on command failure
34 */
35 typedef function<void(uint32_t/*code*/,const std::string&/*reason*/)> CommandFailCallback;
36
Yingdi Yue66bf2a2014-04-28 17:07:36 -070037 /** \brief a callback on signing command interest
38 */
39 typedef function<void(Interest&)> Sign;
40
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070041 explicit
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080042 Controller(Face& face);
Alexander Afanasyeve289b532014-02-09 22:14:44 -080043
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070044 /** \brief start command execution
45 */
46 template<typename Command>
47 void
48 start(const ControlParameters& parameters,
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070049 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -070050 const CommandFailCallback& onFailure,
Yingdi Yue66bf2a2014-04-28 17:07:36 -070051 const time::milliseconds& timeout = getDefaultCommandTimeout())
52 {
53 start<Command>(parameters, onSuccess, onFailure,
54 bind(&CommandInterestGenerator::generate,
55 &m_commandInterestGenerator, _1,
56 boost::cref(Name())),
57 timeout);
58 }
59
60 template<typename Command>
61 void
62 start(const ControlParameters& parameters,
63 const CommandSucceedCallback& onSuccess,
64 const CommandFailCallback& onFailure,
65 const IdentityCertificate& certificate,
66 const time::milliseconds& timeout = getDefaultCommandTimeout())
67 {
68 start<Command>(parameters, onSuccess, onFailure,
69 bind(&CommandInterestGenerator::generate,
70 &m_commandInterestGenerator, _1,
71 boost::cref(certificate.getName())),
72 timeout);
73 }
74
75 template<typename Command>
76 void
77 start(const ControlParameters& parameters,
78 const CommandSucceedCallback& onSuccess,
79 const CommandFailCallback& onFailure,
80 const Name& identity,
81 const time::milliseconds& timeout = getDefaultCommandTimeout())
82 {
83 start<Command>(parameters, onSuccess, onFailure,
84 bind(&CommandInterestGenerator::generateWithIdentity,
85 &m_commandInterestGenerator, _1,
86 boost::cref(identity)),
87 timeout);
88 }
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070089
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070090public: // selfreg using FIB Management commands
Alexander Afanasyeve289b532014-02-09 22:14:44 -080091 virtual void
92 selfRegisterPrefix(const Name& prefixToRegister,
93 const SuccessCallback& onSuccess,
Yingdi Yue66bf2a2014-04-28 17:07:36 -070094 const FailCallback& onFail)
95 {
96 this->selfRegisterPrefix(prefixToRegister, onSuccess, onFail,
97 bind(&CommandInterestGenerator::generate,
98 &m_commandInterestGenerator, _1,
99 boost::cref(Name())));
100 }
101
102 virtual void
103 selfRegisterPrefix(const Name& prefixToRegister,
104 const SuccessCallback& onSuccess,
105 const FailCallback& onFail,
106 const IdentityCertificate& certificate)
107 {
108 this->selfRegisterPrefix(prefixToRegister, onSuccess, onFail,
109 bind(&CommandInterestGenerator::generate,
110 &m_commandInterestGenerator, _1,
111 boost::cref(certificate.getName())));
112 }
113
114 virtual void
115 selfRegisterPrefix(const Name& prefixToRegister,
116 const SuccessCallback& onSuccess,
117 const FailCallback& onFail,
118 const Name& identity)
119 {
120 this->selfRegisterPrefix(prefixToRegister, onSuccess, onFail,
121 bind(&CommandInterestGenerator::generateWithIdentity,
122 &m_commandInterestGenerator, _1,
123 boost::cref(identity)));
124 }
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800125
126 virtual void
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000127 selfDeregisterPrefix(const Name& prefixToDeRegister,
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800128 const SuccessCallback& onSuccess,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700129 const FailCallback& onFail)
130 {
131 this->selfDeregisterPrefix(prefixToDeRegister, onSuccess, onFail,
132 bind(&CommandInterestGenerator::generate,
133 &m_commandInterestGenerator, _1,
134 boost::cref(Name())));
135 }
136
137 virtual void
138 selfDeregisterPrefix(const Name& prefixToDeRegister,
139 const SuccessCallback& onSuccess,
140 const FailCallback& onFail,
141 const IdentityCertificate& certificate)
142 {
143 this->selfDeregisterPrefix(prefixToDeRegister, onSuccess, onFail,
144 bind(&CommandInterestGenerator::generate,
145 &m_commandInterestGenerator, _1,
146 boost::cref(certificate.getName())));
147 }
148
149 virtual void
150 selfDeregisterPrefix(const Name& prefixToDeRegister,
151 const SuccessCallback& onSuccess,
152 const FailCallback& onFail,
153 const Name& identity)
154 {
155 this->selfDeregisterPrefix(prefixToDeRegister, onSuccess, onFail,
156 bind(&CommandInterestGenerator::generateWithIdentity,
157 &m_commandInterestGenerator, _1,
158 boost::cref(identity)));
159 }
160
161protected:
162 template<typename Command>
163 void
164 start(const ControlParameters& parameters,
165 const CommandSucceedCallback& onSuccess,
166 const CommandFailCallback& onFailure,
167 const Sign& sign,
168 const time::milliseconds& timeout = getDefaultCommandTimeout());
169
170 virtual void
171 selfRegisterPrefix(const Name& prefixToRegister,
172 const SuccessCallback& onSuccess,
173 const FailCallback& onFail,
174 const Sign& sign);
175
176 virtual void
177 selfDeregisterPrefix(const Name& prefixToDeRegister,
178 const SuccessCallback& onSuccess,
179 const FailCallback& onFail,
180 const Sign& sign);
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800181
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800182private:
183 void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700184 processCommandResponse(const Data& data,
185 const shared_ptr<ControlCommand>& command,
186 const CommandSucceedCallback& onSuccess,
187 const CommandFailCallback& onFailure);
hilata7d160f22014-03-13 19:51:42 -0500188
Junxiao Shi5c785d62014-04-20 18:10:20 -0700189public:
190 static time::milliseconds
191 getDefaultCommandTimeout()
192 {
193 return time::milliseconds(10000);
194 }
195
hilataa99e37e2014-02-15 23:52:46 -0600196protected:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800197 Face& m_face;
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000198 CommandInterestGenerator m_commandInterestGenerator;
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800199};
200
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700201
202template<typename Command>
203void
204Controller::start(const ControlParameters& parameters,
205 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -0700206 const CommandFailCallback& onFailure,
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700207 const Sign& sign,
208 const time::milliseconds& timeout)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700209{
Junxiao Shi5c785d62014-04-20 18:10:20 -0700210 BOOST_ASSERT(timeout > time::milliseconds::zero());
211
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700212 shared_ptr<ControlCommand> command = make_shared<Command>();
213
Yingdi Yue66bf2a2014-04-28 17:07:36 -0700214 Interest commandInterest = command->makeCommandInterest(parameters, sign);
215
Junxiao Shi5c785d62014-04-20 18:10:20 -0700216 commandInterest.setInterestLifetime(timeout);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700217
218 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668.aspx
219 const uint32_t timeoutCode = 10060;
220 m_face.expressInterest(commandInterest,
221 bind(&Controller::processCommandResponse, this, _2,
222 command, onSuccess, onFailure),
223 bind(onFailure, timeoutCode, "Command Interest timed out"));
224}
225
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800226} // namespace nfd
227} // namespace ndn
228
Alexander Afanasyev26c24d22014-03-20 09:31:21 -0700229#endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP