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" |
| 23 | #include "util/io-util.hpp" |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace ntorrent { |
| 27 | |
| 28 | SequentialDataFetcher::SequentialDataFetcher(const ndn::Name& torrentFileName, |
| 29 | const std::string& dataPath) |
| 30 | : m_dataPath(dataPath) |
| 31 | , m_torrentFileName(torrentFileName) |
| 32 | { |
| 33 | m_manager = make_shared<TorrentManager>(m_torrentFileName, m_dataPath); |
| 34 | } |
| 35 | |
| 36 | SequentialDataFetcher::~SequentialDataFetcher() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | SequentialDataFetcher::start() |
| 42 | { |
| 43 | m_manager->Initialize(); |
| 44 | // downloading logic |
| 45 | this->implementSequentialLogic(); |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | SequentialDataFetcher::pause() |
| 50 | { |
| 51 | // TODO(Spyros): Implement asyncrhonous pause of the torrent downloading |
| 52 | // For now, do nothing... |
| 53 | throw(Error("Not implemented yet")); |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | SequentialDataFetcher::resume() |
| 58 | { |
| 59 | // TODO(Spyros): Implement asyncrhonous re-establishment of the torrent downloading |
| 60 | // For now, do nothing... |
| 61 | throw(Error("Not implemented yet")); |
| 62 | } |
| 63 | |
| 64 | std::vector<ndn::Name> |
| 65 | SequentialDataFetcher::downloadTorrentFile() |
| 66 | { |
| 67 | std::vector<ndn::Name> returnedNames; |
| 68 | returnedNames = m_manager->downloadTorrentFile(".appdata/torrent_files/"); |
| 69 | std::cout << "Torrent File Received: " |
| 70 | << m_torrentFileName.getSubName(0, m_torrentFileName.size() - 1) << std::endl; |
| 71 | return returnedNames; |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | SequentialDataFetcher::downloadManifestFiles(const std::vector<ndn::Name>& manifestsName) |
| 76 | { |
| 77 | std::vector<ndn::Name> packetsName; |
| 78 | for (auto i = manifestsName.begin(); i != manifestsName.end(); i++) { |
| 79 | m_manager->download_file_manifest(*i, |
| 80 | ".appdata/manifests/", |
| 81 | bind(&SequentialDataFetcher::onManifestReceived, this, _1), |
| 82 | bind(&SequentialDataFetcher::onDataRetrievalFailure, this, _1, _2)); |
Mickey Sweatt | b7ee19c | 2016-04-21 12:18:15 -0700 | [diff] [blame^] | 83 | m_manager->processEvents(); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 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)); |
Mickey Sweatt | b7ee19c | 2016-04-21 12:18:15 -0700 | [diff] [blame^] | 94 | m_manager->processEvents(); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | SequentialDataFetcher::implementSequentialLogic() { |
| 100 | std::vector<ndn::Name> returnedNames; |
| 101 | returnedNames = this->downloadTorrentFile(); |
| 102 | if (returnedNames.empty()) { |
| 103 | // we have downloaded the entire torrent (including manifests, data packets, etc..) |
| 104 | return; |
| 105 | } |
| 106 | // check the first returned name whether it is the name of a file manifest or a data packet |
| 107 | const Name& nameToCheck = returnedNames[0]; |
| 108 | if (IoUtil::findType(nameToCheck) == IoUtil::DATA_PACKET) { |
| 109 | // In this case, the returned names correspond to data packets |
| 110 | this->downloadPackets(returnedNames); |
| 111 | } |
| 112 | else { |
| 113 | // In this case, the returned names correspond to file manifests |
| 114 | this->downloadManifestFiles(returnedNames); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void |
| 119 | SequentialDataFetcher::onDataPacketReceived(const ndn::Data& data) |
| 120 | { |
| 121 | // Data Packet Received |
| 122 | std::cout << "Data Packet Received: " << data.getName(); |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | SequentialDataFetcher::onManifestReceived(const std::vector<Name>& packetNames) |
| 127 | { |
| 128 | std::cout << "Manifest File Received: " |
| 129 | << packetNames[0].getSubName(0, packetNames[0].size()- 3) << std::endl; |
| 130 | this->downloadPackets(packetNames); |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | SequentialDataFetcher::onDataRetrievalFailure(const ndn::Interest& interest, |
| 135 | const std::string& errorCode) |
| 136 | { |
| 137 | std::cerr << "Data Retrieval Failed: " << interest.getName() << std::endl; |
Mickey Sweatt | b7ee19c | 2016-04-21 12:18:15 -0700 | [diff] [blame^] | 138 | |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 139 | // Data retrieval failure |
| 140 | uint32_t nameType = IoUtil::findType(interest.getName()); |
| 141 | if (nameType == IoUtil::TORRENT_FILE) { |
| 142 | // this should never happen |
| 143 | std::cerr << "Torrent File Segment Downloading Failed: " << interest.getName(); |
| 144 | this->downloadTorrentFile(); |
| 145 | } |
| 146 | else if (nameType == IoUtil::FILE_MANIFEST) { |
| 147 | std::cerr << "Manifest File Segment Downloading Failed: " << interest.getName(); |
| 148 | this->downloadManifestFiles({ interest.getName() }); |
| 149 | } |
| 150 | else if (nameType == IoUtil::DATA_PACKET) { |
| 151 | std::cerr << "Data Packet Downloading Failed: " << interest.getName(); |
| 152 | this->downloadPackets({ interest.getName() }); |
| 153 | } |
| 154 | else { |
| 155 | // This should never happen |
| 156 | std::cerr << "Unknown Packet Type Downloading Failed: " << interest.getName(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | } // namespace ntorrent |
| 161 | } // namespace ndn |