blob: 9f2e3b53d15253a3cd36393296a07a67505b32a4 [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +00002/*
Davide Pesaventoa0e6b602021-01-21 19:47:04 -05003 * Copyright (c) 2016-2021, Regents of the University of California,
Davide Pesaventobf1c0692017-01-15 19:15:09 -05004 * 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
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +000028 * @author Chavoosh Ghasemi
Andrea Tosatto672b9a72016-01-05 16:18:20 +010029 */
30
31#include "pipeline-interests.hpp"
Davide Pesavento97a33b22019-10-17 22:10:47 -040032#include "data-fetcher.hpp"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010033
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050034#include <boost/asio/io_service.hpp>
35
Andrea Tosatto672b9a72016-01-05 16:18:20 +010036namespace ndn {
37namespace chunks {
38
Davide Pesavento97a33b22019-10-17 22:10:47 -040039PipelineInterests::PipelineInterests(Face& face, const Options& opts)
40 : m_options(opts)
41 , m_face(face)
Davide Pesaventoe9c69852017-11-04 18:08:37 -040042 , m_hasFinalBlockId(false)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010043 , m_lastSegmentNo(0)
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +000044 , m_nReceived(0)
Davide Pesaventob587cfd2017-11-04 16:29:50 -040045 , m_receivedSize(0)
Davide Pesaventob587cfd2017-11-04 16:29:50 -040046 , m_nextSegmentNo(0)
Weiwei Liue4765012016-06-01 00:10:29 -070047 , m_isStopping(false)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010048{
Andrea Tosatto672b9a72016-01-05 16:18:20 +010049}
50
Weiwei Liue4765012016-06-01 00:10:29 -070051PipelineInterests::~PipelineInterests() = default;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010052
53void
Chavoosh Ghasemi5cb67012019-02-15 09:56:57 -080054PipelineInterests::run(const Name& versionedName, DataCallback dataCb, FailureCallback failureCb)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010055{
Davide Pesavento296b3852019-10-20 16:00:16 -040056 BOOST_ASSERT(m_options.disableVersionDiscovery ||
57 (!versionedName.empty() && versionedName[-1].isVersion()));
Davide Pesaventoe9c69852017-11-04 18:08:37 -040058 BOOST_ASSERT(dataCb != nullptr);
Davide Pesavento296b3852019-10-20 16:00:16 -040059
Chavoosh Ghasemi5cb67012019-02-15 09:56:57 -080060 m_prefix = versionedName;
Davide Pesaventoe9c69852017-11-04 18:08:37 -040061 m_onData = std::move(dataCb);
62 m_onFailure = std::move(failureCb);
Davide Pesaventoe9c69852017-11-04 18:08:37 -040063
Davide Pesaventobf1c0692017-01-15 19:15:09 -050064 // record the start time of the pipeline
65 m_startTime = time::steady_clock::now();
66
Weiwei Liue4765012016-06-01 00:10:29 -070067 doRun();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010068}
69
70void
71PipelineInterests::cancel()
72{
Weiwei Liue4765012016-06-01 00:10:29 -070073 if (m_isStopping)
74 return;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010075
Weiwei Liue4765012016-06-01 00:10:29 -070076 m_isStopping = true;
77 doCancel();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010078}
79
Ryan Wickman2c9933c2018-06-12 11:51:51 -050080bool
81PipelineInterests::allSegmentsReceived() const
82{
Chavoosh Ghasemi5cb67012019-02-15 09:56:57 -080083 return m_nReceived > 0 &&
84 m_hasFinalBlockId &&
85 static_cast<uint64_t>(m_nReceived - 1) >= m_lastSegmentNo;
Ryan Wickman2c9933c2018-06-12 11:51:51 -050086}
87
Davide Pesaventob587cfd2017-11-04 16:29:50 -040088uint64_t
89PipelineInterests::getNextSegmentNo()
90{
Davide Pesaventob587cfd2017-11-04 16:29:50 -040091 return m_nextSegmentNo++;
92}
93
Andrea Tosatto672b9a72016-01-05 16:18:20 +010094void
Davide Pesaventoe9c69852017-11-04 18:08:37 -040095PipelineInterests::onData(const Data& data)
96{
97 m_nReceived++;
98 m_receivedSize += data.getContent().value_size();
99
100 m_onData(data);
101}
102
103void
Weiwei Liue4765012016-06-01 00:10:29 -0700104PipelineInterests::onFailure(const std::string& reason)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100105{
Weiwei Liue4765012016-06-01 00:10:29 -0700106 if (m_isStopping)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100107 return;
108
Weiwei Liue4765012016-06-01 00:10:29 -0700109 cancel();
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100110
Weiwei Liue4765012016-06-01 00:10:29 -0700111 if (m_onFailure)
112 m_face.getIoService().post([this, reason] { m_onFailure(reason); });
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100113}
114
Davide Pesavento97a33b22019-10-17 22:10:47 -0400115void
116PipelineInterests::printOptions() const
117{
118 std::cerr << "Pipeline parameters:\n"
119 << "\tRequest fresh content = " << (m_options.mustBeFresh ? "yes" : "no") << "\n"
120 << "\tInterest lifetime = " << m_options.interestLifetime << "\n"
121 << "\tMax retries on timeout or Nack = " <<
122 (m_options.maxRetriesOnTimeoutOrNack == DataFetcher::MAX_RETRIES_INFINITE ?
123 "infinite" : to_string(m_options.maxRetriesOnTimeoutOrNack)) << "\n";
124}
125
126void
127PipelineInterests::printSummary() const
128{
129 using namespace ndn::time;
130 duration<double, seconds::period> timeElapsed = steady_clock::now() - getStartTime();
131 double throughput = 8 * m_receivedSize / timeElapsed.count();
132
133 std::cerr << "\n\nAll segments have been received.\n"
134 << "Time elapsed: " << timeElapsed << "\n"
135 << "Segments received: " << m_nReceived << "\n"
136 << "Transferred size: " << m_receivedSize / 1e3 << " kB" << "\n"
137 << "Goodput: " << formatThroughput(throughput) << "\n";
138}
139
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000140std::string
141PipelineInterests::formatThroughput(double throughput)
142{
143 int pow = 0;
144 while (throughput >= 1000.0 && pow < 4) {
145 throughput /= 1000.0;
146 pow++;
147 }
148 switch (pow) {
149 case 0:
150 return to_string(throughput) + " bit/s";
151 case 1:
152 return to_string(throughput) + " kbit/s";
153 case 2:
154 return to_string(throughput) + " Mbit/s";
155 case 3:
156 return to_string(throughput) + " Gbit/s";
157 case 4:
158 return to_string(throughput) + " Tbit/s";
159 }
160 return "";
161}
162
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100163} // namespace chunks
164} // namespace ndn