Create a sequential torrent data fetching strategy
Change-Id: I465297bfa3b8c4c8e6e7f7cd028b2d4afeb4e669
Refs: #3507
diff --git a/src/fetching-strategy-manager.hpp b/src/fetching-strategy-manager.hpp
index 3963c75..53de939 100644
--- a/src/fetching-strategy-manager.hpp
+++ b/src/fetching-strategy-manager.hpp
@@ -39,7 +39,7 @@
* @brief Class Destructor
*/
virtual
- ~FetchingStrategyManager() = default;
+ ~FetchingStrategyManager();
/**
* @brief Method called to start the torrent downloading
@@ -87,6 +87,11 @@
onManifestReceived(const std::vector<Name>& packetNames) = 0;
};
+inline
+FetchingStrategyManager::~FetchingStrategyManager()
+{
+}
+
} // namespace ntorrent
} // namespace ndn
diff --git a/src/sequential-data-fetcher.cpp b/src/sequential-data-fetcher.cpp
new file mode 100644
index 0000000..971b59f
--- /dev/null
+++ b/src/sequential-data-fetcher.cpp
@@ -0,0 +1,159 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+* Copyright (c) 2016 Regents of the University of California.
+*
+* This file is part of the nTorrent codebase.
+*
+* nTorrent is free software: you can redistribute it and/or modify it under the
+* terms of the GNU Lesser General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later version.
+*
+* nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY
+* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+*
+* You should have received copies of the GNU General Public License and GNU Lesser
+* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
+* <http://www.gnu.org/licenses/>.
+*
+* See AUTHORS for complete list of nTorrent authors and contributors.
+*/
+
+#include "sequential-data-fetcher.hpp"
+#include "util/io-util.hpp"
+
+namespace ndn {
+namespace ntorrent {
+
+SequentialDataFetcher::SequentialDataFetcher(const ndn::Name& torrentFileName,
+ const std::string& dataPath)
+ : m_dataPath(dataPath)
+ , m_torrentFileName(torrentFileName)
+{
+ m_manager = make_shared<TorrentManager>(m_torrentFileName, m_dataPath);
+}
+
+SequentialDataFetcher::~SequentialDataFetcher()
+{
+}
+
+void
+SequentialDataFetcher::start()
+{
+ m_manager->Initialize();
+ // downloading logic
+ this->implementSequentialLogic();
+}
+
+void
+SequentialDataFetcher::pause()
+{
+ // TODO(Spyros): Implement asyncrhonous pause of the torrent downloading
+ // For now, do nothing...
+ throw(Error("Not implemented yet"));
+}
+
+void
+SequentialDataFetcher::resume()
+{
+ // TODO(Spyros): Implement asyncrhonous re-establishment of the torrent downloading
+ // For now, do nothing...
+ throw(Error("Not implemented yet"));
+}
+
+std::vector<ndn::Name>
+SequentialDataFetcher::downloadTorrentFile()
+{
+ std::vector<ndn::Name> returnedNames;
+ returnedNames = m_manager->downloadTorrentFile(".appdata/torrent_files/");
+ std::cout << "Torrent File Received: "
+ << m_torrentFileName.getSubName(0, m_torrentFileName.size() - 1) << std::endl;
+ return returnedNames;
+}
+
+void
+SequentialDataFetcher::downloadManifestFiles(const std::vector<ndn::Name>& manifestsName)
+{
+ std::vector<ndn::Name> packetsName;
+ for (auto i = manifestsName.begin(); i != manifestsName.end(); i++) {
+ m_manager->download_file_manifest(*i,
+ ".appdata/manifests/",
+ bind(&SequentialDataFetcher::onManifestReceived, this, _1),
+ bind(&SequentialDataFetcher::onDataRetrievalFailure, this, _1, _2));
+ }
+}
+
+void
+SequentialDataFetcher::downloadPackets(const std::vector<ndn::Name>& packetsName)
+{
+ for (auto i = packetsName.begin(); i != packetsName.end(); i++) {
+ m_manager->download_data_packet(*i,
+ bind(&SequentialDataFetcher::onDataPacketReceived, this, _1),
+ bind(&SequentialDataFetcher::onDataRetrievalFailure, this, _1, _2));
+ }
+}
+
+void
+SequentialDataFetcher::implementSequentialLogic() {
+ std::vector<ndn::Name> returnedNames;
+ returnedNames = this->downloadTorrentFile();
+ if (returnedNames.empty()) {
+ // we have downloaded the entire torrent (including manifests, data packets, etc..)
+ return;
+ }
+ // check the first returned name whether it is the name of a file manifest or a data packet
+ const Name& nameToCheck = returnedNames[0];
+ if (IoUtil::findType(nameToCheck) == IoUtil::DATA_PACKET) {
+ // In this case, the returned names correspond to data packets
+ this->downloadPackets(returnedNames);
+ }
+ else {
+ // In this case, the returned names correspond to file manifests
+ this->downloadManifestFiles(returnedNames);
+ }
+}
+
+void
+SequentialDataFetcher::onDataPacketReceived(const ndn::Data& data)
+{
+ // Data Packet Received
+ std::cout << "Data Packet Received: " << data.getName();
+}
+
+void
+SequentialDataFetcher::onManifestReceived(const std::vector<Name>& packetNames)
+{
+ std::cout << "Manifest File Received: "
+ << packetNames[0].getSubName(0, packetNames[0].size()- 3) << std::endl;
+ this->downloadPackets(packetNames);
+}
+
+void
+SequentialDataFetcher::onDataRetrievalFailure(const ndn::Interest& interest,
+ const std::string& errorCode)
+{
+ std::cerr << "Data Retrieval Failed: " << interest.getName() << std::endl;
+
+ // Data retrieval failure
+ uint32_t nameType = IoUtil::findType(interest.getName());
+ if (nameType == IoUtil::TORRENT_FILE) {
+ // this should never happen
+ std::cerr << "Torrent File Segment Downloading Failed: " << interest.getName();
+ this->downloadTorrentFile();
+ }
+ else if (nameType == IoUtil::FILE_MANIFEST) {
+ std::cerr << "Manifest File Segment Downloading Failed: " << interest.getName();
+ this->downloadManifestFiles({ interest.getName() });
+ }
+ else if (nameType == IoUtil::DATA_PACKET) {
+ std::cerr << "Data Packet Downloading Failed: " << interest.getName();
+ this->downloadPackets({ interest.getName() });
+ }
+ else {
+ // This should never happen
+ std::cerr << "Unknown Packet Type Downloading Failed: " << interest.getName();
+ }
+}
+
+} // namespace ntorrent
+} // namespace ndn
diff --git a/src/sequential-data-fetcher.hpp b/src/sequential-data-fetcher.hpp
new file mode 100644
index 0000000..ea6ce90
--- /dev/null
+++ b/src/sequential-data-fetcher.hpp
@@ -0,0 +1,104 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+* Copyright (c) 2016 Regents of the University of California.
+*
+* This file is part of the nTorrent codebase.
+*
+* nTorrent is free software: you can redistribute it and/or modify it under the
+* terms of the GNU Lesser General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later version.
+*
+* nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY
+* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+*
+* You should have received copies of the GNU General Public License and GNU Lesser
+* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
+* <http://www.gnu.org/licenses/>.
+*
+* See AUTHORS for complete list of nTorrent authors and contributors.
+*/
+
+#ifndef SEQUENTIAL_DATA_FETCHER_HPP
+#define SEQUENTIAL_DATA_FETCHER_HPP
+
+#include "fetching-strategy-manager.hpp"
+#include "torrent-manager.hpp"
+
+#include <ndn-cxx/name.hpp>
+
+namespace ndn {
+namespace ntorrent {
+
+class SequentialDataFetcher : FetchingStrategyManager {
+ public:
+ class Error : public std::runtime_error
+ {
+ public:
+ explicit
+ Error(const std::string& what)
+ : std::runtime_error(what)
+ {
+ }
+ };
+ /**
+ * @brief Create a new SequentialDataFetcher
+ * @param torrentFileName The name of the torrent file
+ * @param dataPath The path that the manager would look for already stored data packets and
+ * will write new data packets
+ */
+ SequentialDataFetcher(const ndn::Name& torrentFileName,
+ const std::string& dataPath);
+
+ ~SequentialDataFetcher();
+
+ /**
+ * @brief Start the sequential data fetcher
+ */
+ void
+ start();
+
+ /**
+ * @brief Pause the sequential data fetcher
+ */
+ void
+ pause();
+
+ /**
+ * @brief Resume the sequential data fetcher
+ */
+ void
+ resume();
+
+ protected:
+ std::vector<ndn::Name>
+ downloadTorrentFile();
+
+ void
+ downloadManifestFiles(const std::vector<ndn::Name>& manifestsName);
+
+ void
+ downloadPackets(const std::vector<ndn::Name>& packetsName);
+
+ void
+ implementSequentialLogic();
+
+ virtual void
+ onDataPacketReceived(const ndn::Data& data);
+
+ virtual void
+ onDataRetrievalFailure(const ndn::Interest& interest, const std::string& errorCode);
+
+ virtual void
+ onManifestReceived(const std::vector<Name>& packetNames);
+
+ private:
+ std::string m_dataPath;
+ ndn::Name m_torrentFileName;
+ shared_ptr<TorrentManager> m_manager;
+};
+
+} // namespace ntorrent
+} // namespace ndn
+
+#endif // SEQUENTIAL_DATA_FETCHER_HPP