spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2016 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of the nTorrent codebase. |
| 6 | * |
| 7 | * nTorrent is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with nTorrent, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS for complete list of nTorrent authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "sequential-data-fetcher.hpp" |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 23 | #include "util/logging.hpp" |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 24 | #include "util/io-util.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace ntorrent { |
| 28 | |
| 29 | SequentialDataFetcher::SequentialDataFetcher(const ndn::Name& torrentFileName, |
| 30 | const std::string& dataPath) |
| 31 | : m_dataPath(dataPath) |
| 32 | , m_torrentFileName(torrentFileName) |
| 33 | { |
| 34 | m_manager = make_shared<TorrentManager>(m_torrentFileName, m_dataPath); |
| 35 | } |
| 36 | |
| 37 | SequentialDataFetcher::~SequentialDataFetcher() |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | void |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 42 | SequentialDataFetcher::start(const time::milliseconds& timeout) |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 43 | { |
| 44 | m_manager->Initialize(); |
| 45 | // downloading logic |
| 46 | this->implementSequentialLogic(); |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 47 | m_manager->processEvents(timeout); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
| 51 | SequentialDataFetcher::pause() |
| 52 | { |
| 53 | // TODO(Spyros): Implement asyncrhonous pause of the torrent downloading |
| 54 | // For now, do nothing... |
| 55 | throw(Error("Not implemented yet")); |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | SequentialDataFetcher::resume() |
| 60 | { |
| 61 | // TODO(Spyros): Implement asyncrhonous re-establishment of the torrent downloading |
| 62 | // For now, do nothing... |
| 63 | throw(Error("Not implemented yet")); |
| 64 | } |
| 65 | |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 66 | void |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 67 | SequentialDataFetcher::downloadTorrentFile() |
| 68 | { |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 69 | auto torrentPath = ".appdata/" + m_torrentFileName.get(-3).toUri() + "/torrent_files/"; |
| 70 | m_manager->downloadTorrentFile(torrentPath, |
| 71 | bind(&SequentialDataFetcher::onTorrentFileSegmentReceived, this, _1), |
| 72 | bind(&SequentialDataFetcher::onDataRetrievalFailure, this, _1, _2)); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 76 | SequentialDataFetcher::downloadManifestFiles(const std::vector<ndn::Name>& manifestNames) |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 77 | { |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 78 | auto manifestPath = ".appdata/" + m_torrentFileName.get(-3).toUri() + "/manifests/"; |
| 79 | for (auto i = manifestNames.begin(); i != manifestNames.end(); i++) { |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 80 | m_manager->download_file_manifest(*i, |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 81 | manifestPath, |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 82 | bind(&SequentialDataFetcher::onManifestReceived, this, _1), |
| 83 | bind(&SequentialDataFetcher::onDataRetrievalFailure, this, _1, _2)); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | SequentialDataFetcher::downloadPackets(const std::vector<ndn::Name>& packetsName) |
| 89 | { |
| 90 | for (auto i = packetsName.begin(); i != packetsName.end(); i++) { |
| 91 | m_manager->download_data_packet(*i, |
| 92 | bind(&SequentialDataFetcher::onDataPacketReceived, this, _1), |
| 93 | bind(&SequentialDataFetcher::onDataRetrievalFailure, this, _1, _2)); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | SequentialDataFetcher::implementSequentialLogic() { |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 99 | // TODO(?) Fix seeding, and implement windowing: |
| 100 | /* |
| 101 | Alex says look at ndn-cxx: |
| 102 | * fetcher with queue (with window) |
| 103 | * segment fetcher ? |
| 104 | * catchunks (pipeline?) |
| 105 | */ |
| 106 | if (!m_manager->hasAllTorrentSegments()) { |
| 107 | this->downloadTorrentFile(); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 108 | } |
| 109 | else { |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 110 | LOG_INFO << m_torrentFileName << " complete" << std::endl; |
| 111 | std::vector<ndn::Name> namesToFetch; |
| 112 | m_manager->findFileManifestsToDownload(namesToFetch); |
| 113 | if (!namesToFetch.empty()) { |
| 114 | this->downloadManifestFiles(namesToFetch); |
| 115 | } |
| 116 | else { |
| 117 | LOG_INFO << "All manifests complete" << std::endl; |
| 118 | m_manager->findAllMissingDataPackets(namesToFetch); |
| 119 | if (!namesToFetch.empty()) { |
| 120 | this->downloadPackets(namesToFetch); |
| 121 | } |
| 122 | else { |
| 123 | LOG_INFO << "All data complete" << std::endl; |
| 124 | } |
| 125 | } |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | SequentialDataFetcher::onDataPacketReceived(const ndn::Data& data) |
| 131 | { |
| 132 | // Data Packet Received |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 133 | LOG_INFO << "Data Packet Received: " << data.getName(); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 137 | SequentialDataFetcher::onTorrentFileSegmentReceived(const std::vector<Name>& manifestNames) |
| 138 | { |
| 139 | this->downloadManifestFiles(manifestNames); |
| 140 | } |
| 141 | |
| 142 | void |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 143 | SequentialDataFetcher::onManifestReceived(const std::vector<Name>& packetNames) |
| 144 | { |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 145 | LOG_INFO << "Manifest File Received: " |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 146 | << packetNames[0].getSubName(0, packetNames[0].size()- 3) << std::endl; |
| 147 | this->downloadPackets(packetNames); |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | SequentialDataFetcher::onDataRetrievalFailure(const ndn::Interest& interest, |
| 152 | const std::string& errorCode) |
| 153 | { |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 154 | // Data retrieval failure |
| 155 | uint32_t nameType = IoUtil::findType(interest.getName()); |
| 156 | if (nameType == IoUtil::TORRENT_FILE) { |
| 157 | // this should never happen |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 158 | LOG_ERROR << "Torrent File Segment Downloading Failed: " << interest.getName(); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 159 | this->downloadTorrentFile(); |
| 160 | } |
| 161 | else if (nameType == IoUtil::FILE_MANIFEST) { |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 162 | LOG_ERROR << "Manifest File Segment Downloading Failed: " << interest.getName(); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 163 | this->downloadManifestFiles({ interest.getName() }); |
| 164 | } |
| 165 | else if (nameType == IoUtil::DATA_PACKET) { |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 166 | LOG_ERROR << "Torrent File Segment Downloading Failed: " << interest.getName(); |
| 167 | this->downloadTorrentFile(); |
| 168 | } |
| 169 | else if (nameType == IoUtil::FILE_MANIFEST) { |
| 170 | LOG_ERROR << "Manifest File Segment Downloading Failed: " << interest.getName(); |
| 171 | this->downloadManifestFiles({ interest.getName() }); |
| 172 | } |
| 173 | else if (nameType == IoUtil::DATA_PACKET) { |
| 174 | LOG_ERROR << "Data Packet Downloading Failed: " << interest.getName(); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 175 | this->downloadPackets({ interest.getName() }); |
| 176 | } |
| 177 | else { |
| 178 | // This should never happen |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 179 | LOG_ERROR << "Unknown Packet Type Downloading Failed: " << interest.getName(); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | } // namespace ntorrent |
| 184 | } // namespace ndn |