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 | #ifndef SEQUENTIAL_DATA_FETCHER_HPP |
| 23 | #define SEQUENTIAL_DATA_FETCHER_HPP |
| 24 | |
| 25 | #include "fetching-strategy-manager.hpp" |
| 26 | #include "torrent-manager.hpp" |
| 27 | |
| 28 | #include <ndn-cxx/name.hpp> |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace ntorrent { |
| 32 | |
| 33 | class SequentialDataFetcher : FetchingStrategyManager { |
| 34 | public: |
| 35 | class Error : public std::runtime_error |
| 36 | { |
| 37 | public: |
| 38 | explicit |
| 39 | Error(const std::string& what) |
| 40 | : std::runtime_error(what) |
| 41 | { |
| 42 | } |
| 43 | }; |
| 44 | /** |
| 45 | * @brief Create a new SequentialDataFetcher |
| 46 | * @param torrentFileName The name of the torrent file |
| 47 | * @param dataPath The path that the manager would look for already stored data packets and |
| 48 | * will write new data packets |
| 49 | */ |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 50 | SequentialDataFetcher(const ndn::Name& torrentFileName, |
| 51 | const std::string& dataPath, |
| 52 | bool seed = true); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 53 | |
| 54 | ~SequentialDataFetcher(); |
| 55 | |
| 56 | /** |
| 57 | * @brief Start the sequential data fetcher |
| 58 | */ |
| 59 | void |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 60 | start(const time::milliseconds& timeout = time::milliseconds::zero()); |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * @brief Pause the sequential data fetcher |
| 64 | */ |
| 65 | void |
| 66 | pause(); |
| 67 | |
| 68 | /** |
| 69 | * @brief Resume the sequential data fetcher |
| 70 | */ |
| 71 | void |
| 72 | resume(); |
| 73 | |
| 74 | protected: |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 75 | void |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 76 | downloadTorrentFile(); |
| 77 | |
| 78 | void |
| 79 | downloadManifestFiles(const std::vector<ndn::Name>& manifestsName); |
| 80 | |
| 81 | void |
| 82 | downloadPackets(const std::vector<ndn::Name>& packetsName); |
| 83 | |
| 84 | void |
| 85 | implementSequentialLogic(); |
| 86 | |
| 87 | virtual void |
| 88 | onDataPacketReceived(const ndn::Data& data); |
| 89 | |
| 90 | virtual void |
| 91 | onDataRetrievalFailure(const ndn::Interest& interest, const std::string& errorCode); |
| 92 | |
| 93 | virtual void |
| 94 | onManifestReceived(const std::vector<Name>& packetNames); |
| 95 | |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 96 | virtual void |
| 97 | onTorrentFileSegmentReceived(const std::vector<Name>& manifestNames); |
| 98 | |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 99 | private: |
| 100 | std::string m_dataPath; |
| 101 | ndn::Name m_torrentFileName; |
| 102 | shared_ptr<TorrentManager> m_manager; |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 103 | bool m_seedFlag; |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
spirosmastorakis | 2f769c2 | 2016-03-12 11:46:04 -0800 | [diff] [blame] | 106 | } // namespace ntorrent |
| 107 | } // namespace ndn |
| 108 | |
| 109 | #endif // SEQUENTIAL_DATA_FETCHER_HPP |