blob: 9fa721b81a70cc2d1a3e07d473ad20462e2a3a57 [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/**
Junxiao Shi034c1882016-06-24 18:06:51 +00003 * Copyright (c) 2013-2016 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"
Junxiao Shi034c1882016-06-24 18:06:51 +000026#include "nfd-status-dataset.hpp"
27#include "nfd-command-options.hpp"
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070028#include "../face.hpp"
Junxiao Shi70911652014-08-12 10:14:24 -070029#include "../security/key-chain.hpp"
Junxiao Shi034c1882016-06-24 18:06:51 +000030#include "../security/validator-null.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080031
32namespace ndn {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080033namespace nfd {
34
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040035/**
Alexander Afanasyev197e5652014-06-13 16:56:31 -070036 * \defgroup management Management
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040037 * \brief Classes and data structures to manage NDN forwarder
38 */
Davide Pesavento18cf81b2015-09-12 23:36:43 +020039
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040040/**
41 * \ingroup management
42 * \brief NFD Management protocol - ControlCommand client
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070043 */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070044class Controller : noncopyable
Alexander Afanasyeve289b532014-02-09 22:14:44 -080045{
46public:
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070047 /** \brief a callback on command success
Alexander Afanasyeve289b532014-02-09 22:14:44 -080048 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070049 typedef function<void(const ControlParameters&)> CommandSucceedCallback;
50
51 /** \brief a callback on command failure
52 */
Junxiao Shi034c1882016-06-24 18:06:51 +000053 typedef function<void(uint32_t code, const std::string& reason)> CommandFailCallback;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070054
Junxiao Shi70911652014-08-12 10:14:24 -070055 /** \brief construct a Controller that uses face for transport,
56 * and uses the passed KeyChain to sign commands
57 */
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000058 Controller(Face& face, KeyChain& keyChain, Validator& validator = s_validatorNull);
Junxiao Shi70911652014-08-12 10:14:24 -070059
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070060 /** \brief start command execution
61 */
62 template<typename Command>
63 void
64 start(const ControlParameters& parameters,
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070065 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -070066 const CommandFailCallback& onFailure,
Junxiao Shi5de006b2014-10-26 20:20:52 -070067 const CommandOptions& options = CommandOptions())
Yingdi Yue66bf2a2014-04-28 17:07:36 -070068 {
Junxiao Shi5de006b2014-10-26 20:20:52 -070069 shared_ptr<ControlCommand> command = make_shared<Command>();
70 this->startCommand(command, parameters, onSuccess, onFailure, options);
71 }
72
Junxiao Shi034c1882016-06-24 18:06:51 +000073 /** \brief start dataset fetching
74 */
75 template<typename Dataset>
76 typename std::enable_if<std::is_default_constructible<Dataset>::value>::type
77 fetch(const std::function<void(typename Dataset::ResultType)>& onSuccess,
78 const CommandFailCallback& onFailure,
79 const CommandOptions& options = CommandOptions())
80 {
81 this->fetchDataset(make_shared<Dataset>(), onSuccess, onFailure, options);
82 }
83
84 /** \brief start dataset fetching
85 */
86 template<typename Dataset, typename ParamType = typename Dataset::ParamType>
87 void
88 fetch(const ParamType& param,
89 const std::function<void(typename Dataset::ResultType)>& onSuccess,
90 const CommandFailCallback& onFailure,
91 const CommandOptions& options = CommandOptions())
92 {
93 this->fetchDataset(make_shared<Dataset>(param), onSuccess, onFailure, options);
94 }
95
Alexander Afanasyeve289b532014-02-09 22:14:44 -080096private:
97 void
Junxiao Shi70911652014-08-12 10:14:24 -070098 startCommand(const shared_ptr<ControlCommand>& command,
99 const ControlParameters& parameters,
100 const CommandSucceedCallback& onSuccess,
101 const CommandFailCallback& onFailure,
Junxiao Shi5de006b2014-10-26 20:20:52 -0700102 const CommandOptions& options);
103
Junxiao Shi70911652014-08-12 10:14:24 -0700104 void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700105 processCommandResponse(const Data& data,
106 const shared_ptr<ControlCommand>& command,
107 const CommandSucceedCallback& onSuccess,
108 const CommandFailCallback& onFailure);
hilata7d160f22014-03-13 19:51:42 -0500109
Junxiao Shi034c1882016-06-24 18:06:51 +0000110 template<typename Dataset>
111 void
112 fetchDataset(shared_ptr<Dataset> dataset,
113 const std::function<void(typename Dataset::ResultType)>& onSuccess,
114 const CommandFailCallback& onFailure,
115 const CommandOptions& options);
116
117 void
118 fetchDataset(const Name& prefix,
119 const std::function<void(const ConstBufferPtr&)>& processResponse,
120 const CommandFailCallback& onFailure,
121 const CommandOptions& options);
122
123 template<typename Dataset>
124 void
125 processDatasetResponse(shared_ptr<Dataset> dataset,
126 const std::function<void(typename Dataset::ResultType)>& onSuccess,
127 const CommandFailCallback& onFailure,
128 ConstBufferPtr payload);
129
130 void
131 processDatasetFetchError(const CommandFailCallback& onFailure, uint32_t code, std::string msg);
132
133
Junxiao Shi5c785d62014-04-20 18:10:20 -0700134public:
Junxiao Shi5de006b2014-10-26 20:20:52 -0700135 /** \brief error code for timeout
Junxiao Shi5de006b2014-10-26 20:20:52 -0700136 */
137 static const uint32_t ERROR_TIMEOUT;
138
Junxiao Shib1990df2015-11-05 00:14:44 +0000139 /** \brief error code for network Nack
140 */
141 static const uint32_t ERROR_NACK;
142
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000143 /** \brief error code for response validation failure
144 */
145 static const uint32_t ERROR_VALIDATION;
146
Junxiao Shi5de006b2014-10-26 20:20:52 -0700147 /** \brief error code for server error
148 */
149 static const uint32_t ERROR_SERVER;
150
151 /** \brief inclusive lower bound of error codes
152 */
153 static const uint32_t ERROR_LBOUND;
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 KeyChain& m_keyChain;
Junxiao Shi034c1882016-06-24 18:06:51 +0000158 Validator& m_validator;
159
160private:
161 static ValidatorNull s_validatorNull;
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800162};
163
Junxiao Shi034c1882016-06-24 18:06:51 +0000164template<typename Dataset>
165inline void
166Controller::fetchDataset(shared_ptr<Dataset> dataset,
Junxiao Shi600f7112016-07-16 11:57:18 +0000167 const std::function<void(typename Dataset::ResultType)>& onSuccess1,
168 const CommandFailCallback& onFailure1,
Junxiao Shi034c1882016-06-24 18:06:51 +0000169 const CommandOptions& options)
170{
Junxiao Shi600f7112016-07-16 11:57:18 +0000171 const std::function<void(typename Dataset::ResultType)>& onSuccess = onSuccess1 ?
172 onSuccess1 : [] (const typename Dataset::ResultType&) {};
173 const CommandFailCallback& onFailure = onFailure1 ?
174 onFailure1 : [] (uint32_t, const std::string&) {};
175
Junxiao Shi034c1882016-06-24 18:06:51 +0000176 Name prefix = dataset->getDatasetPrefix(options.getPrefix());
177 this->fetchDataset(prefix,
178 bind(&Controller::processDatasetResponse<Dataset>, this, dataset, onSuccess, onFailure, _1),
179 onFailure,
180 options);
181}
182
183template<typename Dataset>
184inline void
185Controller::processDatasetResponse(shared_ptr<Dataset> dataset,
186 const std::function<void(typename Dataset::ResultType)>& onSuccess,
187 const CommandFailCallback& onFailure,
188 ConstBufferPtr payload)
189{
190 typename Dataset::ResultType result;
191 try {
192 result = dataset->parseResult(payload);
193 }
194 catch (const tlv::Error& ex) {
195 onFailure(ERROR_SERVER, ex.what());
196 return;
197 }
Junxiao Shi600f7112016-07-16 11:57:18 +0000198
Junxiao Shi034c1882016-06-24 18:06:51 +0000199 onSuccess(result);
200}
201
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800202} // namespace nfd
203} // namespace ndn
204
Alexander Afanasyev26c24d22014-03-20 09:31:21 -0700205#endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP