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 | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 33 | const uint32_t Controller::ERROR_SERVER = 500; |
| 34 | const uint32_t Controller::ERROR_LBOUND = 400; |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 35 | ValidatorNull Controller::s_validatorNull; |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 36 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 37 | Controller::Controller(Face& face, KeyChain& keyChain) |
| 38 | : m_face(face) |
| 39 | , m_keyChain(keyChain) |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 40 | , m_validator(s_validatorNull) /// \todo #3653 accept validator as constructor parameter |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | Controller::startCommand(const shared_ptr<ControlCommand>& command, |
| 46 | const ControlParameters& parameters, |
| 47 | const CommandSucceedCallback& onSuccess, |
| 48 | const CommandFailCallback& onFailure, |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 49 | const CommandOptions& options) |
| 50 | { |
| 51 | Name requestName = command->getRequestName(options.getPrefix(), parameters); |
| 52 | Interest interest(requestName); |
| 53 | interest.setInterestLifetime(options.getTimeout()); |
Junxiao Shi | c6acc7a | 2015-06-23 10:03:56 -0700 | [diff] [blame] | 54 | m_keyChain.sign(interest, options.getSigningInfo()); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 55 | |
| 56 | m_face.expressInterest(interest, |
| 57 | bind(&Controller::processCommandResponse, this, _2, |
| 58 | command, onSuccess, onFailure), |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 59 | bind(onFailure, ERROR_NACK, "network Nack received"), |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 60 | bind(onFailure, ERROR_TIMEOUT, "request timed out")); |
| 61 | } |
| 62 | |
| 63 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 64 | Controller::processCommandResponse(const Data& data, |
| 65 | const shared_ptr<ControlCommand>& command, |
| 66 | const CommandSucceedCallback& onSuccess, |
| 67 | const CommandFailCallback& onFailure) |
| 68 | { |
| 69 | /// \todo verify Data signature |
| 70 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 71 | ControlResponse response; |
| 72 | try { |
| 73 | response.wireDecode(data.getContent().blockFromValue()); |
| 74 | } |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 75 | catch (tlv::Error& e) { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 76 | if (static_cast<bool>(onFailure)) |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 77 | onFailure(ERROR_SERVER, e.what()); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | |
| 81 | uint32_t code = response.getCode(); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 82 | if (code >= ERROR_LBOUND) { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 83 | if (static_cast<bool>(onFailure)) |
| 84 | onFailure(code, response.getText()); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | ControlParameters parameters; |
| 89 | try { |
| 90 | parameters.wireDecode(response.getBody()); |
| 91 | } |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 92 | catch (tlv::Error& e) { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 93 | if (static_cast<bool>(onFailure)) |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 94 | onFailure(ERROR_SERVER, e.what()); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | |
| 98 | try { |
| 99 | command->validateResponse(parameters); |
| 100 | } |
| 101 | catch (ControlCommand::ArgumentError& e) { |
| 102 | if (static_cast<bool>(onFailure)) |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 103 | onFailure(ERROR_SERVER, e.what()); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 107 | if (static_cast<bool>(onSuccess)) |
| 108 | onSuccess(parameters); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 111 | void |
| 112 | Controller::fetchDataset(const Name& prefix, |
| 113 | const std::function<void(const ConstBufferPtr&)>& processResponse, |
| 114 | const CommandFailCallback& onFailure, |
| 115 | const CommandOptions& options) |
| 116 | { |
| 117 | Interest baseInterest(prefix); |
| 118 | baseInterest.setInterestLifetime(options.getTimeout()); |
| 119 | |
| 120 | SegmentFetcher::fetch(m_face, baseInterest, m_validator, processResponse, |
| 121 | bind(&Controller::processDatasetFetchError, this, onFailure, _1, _2)); |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | Controller::processDatasetFetchError(const CommandFailCallback& onFailure, |
| 126 | uint32_t code, std::string msg) |
| 127 | { |
| 128 | switch (static_cast<SegmentFetcher::ErrorCode>(code)) { |
| 129 | // It's intentional to cast as SegmentFetcher::ErrorCode, and to not have a 'default' clause. |
| 130 | // This forces the switch statement to handle every defined SegmentFetcher::ErrorCode, |
| 131 | // and breaks compilation if it does not. |
| 132 | case SegmentFetcher::ErrorCode::INTEREST_TIMEOUT: |
| 133 | onFailure(ERROR_TIMEOUT, msg); |
| 134 | break; |
| 135 | case SegmentFetcher::ErrorCode::DATA_HAS_NO_SEGMENT: |
| 136 | onFailure(ERROR_SERVER, msg); |
| 137 | break; |
| 138 | case SegmentFetcher::ErrorCode::SEGMENT_VALIDATION_FAIL: |
| 139 | BOOST_ASSERT(false); /// \todo #3653 introduce ERROR_VALIDATION |
| 140 | break; |
| 141 | case SegmentFetcher::ErrorCode::NACK_ERROR: |
| 142 | onFailure(ERROR_NACK, msg); |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 147 | } // namespace nfd |
| 148 | } // namespace ndn |