blob: 90cd4542f65da08cb9a3ad5f702d9d7293fb3760 [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
Junxiao Shi5de006b2014-10-26 20:20:52 -070028const uint32_t Controller::ERROR_TIMEOUT = 10060;
29const uint32_t Controller::ERROR_SERVER = 500;
30const uint32_t Controller::ERROR_LBOUND = 400;
31
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080032Controller::Controller(Face& face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080033 : m_face(face)
Junxiao Shi70911652014-08-12 10:14:24 -070034 , m_internalKeyChain(make_shared<KeyChain>())
35 , m_keyChain(*m_internalKeyChain)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080036{
37}
38
Junxiao Shi70911652014-08-12 10:14:24 -070039Controller::Controller(Face& face, KeyChain& keyChain)
40 : m_face(face)
41 , m_keyChain(keyChain)
42{
43}
44
45void
46Controller::startCommand(const shared_ptr<ControlCommand>& command,
47 const ControlParameters& parameters,
48 const CommandSucceedCallback& onSuccess,
49 const CommandFailCallback& onFailure,
Junxiao Shi5de006b2014-10-26 20:20:52 -070050 const CommandOptions& options)
51{
52 Name requestName = command->getRequestName(options.getPrefix(), parameters);
53 Interest interest(requestName);
54 interest.setInterestLifetime(options.getTimeout());
55
56 switch (options.getSigningParamsKind()) {
57 case CommandOptions::SIGNING_PARAMS_DEFAULT:
58 m_keyChain.sign(interest);
59 break;
60 case CommandOptions::SIGNING_PARAMS_IDENTITY:
61 m_keyChain.signByIdentity(interest, options.getSigningIdentity());
62 break;
63 case CommandOptions::SIGNING_PARAMS_CERTIFICATE:
64 m_keyChain.sign(interest, options.getSigningCertificate());
65 break;
66 default:
67 BOOST_ASSERT(false);
68 break;
69 }
70
71 m_face.expressInterest(interest,
72 bind(&Controller::processCommandResponse, this, _2,
73 command, onSuccess, onFailure),
74 bind(onFailure, ERROR_TIMEOUT, "request timed out"));
75}
76
77void
78Controller::startCommand(const shared_ptr<ControlCommand>& command,
79 const ControlParameters& parameters,
80 const CommandSucceedCallback& onSuccess,
81 const CommandFailCallback& onFailure,
Junxiao Shi70911652014-08-12 10:14:24 -070082 const Sign& sign,
83 const time::milliseconds& timeout)
84{
85 BOOST_ASSERT(timeout > time::milliseconds::zero());
86
Junxiao Shi415b17c2014-11-12 00:43:25 -070087 Name requestName = command->getRequestName(CommandOptions::DEFAULT_PREFIX, parameters);
Junxiao Shi70911652014-08-12 10:14:24 -070088 Interest interest(requestName);
89 interest.setInterestLifetime(timeout);
90 sign(interest);
91
Junxiao Shi70911652014-08-12 10:14:24 -070092 m_face.expressInterest(interest,
93 bind(&Controller::processCommandResponse, this, _2,
94 command, onSuccess, onFailure),
Junxiao Shi5de006b2014-10-26 20:20:52 -070095 bind(onFailure, ERROR_TIMEOUT, "request timed out"));
Junxiao Shi70911652014-08-12 10:14:24 -070096}
97
Alexander Afanasyeve289b532014-02-09 22:14:44 -080098void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070099Controller::processCommandResponse(const Data& data,
100 const shared_ptr<ControlCommand>& command,
101 const CommandSucceedCallback& onSuccess,
102 const CommandFailCallback& onFailure)
103{
104 /// \todo verify Data signature
105
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700106 ControlResponse response;
107 try {
108 response.wireDecode(data.getContent().blockFromValue());
109 }
Junxiao Shi5de006b2014-10-26 20:20:52 -0700110 catch (tlv::Error& e) {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700111 if (static_cast<bool>(onFailure))
Junxiao Shi5de006b2014-10-26 20:20:52 -0700112 onFailure(ERROR_SERVER, e.what());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700113 return;
114 }
115
116 uint32_t code = response.getCode();
Junxiao Shi5de006b2014-10-26 20:20:52 -0700117 if (code >= ERROR_LBOUND) {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700118 if (static_cast<bool>(onFailure))
119 onFailure(code, response.getText());
120 return;
121 }
122
123 ControlParameters parameters;
124 try {
125 parameters.wireDecode(response.getBody());
126 }
Junxiao Shi5de006b2014-10-26 20:20:52 -0700127 catch (tlv::Error& e) {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700128 if (static_cast<bool>(onFailure))
Junxiao Shi5de006b2014-10-26 20:20:52 -0700129 onFailure(ERROR_SERVER, e.what());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700130 return;
131 }
132
133 try {
134 command->validateResponse(parameters);
135 }
136 catch (ControlCommand::ArgumentError& e) {
137 if (static_cast<bool>(onFailure))
Junxiao Shi5de006b2014-10-26 20:20:52 -0700138 onFailure(ERROR_SERVER, e.what());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700139 return;
140 }
141
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700142 if (static_cast<bool>(onSuccess))
143 onSuccess(parameters);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700144}
145
146
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800147} // namespace nfd
148} // namespace ndn