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 | /* |
Chavoosh Ghasemi | 5cb6701 | 2019-02-15 09:56:57 -0800 | [diff] [blame] | 3 | * Copyright (c) 2016-2019, Regents of the University of California, |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 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) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 43 | , m_isStopping(false) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 44 | { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 47 | PipelineInterests::~PipelineInterests() = default; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 48 | |
| 49 | void |
Chavoosh Ghasemi | 5cb6701 | 2019-02-15 09:56:57 -0800 | [diff] [blame] | 50 | PipelineInterests::run(const Name& versionedName, DataCallback dataCb, FailureCallback failureCb) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 51 | { |
Chavoosh Ghasemi | 5cb6701 | 2019-02-15 09:56:57 -0800 | [diff] [blame] | 52 | BOOST_ASSERT(!versionedName.empty() && versionedName[-1].isVersion()); |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 53 | BOOST_ASSERT(dataCb != nullptr); |
Chavoosh Ghasemi | 5cb6701 | 2019-02-15 09:56:57 -0800 | [diff] [blame] | 54 | m_prefix = versionedName; |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 55 | m_onData = std::move(dataCb); |
| 56 | m_onFailure = std::move(failureCb); |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 57 | |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 58 | // record the start time of the pipeline |
| 59 | m_startTime = time::steady_clock::now(); |
| 60 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 61 | doRun(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void |
| 65 | PipelineInterests::cancel() |
| 66 | { |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 67 | if (m_isStopping) |
| 68 | return; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 69 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 70 | m_isStopping = true; |
| 71 | doCancel(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Ryan Wickman | 2c9933c | 2018-06-12 11:51:51 -0500 | [diff] [blame] | 74 | bool |
| 75 | PipelineInterests::allSegmentsReceived() const |
| 76 | { |
Chavoosh Ghasemi | 5cb6701 | 2019-02-15 09:56:57 -0800 | [diff] [blame] | 77 | return m_nReceived > 0 && |
| 78 | m_hasFinalBlockId && |
| 79 | static_cast<uint64_t>(m_nReceived - 1) >= m_lastSegmentNo; |
Ryan Wickman | 2c9933c | 2018-06-12 11:51:51 -0500 | [diff] [blame] | 80 | } |
| 81 | |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 82 | uint64_t |
| 83 | PipelineInterests::getNextSegmentNo() |
| 84 | { |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 85 | return m_nextSegmentNo++; |
| 86 | } |
| 87 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 88 | void |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 89 | PipelineInterests::onData(const Data& data) |
| 90 | { |
| 91 | m_nReceived++; |
| 92 | m_receivedSize += data.getContent().value_size(); |
| 93 | |
| 94 | m_onData(data); |
| 95 | } |
| 96 | |
| 97 | void |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 98 | PipelineInterests::onFailure(const std::string& reason) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 99 | { |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 100 | if (m_isStopping) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 101 | return; |
| 102 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 103 | cancel(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 104 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 105 | if (m_onFailure) |
| 106 | m_face.getIoService().post([this, reason] { m_onFailure(reason); }); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 109 | std::string |
| 110 | PipelineInterests::formatThroughput(double throughput) |
| 111 | { |
| 112 | int pow = 0; |
| 113 | while (throughput >= 1000.0 && pow < 4) { |
| 114 | throughput /= 1000.0; |
| 115 | pow++; |
| 116 | } |
| 117 | switch (pow) { |
| 118 | case 0: |
| 119 | return to_string(throughput) + " bit/s"; |
| 120 | case 1: |
| 121 | return to_string(throughput) + " kbit/s"; |
| 122 | case 2: |
| 123 | return to_string(throughput) + " Mbit/s"; |
| 124 | case 3: |
| 125 | return to_string(throughput) + " Gbit/s"; |
| 126 | case 4: |
| 127 | return to_string(throughput) + " Tbit/s"; |
| 128 | } |
| 129 | return ""; |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | PipelineInterests::printSummary() const |
| 134 | { |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 135 | using namespace ndn::time; |
Davide Pesavento | ba56066 | 2019-06-26 22:45:44 -0400 | [diff] [blame^] | 136 | duration<double, seconds::period> timeElapsed = steady_clock::now() - getStartTime(); |
| 137 | double throughput = 8 * m_receivedSize / timeElapsed.count(); |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 138 | |
schneiderklaus | 8ff3abd | 2019-03-12 22:15:12 -0700 | [diff] [blame] | 139 | std::cerr << "\n\nAll segments have been received.\n" |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 140 | << "Time elapsed: " << timeElapsed << "\n" |
schneiderklaus | 8ff3abd | 2019-03-12 22:15:12 -0700 | [diff] [blame] | 141 | << "Segments received: " << m_nReceived << "\n" |
Davide Pesavento | ba56066 | 2019-06-26 22:45:44 -0400 | [diff] [blame^] | 142 | << "Transferred size: " << m_receivedSize / 1e3 << " kB" << "\n" |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 143 | << "Goodput: " << formatThroughput(throughput) << "\n"; |
| 144 | } |
| 145 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 146 | } // namespace chunks |
| 147 | } // namespace ndn |