blob: 05570fdc07746af26bf2d671e36db66c1969660b [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -08002/*
Eric Newberrye345baa2018-05-23 18:17:07 -07003 * Copyright (c) 2013-2018 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
Junxiao Shi7357ef22016-09-07 02:39:37 +000022#include "controller.hpp"
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080023#include "face.hpp"
24#include "security/v2/key-chain.hpp"
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080025
26#include <boost/lexical_cast.hpp>
Alexander Afanasyeve289b532014-02-09 22:14:44 -080027
28namespace ndn {
29namespace nfd {
30
Junxiao Shi034c1882016-06-24 18:06:51 +000031using ndn::util::SegmentFetcher;
32
Junxiao Shib1990df2015-11-05 00:14:44 +000033const uint32_t Controller::ERROR_TIMEOUT = 10060; // WinSock ESAETIMEDOUT
34const uint32_t Controller::ERROR_NACK = 10800; // 10000 + TLV-TYPE of Nack header
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000035const uint32_t Controller::ERROR_VALIDATION = 10021; // 10000 + TLS1_ALERT_DECRYPTION_FAILED
Junxiao Shi5de006b2014-10-26 20:20:52 -070036const uint32_t Controller::ERROR_SERVER = 500;
37const uint32_t Controller::ERROR_LBOUND = 400;
38
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080039Controller::Controller(Face& face, KeyChain& keyChain, security::v2::Validator& validator)
Junxiao Shi70911652014-08-12 10:14:24 -070040 : m_face(face)
41 , m_keyChain(keyChain)
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000042 , m_validator(validator)
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080043 , m_signer(keyChain)
Junxiao Shi70911652014-08-12 10:14:24 -070044{
45}
46
Ashlesh Gawande3e3a9892018-11-16 20:26:15 -060047Controller::~Controller()
48{
49 for (const auto& sp : m_fetchers) {
50 sp->stop();
51 }
52}
53
Junxiao Shi70911652014-08-12 10:14:24 -070054void
55Controller::startCommand(const shared_ptr<ControlCommand>& command,
56 const ControlParameters& parameters,
Davide Pesavento3b101d02018-07-21 22:44:09 -040057 const CommandSucceedCallback& onSuccess,
58 const CommandFailCallback& onFailure,
Junxiao Shi5de006b2014-10-26 20:20:52 -070059 const CommandOptions& options)
60{
61 Name requestName = command->getRequestName(options.getPrefix(), parameters);
Alexander Afanasyev80782e02017-01-04 13:16:54 -080062 Interest interest = m_signer.makeCommandInterest(requestName, options.getSigningInfo());
Junxiao Shi5de006b2014-10-26 20:20:52 -070063 interest.setInterestLifetime(options.getTimeout());
Junxiao Shi5de006b2014-10-26 20:20:52 -070064
65 m_face.expressInterest(interest,
Junxiao Shie7c7f152016-08-20 22:36:22 +000066 [=] (const Interest&, const Data& data) {
Davide Pesavento3b101d02018-07-21 22:44:09 -040067 processCommandResponse(data, command, onSuccess, onFailure);
Junxiao Shie7c7f152016-08-20 22:36:22 +000068 },
69 [=] (const Interest&, const lp::Nack&) {
Davide Pesavento3b101d02018-07-21 22:44:09 -040070 if (onFailure)
71 onFailure(ControlResponse(Controller::ERROR_NACK, "network Nack received"));
Junxiao Shie7c7f152016-08-20 22:36:22 +000072 },
73 [=] (const Interest&) {
Davide Pesavento3b101d02018-07-21 22:44:09 -040074 if (onFailure)
75 onFailure(ControlResponse(Controller::ERROR_TIMEOUT, "request timed out"));
Junxiao Shie7c7f152016-08-20 22:36:22 +000076 });
Junxiao Shi5de006b2014-10-26 20:20:52 -070077}
78
79void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070080Controller::processCommandResponse(const Data& data,
81 const shared_ptr<ControlCommand>& command,
82 const CommandSucceedCallback& onSuccess,
83 const CommandFailCallback& onFailure)
84{
Junxiao Shi54f727d2016-08-08 20:29:11 +000085 m_validator.validate(data,
Alexander Afanasyev6dfeffe2017-01-30 22:40:32 -080086 [=] (const Data& data) {
Davide Pesavento3b101d02018-07-21 22:44:09 -040087 processValidatedCommandResponse(data, command, onSuccess, onFailure);
Junxiao Shi54f727d2016-08-08 20:29:11 +000088 },
Davide Pesavento3b101d02018-07-21 22:44:09 -040089 [=] (const Data&, const auto& error) {
90 if (onFailure)
91 onFailure(ControlResponse(ERROR_VALIDATION, boost::lexical_cast<std::string>(error)));
Junxiao Shi54f727d2016-08-08 20:29:11 +000092 }
93 );
94}
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070095
Junxiao Shi54f727d2016-08-08 20:29:11 +000096void
97Controller::processValidatedCommandResponse(const Data& data,
98 const shared_ptr<ControlCommand>& command,
99 const CommandSucceedCallback& onSuccess,
100 const CommandFailCallback& onFailure)
101{
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700102 ControlResponse response;
103 try {
104 response.wireDecode(data.getContent().blockFromValue());
105 }
Junxiao Shi600f7112016-07-16 11:57:18 +0000106 catch (const tlv::Error& e) {
Davide Pesavento3b101d02018-07-21 22:44:09 -0400107 if (onFailure)
108 onFailure(ControlResponse(ERROR_SERVER, e.what()));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700109 return;
110 }
111
112 uint32_t code = response.getCode();
Junxiao Shi5de006b2014-10-26 20:20:52 -0700113 if (code >= ERROR_LBOUND) {
Davide Pesavento3b101d02018-07-21 22:44:09 -0400114 if (onFailure)
115 onFailure(response);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700116 return;
117 }
118
119 ControlParameters parameters;
120 try {
121 parameters.wireDecode(response.getBody());
122 }
Junxiao Shi600f7112016-07-16 11:57:18 +0000123 catch (const tlv::Error& e) {
Davide Pesavento3b101d02018-07-21 22:44:09 -0400124 if (onFailure)
125 onFailure(ControlResponse(ERROR_SERVER, e.what()));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700126 return;
127 }
128
129 try {
130 command->validateResponse(parameters);
131 }
Junxiao Shi600f7112016-07-16 11:57:18 +0000132 catch (const ControlCommand::ArgumentError& e) {
Davide Pesavento3b101d02018-07-21 22:44:09 -0400133 if (onFailure)
134 onFailure(ControlResponse(ERROR_SERVER, e.what()));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700135 return;
136 }
137
Davide Pesavento3b101d02018-07-21 22:44:09 -0400138 if (onSuccess)
139 onSuccess(parameters);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700140}
141
Junxiao Shi034c1882016-06-24 18:06:51 +0000142void
143Controller::fetchDataset(const Name& prefix,
Eric Newberrye345baa2018-05-23 18:17:07 -0700144 const std::function<void(ConstBufferPtr)>& processResponse,
Junxiao Shie7c7f152016-08-20 22:36:22 +0000145 const DatasetFailCallback& onFailure,
Junxiao Shi034c1882016-06-24 18:06:51 +0000146 const CommandOptions& options)
147{
Eric Newberrye345baa2018-05-23 18:17:07 -0700148 SegmentFetcher::Options fetcherOptions;
149 fetcherOptions.maxTimeout = options.getTimeout();
Davide Pesavento3b101d02018-07-21 22:44:09 -0400150
151 auto fetcher = SegmentFetcher::start(m_face, Interest(prefix), m_validator, fetcherOptions);
152 if (processResponse) {
153 fetcher->onComplete.connect(processResponse);
154 }
155 if (onFailure) {
156 fetcher->onError.connect([=] (uint32_t code, const std::string& msg) {
Eric Newberrye345baa2018-05-23 18:17:07 -0700157 processDatasetFetchError(onFailure, code, msg);
158 });
Davide Pesavento3b101d02018-07-21 22:44:09 -0400159 }
Ashlesh Gawande3e3a9892018-11-16 20:26:15 -0600160
161 auto it = m_fetchers.insert(fetcher).first;
162 fetcher->onComplete.connect([this, it] (ConstBufferPtr) { m_fetchers.erase(it); });
163 fetcher->onError.connect([this, it] (uint32_t, const std::string&) { m_fetchers.erase(it); });
Junxiao Shi034c1882016-06-24 18:06:51 +0000164}
165
166void
Junxiao Shie7c7f152016-08-20 22:36:22 +0000167Controller::processDatasetFetchError(const DatasetFailCallback& onFailure,
Junxiao Shi034c1882016-06-24 18:06:51 +0000168 uint32_t code, std::string msg)
169{
Davide Pesavento3b101d02018-07-21 22:44:09 -0400170 BOOST_ASSERT(onFailure);
171
Junxiao Shi034c1882016-06-24 18:06:51 +0000172 switch (static_cast<SegmentFetcher::ErrorCode>(code)) {
173 // It's intentional to cast as SegmentFetcher::ErrorCode, and to not have a 'default' clause.
174 // This forces the switch statement to handle every defined SegmentFetcher::ErrorCode,
175 // and breaks compilation if it does not.
176 case SegmentFetcher::ErrorCode::INTEREST_TIMEOUT:
177 onFailure(ERROR_TIMEOUT, msg);
178 break;
179 case SegmentFetcher::ErrorCode::DATA_HAS_NO_SEGMENT:
Eric Newberrye345baa2018-05-23 18:17:07 -0700180 case SegmentFetcher::ErrorCode::FINALBLOCKID_NOT_SEGMENT:
Junxiao Shi034c1882016-06-24 18:06:51 +0000181 onFailure(ERROR_SERVER, msg);
182 break;
183 case SegmentFetcher::ErrorCode::SEGMENT_VALIDATION_FAIL:
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000184 /// \todo When SegmentFetcher exposes validator error code, Controller::ERROR_VALIDATION
185 /// should be replaced with a range that corresponds to validator error codes.
186 onFailure(ERROR_VALIDATION, msg);
Junxiao Shi034c1882016-06-24 18:06:51 +0000187 break;
188 case SegmentFetcher::ErrorCode::NACK_ERROR:
189 onFailure(ERROR_NACK, msg);
190 break;
191 }
192}
193
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800194} // namespace nfd
195} // namespace ndn