blob: e520bfe4302e2a50488d3834f67e0376bdb1f876 [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 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
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"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010032
33namespace ndn {
34namespace chunks {
35
Weiwei Liue4765012016-06-01 00:10:29 -070036PipelineInterests::PipelineInterests(Face& face)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010037 : m_face(face)
Davide Pesaventoe9c69852017-11-04 18:08:37 -040038 , m_hasFinalBlockId(false)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010039 , m_lastSegmentNo(0)
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +000040 , m_nReceived(0)
Davide Pesaventob587cfd2017-11-04 16:29:50 -040041 , m_receivedSize(0)
Davide Pesaventob587cfd2017-11-04 16:29:50 -040042 , m_nextSegmentNo(0)
43 , m_excludedSegmentNo(0)
Weiwei Liue4765012016-06-01 00:10:29 -070044 , m_isStopping(false)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010045{
Andrea Tosatto672b9a72016-01-05 16:18:20 +010046}
47
Weiwei Liue4765012016-06-01 00:10:29 -070048PipelineInterests::~PipelineInterests() = default;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010049
50void
Davide Pesaventoe9c69852017-11-04 18:08:37 -040051PipelineInterests::run(const Data& data, DataCallback dataCb, FailureCallback failureCb)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010052{
Davide Pesaventoe9c69852017-11-04 18:08:37 -040053 BOOST_ASSERT(dataCb != nullptr);
54 m_onData = std::move(dataCb);
55 m_onFailure = std::move(failureCb);
Weiwei Liue4765012016-06-01 00:10:29 -070056 m_prefix = data.getName().getPrefix(-1);
Davide Pesaventobf1c0692017-01-15 19:15:09 -050057 m_excludedSegmentNo = getSegmentFromPacket(data);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010058
59 if (!data.getFinalBlockId().empty()) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010060 m_lastSegmentNo = data.getFinalBlockId().toSegment();
Weiwei Liue4765012016-06-01 00:10:29 -070061 m_hasFinalBlockId = true;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010062 }
63
Davide Pesaventoe9c69852017-11-04 18:08:37 -040064 onData(data);
65
Davide Pesaventobf1c0692017-01-15 19:15:09 -050066 // record the start time of the pipeline
67 m_startTime = time::steady_clock::now();
68
Weiwei Liue4765012016-06-01 00:10:29 -070069 doRun();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010070}
71
72void
73PipelineInterests::cancel()
74{
Weiwei Liue4765012016-06-01 00:10:29 -070075 if (m_isStopping)
76 return;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010077
Weiwei Liue4765012016-06-01 00:10:29 -070078 m_isStopping = true;
79 doCancel();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010080}
81
Davide Pesaventob587cfd2017-11-04 16:29:50 -040082uint64_t
83PipelineInterests::getNextSegmentNo()
84{
85 // skip the excluded segment
86 if (m_nextSegmentNo == m_excludedSegmentNo)
87 m_nextSegmentNo++;
88
89 return m_nextSegmentNo++;
90}
91
Andrea Tosatto672b9a72016-01-05 16:18:20 +010092void
Davide Pesaventoe9c69852017-11-04 18:08:37 -040093PipelineInterests::onData(const Data& data)
94{
95 m_nReceived++;
96 m_receivedSize += data.getContent().value_size();
97
98 m_onData(data);
99}
100
101void
Weiwei Liue4765012016-06-01 00:10:29 -0700102PipelineInterests::onFailure(const std::string& reason)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100103{
Weiwei Liue4765012016-06-01 00:10:29 -0700104 if (m_isStopping)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100105 return;
106
Weiwei Liue4765012016-06-01 00:10:29 -0700107 cancel();
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100108
Weiwei Liue4765012016-06-01 00:10:29 -0700109 if (m_onFailure)
110 m_face.getIoService().post([this, reason] { m_onFailure(reason); });
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100111}
112
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000113std::string
114PipelineInterests::formatThroughput(double throughput)
115{
116 int pow = 0;
117 while (throughput >= 1000.0 && pow < 4) {
118 throughput /= 1000.0;
119 pow++;
120 }
121 switch (pow) {
122 case 0:
123 return to_string(throughput) + " bit/s";
124 case 1:
125 return to_string(throughput) + " kbit/s";
126 case 2:
127 return to_string(throughput) + " Mbit/s";
128 case 3:
129 return to_string(throughput) + " Gbit/s";
130 case 4:
131 return to_string(throughput) + " Tbit/s";
132 }
133 return "";
134}
135
136void
137PipelineInterests::printSummary() const
138{
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400139 using namespace ndn::time;
140 duration<double, milliseconds::period> timeElapsed = steady_clock::now() - getStartTime();
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000141 double throughput = (8 * m_receivedSize * 1000) / timeElapsed.count();
142
143 std::cerr << "\nAll segments have been received.\n"
144 << "Time elapsed: " << timeElapsed << "\n"
145 << "Total # of segments received: " << m_nReceived << "\n"
146 << "Total size: " << static_cast<double>(m_receivedSize) / 1000 << "kB" << "\n"
147 << "Goodput: " << formatThroughput(throughput) << "\n";
148}
149
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100150} // namespace chunks
151} // namespace ndn