blob: 25800ffca3885da3438753be841ef677d2e4d214 [file] [log] [blame]
Alexander Afanasyeve289b532014-02-09 22:14:44 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Afanasyeve289b532014-02-09 22:14:44 -080011 */
12
Alexander Afanasyeve289b532014-02-09 22:14:44 -080013#include "nfd-controller.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080014#include "nfd-control-response.hpp"
Yingdi Yue66bf2a2014-04-28 17:07:36 -070015#include "../security/identity-certificate.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080016
17namespace ndn {
18namespace nfd {
19
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080020Controller::Controller(Face& face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080021 : m_face(face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080022{
23}
24
25void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070026Controller::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 Afanasyevee8bb1e2014-05-02 17:39:54 -070072 if (static_cast<bool>(onSuccess))
73 onSuccess(parameters);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070074}
75
76
Alexander Afanasyeve289b532014-02-09 22:14:44 -080077} // namespace nfd
78} // namespace ndn