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 | 11fc3eb | 2024-01-26 01:46:56 -0500 | [diff] [blame] | 3 | * Copyright (c) 2016-2024, 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" |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 32 | #include "data-fetcher.hpp" |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 33 | |
Davide Pesavento | 2ab04a2 | 2023-09-15 22:08:22 -0400 | [diff] [blame] | 34 | #include <boost/asio/io_context.hpp> |
| 35 | #include <boost/asio/post.hpp> |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 36 | |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 37 | #include <iostream> |
| 38 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 39 | namespace ndn::chunks { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 40 | |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 41 | PipelineInterests::PipelineInterests(Face& face, const Options& opts) |
| 42 | : m_options(opts) |
| 43 | , m_face(face) |
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 | { |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame] | 52 | BOOST_ASSERT(m_options.disableVersionDiscovery || |
| 53 | (!versionedName.empty() && versionedName[-1].isVersion())); |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 54 | BOOST_ASSERT(dataCb != nullptr); |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame] | 55 | |
Chavoosh Ghasemi | 5cb6701 | 2019-02-15 09:56:57 -0800 | [diff] [blame] | 56 | m_prefix = versionedName; |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 57 | m_onData = std::move(dataCb); |
| 58 | m_onFailure = std::move(failureCb); |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 59 | |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 60 | // record the start time of the pipeline |
| 61 | m_startTime = time::steady_clock::now(); |
| 62 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 63 | doRun(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void |
| 67 | PipelineInterests::cancel() |
| 68 | { |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 69 | if (m_isStopping) |
| 70 | return; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 71 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 72 | m_isStopping = true; |
| 73 | doCancel(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Ryan Wickman | 2c9933c | 2018-06-12 11:51:51 -0500 | [diff] [blame] | 76 | bool |
| 77 | PipelineInterests::allSegmentsReceived() const |
| 78 | { |
Chavoosh Ghasemi | 5cb6701 | 2019-02-15 09:56:57 -0800 | [diff] [blame] | 79 | return m_nReceived > 0 && |
| 80 | m_hasFinalBlockId && |
| 81 | static_cast<uint64_t>(m_nReceived - 1) >= m_lastSegmentNo; |
Ryan Wickman | 2c9933c | 2018-06-12 11:51:51 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 84 | uint64_t |
| 85 | PipelineInterests::getNextSegmentNo() |
| 86 | { |
Davide Pesavento | b587cfd | 2017-11-04 16:29:50 -0400 | [diff] [blame] | 87 | return m_nextSegmentNo++; |
| 88 | } |
| 89 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 90 | void |
Davide Pesavento | e9c6985 | 2017-11-04 18:08:37 -0400 | [diff] [blame] | 91 | PipelineInterests::onData(const Data& data) |
| 92 | { |
| 93 | m_nReceived++; |
| 94 | m_receivedSize += data.getContent().value_size(); |
| 95 | |
| 96 | m_onData(data); |
| 97 | } |
| 98 | |
| 99 | void |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 100 | PipelineInterests::onFailure(const std::string& reason) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 101 | { |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 102 | if (m_isStopping) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 103 | return; |
| 104 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 105 | cancel(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 106 | |
Davide Pesavento | 2ab04a2 | 2023-09-15 22:08:22 -0400 | [diff] [blame] | 107 | if (m_onFailure) { |
Davide Pesavento | 7e9d7e4 | 2023-11-11 15:00:03 -0500 | [diff] [blame] | 108 | boost::asio::post(m_face.getIoContext(), [this, reason] { m_onFailure(reason); }); |
Davide Pesavento | 2ab04a2 | 2023-09-15 22:08:22 -0400 | [diff] [blame] | 109 | } |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 112 | void |
| 113 | PipelineInterests::printOptions() const |
| 114 | { |
| 115 | std::cerr << "Pipeline parameters:\n" |
| 116 | << "\tRequest fresh content = " << (m_options.mustBeFresh ? "yes" : "no") << "\n" |
| 117 | << "\tInterest lifetime = " << m_options.interestLifetime << "\n" |
| 118 | << "\tMax retries on timeout or Nack = " << |
| 119 | (m_options.maxRetriesOnTimeoutOrNack == DataFetcher::MAX_RETRIES_INFINITE ? |
Davide Pesavento | 11fc3eb | 2024-01-26 01:46:56 -0500 | [diff] [blame] | 120 | "infinite" : std::to_string(m_options.maxRetriesOnTimeoutOrNack)) << "\n"; |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void |
| 124 | PipelineInterests::printSummary() const |
| 125 | { |
| 126 | using namespace ndn::time; |
| 127 | duration<double, seconds::period> timeElapsed = steady_clock::now() - getStartTime(); |
| 128 | double throughput = 8 * m_receivedSize / timeElapsed.count(); |
| 129 | |
| 130 | std::cerr << "\n\nAll segments have been received.\n" |
| 131 | << "Time elapsed: " << timeElapsed << "\n" |
| 132 | << "Segments received: " << m_nReceived << "\n" |
| 133 | << "Transferred size: " << m_receivedSize / 1e3 << " kB" << "\n" |
| 134 | << "Goodput: " << formatThroughput(throughput) << "\n"; |
| 135 | } |
| 136 | |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 137 | std::string |
| 138 | PipelineInterests::formatThroughput(double throughput) |
| 139 | { |
| 140 | int pow = 0; |
| 141 | while (throughput >= 1000.0 && pow < 4) { |
| 142 | throughput /= 1000.0; |
| 143 | pow++; |
| 144 | } |
| 145 | switch (pow) { |
| 146 | case 0: |
Davide Pesavento | 11fc3eb | 2024-01-26 01:46:56 -0500 | [diff] [blame] | 147 | return std::to_string(throughput) + " bit/s"; |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 148 | case 1: |
Davide Pesavento | 11fc3eb | 2024-01-26 01:46:56 -0500 | [diff] [blame] | 149 | return std::to_string(throughput) + " kbit/s"; |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 150 | case 2: |
Davide Pesavento | 11fc3eb | 2024-01-26 01:46:56 -0500 | [diff] [blame] | 151 | return std::to_string(throughput) + " Mbit/s"; |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 152 | case 3: |
Davide Pesavento | 11fc3eb | 2024-01-26 01:46:56 -0500 | [diff] [blame] | 153 | return std::to_string(throughput) + " Gbit/s"; |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 154 | case 4: |
Davide Pesavento | 11fc3eb | 2024-01-26 01:46:56 -0500 | [diff] [blame] | 155 | return std::to_string(throughput) + " Tbit/s"; |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame] | 156 | } |
| 157 | return ""; |
| 158 | } |
| 159 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 160 | } // namespace ndn::chunks |