blob: 8d823191554287bbf57f7046fa15a62fe0c67260 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyeve289b532014-02-09 22:14:44 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyeve289b532014-02-09 22:14:44 -080020 */
21
Alexander Afanasyeve289b532014-02-09 22:14:44 -080022#include "nfd-controller.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080023#include "nfd-control-response.hpp"
Yingdi Yue66bf2a2014-04-28 17:07:36 -070024#include "../security/identity-certificate.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080025
26namespace ndn {
27namespace nfd {
28
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080029Controller::Controller(Face& face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080030 : m_face(face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080031{
32}
33
34void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070035Controller::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 Afanasyevee8bb1e2014-05-02 17:39:54 -070081 if (static_cast<bool>(onSuccess))
82 onSuccess(parameters);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070083}
84
85
Alexander Afanasyeve289b532014-02-09 22:14:44 -080086} // namespace nfd
87} // namespace ndn