blob: 23183782c7474eeebce7ee122b9569d835ab1cf6 [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 Pesavento969cd5a2018-04-20 16:27:47 -04003 * Copyright (c) 2016-2018, 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"
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
Davide Pesavento969cd5a2018-04-20 16:27:47 -040059 if (data.getFinalBlock()) {
60 m_lastSegmentNo = data.getFinalBlock()->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
Ryan Wickman2c9933c2018-06-12 11:51:51 -050082bool
83PipelineInterests::allSegmentsReceived() const
84{
85 BOOST_ASSERT(m_nReceived > 0);
86 return m_hasFinalBlockId && static_cast<uint64_t>(m_nReceived - 1) >= m_lastSegmentNo;
87}
88
Davide Pesaventob587cfd2017-11-04 16:29:50 -040089uint64_t
90PipelineInterests::getNextSegmentNo()
91{
92 // skip the excluded segment
93 if (m_nextSegmentNo == m_excludedSegmentNo)
94 m_nextSegmentNo++;
95
96 return m_nextSegmentNo++;
97}
98
Andrea Tosatto672b9a72016-01-05 16:18:20 +010099void
Davide Pesaventoe9c69852017-11-04 18:08:37 -0400100PipelineInterests::onData(const Data& data)
101{
102 m_nReceived++;
103 m_receivedSize += data.getContent().value_size();
104
105 m_onData(data);
106}
107
108void
Weiwei Liue4765012016-06-01 00:10:29 -0700109PipelineInterests::onFailure(const std::string& reason)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100110{
Weiwei Liue4765012016-06-01 00:10:29 -0700111 if (m_isStopping)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100112 return;
113
Weiwei Liue4765012016-06-01 00:10:29 -0700114 cancel();
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100115
Weiwei Liue4765012016-06-01 00:10:29 -0700116 if (m_onFailure)
117 m_face.getIoService().post([this, reason] { m_onFailure(reason); });
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100118}
119
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000120std::string
121PipelineInterests::formatThroughput(double throughput)
122{
123 int pow = 0;
124 while (throughput >= 1000.0 && pow < 4) {
125 throughput /= 1000.0;
126 pow++;
127 }
128 switch (pow) {
129 case 0:
130 return to_string(throughput) + " bit/s";
131 case 1:
132 return to_string(throughput) + " kbit/s";
133 case 2:
134 return to_string(throughput) + " Mbit/s";
135 case 3:
136 return to_string(throughput) + " Gbit/s";
137 case 4:
138 return to_string(throughput) + " Tbit/s";
139 }
140 return "";
141}
142
143void
144PipelineInterests::printSummary() const
145{
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400146 using namespace ndn::time;
147 duration<double, milliseconds::period> timeElapsed = steady_clock::now() - getStartTime();
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000148 double throughput = (8 * m_receivedSize * 1000) / timeElapsed.count();
149
150 std::cerr << "\nAll segments have been received.\n"
151 << "Time elapsed: " << timeElapsed << "\n"
152 << "Total # of segments received: " << m_nReceived << "\n"
153 << "Total size: " << static_cast<double>(m_receivedSize) / 1000 << "kB" << "\n"
154 << "Goodput: " << formatThroughput(throughput) << "\n";
155}
156
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100157} // namespace chunks
158} // namespace ndn