blob: 965492f68ae6417fed07060f6bad9564c30df68c [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)
Weiwei Liue4765012016-06-01 00:10:29 -070039 , m_excludedSegmentNo(0)
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +000040 , m_receivedSize(0)
41 , m_nReceived(0)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010042 , m_hasFinalBlockId(false)
Weiwei Liue4765012016-06-01 00:10:29 -070043 , m_isStopping(false)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010044{
Andrea Tosatto672b9a72016-01-05 16:18:20 +010045}
46
Weiwei Liue4765012016-06-01 00:10:29 -070047PipelineInterests::~PipelineInterests() = default;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010048
49void
Weiwei Liue4765012016-06-01 00:10:29 -070050PipelineInterests::run(const Data& data, DataCallback onData, FailureCallback onFailure)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010051{
52 BOOST_ASSERT(onData != nullptr);
53 m_onData = std::move(onData);
54 m_onFailure = std::move(onFailure);
Weiwei Liue4765012016-06-01 00:10:29 -070055 m_prefix = data.getName().getPrefix(-1);
Davide Pesaventobf1c0692017-01-15 19:15:09 -050056 m_excludedSegmentNo = getSegmentFromPacket(data);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010057
58 if (!data.getFinalBlockId().empty()) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010059 m_lastSegmentNo = data.getFinalBlockId().toSegment();
Weiwei Liue4765012016-06-01 00:10:29 -070060 m_hasFinalBlockId = true;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010061 }
62
Davide Pesaventobf1c0692017-01-15 19:15:09 -050063 // record the start time of the pipeline
64 m_startTime = time::steady_clock::now();
65
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +000066 m_nReceived++;
67 m_receivedSize += data.getContent().value_size();
Weiwei Liue4765012016-06-01 00:10:29 -070068 doRun();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010069}
70
71void
72PipelineInterests::cancel()
73{
Weiwei Liue4765012016-06-01 00:10:29 -070074 if (m_isStopping)
75 return;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010076
Weiwei Liue4765012016-06-01 00:10:29 -070077 m_isStopping = true;
78 doCancel();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010079}
80
81void
Weiwei Liue4765012016-06-01 00:10:29 -070082PipelineInterests::onFailure(const std::string& reason)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010083{
Weiwei Liue4765012016-06-01 00:10:29 -070084 if (m_isStopping)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010085 return;
86
Weiwei Liue4765012016-06-01 00:10:29 -070087 cancel();
Andrea Tosatto672b9a72016-01-05 16:18:20 +010088
Weiwei Liue4765012016-06-01 00:10:29 -070089 if (m_onFailure)
90 m_face.getIoService().post([this, reason] { m_onFailure(reason); });
Andrea Tosatto672b9a72016-01-05 16:18:20 +010091}
92
Chavoosh Ghasemi4d36ed52017-10-31 22:26:25 +000093std::string
94PipelineInterests::formatThroughput(double throughput)
95{
96 int pow = 0;
97 while (throughput >= 1000.0 && pow < 4) {
98 throughput /= 1000.0;
99 pow++;
100 }
101 switch (pow) {
102 case 0:
103 return to_string(throughput) + " bit/s";
104 case 1:
105 return to_string(throughput) + " kbit/s";
106 case 2:
107 return to_string(throughput) + " Mbit/s";
108 case 3:
109 return to_string(throughput) + " Gbit/s";
110 case 4:
111 return to_string(throughput) + " Tbit/s";
112 }
113 return "";
114}
115
116void
117PipelineInterests::printSummary() const
118{
119 typedef time::duration<double, time::milliseconds::period> Milliseconds;
120 Milliseconds timeElapsed = time::steady_clock::now() - getStartTime();
121 double throughput = (8 * m_receivedSize * 1000) / timeElapsed.count();
122
123 std::cerr << "\nAll segments have been received.\n"
124 << "Time elapsed: " << timeElapsed << "\n"
125 << "Total # of segments received: " << m_nReceived << "\n"
126 << "Total size: " << static_cast<double>(m_receivedSize) / 1000 << "kB" << "\n"
127 << "Goodput: " << formatThroughput(throughput) << "\n";
128}
129
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100130} // namespace chunks
131} // namespace ndn