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 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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" |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace nfd { |
| 27 | |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 28 | const uint32_t Controller::ERROR_TIMEOUT = 10060; |
| 29 | const uint32_t Controller::ERROR_SERVER = 500; |
| 30 | const uint32_t Controller::ERROR_LBOUND = 400; |
| 31 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 32 | Controller::Controller(Face& face, KeyChain& keyChain) |
| 33 | : m_face(face) |
| 34 | , m_keyChain(keyChain) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | void |
| 39 | Controller::startCommand(const shared_ptr<ControlCommand>& command, |
| 40 | const ControlParameters& parameters, |
| 41 | const CommandSucceedCallback& onSuccess, |
| 42 | const CommandFailCallback& onFailure, |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 43 | const CommandOptions& options) |
| 44 | { |
| 45 | Name requestName = command->getRequestName(options.getPrefix(), parameters); |
| 46 | Interest interest(requestName); |
| 47 | interest.setInterestLifetime(options.getTimeout()); |
| 48 | |
| 49 | switch (options.getSigningParamsKind()) { |
| 50 | case CommandOptions::SIGNING_PARAMS_DEFAULT: |
| 51 | m_keyChain.sign(interest); |
| 52 | break; |
| 53 | case CommandOptions::SIGNING_PARAMS_IDENTITY: |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 54 | m_keyChain.sign(interest, security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 55 | options.getSigningIdentity())); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 56 | break; |
| 57 | case CommandOptions::SIGNING_PARAMS_CERTIFICATE: |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame^] | 58 | m_keyChain.sign(interest, security::SigningInfo(security::SigningInfo::SIGNER_TYPE_CERT, |
| 59 | options.getSigningCertificate())); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 60 | break; |
| 61 | default: |
| 62 | BOOST_ASSERT(false); |
| 63 | break; |
| 64 | } |
| 65 | |
| 66 | m_face.expressInterest(interest, |
| 67 | bind(&Controller::processCommandResponse, this, _2, |
| 68 | command, onSuccess, onFailure), |
| 69 | bind(onFailure, ERROR_TIMEOUT, "request timed out")); |
| 70 | } |
| 71 | |
| 72 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 73 | Controller::processCommandResponse(const Data& data, |
| 74 | const shared_ptr<ControlCommand>& command, |
| 75 | const CommandSucceedCallback& onSuccess, |
| 76 | const CommandFailCallback& onFailure) |
| 77 | { |
| 78 | /// \todo verify Data signature |
| 79 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 80 | ControlResponse response; |
| 81 | try { |
| 82 | response.wireDecode(data.getContent().blockFromValue()); |
| 83 | } |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 84 | catch (tlv::Error& e) { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 85 | if (static_cast<bool>(onFailure)) |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 86 | onFailure(ERROR_SERVER, e.what()); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | |
| 90 | uint32_t code = response.getCode(); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 91 | if (code >= ERROR_LBOUND) { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 92 | if (static_cast<bool>(onFailure)) |
| 93 | onFailure(code, response.getText()); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | ControlParameters parameters; |
| 98 | try { |
| 99 | parameters.wireDecode(response.getBody()); |
| 100 | } |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 101 | catch (tlv::Error& e) { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 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 | |
| 107 | try { |
| 108 | command->validateResponse(parameters); |
| 109 | } |
| 110 | catch (ControlCommand::ArgumentError& e) { |
| 111 | if (static_cast<bool>(onFailure)) |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 112 | onFailure(ERROR_SERVER, e.what()); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 113 | return; |
| 114 | } |
| 115 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 116 | if (static_cast<bool>(onSuccess)) |
| 117 | onSuccess(parameters); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 120 | } // namespace nfd |
| 121 | } // namespace ndn |