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 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 28 | Controller::Controller(Face& face) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 29 | : m_face(face) |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame^] | 30 | , m_internalKeyChain(make_shared<KeyChain>()) |
| 31 | , m_keyChain(*m_internalKeyChain) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 32 | { |
| 33 | } |
| 34 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame^] | 35 | Controller::Controller(Face& face, KeyChain& keyChain) |
| 36 | : m_face(face) |
| 37 | , m_keyChain(keyChain) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | Controller::startCommand(const shared_ptr<ControlCommand>& command, |
| 43 | const ControlParameters& parameters, |
| 44 | const CommandSucceedCallback& onSuccess, |
| 45 | const CommandFailCallback& onFailure, |
| 46 | const Sign& sign, |
| 47 | const time::milliseconds& timeout) |
| 48 | { |
| 49 | BOOST_ASSERT(timeout > time::milliseconds::zero()); |
| 50 | |
| 51 | Name requestName = command->getRequestName(parameters); |
| 52 | Interest interest(requestName); |
| 53 | interest.setInterestLifetime(timeout); |
| 54 | sign(interest); |
| 55 | |
| 56 | // http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668.aspx |
| 57 | const uint32_t timeoutCode = 10060; |
| 58 | m_face.expressInterest(interest, |
| 59 | bind(&Controller::processCommandResponse, this, _2, |
| 60 | command, onSuccess, onFailure), |
| 61 | bind(onFailure, timeoutCode, "Command Interest timed out")); |
| 62 | } |
| 63 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 64 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 65 | Controller::processCommandResponse(const Data& data, |
| 66 | const shared_ptr<ControlCommand>& command, |
| 67 | const CommandSucceedCallback& onSuccess, |
| 68 | const CommandFailCallback& onFailure) |
| 69 | { |
| 70 | /// \todo verify Data signature |
| 71 | |
| 72 | const uint32_t serverErrorCode = 500; |
| 73 | |
| 74 | ControlResponse response; |
| 75 | try { |
| 76 | response.wireDecode(data.getContent().blockFromValue()); |
| 77 | } |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 78 | catch (ndn::tlv::Error& e) { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 79 | if (static_cast<bool>(onFailure)) |
| 80 | onFailure(serverErrorCode, e.what()); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | uint32_t code = response.getCode(); |
| 85 | const uint32_t errorCodeLowerBound = 400; |
| 86 | if (code >= errorCodeLowerBound) { |
| 87 | if (static_cast<bool>(onFailure)) |
| 88 | onFailure(code, response.getText()); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | ControlParameters parameters; |
| 93 | try { |
| 94 | parameters.wireDecode(response.getBody()); |
| 95 | } |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 96 | catch (ndn::tlv::Error& e) { |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 97 | if (static_cast<bool>(onFailure)) |
| 98 | onFailure(serverErrorCode, e.what()); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | try { |
| 103 | command->validateResponse(parameters); |
| 104 | } |
| 105 | catch (ControlCommand::ArgumentError& e) { |
| 106 | if (static_cast<bool>(onFailure)) |
| 107 | onFailure(serverErrorCode, e.what()); |
| 108 | return; |
| 109 | } |
| 110 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 111 | if (static_cast<bool>(onSuccess)) |
| 112 | onSuccess(parameters); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 116 | } // namespace nfd |
| 117 | } // namespace ndn |