blob: b7251edf754eb8b9d46e7b9233adc57ab560a048 [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 Pesavento2ab04a22023-09-15 22:08:22 -04003 * Copyright (c) 2016-2023, 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 Pesavento2ab04a22023-09-15 22:08:22 -040034#include <boost/asio/io_context.hpp>
35#include <boost/asio/post.hpp>
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050036
Davide Pesaventob3570c62022-02-19 19:19:00 -050037namespace ndn::chunks {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010038
Davide Pesavento97a33b22019-10-17 22:10:47 -040039PipelineInterests::PipelineInterests(Face& face, const Options& opts)
40 : m_options(opts)
41 , m_face(face)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010042{
Andrea Tosatto672b9a72016-01-05 16:18:20 +010043}
44
Weiwei Liue4765012016-06-01 00:10:29 -070045PipelineInterests::~PipelineInterests() = default;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010046
47void
Chavoosh Ghasemi5cb67012019-02-15 09:56:57 -080048PipelineInterests::run(const Name& versionedName, DataCallback dataCb, FailureCallback failureCb)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010049{
Davide Pesavento296b3852019-10-20 16:00:16 -040050 BOOST_ASSERT(m_options.disableVersionDiscovery ||
51 (!versionedName.empty() && versionedName[-1].isVersion()));
Davide Pesaventoe9c69852017-11-04 18:08:37 -040052 BOOST_ASSERT(dataCb != nullptr);
Davide Pesavento296b3852019-10-20 16:00:16 -040053
Chavoosh Ghasemi5cb67012019-02-15 09:56:57 -080054 m_prefix = versionedName;
Davide Pesaventoe9c69852017-11-04 18:08:37 -040055 m_onData = std::move(dataCb);
56 m_onFailure = std::move(failureCb);
Davide Pesaventoe9c69852017-11-04 18:08:37 -040057
Davide Pesaventobf1c0692017-01-15 19:15:09 -050058 // record the start time of the pipeline
59 m_startTime = time::steady_clock::now();
60
Weiwei Liue4765012016-06-01 00:10:29 -070061 doRun();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010062}
63
64void
65PipelineInterests::cancel()
66{
Weiwei Liue4765012016-06-01 00:10:29 -070067 if (m_isStopping)
68 return;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010069
Weiwei Liue4765012016-06-01 00:10:29 -070070 m_isStopping = true;
71 doCancel();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010072}
73
Ryan Wickman2c9933c2018-06-12 11:51:51 -050074bool
75PipelineInterests::allSegmentsReceived() const
76{
Chavoosh Ghasemi5cb67012019-02-15 09:56:57 -080077 return m_nReceived > 0 &&
78 m_hasFinalBlockId &&
79 static_cast<uint64_t>(m_nReceived - 1) >= m_lastSegmentNo;
Ryan Wickman2c9933c2018-06-12 11:51:51 -050080}
81
Davide Pesaventob587cfd2017-11-04 16:29:50 -040082uint64_t
83PipelineInterests::getNextSegmentNo()
84{
Davide Pesaventob587cfd2017-11-04 16:29:50 -040085 return m_nextSegmentNo++;
86}
87
Andrea Tosatto672b9a72016-01-05 16:18:20 +010088void
Davide Pesaventoe9c69852017-11-04 18:08:37 -040089PipelineInterests::onData(const Data& data)
90{
91 m_nReceived++;
92 m_receivedSize += data.getContent().value_size();
93
94 m_onData(data);
95}
96
97void
Weiwei Liue4765012016-06-01 00:10:29 -070098PipelineInterests::onFailure(const std::string& reason)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010099{
Weiwei Liue4765012016-06-01 00:10:29 -0700100 if (m_isStopping)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100101 return;
102
Weiwei Liue4765012016-06-01 00:10:29 -0700103 cancel();
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100104
Davide Pesavento2ab04a22023-09-15 22:08:22 -0400105 if (m_onFailure) {
Davide Pesavento7e9d7e42023-11-11 15:00:03 -0500106 boost::asio::post(m_face.getIoContext(), [this, reason] { m_onFailure(reason); });
Davide Pesavento2ab04a22023-09-15 22:08:22 -0400107 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100108}
109
Davide Pesavento97a33b22019-10-17 22:10:47 -0400110void
111PipelineInterests::printOptions() const
112{
113 std::cerr << "Pipeline parameters:\n"
114 << "\tRequest fresh content = " << (m_options.mustBeFresh ? "yes" : "no") << "\n"
115 << "\tInterest lifetime = " << m_options.interestLifetime << "\n"
116 << "\tMax retries on timeout or Nack = " <<
117 (m_options.maxRetriesOnTimeoutOrNack == DataFetcher::MAX_RETRIES_INFINITE ?
118 "infinite" : to_string(m_options.maxRetriesOnTimeoutOrNack)) << "\n";
119}
120
121void
122PipelineInterests::printSummary() const
123{
124 using namespace ndn::time;
125 duration<double, seconds::period> timeElapsed = steady_clock::now() - getStartTime();
126 double throughput = 8 * m_receivedSize / timeElapsed.count();
127
128 std::cerr << "\n\nAll segments have been received.\n"
129 << "Time elapsed: " << timeElapsed << "\n"
130 << "Segments received: " << m_nReceived << "\n"
131 << "Transferred size: " << m_receivedSize / 1e3 << " kB" << "\n"
132 << "Goodput: " << formatThroughput(throughput) << "\n";
133}
134
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000135std::string
136PipelineInterests::formatThroughput(double throughput)
137{
138 int pow = 0;
139 while (throughput >= 1000.0 && pow < 4) {
140 throughput /= 1000.0;
141 pow++;
142 }
143 switch (pow) {
144 case 0:
145 return to_string(throughput) + " bit/s";
146 case 1:
147 return to_string(throughput) + " kbit/s";
148 case 2:
149 return to_string(throughput) + " Mbit/s";
150 case 3:
151 return to_string(throughput) + " Gbit/s";
152 case 4:
153 return to_string(throughput) + " Tbit/s";
154 }
155 return "";
156}
157
Davide Pesaventob3570c62022-02-19 19:19:00 -0500158} // namespace ndn::chunks