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 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 32 | Controller::Controller(Face& face) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 33 | : m_face(face) |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 34 | , m_internalKeyChain(make_shared<KeyChain>()) |
| 35 | , m_keyChain(*m_internalKeyChain) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 39 | Controller::Controller(Face& face, KeyChain& keyChain) |
| 40 | : m_face(face) |
| 41 | , m_keyChain(keyChain) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | Controller::startCommand(const shared_ptr<ControlCommand>& command, |
| 47 | const ControlParameters& parameters, |
| 48 | const CommandSucceedCallback& onSuccess, |
| 49 | const CommandFailCallback& onFailure, |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 50 | const CommandOptions& options) |
| 51 | { |
| 52 | Name requestName = command->getRequestName(options.getPrefix(), parameters); |
| 53 | Interest interest(requestName); |
| 54 | interest.setInterestLifetime(options.getTimeout()); |
| 55 | |
| 56 | switch (options.getSigningParamsKind()) { |
| 57 | case CommandOptions::SIGNING_PARAMS_DEFAULT: |
| 58 | m_keyChain.sign(interest); |
| 59 | break; |
| 60 | case CommandOptions::SIGNING_PARAMS_IDENTITY: |
| 61 | m_keyChain.signByIdentity(interest, options.getSigningIdentity()); |
| 62 | break; |
| 63 | case CommandOptions::SIGNING_PARAMS_CERTIFICATE: |
| 64 | m_keyChain.sign(interest, options.getSigningCertificate()); |
| 65 | break; |
| 66 | default: |
| 67 | BOOST_ASSERT(false); |
| 68 | break; |
| 69 | } |
| 70 | |
| 71 | m_face.expressInterest(interest, |
| 72 | bind(&Controller::processCommandResponse, this, _2, |
| 73 | command, onSuccess, onFailure), |
| 74 | bind(onFailure, ERROR_TIMEOUT, "request timed out")); |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | Controller::startCommand(const shared_ptr<ControlCommand>& command, |
| 79 | const ControlParameters& parameters, |
| 80 | const CommandSucceedCallback& onSuccess, |
| 81 | const CommandFailCallback& onFailure, |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 82 | const Sign& sign, |
| 83 | const time::milliseconds& timeout) |
| 84 | { |
| 85 | BOOST_ASSERT(timeout > time::milliseconds::zero()); |
| 86 | |
| 87 | Name requestName = command->getRequestName(parameters); |
| 88 | Interest interest(requestName); |
| 89 | interest.setInterestLifetime(timeout); |
| 90 | sign(interest); |
| 91 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 92 | m_face.expressInterest(interest, |
| 93 | bind(&Controller::processCommandResponse, this, _2, |
| 94 | command, onSuccess, onFailure), |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 95 | bind(onFailure, ERROR_TIMEOUT, "request timed out")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 98 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 99 | Controller::processCommandResponse(const Data& data, |
| 100 | const shared_ptr<ControlCommand>& command, |
| 101 | const CommandSucceedCallback& onSuccess, |
| 102 | const CommandFailCallback& onFailure) |
| 103 | { |
| 104 | /// \todo verify Data signature |
| 105 | |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 106 | ControlResponse response; |
| 107 | try { |
| 108 | response.wireDecode(data.getContent().blockFromValue()); |
| 109 | } |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 110 | catch (tlv::Error& e) { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 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 | |
| 116 | uint32_t code = response.getCode(); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 117 | if (code >= ERROR_LBOUND) { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 118 | if (static_cast<bool>(onFailure)) |
| 119 | onFailure(code, response.getText()); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | ControlParameters parameters; |
| 124 | try { |
| 125 | parameters.wireDecode(response.getBody()); |
| 126 | } |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 127 | catch (tlv::Error& e) { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 128 | if (static_cast<bool>(onFailure)) |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 129 | onFailure(ERROR_SERVER, e.what()); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | |
| 133 | try { |
| 134 | command->validateResponse(parameters); |
| 135 | } |
| 136 | catch (ControlCommand::ArgumentError& e) { |
| 137 | if (static_cast<bool>(onFailure)) |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 138 | onFailure(ERROR_SERVER, e.what()); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 142 | if (static_cast<bool>(onSuccess)) |
| 143 | onSuccess(parameters); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 147 | } // namespace nfd |
| 148 | } // namespace ndn |