Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 13 | #include "nfd-controller.hpp" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 14 | #include "nfd-control-response.hpp" |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 15 | #include "../security/identity-certificate.hpp" |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 16 | |
| 17 | namespace ndn { |
| 18 | namespace nfd { |
| 19 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 20 | Controller::Controller(Face& face) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 21 | : m_face(face) |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 22 | { |
| 23 | } |
| 24 | |
| 25 | void |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 26 | Controller::processCommandResponse(const Data& data, |
| 27 | const shared_ptr<ControlCommand>& command, |
| 28 | const CommandSucceedCallback& onSuccess, |
| 29 | const CommandFailCallback& onFailure) |
| 30 | { |
| 31 | /// \todo verify Data signature |
| 32 | |
| 33 | const uint32_t serverErrorCode = 500; |
| 34 | |
| 35 | ControlResponse response; |
| 36 | try { |
| 37 | response.wireDecode(data.getContent().blockFromValue()); |
| 38 | } |
| 39 | catch (ndn::Tlv::Error& e) { |
| 40 | if (static_cast<bool>(onFailure)) |
| 41 | onFailure(serverErrorCode, e.what()); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | uint32_t code = response.getCode(); |
| 46 | const uint32_t errorCodeLowerBound = 400; |
| 47 | if (code >= errorCodeLowerBound) { |
| 48 | if (static_cast<bool>(onFailure)) |
| 49 | onFailure(code, response.getText()); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | ControlParameters parameters; |
| 54 | try { |
| 55 | parameters.wireDecode(response.getBody()); |
| 56 | } |
| 57 | catch (ndn::Tlv::Error& e) { |
| 58 | if (static_cast<bool>(onFailure)) |
| 59 | onFailure(serverErrorCode, e.what()); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | try { |
| 64 | command->validateResponse(parameters); |
| 65 | } |
| 66 | catch (ControlCommand::ArgumentError& e) { |
| 67 | if (static_cast<bool>(onFailure)) |
| 68 | onFailure(serverErrorCode, e.what()); |
| 69 | return; |
| 70 | } |
| 71 | |
Alexander Afanasyev | ee8bb1e | 2014-05-02 17:39:54 -0700 | [diff] [blame] | 72 | if (static_cast<bool>(onSuccess)) |
| 73 | onSuccess(parameters); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
Alexander Afanasyev | e289b53 | 2014-02-09 22:14:44 -0800 | [diff] [blame] | 77 | } // namespace nfd |
| 78 | } // namespace ndn |