blob: b395d6b350fa58d91ddd69a9ae8be188f0804b7f [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 Afanasyeve289b532014-02-09 22:14:44 -080022#include "nfd-controller.hpp"
Junxiao Shie7c7f152016-08-20 22:36:22 +000023#include "../face.hpp"
24#include "../security/key-chain.hpp"
Junxiao Shi034c1882016-06-24 18:06:51 +000025#include "../util/segment-fetcher.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080026
27namespace ndn {
28namespace nfd {
29
Junxiao Shi034c1882016-06-24 18:06:51 +000030using ndn::util::SegmentFetcher;
31
Junxiao Shib1990df2015-11-05 00:14:44 +000032const uint32_t Controller::ERROR_TIMEOUT = 10060; // WinSock ESAETIMEDOUT
33const uint32_t Controller::ERROR_NACK = 10800; // 10000 + TLV-TYPE of Nack header
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000034const uint32_t Controller::ERROR_VALIDATION = 10021; // 10000 + TLS1_ALERT_DECRYPTION_FAILED
Junxiao Shi5de006b2014-10-26 20:20:52 -070035const uint32_t Controller::ERROR_SERVER = 500;
36const uint32_t Controller::ERROR_LBOUND = 400;
Junxiao Shi034c1882016-06-24 18:06:51 +000037ValidatorNull Controller::s_validatorNull;
Junxiao Shi5de006b2014-10-26 20:20:52 -070038
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000039Controller::Controller(Face& face, KeyChain& keyChain, 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)
Junxiao Shi70911652014-08-12 10:14:24 -070043{
44}
45
46void
47Controller::startCommand(const shared_ptr<ControlCommand>& command,
48 const ControlParameters& parameters,
Junxiao Shi600f7112016-07-16 11:57:18 +000049 const CommandSucceedCallback& onSuccess1,
50 const CommandFailCallback& onFailure1,
Junxiao Shi5de006b2014-10-26 20:20:52 -070051 const CommandOptions& options)
52{
Junxiao Shi600f7112016-07-16 11:57:18 +000053 const CommandSucceedCallback& onSuccess = onSuccess1 ?
54 onSuccess1 : [] (const ControlParameters&) {};
55 const CommandFailCallback& onFailure = onFailure1 ?
Junxiao Shie7c7f152016-08-20 22:36:22 +000056 onFailure1 : [] (const ControlResponse&) {};
Junxiao Shi600f7112016-07-16 11:57:18 +000057
Junxiao Shi5de006b2014-10-26 20:20:52 -070058 Name requestName = command->getRequestName(options.getPrefix(), parameters);
59 Interest interest(requestName);
60 interest.setInterestLifetime(options.getTimeout());
Junxiao Shic6acc7a2015-06-23 10:03:56 -070061 m_keyChain.sign(interest, options.getSigningInfo());
Junxiao Shi5de006b2014-10-26 20:20:52 -070062
63 m_face.expressInterest(interest,
Junxiao Shie7c7f152016-08-20 22:36:22 +000064 [=] (const Interest&, const Data& data) {
65 this->processCommandResponse(data, command, onSuccess, onFailure);
66 },
67 [=] (const Interest&, const lp::Nack&) {
68 onFailure(ControlResponse(Controller::ERROR_NACK, "network Nack received"));
69 },
70 [=] (const Interest&) {
71 onFailure(ControlResponse(Controller::ERROR_TIMEOUT, "request timed out"));
72 });
Junxiao Shi5de006b2014-10-26 20:20:52 -070073}
74
75void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070076Controller::processCommandResponse(const Data& data,
77 const shared_ptr<ControlCommand>& command,
78 const CommandSucceedCallback& onSuccess,
79 const CommandFailCallback& onFailure)
80{
Junxiao Shi54f727d2016-08-08 20:29:11 +000081 m_validator.validate(data,
82 [=] (const shared_ptr<const Data>& data) {
83 this->processValidatedCommandResponse(*data, command, onSuccess, onFailure);
84 },
85 [=] (const shared_ptr<const Data>&, const std::string& msg) {
Junxiao Shie7c7f152016-08-20 22:36:22 +000086 onFailure(ControlResponse(ERROR_VALIDATION, msg));
Junxiao Shi54f727d2016-08-08 20:29:11 +000087 }
88 );
89}
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070090
Junxiao Shi54f727d2016-08-08 20:29:11 +000091void
92Controller::processValidatedCommandResponse(const Data& data,
93 const shared_ptr<ControlCommand>& command,
94 const CommandSucceedCallback& onSuccess,
95 const CommandFailCallback& onFailure)
96{
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070097 ControlResponse response;
98 try {
99 response.wireDecode(data.getContent().blockFromValue());
100 }
Junxiao Shi600f7112016-07-16 11:57:18 +0000101 catch (const tlv::Error& e) {
Junxiao Shie7c7f152016-08-20 22:36:22 +0000102 onFailure(ControlResponse(ERROR_SERVER, e.what()));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700103 return;
104 }
105
106 uint32_t code = response.getCode();
Junxiao Shi5de006b2014-10-26 20:20:52 -0700107 if (code >= ERROR_LBOUND) {
Junxiao Shie7c7f152016-08-20 22:36:22 +0000108 onFailure(response);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700109 return;
110 }
111
112 ControlParameters parameters;
113 try {
114 parameters.wireDecode(response.getBody());
115 }
Junxiao Shi600f7112016-07-16 11:57:18 +0000116 catch (const tlv::Error& e) {
Junxiao Shie7c7f152016-08-20 22:36:22 +0000117 onFailure(ControlResponse(ERROR_SERVER, e.what()));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700118 return;
119 }
120
121 try {
122 command->validateResponse(parameters);
123 }
Junxiao Shi600f7112016-07-16 11:57:18 +0000124 catch (const ControlCommand::ArgumentError& e) {
Junxiao Shie7c7f152016-08-20 22:36:22 +0000125 onFailure(ControlResponse(ERROR_SERVER, e.what()));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700126 return;
127 }
128
Junxiao Shi600f7112016-07-16 11:57:18 +0000129 onSuccess(parameters);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700130}
131
Junxiao Shi034c1882016-06-24 18:06:51 +0000132void
133Controller::fetchDataset(const Name& prefix,
134 const std::function<void(const ConstBufferPtr&)>& processResponse,
Junxiao Shie7c7f152016-08-20 22:36:22 +0000135 const DatasetFailCallback& onFailure,
Junxiao Shi034c1882016-06-24 18:06:51 +0000136 const CommandOptions& options)
137{
138 Interest baseInterest(prefix);
139 baseInterest.setInterestLifetime(options.getTimeout());
140
141 SegmentFetcher::fetch(m_face, baseInterest, m_validator, processResponse,
142 bind(&Controller::processDatasetFetchError, this, onFailure, _1, _2));
143}
144
145void
Junxiao Shie7c7f152016-08-20 22:36:22 +0000146Controller::processDatasetFetchError(const DatasetFailCallback& onFailure,
Junxiao Shi034c1882016-06-24 18:06:51 +0000147 uint32_t code, std::string msg)
148{
149 switch (static_cast<SegmentFetcher::ErrorCode>(code)) {
150 // It's intentional to cast as SegmentFetcher::ErrorCode, and to not have a 'default' clause.
151 // This forces the switch statement to handle every defined SegmentFetcher::ErrorCode,
152 // and breaks compilation if it does not.
153 case SegmentFetcher::ErrorCode::INTEREST_TIMEOUT:
154 onFailure(ERROR_TIMEOUT, msg);
155 break;
156 case SegmentFetcher::ErrorCode::DATA_HAS_NO_SEGMENT:
157 onFailure(ERROR_SERVER, msg);
158 break;
159 case SegmentFetcher::ErrorCode::SEGMENT_VALIDATION_FAIL:
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000160 /// \todo When SegmentFetcher exposes validator error code, Controller::ERROR_VALIDATION
161 /// should be replaced with a range that corresponds to validator error codes.
162 onFailure(ERROR_VALIDATION, msg);
Junxiao Shi034c1882016-06-24 18:06:51 +0000163 break;
164 case SegmentFetcher::ErrorCode::NACK_ERROR:
165 onFailure(ERROR_NACK, msg);
166 break;
167 }
168}
169
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800170} // namespace nfd
171} // namespace ndn