blob: 5fc755d03c48f6b4f6bf1a5a402b178b55ad6460 [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"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080023#include "nfd-control-response.hpp"
Junxiao Shi034c1882016-06-24 18:06:51 +000024#include "../util/segment-fetcher.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080025
26namespace ndn {
27namespace nfd {
28
Junxiao Shi034c1882016-06-24 18:06:51 +000029using ndn::util::SegmentFetcher;
30
Junxiao Shib1990df2015-11-05 00:14:44 +000031const uint32_t Controller::ERROR_TIMEOUT = 10060; // WinSock ESAETIMEDOUT
32const uint32_t Controller::ERROR_NACK = 10800; // 10000 + TLV-TYPE of Nack header
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000033const uint32_t Controller::ERROR_VALIDATION = 10021; // 10000 + TLS1_ALERT_DECRYPTION_FAILED
Junxiao Shi5de006b2014-10-26 20:20:52 -070034const uint32_t Controller::ERROR_SERVER = 500;
35const uint32_t Controller::ERROR_LBOUND = 400;
Junxiao Shi034c1882016-06-24 18:06:51 +000036ValidatorNull Controller::s_validatorNull;
Junxiao Shi5de006b2014-10-26 20:20:52 -070037
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000038Controller::Controller(Face& face, KeyChain& keyChain, Validator& validator)
Junxiao Shi70911652014-08-12 10:14:24 -070039 : m_face(face)
40 , m_keyChain(keyChain)
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000041 , m_validator(validator)
Junxiao Shi70911652014-08-12 10:14:24 -070042{
43}
44
45void
46Controller::startCommand(const shared_ptr<ControlCommand>& command,
47 const ControlParameters& parameters,
Junxiao Shi600f7112016-07-16 11:57:18 +000048 const CommandSucceedCallback& onSuccess1,
49 const CommandFailCallback& onFailure1,
Junxiao Shi5de006b2014-10-26 20:20:52 -070050 const CommandOptions& options)
51{
Junxiao Shi600f7112016-07-16 11:57:18 +000052 const CommandSucceedCallback& onSuccess = onSuccess1 ?
53 onSuccess1 : [] (const ControlParameters&) {};
54 const CommandFailCallback& onFailure = onFailure1 ?
55 onFailure1 : [] (uint32_t, const std::string&) {};
56
Junxiao Shi5de006b2014-10-26 20:20:52 -070057 Name requestName = command->getRequestName(options.getPrefix(), parameters);
58 Interest interest(requestName);
59 interest.setInterestLifetime(options.getTimeout());
Junxiao Shic6acc7a2015-06-23 10:03:56 -070060 m_keyChain.sign(interest, options.getSigningInfo());
Junxiao Shi5de006b2014-10-26 20:20:52 -070061
62 m_face.expressInterest(interest,
63 bind(&Controller::processCommandResponse, this, _2,
64 command, onSuccess, onFailure),
Junxiao Shib1990df2015-11-05 00:14:44 +000065 bind(onFailure, ERROR_NACK, "network Nack received"),
Junxiao Shi5de006b2014-10-26 20:20:52 -070066 bind(onFailure, ERROR_TIMEOUT, "request timed out"));
67}
68
69void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070070Controller::processCommandResponse(const Data& data,
71 const shared_ptr<ControlCommand>& command,
72 const CommandSucceedCallback& onSuccess,
73 const CommandFailCallback& onFailure)
74{
75 /// \todo verify Data signature
76
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070077 ControlResponse response;
78 try {
79 response.wireDecode(data.getContent().blockFromValue());
80 }
Junxiao Shi600f7112016-07-16 11:57:18 +000081 catch (const tlv::Error& e) {
82 onFailure(ERROR_SERVER, e.what());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070083 return;
84 }
85
86 uint32_t code = response.getCode();
Junxiao Shi5de006b2014-10-26 20:20:52 -070087 if (code >= ERROR_LBOUND) {
Junxiao Shi600f7112016-07-16 11:57:18 +000088 onFailure(code, response.getText());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070089 return;
90 }
91
92 ControlParameters parameters;
93 try {
94 parameters.wireDecode(response.getBody());
95 }
Junxiao Shi600f7112016-07-16 11:57:18 +000096 catch (const tlv::Error& e) {
97 onFailure(ERROR_SERVER, e.what());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070098 return;
99 }
100
101 try {
102 command->validateResponse(parameters);
103 }
Junxiao Shi600f7112016-07-16 11:57:18 +0000104 catch (const ControlCommand::ArgumentError& e) {
105 onFailure(ERROR_SERVER, e.what());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700106 return;
107 }
108
Junxiao Shi600f7112016-07-16 11:57:18 +0000109 onSuccess(parameters);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700110}
111
Junxiao Shi034c1882016-06-24 18:06:51 +0000112void
113Controller::fetchDataset(const Name& prefix,
114 const std::function<void(const ConstBufferPtr&)>& processResponse,
115 const CommandFailCallback& onFailure,
116 const CommandOptions& options)
117{
118 Interest baseInterest(prefix);
119 baseInterest.setInterestLifetime(options.getTimeout());
120
121 SegmentFetcher::fetch(m_face, baseInterest, m_validator, processResponse,
122 bind(&Controller::processDatasetFetchError, this, onFailure, _1, _2));
123}
124
125void
126Controller::processDatasetFetchError(const CommandFailCallback& onFailure,
127 uint32_t code, std::string msg)
128{
129 switch (static_cast<SegmentFetcher::ErrorCode>(code)) {
130 // It's intentional to cast as SegmentFetcher::ErrorCode, and to not have a 'default' clause.
131 // This forces the switch statement to handle every defined SegmentFetcher::ErrorCode,
132 // and breaks compilation if it does not.
133 case SegmentFetcher::ErrorCode::INTEREST_TIMEOUT:
134 onFailure(ERROR_TIMEOUT, msg);
135 break;
136 case SegmentFetcher::ErrorCode::DATA_HAS_NO_SEGMENT:
137 onFailure(ERROR_SERVER, msg);
138 break;
139 case SegmentFetcher::ErrorCode::SEGMENT_VALIDATION_FAIL:
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000140 /// \todo When SegmentFetcher exposes validator error code, Controller::ERROR_VALIDATION
141 /// should be replaced with a range that corresponds to validator error codes.
142 onFailure(ERROR_VALIDATION, msg);
Junxiao Shi034c1882016-06-24 18:06:51 +0000143 break;
144 case SegmentFetcher::ErrorCode::NACK_ERROR:
145 onFailure(ERROR_NACK, msg);
146 break;
147 }
148}
149
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800150} // namespace nfd
151} // namespace ndn