blob: 587337478a88459b7956302cb574c5cfa8d1a18e [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));
Mickey Sweattb7ee19c2016-04-21 12:18:15 -070083 m_manager->processEvents();
spirosmastorakis2f769c22016-03-12 11:46:04 -080084 }
85}
86
87void
88SequentialDataFetcher::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 Sweattb7ee19c2016-04-21 12:18:15 -070094 m_manager->processEvents();
spirosmastorakis2f769c22016-03-12 11:46:04 -080095 }
96}
97
98void
99SequentialDataFetcher::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
118void
119SequentialDataFetcher::onDataPacketReceived(const ndn::Data& data)
120{
121 // Data Packet Received
122 std::cout << "Data Packet Received: " << data.getName();
123}
124
125void
126SequentialDataFetcher::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
133void
134SequentialDataFetcher::onDataRetrievalFailure(const ndn::Interest& interest,
135 const std::string& errorCode)
136{
137 std::cerr << "Data Retrieval Failed: " << interest.getName() << std::endl;
Mickey Sweattb7ee19c2016-04-21 12:18:15 -0700138
spirosmastorakis2f769c22016-03-12 11:46:04 -0800139 // 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