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" |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 24 | #include "../security/identity-certificate.hpp" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace nfd { |
| 28 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 29 | Controller::Controller(Face& face) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 30 | : m_face(face) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 31 | { |
| 32 | } |
| 33 | |
| 34 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 35 | Controller::processCommandResponse(const Data& data, |
| 36 | const shared_ptr<ControlCommand>& command, |
| 37 | const CommandSucceedCallback& onSuccess, |
| 38 | const CommandFailCallback& onFailure) |
| 39 | { |
| 40 | /// \todo verify Data signature |
| 41 | |
| 42 | const uint32_t serverErrorCode = 500; |
| 43 | |
| 44 | ControlResponse response; |
| 45 | try { |
| 46 | response.wireDecode(data.getContent().blockFromValue()); |
| 47 | } |
| 48 | catch (ndn::Tlv::Error& e) { |
| 49 | if (static_cast<bool>(onFailure)) |
| 50 | onFailure(serverErrorCode, e.what()); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | uint32_t code = response.getCode(); |
| 55 | const uint32_t errorCodeLowerBound = 400; |
| 56 | if (code >= errorCodeLowerBound) { |
| 57 | if (static_cast<bool>(onFailure)) |
| 58 | onFailure(code, response.getText()); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | ControlParameters parameters; |
| 63 | try { |
| 64 | parameters.wireDecode(response.getBody()); |
| 65 | } |
| 66 | catch (ndn::Tlv::Error& e) { |
| 67 | if (static_cast<bool>(onFailure)) |
| 68 | onFailure(serverErrorCode, e.what()); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | try { |
| 73 | command->validateResponse(parameters); |
| 74 | } |
| 75 | catch (ControlCommand::ArgumentError& e) { |
| 76 | if (static_cast<bool>(onFailure)) |
| 77 | onFailure(serverErrorCode, e.what()); |
| 78 | return; |
| 79 | } |
| 80 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 81 | if (static_cast<bool>(onSuccess)) |
| 82 | onSuccess(parameters); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 86 | } // namespace nfd |
| 87 | } // namespace ndn |