blob: acd243594efd1434ee400dff210101a28b2d235e [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/**
Junxiao Shi034c1882016-06-24 18:06:51 +00003 * Copyright (c) 2013-2016 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"
Junxiao Shi034c1882016-06-24 18:06:51 +000024#include "../util/segment-fetcher.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080025
26namespace ndn {
27namespace nfd {
28
Junxiao Shi034c1882016-06-24 18:06:51 +000029using ndn::util::SegmentFetcher;
30
Junxiao Shib1990df2015-11-05 00:14:44 +000031const uint32_t Controller::ERROR_TIMEOUT = 10060; // WinSock ESAETIMEDOUT
32const uint32_t Controller::ERROR_NACK = 10800; // 10000 + TLV-TYPE of Nack header
Junxiao Shi5de006b2014-10-26 20:20:52 -070033const uint32_t Controller::ERROR_SERVER = 500;
34const uint32_t Controller::ERROR_LBOUND = 400;
Junxiao Shi034c1882016-06-24 18:06:51 +000035ValidatorNull Controller::s_validatorNull;
Junxiao Shi5de006b2014-10-26 20:20:52 -070036
Junxiao Shi70911652014-08-12 10:14:24 -070037Controller::Controller(Face& face, KeyChain& keyChain)
38 : m_face(face)
39 , m_keyChain(keyChain)
Junxiao Shi034c1882016-06-24 18:06:51 +000040 , m_validator(s_validatorNull) /// \todo #3653 accept validator as constructor parameter
Junxiao Shi70911652014-08-12 10:14:24 -070041{
42}
43
44void
45Controller::startCommand(const shared_ptr<ControlCommand>& command,
46 const ControlParameters& parameters,
47 const CommandSucceedCallback& onSuccess,
48 const CommandFailCallback& onFailure,
Junxiao Shi5de006b2014-10-26 20:20:52 -070049 const CommandOptions& options)
50{
51 Name requestName = command->getRequestName(options.getPrefix(), parameters);
52 Interest interest(requestName);
53 interest.setInterestLifetime(options.getTimeout());
Junxiao Shic6acc7a2015-06-23 10:03:56 -070054 m_keyChain.sign(interest, options.getSigningInfo());
Junxiao Shi5de006b2014-10-26 20:20:52 -070055
56 m_face.expressInterest(interest,
57 bind(&Controller::processCommandResponse, this, _2,
58 command, onSuccess, onFailure),
Junxiao Shib1990df2015-11-05 00:14:44 +000059 bind(onFailure, ERROR_NACK, "network Nack received"),
Junxiao Shi5de006b2014-10-26 20:20:52 -070060 bind(onFailure, ERROR_TIMEOUT, "request timed out"));
61}
62
63void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070064Controller::processCommandResponse(const Data& data,
65 const shared_ptr<ControlCommand>& command,
66 const CommandSucceedCallback& onSuccess,
67 const CommandFailCallback& onFailure)
68{
69 /// \todo verify Data signature
70
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070071 ControlResponse response;
72 try {
73 response.wireDecode(data.getContent().blockFromValue());
74 }
Junxiao Shi5de006b2014-10-26 20:20:52 -070075 catch (tlv::Error& e) {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070076 if (static_cast<bool>(onFailure))
Junxiao Shi5de006b2014-10-26 20:20:52 -070077 onFailure(ERROR_SERVER, e.what());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070078 return;
79 }
80
81 uint32_t code = response.getCode();
Junxiao Shi5de006b2014-10-26 20:20:52 -070082 if (code >= ERROR_LBOUND) {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070083 if (static_cast<bool>(onFailure))
84 onFailure(code, response.getText());
85 return;
86 }
87
88 ControlParameters parameters;
89 try {
90 parameters.wireDecode(response.getBody());
91 }
Junxiao Shi5de006b2014-10-26 20:20:52 -070092 catch (tlv::Error& e) {
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070093 if (static_cast<bool>(onFailure))
Junxiao Shi5de006b2014-10-26 20:20:52 -070094 onFailure(ERROR_SERVER, e.what());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070095 return;
96 }
97
98 try {
99 command->validateResponse(parameters);
100 }
101 catch (ControlCommand::ArgumentError& e) {
102 if (static_cast<bool>(onFailure))
Junxiao Shi5de006b2014-10-26 20:20:52 -0700103 onFailure(ERROR_SERVER, e.what());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700104 return;
105 }
106
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700107 if (static_cast<bool>(onSuccess))
108 onSuccess(parameters);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700109}
110
Junxiao Shi034c1882016-06-24 18:06:51 +0000111void
112Controller::fetchDataset(const Name& prefix,
113 const std::function<void(const ConstBufferPtr&)>& processResponse,
114 const CommandFailCallback& onFailure,
115 const CommandOptions& options)
116{
117 Interest baseInterest(prefix);
118 baseInterest.setInterestLifetime(options.getTimeout());
119
120 SegmentFetcher::fetch(m_face, baseInterest, m_validator, processResponse,
121 bind(&Controller::processDatasetFetchError, this, onFailure, _1, _2));
122}
123
124void
125Controller::processDatasetFetchError(const CommandFailCallback& onFailure,
126 uint32_t code, std::string msg)
127{
128 switch (static_cast<SegmentFetcher::ErrorCode>(code)) {
129 // It's intentional to cast as SegmentFetcher::ErrorCode, and to not have a 'default' clause.
130 // This forces the switch statement to handle every defined SegmentFetcher::ErrorCode,
131 // and breaks compilation if it does not.
132 case SegmentFetcher::ErrorCode::INTEREST_TIMEOUT:
133 onFailure(ERROR_TIMEOUT, msg);
134 break;
135 case SegmentFetcher::ErrorCode::DATA_HAS_NO_SEGMENT:
136 onFailure(ERROR_SERVER, msg);
137 break;
138 case SegmentFetcher::ErrorCode::SEGMENT_VALIDATION_FAIL:
139 BOOST_ASSERT(false); /// \todo #3653 introduce ERROR_VALIDATION
140 break;
141 case SegmentFetcher::ErrorCode::NACK_ERROR:
142 onFailure(ERROR_NACK, msg);
143 break;
144 }
145}
146
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800147} // namespace nfd
148} // namespace ndn