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 | |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 29 | SequentialDataFetcher::SequentialDataFetcher(const ndn::Name& torrentFileName, |
| 30 | const std::string& dataPath, |
| 31 | bool seed) |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 32 | : m_dataPath(dataPath) |
| 33 | , m_torrentFileName(torrentFileName) |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 34 | , m_seedFlag(seed) |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 35 | { |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 36 | m_manager = make_shared<TorrentManager>(m_torrentFileName, m_dataPath, seed); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | SequentialDataFetcher::~SequentialDataFetcher() |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | void |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 44 | SequentialDataFetcher::start(const time::milliseconds& timeout) |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 45 | { |
| 46 | m_manager->Initialize(); |
| 47 | // downloading logic |
| 48 | this->implementSequentialLogic(); |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 49 | m_manager->processEvents(timeout); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void |
| 53 | SequentialDataFetcher::pause() |
| 54 | { |
| 55 | // TODO(Spyros): Implement asyncrhonous pause of the torrent downloading |
| 56 | // For now, do nothing... |
| 57 | throw(Error("Not implemented yet")); |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | SequentialDataFetcher::resume() |
| 62 | { |
| 63 | // TODO(Spyros): Implement asyncrhonous re-establishment of the torrent downloading |
| 64 | // For now, do nothing... |
| 65 | throw(Error("Not implemented yet")); |
| 66 | } |
| 67 | |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 68 | void |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 69 | SequentialDataFetcher::downloadTorrentFile() |
| 70 | { |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 71 | auto torrentPath = ".appdata/" + m_torrentFileName.get(-3).toUri() + "/torrent_files/"; |
| 72 | m_manager->downloadTorrentFile(torrentPath, |
| 73 | bind(&SequentialDataFetcher::onTorrentFileSegmentReceived, this, _1), |
| 74 | bind(&SequentialDataFetcher::onDataRetrievalFailure, this, _1, _2)); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 78 | SequentialDataFetcher::downloadManifestFiles(const std::vector<ndn::Name>& manifestNames) |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 79 | { |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 80 | auto manifestPath = ".appdata/" + m_torrentFileName.get(-3).toUri() + "/manifests/"; |
| 81 | for (auto i = manifestNames.begin(); i != manifestNames.end(); i++) { |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 82 | m_manager->download_file_manifest(*i, |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 83 | manifestPath, |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 84 | bind(&SequentialDataFetcher::onManifestReceived, this, _1), |
| 85 | bind(&SequentialDataFetcher::onDataRetrievalFailure, this, _1, _2)); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | SequentialDataFetcher::downloadPackets(const std::vector<ndn::Name>& packetsName) |
| 91 | { |
| 92 | for (auto i = packetsName.begin(); i != packetsName.end(); i++) { |
| 93 | m_manager->download_data_packet(*i, |
| 94 | bind(&SequentialDataFetcher::onDataPacketReceived, this, _1), |
| 95 | bind(&SequentialDataFetcher::onDataRetrievalFailure, this, _1, _2)); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | SequentialDataFetcher::implementSequentialLogic() { |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 101 | // TODO(?) Fix seeding, and implement windowing: |
| 102 | /* |
| 103 | Alex says look at ndn-cxx: |
| 104 | * fetcher with queue (with window) |
| 105 | * segment fetcher ? |
| 106 | * catchunks (pipeline?) |
| 107 | */ |
| 108 | if (!m_manager->hasAllTorrentSegments()) { |
| 109 | this->downloadTorrentFile(); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 110 | } |
| 111 | else { |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 112 | LOG_INFO << m_torrentFileName << " complete" << std::endl; |
| 113 | std::vector<ndn::Name> namesToFetch; |
| 114 | m_manager->findFileManifestsToDownload(namesToFetch); |
| 115 | if (!namesToFetch.empty()) { |
| 116 | this->downloadManifestFiles(namesToFetch); |
| 117 | } |
| 118 | else { |
| 119 | LOG_INFO << "All manifests complete" << std::endl; |
| 120 | m_manager->findAllMissingDataPackets(namesToFetch); |
| 121 | if (!namesToFetch.empty()) { |
| 122 | this->downloadPackets(namesToFetch); |
| 123 | } |
| 124 | else { |
| 125 | LOG_INFO << "All data complete" << std::endl; |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 126 | if (!m_seedFlag) { |
| 127 | m_manager->shutdown(); |
| 128 | } |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | SequentialDataFetcher::onDataPacketReceived(const ndn::Data& data) |
| 136 | { |
| 137 | // Data Packet Received |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 138 | LOG_INFO << "Data Packet Received: " << data.getName(); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 142 | SequentialDataFetcher::onTorrentFileSegmentReceived(const std::vector<Name>& manifestNames) |
| 143 | { |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 144 | // TODO(msweatt) Add parameter for torrent file |
| 145 | LOG_INFO << "Torrent Segment Received: " << m_torrentFileName << std::endl; |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 146 | this->downloadManifestFiles(manifestNames); |
| 147 | } |
| 148 | |
| 149 | void |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 150 | SequentialDataFetcher::onManifestReceived(const std::vector<Name>& packetNames) |
| 151 | { |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 152 | LOG_INFO << "Manifest File Received: " |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 153 | << packetNames[0].getSubName(0, packetNames[0].size()- 3) << std::endl; |
| 154 | this->downloadPackets(packetNames); |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | SequentialDataFetcher::onDataRetrievalFailure(const ndn::Interest& interest, |
| 159 | const std::string& errorCode) |
| 160 | { |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 161 | // Data retrieval failure |
| 162 | uint32_t nameType = IoUtil::findType(interest.getName()); |
| 163 | if (nameType == IoUtil::TORRENT_FILE) { |
| 164 | // this should never happen |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 165 | LOG_ERROR << "Torrent File Segment Downloading Failed: " << interest.getName(); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 166 | this->downloadTorrentFile(); |
| 167 | } |
| 168 | else if (nameType == IoUtil::FILE_MANIFEST) { |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 169 | LOG_ERROR << "Manifest File Segment Downloading Failed: " << interest.getName(); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 170 | this->downloadManifestFiles({ interest.getName() }); |
| 171 | } |
| 172 | else if (nameType == IoUtil::DATA_PACKET) { |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 173 | LOG_ERROR << "Data Packet Downloading Failed: " << interest.getName(); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 174 | this->downloadPackets({ interest.getName() }); |
| 175 | } |
| 176 | else { |
| 177 | // This should never happen |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 178 | LOG_ERROR << "Unknown Packet Type Downloading Failed: " << interest.getName(); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | } // namespace ntorrent |
| 183 | } // namespace ndn |