blob: 2ebdb4a377eddd00041d9c1593db42a3ab75dc13 [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 Afanasyev73e30042015-09-17 17:09:51 -07003 * Copyright (c) 2013-2015 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
Junxiao Shib1990df2015-11-05 00:14:44 +000028const uint32_t Controller::ERROR_TIMEOUT = 10060; // WinSock ESAETIMEDOUT
29const uint32_t Controller::ERROR_NACK = 10800; // 10000 + TLV-TYPE of Nack header
Junxiao Shi5de006b2014-10-26 20:20:52 -070030const uint32_t Controller::ERROR_SERVER = 500;
31const uint32_t Controller::ERROR_LBOUND = 400;
32
Junxiao Shi70911652014-08-12 10:14:24 -070033Controller::Controller(Face& face, KeyChain& keyChain)
34 : m_face(face)
35 , m_keyChain(keyChain)
36{
37}
38
39void
40Controller::startCommand(const shared_ptr<ControlCommand>& command,
41 const ControlParameters& parameters,
42 const CommandSucceedCallback& onSuccess,
43 const CommandFailCallback& onFailure,
Junxiao Shi5de006b2014-10-26 20:20:52 -070044 const CommandOptions& options)
45{
46 Name requestName = command->getRequestName(options.getPrefix(), parameters);
47 Interest interest(requestName);
48 interest.setInterestLifetime(options.getTimeout());
Junxiao Shic6acc7a2015-06-23 10:03:56 -070049 m_keyChain.sign(interest, options.getSigningInfo());
Junxiao Shi5de006b2014-10-26 20:20:52 -070050
51 m_face.expressInterest(interest,
52 bind(&Controller::processCommandResponse, this, _2,
53 command, onSuccess, onFailure),
Junxiao Shib1990df2015-11-05 00:14:44 +000054 bind(onFailure, ERROR_NACK, "network Nack received"),
Junxiao Shi5de006b2014-10-26 20:20:52 -070055 bind(onFailure, ERROR_TIMEOUT, "request timed out"));
56}
57
58void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070059Controller::processCommandResponse(const Data& data,
60 const shared_ptr<ControlCommand>& command,
61 const CommandSucceedCallback& onSuccess,
62 const CommandFailCallback& onFailure)
63{
64 /// \todo verify Data signature
65
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070066 ControlResponse response;
67 try {
68 response.wireDecode(data.getContent().blockFromValue());
69 }
Junxiao Shi5de006b2014-10-26 20:20:52 -070070 catch (tlv::Error& e) {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070071 if (static_cast<bool>(onFailure))
Junxiao Shi5de006b2014-10-26 20:20:52 -070072 onFailure(ERROR_SERVER, e.what());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070073 return;
74 }
75
76 uint32_t code = response.getCode();
Junxiao Shi5de006b2014-10-26 20:20:52 -070077 if (code >= ERROR_LBOUND) {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070078 if (static_cast<bool>(onFailure))
79 onFailure(code, response.getText());
80 return;
81 }
82
83 ControlParameters parameters;
84 try {
85 parameters.wireDecode(response.getBody());
86 }
Junxiao Shi5de006b2014-10-26 20:20:52 -070087 catch (tlv::Error& e) {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070088 if (static_cast<bool>(onFailure))
Junxiao Shi5de006b2014-10-26 20:20:52 -070089 onFailure(ERROR_SERVER, e.what());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070090 return;
91 }
92
93 try {
94 command->validateResponse(parameters);
95 }
96 catch (ControlCommand::ArgumentError& e) {
97 if (static_cast<bool>(onFailure))
Junxiao Shi5de006b2014-10-26 20:20:52 -070098 onFailure(ERROR_SERVER, e.what());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070099 return;
100 }
101
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700102 if (static_cast<bool>(onSuccess))
103 onSuccess(parameters);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700104}
105
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800106} // namespace nfd
107} // namespace ndn