blob: 8687d9c036dc3d2a8afbe9185ece986d073f1a46 [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 */
50 SequentialDataFetcher(const ndn::Name& torrentFileName,
51 const std::string& dataPath);
52
53 ~SequentialDataFetcher();
54
55 /**
56 * @brief Start the sequential data fetcher
57 */
58 void
59 start();
60
61 /**
62 * @brief Pause the sequential data fetcher
63 */
64 void
65 pause();
66
67 /**
68 * @brief Resume the sequential data fetcher
69 */
70 void
71 resume();
72
Mickey Sweattb7ee19c2016-04-21 12:18:15 -070073 void
74 seed(const time::milliseconds& timeout = time::milliseconds::zero()) const;
75
spirosmastorakis2f769c22016-03-12 11:46:04 -080076 protected:
77 std::vector<ndn::Name>
78 downloadTorrentFile();
79
80 void
81 downloadManifestFiles(const std::vector<ndn::Name>& manifestsName);
82
83 void
84 downloadPackets(const std::vector<ndn::Name>& packetsName);
85
86 void
87 implementSequentialLogic();
88
89 virtual void
90 onDataPacketReceived(const ndn::Data& data);
91
92 virtual void
93 onDataRetrievalFailure(const ndn::Interest& interest, const std::string& errorCode);
94
95 virtual void
96 onManifestReceived(const std::vector<Name>& packetNames);
97
98 private:
99 std::string m_dataPath;
100 ndn::Name m_torrentFileName;
101 shared_ptr<TorrentManager> m_manager;
102};
103
Mickey Sweattb7ee19c2016-04-21 12:18:15 -0700104inline
105void
106SequentialDataFetcher::seed(const time::milliseconds& timeout) const
107{
108 m_manager->processEvents(timeout);
109}
110
spirosmastorakis2f769c22016-03-12 11:46:04 -0800111} // namespace ntorrent
112} // namespace ndn
113
114#endif // SEQUENTIAL_DATA_FETCHER_HPP