Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 3 | * Copyright (c) 2016-2017, Regents of the University of California, |
| 4 | * Colorado State University, |
| 5 | * University Pierre & Marie Curie, Sorbonne University. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 6 | * |
| 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 Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 26 | * @author Davide Pesavento |
| 27 | * @author Weiwei Liu |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 28 | * @author Chavoosh Ghasemi |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | #include "pipeline-interests.hpp" |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 32 | |
| 33 | namespace ndn { |
| 34 | namespace chunks { |
| 35 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 36 | PipelineInterests::PipelineInterests(Face& face) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 37 | : m_face(face) |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame^] | 38 | , m_hasFinalBlockId(false) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 39 | , m_lastSegmentNo(0) |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 40 | , m_nReceived(0) |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 41 | , m_receivedSize(0) |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 42 | , m_nextSegmentNo(0) |
| 43 | , m_excludedSegmentNo(0) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 44 | , m_isStopping(false) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 45 | { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 46 | } |
| 47 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 48 | PipelineInterests::~PipelineInterests() = default; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 49 | |
| 50 | void |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame^] | 51 | PipelineInterests::run(const Data& data, DataCallback dataCb, FailureCallback failureCb) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 52 | { |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame^] | 53 | BOOST_ASSERT(dataCb != nullptr); |
| 54 | m_onData = std::move(dataCb); |
| 55 | m_onFailure = std::move(failureCb); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 56 | m_prefix = data.getName().getPrefix(-1); |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 57 | m_excludedSegmentNo = getSegmentFromPacket(data); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 58 | |
| 59 | if (!data.getFinalBlockId().empty()) { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 60 | m_lastSegmentNo = data.getFinalBlockId().toSegment(); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 61 | m_hasFinalBlockId = true; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame^] | 64 | onData(data); |
| 65 | |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 66 | // record the start time of the pipeline |
| 67 | m_startTime = time::steady_clock::now(); |
| 68 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 69 | doRun(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void |
| 73 | PipelineInterests::cancel() |
| 74 | { |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 75 | if (m_isStopping) |
| 76 | return; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 77 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 78 | m_isStopping = true; |
| 79 | doCancel(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 82 | uint64_t |
| 83 | PipelineInterests::getNextSegmentNo() |
| 84 | { |
| 85 | // skip the excluded segment |
| 86 | if (m_nextSegmentNo == m_excludedSegmentNo) |
| 87 | m_nextSegmentNo++; |
| 88 | |
| 89 | return m_nextSegmentNo++; |
| 90 | } |
| 91 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 92 | void |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame^] | 93 | PipelineInterests::onData(const Data& data) |
| 94 | { |
| 95 | m_nReceived++; |
| 96 | m_receivedSize += data.getContent().value_size(); |
| 97 | |
| 98 | m_onData(data); |
| 99 | } |
| 100 | |
| 101 | void |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 102 | PipelineInterests::onFailure(const std::string& reason) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 103 | { |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 104 | if (m_isStopping) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 105 | return; |
| 106 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 107 | cancel(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 108 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 109 | if (m_onFailure) |
| 110 | m_face.getIoService().post([this, reason] { m_onFailure(reason); }); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 113 | std::string |
| 114 | PipelineInterests::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 | |
| 136 | void |
| 137 | PipelineInterests::printSummary() const |
| 138 | { |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 139 | using namespace ndn::time; |
| 140 | duration<double, milliseconds::period> timeElapsed = steady_clock::now() - getStartTime(); |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 141 | 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 Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 150 | } // namespace chunks |
| 151 | } // namespace ndn |