blob: ba99c79ffa940288094430d7c8adc7652d589956 [file] [log] [blame]
spirosmastorakis2f769c22016-03-12 11:46:04 -08001/* -*- 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
30namespace ndn {
31namespace ntorrent {
32
33class 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 Sweatt44e4fd92016-05-02 15:43:11 -070050 SequentialDataFetcher(const ndn::Name& torrentFileName,
51 const std::string& dataPath,
52 bool seed = true);
spirosmastorakis2f769c22016-03-12 11:46:04 -080053
54 ~SequentialDataFetcher();
55
56 /**
57 * @brief Start the sequential data fetcher
58 */
59 void
Mickey Sweatt15dde2d2016-04-28 23:42:45 -070060 start(const time::milliseconds& timeout = time::milliseconds::zero());
spirosmastorakis2f769c22016-03-12 11:46:04 -080061
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 Sweatt15dde2d2016-04-28 23:42:45 -070075 void
spirosmastorakis2f769c22016-03-12 11:46:04 -080076 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 Sweatt15dde2d2016-04-28 23:42:45 -070096 virtual void
97 onTorrentFileSegmentReceived(const std::vector<Name>& manifestNames);
98
spirosmastorakis2f769c22016-03-12 11:46:04 -080099 private:
100 std::string m_dataPath;
101 ndn::Name m_torrentFileName;
102 shared_ptr<TorrentManager> m_manager;
Mickey Sweatt44e4fd92016-05-02 15:43:11 -0700103 bool m_seedFlag;
spirosmastorakis2f769c22016-03-12 11:46:04 -0800104};
105
spirosmastorakis2f769c22016-03-12 11:46:04 -0800106} // namespace ntorrent
107} // namespace ndn
108
109#endif // SEQUENTIAL_DATA_FETCHER_HPP