blob: dd6640d387df610a6ddb2796c9e16442c4114bef [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventobf1c0692017-01-15 19:15:09 -05003 * Copyright (c) 2016-2017, Regents of the University of California,
4 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
Andrea Tosatto672b9a72016-01-05 16:18:20 +01006 *
7 * This file is part of ndn-tools (Named Data Networking Essential Tools).
8 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
9 *
10 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
22 *
23 * @author Wentao Shang
24 * @author Steve DiBenedetto
25 * @author Andrea Tosatto
Weiwei Liue4765012016-06-01 00:10:29 -070026 * @author Davide Pesavento
27 * @author Weiwei Liu
Andrea Tosatto672b9a72016-01-05 16:18:20 +010028 */
29
30#include "pipeline-interests.hpp"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010031
32namespace ndn {
33namespace chunks {
34
Weiwei Liue4765012016-06-01 00:10:29 -070035PipelineInterests::PipelineInterests(Face& face)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010036 : m_face(face)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010037 , m_lastSegmentNo(0)
Weiwei Liue4765012016-06-01 00:10:29 -070038 , m_excludedSegmentNo(0)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010039 , m_hasFinalBlockId(false)
Weiwei Liue4765012016-06-01 00:10:29 -070040 , m_isStopping(false)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010041{
Andrea Tosatto672b9a72016-01-05 16:18:20 +010042}
43
Weiwei Liue4765012016-06-01 00:10:29 -070044PipelineInterests::~PipelineInterests() = default;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010045
46void
Weiwei Liue4765012016-06-01 00:10:29 -070047PipelineInterests::run(const Data& data, DataCallback onData, FailureCallback onFailure)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010048{
49 BOOST_ASSERT(onData != nullptr);
50 m_onData = std::move(onData);
51 m_onFailure = std::move(onFailure);
Weiwei Liue4765012016-06-01 00:10:29 -070052 m_prefix = data.getName().getPrefix(-1);
Davide Pesaventobf1c0692017-01-15 19:15:09 -050053 m_excludedSegmentNo = getSegmentFromPacket(data);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010054
55 if (!data.getFinalBlockId().empty()) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010056 m_lastSegmentNo = data.getFinalBlockId().toSegment();
Weiwei Liue4765012016-06-01 00:10:29 -070057 m_hasFinalBlockId = true;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010058 }
59
Davide Pesaventobf1c0692017-01-15 19:15:09 -050060 // record the start time of the pipeline
61 m_startTime = time::steady_clock::now();
62
Weiwei Liue4765012016-06-01 00:10:29 -070063 doRun();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010064}
65
66void
67PipelineInterests::cancel()
68{
Weiwei Liue4765012016-06-01 00:10:29 -070069 if (m_isStopping)
70 return;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010071
Weiwei Liue4765012016-06-01 00:10:29 -070072 m_isStopping = true;
73 doCancel();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010074}
75
76void
Weiwei Liue4765012016-06-01 00:10:29 -070077PipelineInterests::onFailure(const std::string& reason)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010078{
Weiwei Liue4765012016-06-01 00:10:29 -070079 if (m_isStopping)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010080 return;
81
Weiwei Liue4765012016-06-01 00:10:29 -070082 cancel();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010083
Weiwei Liue4765012016-06-01 00:10:29 -070084 if (m_onFailure)
85 m_face.getIoService().post([this, reason] { m_onFailure(reason); });
Andrea Tosatto672b9a72016-01-05 16:18:20 +010086}
87
88} // namespace chunks
89} // namespace ndn