blob: 8c638dd567fbc8085181fe4bd08e5e8ecfdd964d [file] [log] [blame]
Mickey Sweatt527b0492016-03-02 11:07:48 -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#ifndef INCLUDED_TORRENT_FILE_MANAGER_H
22#define INCLUDED_TORRENT_FILE_MANAGER_H
23
Mickey Sweatt527b0492016-03-02 11:07:48 -080024#include "file-manifest.hpp"
spirosmastorakis9b68b532016-05-02 21:40:29 -070025#include "interest-queue.hpp"
spirosmastorakisa46eee42016-04-05 14:24:45 -070026#include "torrent-file.hpp"
spirosmastorakis4ff8c872016-04-14 09:51:38 -070027#include "update-handler.hpp"
Mickey Sweatt527b0492016-03-02 11:07:48 -080028
Mickey Sweatt527b0492016-03-02 11:07:48 -080029#include <ndn-cxx/data.hpp>
spirosmastorakisa46eee42016-04-05 14:24:45 -070030#include <ndn-cxx/face.hpp>
31#include <ndn-cxx/interest.hpp>
32#include <ndn-cxx/link.hpp>
33#include <ndn-cxx/name.hpp>
34#include <ndn-cxx/security/key-chain.hpp>
Mickey Sweatt527b0492016-03-02 11:07:48 -080035
36#include <boost/filesystem/fstream.hpp>
Mickey Sweatt527b0492016-03-02 11:07:48 -080037
38#include <functional>
39#include <memory>
40#include <string>
41#include <unordered_map>
Mickey Sweatt44e4fd92016-05-02 15:43:11 -070042#include <unordered_set>
Mickey Sweatt527b0492016-03-02 11:07:48 -080043#include <vector>
44
45namespace fs = boost::filesystem;
46
47namespace ndn {
48namespace ntorrent {
49
Mickey Sweatt599bfef2016-04-05 19:11:20 -070050class TorrentManager : noncopyable {
Mickey Sweatt527b0492016-03-02 11:07:48 -080051 /**
52 * \class TorrentManager
53 *
54 * \brief A class to manage the interaction with the system in seeding/leaching a torrent
55 */
56 public:
spirosmastorakisa46eee42016-04-05 14:24:45 -070057 typedef std::function<void(const ndn::Name&)> DataReceivedCallback;
58 typedef std::function<void(const std::vector<ndn::Name>&)> ManifestReceivedCallback;
spirosmastorakis50642f82016-04-08 12:11:18 -070059 typedef std::function<void(const std::vector<ndn::Name>&)> TorrentFileReceivedCallback;
spirosmastorakisa46eee42016-04-05 14:24:45 -070060 typedef std::function<void(const ndn::Name&, const std::string&)> FailedCallback;
Mickey Sweatt527b0492016-03-02 11:07:48 -080061
spirosmastorakisa46eee42016-04-05 14:24:45 -070062 /*
63 * \brief Create a new Torrent manager with the specified parameters.
64 * @param torrentFileName The full name of the initial segment of the torrent file
65 * @param dataPath The path to the location on disk to use for the torrent data
66 * @param face Optional face object to be used for data retrieval
67 *
68 * The behavior is undefined unless Initialize() is called before calling any other method on a
69 * TorrentManger object.
70 */
Mickey Sweatte908a5c2016-04-08 14:10:45 -070071 TorrentManager(const ndn::Name& torrentFileName,
72 const std::string& dataPath,
Mickey Sweatt44e4fd92016-05-02 15:43:11 -070073 bool seed = true,
Mickey Sweatte908a5c2016-04-08 14:10:45 -070074 std::shared_ptr<Face> face = nullptr);
spirosmastorakisa46eee42016-04-05 14:24:45 -070075
Mickey Sweatt527b0492016-03-02 11:07:48 -080076 /*
spirosmastorakisa46eee42016-04-05 14:24:45 -070077 * @brief Initialize the state of this object.
Mickey Sweatt527b0492016-03-02 11:07:48 -080078 *
Mickey Sweatt527b0492016-03-02 11:07:48 -080079 * Read and validate from disk all torrent file segments, file manifests, and data packets for
80 * the torrent file managed by this object initializing all state in this manager respectively.
81 * Also seeds all validated data.
spirosmastorakisa46eee42016-04-05 14:24:45 -070082 */
83 void
84 Initialize();
85
Mickey Sweatt15dde2d2016-04-28 23:42:45 -070086 /**
87 * brief Return 'true' if all segments of the torrent file downloaded, 'false' otherwise.
88 */
89 bool
90 hasAllTorrentSegments() const;
91
spirosmastorakisa46eee42016-04-05 14:24:45 -070092 /*
Mickey Sweatt15dde2d2016-04-28 23:42:45 -070093 * \brief Given a data packet name, find whether we have already downloaded this packet
94 * @param dataName The name of the data packet to download
95 * @return True if we have already downloaded this packet, false otherwise
spirosmastorakisa46eee42016-04-05 14:24:45 -070096 *
Mickey Sweatt527b0492016-03-02 11:07:48 -080097 */
Mickey Sweatt15dde2d2016-04-28 23:42:45 -070098 bool
99 hasDataPacket(const Name& dataName) const;
100
101 /*
102 * \brief Find the torrent file segment that we should download
103 * (either we have nothing or we have them all)
104 * @return A shared_ptr to the name of the segment to download or
105 * nullptr if we have all the segments
106 *
107 */
108 shared_ptr<Name>
109 findTorrentFileSegmentToDownload() const;
110
111 /*
112 * \brief Given a file manifest segment name, find the next file manifest segment
113 * that we should download
114 * @param manifestName The name of the file manifest segment that we want to download
115 * @return A shared_ptr to the name of the segment to download or
116 * nullptr if we have all the segments
117 *
118 */
119 shared_ptr<Name>
120 findManifestSegmentToDownload(const Name& manifestName) const;
121
122 /*
123 * \brief Find the segments of all the file manifests that we are missing
124 * @param manifestNames The name of the manifest file segments to download (currently missing)
125 * This parameter is used as an output vector of names
126 */
127 void
128 findFileManifestsToDownload(std::vector<Name>& manifestNames) const;
129
130 /*
131 * \brief Find the names of the data packets of a file manifest that we are currently missing
132 * @param manifestName The name of the manifest
133 * @param packetNames The name of the data packets to be downloaded
134 * (used as an output vector of names)
135 *
136 * No matter what segment of a manifest file the manifestName parameter might refer to, the
137 * missing data packets starting from the first segment of this manifest file would be returned
138 *
139 */
140 void
141 findDataPacketsToDownload(const Name& manifestName, std::vector<Name>& packetNames) const;
142
143 /*
144 * \brief Find all the data packets that we are currently missing
145 * @param packetNames The name of the data packets to be downloaded
146 * (used as an output vector of names)
147 *
148 */
149 void
150 findAllMissingDataPackets(std::vector<Name>& packetNames) const;
Mickey Sweatt527b0492016-03-02 11:07:48 -0800151
spirosmastorakisa46eee42016-04-05 14:24:45 -0700152 /*
Mickey Sweatt44e4fd92016-05-02 15:43:11 -0700153 * @brief Stop all network activities of this manager
154 */
155 void
156 shutdown();
157 /*
spirosmastorakisa46eee42016-04-05 14:24:45 -0700158 * @brief Download the torrent file
159 * @param path The path to write the downloaded segments
160 * @param onSuccess Callback to be called if we successfully download all the
161 * segments of the torrent file. It passes the name of the file manifest
162 * to be downloaded to the callback
163 * @param onFailed Callaback to be called if we fail to download a segment of
164 * the torrent file. It passes the name of the torrent file segment that
165 * failed to download and a failure reason to the callback
166 *
167 * This method provides non-blocking downloading of all the torrent file segments
spirosmastorakisa46eee42016-04-05 14:24:45 -0700168 */
169 void
170 downloadTorrentFile(const std::string& path,
Mickey Sweatt15dde2d2016-04-28 23:42:45 -0700171 TorrentFileReceivedCallback onSuccess = {},
172 FailedCallback onFailed = {});
Mickey Sweatt527b0492016-03-02 11:07:48 -0800173
spirosmastorakisa46eee42016-04-05 14:24:45 -0700174 /*
175 * @brief Download a file manifest
176 * @param manifestName The name of the manifest file to be downloaded
177 * @param path The path to write the downloaded segments
178 * @param onSuccess Callback to be called if we successfully download all the
179 * segments of the file manifest. It passes the names of the data packets
180 * to be downloaded to the callback
181 * @param onFailed Callaback to be called if we fail to download a segment of
182 * the file manifest. It passes the name of the data packet that failed
183 * to download and a failure reason
184 *
185 * This method provides non-blocking downloading of all the file manifest segments
186 *
187 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700188 void
189 download_file_manifest(const Name& manifestName,
190 const std::string& path,
191 ManifestReceivedCallback onSuccess,
192 FailedCallback onFailed);
Mickey Sweatt527b0492016-03-02 11:07:48 -0800193
spirosmastorakisa46eee42016-04-05 14:24:45 -0700194 /*
195 * @brief Download a data packet
196 * @param packetName The name of the data packet to be downloaded
197 * @param onSuccess Callback to be called if we successfully download the data packet.
198 * It passes the name of the data packet to the callback
199 * @param onFailed Callaback to be called if we fail to download the requested data packet
200 * It passes the name of the data packet to the callback and a failure reason
201 *
202 * This method writes the downloaded data packet to m_dataPath on disk
203 *
204 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700205 void
206 download_data_packet(const Name& packetName,
207 DataReceivedCallback onSuccess,
208 FailedCallback onFailed);
Mickey Sweatt527b0492016-03-02 11:07:48 -0800209
Mickey Sweatt527b0492016-03-02 11:07:48 -0800210 // Seed the specified 'data' to the network.
spirosmastorakis50642f82016-04-08 12:11:18 -0700211 void
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700212 seed(const Data& data);
Mickey Sweatt527b0492016-03-02 11:07:48 -0800213
Mickey Sweattb7ee19c2016-04-21 12:18:15 -0700214 /**
215 * @brief Process any data to receive or call timeout callbacks and update prefix list (if needed)
Mickey Sweatt15dde2d2016-04-28 23:42:45 -0700216 * By default this blocks until all operations are complete.
Mickey Sweattb7ee19c2016-04-21 12:18:15 -0700217 */
218 void
Mickey Sweatt15dde2d2016-04-28 23:42:45 -0700219 processEvents(const time::milliseconds& timeout = time::milliseconds(0));
Mickey Sweattb7ee19c2016-04-21 12:18:15 -0700220
Mickey Sweatt527b0492016-03-02 11:07:48 -0800221 protected:
Mickey Sweattb7ee19c2016-04-21 12:18:15 -0700222 /**
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700223 * \brief Write @p packet composed of torrent date to disk.
224 * @param packet The data packet to be written to the disk
225 * Write the Data packet to disk, return 'true' if data successfully written to disk 'false'
226 * otherwise. Behavior is undefined unless the corresponding file manifest has already been
227 * downloaded.
spirosmastorakisa46eee42016-04-05 14:24:45 -0700228 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700229 bool
230 writeData(const Data& packet);
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700231
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700232 /*
233 * \brief Write the @p segment torrent segment to disk at the specified path.
234 * @param segment The torrent file segment to be written to disk
235 * @param path The path at which to write the torrent file segment
236 * Write the segment to disk, return 'true' if data successfully written to disk 'false'
237 * otherwise. Behavior is undefined unless @segment is a correct segment for the torrent file of
238 * this manager and @p path is the directory used for all segments of this torrent file.
239 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700240 bool
241 writeTorrentSegment(const TorrentFile& segment, const std::string& path);
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700242
Mickey Sweatt599bfef2016-04-05 19:11:20 -0700243 /*
244 * \brief Write the @p manifest file manifest to disk at the specified @p path.
245 * @param manifest The file manifest to be written to disk
246 * @param path The path at which to write the file manifest
247 * Write the file manifest to disk, return 'true' if data successfully written to disk 'false'
248 * otherwise. Behavior is undefined unless @manifest is a correct file manifest for a file in the
249 * torrent file of this manager and @p path is the directory used for all file manifests of this
250 * torrent file.
251 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700252 bool
253 writeFileManifest(const FileManifest& manifest, const std::string& path);
Mickey Sweattafda1f12016-04-04 17:15:11 -0700254
spirosmastorakisa46eee42016-04-05 14:24:45 -0700255 /*
256 * \brief Download the segments of the torrent file
257 * @param name The name of the torrent file to be downloaded
258 * @param path The path to write the torrent file on disk
spirosmastorakisa46eee42016-04-05 14:24:45 -0700259 * @param onSuccess Optional callback to be called when all the segments of the torrent file
Mickey Sweatt15dde2d2016-04-28 23:42:45 -0700260 * have been downloaded. The default value is an empty callback.
spirosmastorakisa46eee42016-04-05 14:24:45 -0700261 * @param onFailed Optional callback to be called when we fail to download a segment of the
Mickey Sweatt15dde2d2016-04-28 23:42:45 -0700262 * torrent file. The default value is an empty callback.
spirosmastorakisa46eee42016-04-05 14:24:45 -0700263 *
264 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700265 void
266 downloadTorrentFileSegment(const ndn::Name& name,
267 const std::string& path,
spirosmastorakis50642f82016-04-08 12:11:18 -0700268 TorrentFileReceivedCallback onSuccess,
269 FailedCallback onFailed);
Mickey Sweatt527b0492016-03-02 11:07:48 -0800270
spirosmastorakisa46eee42016-04-05 14:24:45 -0700271 /*
272 * \brief Download the segments of a file manifest
273 * @param manifestName The name of the file manifest to be downloaded
274 * @param path The path to write the file manifest on disk
275 * @param packetNames A vector containing the name of the data packets in the file manifest
276 * @param onSuccess Callback to be called when all the segments of the file manifest have been
277 * downloaded
278 * @param onFailed Callback to be called when we fail to download a file manifest segment
279 *
280 */
spirosmastorakis50642f82016-04-08 12:11:18 -0700281 void
282 downloadFileManifestSegment(const Name& manifestName,
283 const std::string& path,
284 std::shared_ptr<std::vector<Name>> packetNames,
285 ManifestReceivedCallback onSuccess,
286 FailedCallback onFailed);
spirosmastorakisa46eee42016-04-05 14:24:45 -0700287
288 enum {
289 // Number of times to retry if a routable prefix fails to retrieve data
290 MAX_NUM_OF_RETRIES = 5,
291 // Number of Interests to be sent before sorting the stats table
spirosmastorakis9b68b532016-05-02 21:40:29 -0700292 SORTING_INTERVAL = 100,
293 // Maximum window size used for sending new Interests out
294 WINDOW_SIZE = 50
spirosmastorakisa46eee42016-04-05 14:24:45 -0700295 };
Mickey Sweatt527b0492016-03-02 11:07:48 -0800296
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700297 void onDataReceived(const Data& data);
298
299 void
300 onInterestReceived(const InterestFilter& filter, const Interest& interest);
301
302 void
303 onRegisterFailed(const Name& prefix, const std::string& reason);
304
Mickey Sweatt15dde2d2016-04-28 23:42:45 -0700305protected:
Mickey Sweatt527b0492016-03-02 11:07:48 -0800306 // A map from each fileManifest to corresponding file stream on disk and a bitmap of which Data
307 // packets this manager currently has
308 mutable std::unordered_map<Name,
309 std::pair<std::shared_ptr<fs::fstream>,
310 std::vector<bool>>> m_fileStates;
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700311 // A map for each initial manifest to the size for the sub-manifest
312 std::unordered_map<std::string, size_t> m_subManifestSizes;
Mickey Sweatt527b0492016-03-02 11:07:48 -0800313 // The segments of the TorrentFile this manager has
314 std::vector<TorrentFile> m_torrentSegments;
315 // The FileManifests this manager has
316 std::vector<FileManifest> m_fileManifests;
317 // The name of the initial segment of the torrent file for this manager
318 Name m_torrentFileName;
319 // The path to the location on disk of the Data packet for this manager
320 std::string m_dataPath;
spirosmastorakisa46eee42016-04-05 14:24:45 -0700321
322private:
323 shared_ptr<Interest>
324 createInterest(Name name);
325
spirosmastorakis9b68b532016-05-02 21:40:29 -0700326 void
327 sendInterest();
328
329
Mickey Sweatt44e4fd92016-05-02 15:43:11 -0700330 // A flag to determine if upon completion we should continue seeding
331 bool m_seedFlag;
spirosmastorakisa46eee42016-04-05 14:24:45 -0700332 // Face used for network communication
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700333 std::shared_ptr<Face> m_face;
Mickey Sweatt44e4fd92016-05-02 15:43:11 -0700334 // Stats table where routable prefixes are stored
335 StatsTable m_statsTable;
spirosmastorakisa46eee42016-04-05 14:24:45 -0700336 // Iterator to the routable prefix that we currently use
337 StatsTable::iterator m_stats_table_iter;
338 // Number of retries per routable prefix
339 uint64_t m_retries;
340 // Number of Interests sent since last sorting
341 uint64_t m_sortingCounter;
342 // Keychain instance
spirosmastorakis4ff8c872016-04-14 09:51:38 -0700343 shared_ptr<KeyChain> m_keyChain;
Mickey Sweatt0dc0a1e2016-05-04 11:25:49 -0700344 // A collection for all interests that have been sent for which we have not received a response
Mickey Sweatt44e4fd92016-05-02 15:43:11 -0700345 std::unordered_set<ndn::Name> m_pendingInterests;
Mickey Sweatt0dc0a1e2016-05-04 11:25:49 -0700346 // A queue to hold all interests for requested data that we have yet to send
spirosmastorakis9b68b532016-05-02 21:40:29 -0700347 shared_ptr<InterestQueue> m_interestQueue;
Mickey Sweatt15dde2d2016-04-28 23:42:45 -0700348 // TODO(spyros) Fix and reintegrate update handler
349 // // Update Handler instance
350 // shared_ptr<UpdateHandler> m_updateHandler;
Mickey Sweatt527b0492016-03-02 11:07:48 -0800351};
352
353inline
spirosmastorakisa46eee42016-04-05 14:24:45 -0700354TorrentManager::TorrentManager(const ndn::Name& torrentFileName,
355 const std::string& dataPath,
Mickey Sweatt44e4fd92016-05-02 15:43:11 -0700356 bool seed,
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700357 std::shared_ptr<Face> face)
Mickey Sweatt527b0492016-03-02 11:07:48 -0800358: m_fileStates()
359, m_torrentSegments()
360, m_fileManifests()
361, m_torrentFileName(torrentFileName)
362, m_dataPath(dataPath)
Mickey Sweatt44e4fd92016-05-02 15:43:11 -0700363, m_seedFlag(seed)
spirosmastorakisa46eee42016-04-05 14:24:45 -0700364, m_face(face)
365, m_retries(0)
366, m_sortingCounter(0)
367, m_keyChain(new KeyChain())
Mickey Sweatt527b0492016-03-02 11:07:48 -0800368{
spirosmastorakis9b68b532016-05-02 21:40:29 -0700369 m_interestQueue = make_shared<InterestQueue>();
370
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700371 if(face == nullptr) {
spirosmastorakis4ff8c872016-04-14 09:51:38 -0700372 m_face = make_shared<Face>();
Mickey Sweatte908a5c2016-04-08 14:10:45 -0700373 }
spirosmastorakis4ff8c872016-04-14 09:51:38 -0700374
spirosmastorakisa46eee42016-04-05 14:24:45 -0700375 // Hardcoded prefixes for now
376 // TODO(Spyros): Think of something more clever to bootstrap...
377 m_statsTable.insert("/ucla");
378 m_statsTable.insert("/arizona");
379 m_stats_table_iter = m_statsTable.begin();
Mickey Sweatt527b0492016-03-02 11:07:48 -0800380}
381
Mickey Sweattb7ee19c2016-04-21 12:18:15 -0700382inline
383void
384TorrentManager::processEvents(const time::milliseconds& timeout)
385{
386 m_face->processEvents(timeout);
387}
388
Mickey Sweatt15dde2d2016-04-28 23:42:45 -0700389inline
390bool
391TorrentManager::hasAllTorrentSegments() const
392{
393 return findTorrentFileSegmentToDownload() == nullptr;
394}
395
Mickey Sweatt527b0492016-03-02 11:07:48 -0800396} // end ntorrent
397} // end ndn
398
spirosmastorakisa46eee42016-04-05 14:24:45 -0700399#endif // INCLUDED_TORRENT_FILE_MANAGER_H