blob: ff817e490b9b3ba548175b10b7ee322dac327575 [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2016, Regents of the University of California,
4 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
6 *
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);
53 m_excludedSegmentNo = data.getName()[-1].toSegment();
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
Weiwei Liue4765012016-06-01 00:10:29 -070060 doRun();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010061}
62
63void
64PipelineInterests::cancel()
65{
Weiwei Liue4765012016-06-01 00:10:29 -070066 if (m_isStopping)
67 return;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010068
Weiwei Liue4765012016-06-01 00:10:29 -070069 m_isStopping = true;
70 doCancel();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010071}
72
73void
Weiwei Liue4765012016-06-01 00:10:29 -070074PipelineInterests::onFailure(const std::string& reason)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010075{
Weiwei Liue4765012016-06-01 00:10:29 -070076 if (m_isStopping)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010077 return;
78
Weiwei Liue4765012016-06-01 00:10:29 -070079 cancel();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010080
Weiwei Liue4765012016-06-01 00:10:29 -070081 if (m_onFailure)
82 m_face.getIoService().post([this, reason] { m_onFailure(reason); });
Andrea Tosatto672b9a72016-01-05 16:18:20 +010083}
84
85} // namespace chunks
86} // namespace ndn