Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 2 | /** |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 22 | #include "nfd-controller.hpp" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 23 | #include "nfd-control-response.hpp" |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 24 | #include "../util/segment-fetcher.hpp" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace nfd { |
| 28 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 29 | using ndn::util::SegmentFetcher; |
| 30 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 31 | const uint32_t Controller::ERROR_TIMEOUT = 10060; // WinSock ESAETIMEDOUT |
| 32 | const uint32_t Controller::ERROR_NACK = 10800; // 10000 + TLV-TYPE of Nack header |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 33 | const uint32_t Controller::ERROR_VALIDATION = 10021; // 10000 + TLS1_ALERT_DECRYPTION_FAILED |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 34 | const uint32_t Controller::ERROR_SERVER = 500; |
| 35 | const uint32_t Controller::ERROR_LBOUND = 400; |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 36 | ValidatorNull Controller::s_validatorNull; |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 37 | |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 38 | Controller::Controller(Face& face, KeyChain& keyChain, Validator& validator) |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 39 | : m_face(face) |
| 40 | , m_keyChain(keyChain) |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 41 | , m_validator(validator) |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | Controller::startCommand(const shared_ptr<ControlCommand>& command, |
| 47 | const ControlParameters& parameters, |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 48 | const CommandSucceedCallback& onSuccess1, |
| 49 | const CommandFailCallback& onFailure1, |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 50 | const CommandOptions& options) |
| 51 | { |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 52 | const CommandSucceedCallback& onSuccess = onSuccess1 ? |
| 53 | onSuccess1 : [] (const ControlParameters&) {}; |
| 54 | const CommandFailCallback& onFailure = onFailure1 ? |
| 55 | onFailure1 : [] (uint32_t, const std::string&) {}; |
| 56 | |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 57 | Name requestName = command->getRequestName(options.getPrefix(), parameters); |
| 58 | Interest interest(requestName); |
| 59 | interest.setInterestLifetime(options.getTimeout()); |
Junxiao Shi | c6acc7a | 2015-06-23 10:03:56 -0700 | [diff] [blame] | 60 | m_keyChain.sign(interest, options.getSigningInfo()); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 61 | |
| 62 | m_face.expressInterest(interest, |
| 63 | bind(&Controller::processCommandResponse, this, _2, |
| 64 | command, onSuccess, onFailure), |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 65 | bind(onFailure, ERROR_NACK, "network Nack received"), |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 66 | bind(onFailure, ERROR_TIMEOUT, "request timed out")); |
| 67 | } |
| 68 | |
| 69 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 70 | Controller::processCommandResponse(const Data& data, |
| 71 | const shared_ptr<ControlCommand>& command, |
| 72 | const CommandSucceedCallback& onSuccess, |
| 73 | const CommandFailCallback& onFailure) |
| 74 | { |
Junxiao Shi | 54f727d | 2016-08-08 20:29:11 +0000 | [diff] [blame] | 75 | m_validator.validate(data, |
| 76 | [=] (const shared_ptr<const Data>& data) { |
| 77 | this->processValidatedCommandResponse(*data, command, onSuccess, onFailure); |
| 78 | }, |
| 79 | [=] (const shared_ptr<const Data>&, const std::string& msg) { |
| 80 | onFailure(ERROR_VALIDATION, msg); |
| 81 | } |
| 82 | ); |
| 83 | } |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 84 | |
Junxiao Shi | 54f727d | 2016-08-08 20:29:11 +0000 | [diff] [blame] | 85 | void |
| 86 | Controller::processValidatedCommandResponse(const Data& data, |
| 87 | const shared_ptr<ControlCommand>& command, |
| 88 | const CommandSucceedCallback& onSuccess, |
| 89 | const CommandFailCallback& onFailure) |
| 90 | { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 91 | ControlResponse response; |
| 92 | try { |
| 93 | response.wireDecode(data.getContent().blockFromValue()); |
| 94 | } |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 95 | catch (const tlv::Error& e) { |
| 96 | onFailure(ERROR_SERVER, e.what()); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
| 100 | uint32_t code = response.getCode(); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 101 | if (code >= ERROR_LBOUND) { |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 102 | onFailure(code, response.getText()); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | |
| 106 | ControlParameters parameters; |
| 107 | try { |
| 108 | parameters.wireDecode(response.getBody()); |
| 109 | } |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 110 | catch (const tlv::Error& e) { |
| 111 | onFailure(ERROR_SERVER, e.what()); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | |
| 115 | try { |
| 116 | command->validateResponse(parameters); |
| 117 | } |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 118 | catch (const ControlCommand::ArgumentError& e) { |
| 119 | onFailure(ERROR_SERVER, e.what()); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 120 | return; |
| 121 | } |
| 122 | |
Junxiao Shi | 600f711 | 2016-07-16 11:57:18 +0000 | [diff] [blame] | 123 | onSuccess(parameters); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 126 | void |
| 127 | Controller::fetchDataset(const Name& prefix, |
| 128 | const std::function<void(const ConstBufferPtr&)>& processResponse, |
| 129 | const CommandFailCallback& onFailure, |
| 130 | const CommandOptions& options) |
| 131 | { |
| 132 | Interest baseInterest(prefix); |
| 133 | baseInterest.setInterestLifetime(options.getTimeout()); |
| 134 | |
| 135 | SegmentFetcher::fetch(m_face, baseInterest, m_validator, processResponse, |
| 136 | bind(&Controller::processDatasetFetchError, this, onFailure, _1, _2)); |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | Controller::processDatasetFetchError(const CommandFailCallback& onFailure, |
| 141 | uint32_t code, std::string msg) |
| 142 | { |
| 143 | switch (static_cast<SegmentFetcher::ErrorCode>(code)) { |
| 144 | // It's intentional to cast as SegmentFetcher::ErrorCode, and to not have a 'default' clause. |
| 145 | // This forces the switch statement to handle every defined SegmentFetcher::ErrorCode, |
| 146 | // and breaks compilation if it does not. |
| 147 | case SegmentFetcher::ErrorCode::INTEREST_TIMEOUT: |
| 148 | onFailure(ERROR_TIMEOUT, msg); |
| 149 | break; |
| 150 | case SegmentFetcher::ErrorCode::DATA_HAS_NO_SEGMENT: |
| 151 | onFailure(ERROR_SERVER, msg); |
| 152 | break; |
| 153 | case SegmentFetcher::ErrorCode::SEGMENT_VALIDATION_FAIL: |
Junxiao Shi | 0f3f0b4 | 2016-07-14 13:26:37 +0000 | [diff] [blame] | 154 | /// \todo When SegmentFetcher exposes validator error code, Controller::ERROR_VALIDATION |
| 155 | /// should be replaced with a range that corresponds to validator error codes. |
| 156 | onFailure(ERROR_VALIDATION, msg); |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 157 | break; |
| 158 | case SegmentFetcher::ErrorCode::NACK_ERROR: |
| 159 | onFailure(ERROR_NACK, msg); |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 164 | } // namespace nfd |
| 165 | } // namespace ndn |