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) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 38 | , m_lastSegmentNo(0) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 39 | , m_excludedSegmentNo(0) |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame^] | 40 | , m_receivedSize(0) |
| 41 | , m_nReceived(0) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 42 | , m_hasFinalBlockId(false) |
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 |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 50 | PipelineInterests::run(const Data& data, DataCallback onData, FailureCallback onFailure) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 51 | { |
| 52 | BOOST_ASSERT(onData != nullptr); |
| 53 | m_onData = std::move(onData); |
| 54 | m_onFailure = std::move(onFailure); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 55 | m_prefix = data.getName().getPrefix(-1); |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 56 | m_excludedSegmentNo = getSegmentFromPacket(data); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 57 | |
| 58 | if (!data.getFinalBlockId().empty()) { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 59 | m_lastSegmentNo = data.getFinalBlockId().toSegment(); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 60 | m_hasFinalBlockId = true; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Davide Pesavento | bf1c069 | 2017-01-15 19:15:09 -0500 | [diff] [blame] | 63 | // record the start time of the pipeline |
| 64 | m_startTime = time::steady_clock::now(); |
| 65 | |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame^] | 66 | m_nReceived++; |
| 67 | m_receivedSize += data.getContent().value_size(); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 68 | doRun(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void |
| 72 | PipelineInterests::cancel() |
| 73 | { |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 74 | if (m_isStopping) |
| 75 | return; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 76 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 77 | m_isStopping = true; |
| 78 | doCancel(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 82 | PipelineInterests::onFailure(const std::string& reason) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 83 | { |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 84 | if (m_isStopping) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 85 | return; |
| 86 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 87 | cancel(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 88 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 89 | if (m_onFailure) |
| 90 | m_face.getIoService().post([this, reason] { m_onFailure(reason); }); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Chavoosh Ghasemi | 4d36ed5 | 2017-10-31 22:26:25 +0000 | [diff] [blame^] | 93 | std::string |
| 94 | PipelineInterests::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 | |
| 116 | void |
| 117 | PipelineInterests::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 Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 130 | } // namespace chunks |
| 131 | } // namespace ndn |