blob: 2d5b34857b7457bbd657c2adaeb359e0c98ea24a [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"
24
25namespace ndn {
26namespace nfd {
27
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080028Controller::Controller(Face& face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080029 : m_face(face)
Junxiao Shi70911652014-08-12 10:14:24 -070030 , m_internalKeyChain(make_shared<KeyChain>())
31 , m_keyChain(*m_internalKeyChain)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080032{
33}
34
Junxiao Shi70911652014-08-12 10:14:24 -070035Controller::Controller(Face& face, KeyChain& keyChain)
36 : m_face(face)
37 , m_keyChain(keyChain)
38{
39}
40
41void
42Controller::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 Afanasyeve289b532014-02-09 22:14:44 -080064void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070065Controller::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 DiBenedetto54ce6682014-07-22 13:22:57 -060078 catch (ndn::tlv::Error& e) {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070079 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 DiBenedetto54ce6682014-07-22 13:22:57 -060096 catch (ndn::tlv::Error& e) {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070097 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 Afanasyevee8bb1e2014-05-02 17:39:54 -0700111 if (static_cast<bool>(onSuccess))
112 onSuccess(parameters);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700113}
114
115
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800116} // namespace nfd
117} // namespace ndn