blob: c0512ccf51bf429148868a1ef988ebd3a559e3b5 [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)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010038 , m_lastSegmentNo(0)
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +000039 , m_nReceived(0)
Davide Pesaventob587cfd2017-11-04 16:29:50 -040040 , m_receivedSize(0)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010041 , m_hasFinalBlockId(false)
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
Weiwei Liue4765012016-06-01 00:10:29 -070051PipelineInterests::run(const Data& data, DataCallback onData, FailureCallback onFailure)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010052{
53 BOOST_ASSERT(onData != nullptr);
54 m_onData = std::move(onData);
55 m_onFailure = std::move(onFailure);
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 Pesaventobf1c0692017-01-15 19:15:09 -050064 // record the start time of the pipeline
65 m_startTime = time::steady_clock::now();
66
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +000067 m_nReceived++;
68 m_receivedSize += data.getContent().value_size();
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
Weiwei Liue4765012016-06-01 00:10:29 -070093PipelineInterests::onFailure(const std::string& reason)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010094{
Weiwei Liue4765012016-06-01 00:10:29 -070095 if (m_isStopping)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010096 return;
97
Weiwei Liue4765012016-06-01 00:10:29 -070098 cancel();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010099
Weiwei Liue4765012016-06-01 00:10:29 -0700100 if (m_onFailure)
101 m_face.getIoService().post([this, reason] { m_onFailure(reason); });
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100102}
103
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000104std::string
105PipelineInterests::formatThroughput(double throughput)
106{
107 int pow = 0;
108 while (throughput >= 1000.0 && pow < 4) {
109 throughput /= 1000.0;
110 pow++;
111 }
112 switch (pow) {
113 case 0:
114 return to_string(throughput) + " bit/s";
115 case 1:
116 return to_string(throughput) + " kbit/s";
117 case 2:
118 return to_string(throughput) + " Mbit/s";
119 case 3:
120 return to_string(throughput) + " Gbit/s";
121 case 4:
122 return to_string(throughput) + " Tbit/s";
123 }
124 return "";
125}
126
127void
128PipelineInterests::printSummary() const
129{
Davide Pesaventob587cfd2017-11-04 16:29:50 -0400130 using namespace ndn::time;
131 duration<double, milliseconds::period> timeElapsed = steady_clock::now() - getStartTime();
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +0000132 double throughput = (8 * m_receivedSize * 1000) / timeElapsed.count();
133
134 std::cerr << "\nAll segments have been received.\n"
135 << "Time elapsed: " << timeElapsed << "\n"
136 << "Total # of segments received: " << m_nReceived << "\n"
137 << "Total size: " << static_cast<double>(m_receivedSize) / 1000 << "kB" << "\n"
138 << "Goodput: " << formatThroughput(throughput) << "\n";
139}
140
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100141} // namespace chunks
142} // namespace ndn