blob: 971b59fc4c991fd37ab7820c769d78c233653d40 [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#include "sequential-data-fetcher.hpp"
23#include "util/io-util.hpp"
24
25namespace ndn {
26namespace ntorrent {
27
28SequentialDataFetcher::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
36SequentialDataFetcher::~SequentialDataFetcher()
37{
38}
39
40void
41SequentialDataFetcher::start()
42{
43 m_manager->Initialize();
44 // downloading logic
45 this->implementSequentialLogic();
46}
47
48void
49SequentialDataFetcher::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
56void
57SequentialDataFetcher::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
64std::vector<ndn::Name>
65SequentialDataFetcher::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
74void
75SequentialDataFetcher::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));
83 }
84}
85
86void
87SequentialDataFetcher::downloadPackets(const std::vector<ndn::Name>& packetsName)
88{
89 for (auto i = packetsName.begin(); i != packetsName.end(); i++) {
90 m_manager->download_data_packet(*i,
91 bind(&SequentialDataFetcher::onDataPacketReceived, this, _1),
92 bind(&SequentialDataFetcher::onDataRetrievalFailure, this, _1, _2));
93 }
94}
95
96void
97SequentialDataFetcher::implementSequentialLogic() {
98 std::vector<ndn::Name> returnedNames;
99 returnedNames = this->downloadTorrentFile();
100 if (returnedNames.empty()) {
101 // we have downloaded the entire torrent (including manifests, data packets, etc..)
102 return;
103 }
104 // check the first returned name whether it is the name of a file manifest or a data packet
105 const Name& nameToCheck = returnedNames[0];
106 if (IoUtil::findType(nameToCheck) == IoUtil::DATA_PACKET) {
107 // In this case, the returned names correspond to data packets
108 this->downloadPackets(returnedNames);
109 }
110 else {
111 // In this case, the returned names correspond to file manifests
112 this->downloadManifestFiles(returnedNames);
113 }
114}
115
116void
117SequentialDataFetcher::onDataPacketReceived(const ndn::Data& data)
118{
119 // Data Packet Received
120 std::cout << "Data Packet Received: " << data.getName();
121}
122
123void
124SequentialDataFetcher::onManifestReceived(const std::vector<Name>& packetNames)
125{
126 std::cout << "Manifest File Received: "
127 << packetNames[0].getSubName(0, packetNames[0].size()- 3) << std::endl;
128 this->downloadPackets(packetNames);
129}
130
131void
132SequentialDataFetcher::onDataRetrievalFailure(const ndn::Interest& interest,
133 const std::string& errorCode)
134{
135 std::cerr << "Data Retrieval Failed: " << interest.getName() << std::endl;
136
137 // Data retrieval failure
138 uint32_t nameType = IoUtil::findType(interest.getName());
139 if (nameType == IoUtil::TORRENT_FILE) {
140 // this should never happen
141 std::cerr << "Torrent File Segment Downloading Failed: " << interest.getName();
142 this->downloadTorrentFile();
143 }
144 else if (nameType == IoUtil::FILE_MANIFEST) {
145 std::cerr << "Manifest File Segment Downloading Failed: " << interest.getName();
146 this->downloadManifestFiles({ interest.getName() });
147 }
148 else if (nameType == IoUtil::DATA_PACKET) {
149 std::cerr << "Data Packet Downloading Failed: " << interest.getName();
150 this->downloadPackets({ interest.getName() });
151 }
152 else {
153 // This should never happen
154 std::cerr << "Unknown Packet Type Downloading Failed: " << interest.getName();
155 }
156}
157
158} // namespace ntorrent
159} // namespace ndn