blob: fc790a855d7baf012a1f1e021d8b89ee8f116ad2 [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
24#include "torrent-file.hpp"
25#include "file-manifest.hpp"
26
27#include <ndn-cxx/name.hpp>
28#include <ndn-cxx/data.hpp>
29
30#include <boost/filesystem/fstream.hpp>
Mickey Sweatt527b0492016-03-02 11:07:48 -080031
32#include <functional>
33#include <memory>
34#include <string>
35#include <unordered_map>
36#include <vector>
37
38namespace fs = boost::filesystem;
39
40namespace ndn {
41namespace ntorrent {
42
Mickey Sweatt599bfef2016-04-05 19:11:20 -070043class TorrentManager : noncopyable {
Mickey Sweatt527b0492016-03-02 11:07:48 -080044 /**
45 * \class TorrentManager
46 *
47 * \brief A class to manage the interaction with the system in seeding/leaching a torrent
48 */
49 public:
50 typedef std::function<void(const ndn::Name&)> DataReceivedCallback;
51 typedef std::function<void(const std::vector<ndn::Name>&)> ManifestReceivedCallback;
52 typedef std::function<void(const ndn::Name&, const std::string&)> FailedCallback;
53
54 TorrentManager(const ndn::Name& torrentFileName,
55 const std::string& dataPath);
56 /*
57 * \brief Create a new Torrent manager with the specified parameters.
58 * @param torrentFileName The full name of the initial segment of the torrent file
59 * @param filePath The path to the location on disk to use for the torrent data
60 *
61 * The behavior is undefined unless Initialize() is called before calling any other method on a
62 * TorrentManger object.
63 */
64
65 void Initialize();
66 /*
67 * \brief Initialize the state of this object.
68 * Read and validate from disk all torrent file segments, file manifests, and data packets for
69 * the torrent file managed by this object initializing all state in this manager respectively.
70 * Also seeds all validated data.
71 */
72
73 std::vector<Name> downloadTorrentFile(const std::string& path = "");
74 // Request from the network the all segments of the 'torrentFileName' and write onto local disk at
75 // the specified 'path'.
76
77 void download_file_manifest(const Name& manifestName,
78 const std::string& path,
79 ManifestReceivedCallback onSuccess,
80 FailedCallback onFailed) const;
81 // Request from the network the all segments of the 'manifestName' and write onto local disk at
82 // the specified 'path'.
83
84 void download_data_packet(const Name& packetName,
85 DataReceivedCallback onSuccess,
86 FailedCallback onFailed) const;
87 // Request from the network the Data with the specified 'packetName' and write onto local disk at
88 // 'm_dataPath'.
89
90 void seed(const Data& data) const;
91 // Seed the specified 'data' to the network.
92
93 protected:
Mickey Sweattafda1f12016-04-04 17:15:11 -070094 bool writeData(const Data& packet);
Mickey Sweatt599bfef2016-04-05 19:11:20 -070095 /*
96 * \brief Write @p packet composed of torrent date to disk.
97 * @param packet The data packet to be written to the disk
98 * Write the Data packet to disk, return 'true' if data successfully written to disk 'false'
99 * otherwise. Behavior is undefined unless the corresponding file manifest has already been
100 * downloaded.
101 */
102
103 bool writeTorrentSegment(const TorrentFile& segment, const std::string& path);
104 /*
105 * \brief Write the @p segment torrent segment to disk at the specified path.
106 * @param segment The torrent file segment to be written to disk
107 * @param path The path at which to write the torrent file segment
108 * Write the segment to disk, return 'true' if data successfully written to disk 'false'
109 * otherwise. Behavior is undefined unless @segment is a correct segment for the torrent file of
110 * this manager and @p path is the directory used for all segments of this torrent file.
111 */
112
113 bool writeFileManifest(const FileManifest& manifest, const std::string& path);
114 /*
115 * \brief Write the @p manifest file manifest to disk at the specified @p path.
116 * @param manifest The file manifest to be written to disk
117 * @param path The path at which to write the file manifest
118 * Write the file manifest to disk, return 'true' if data successfully written to disk 'false'
119 * otherwise. Behavior is undefined unless @manifest is a correct file manifest for a file in the
120 * torrent file of this manager and @p path is the directory used for all file manifests of this
121 * torrent file.
122 */
Mickey Sweattafda1f12016-04-04 17:15:11 -0700123
Mickey Sweatt527b0492016-03-02 11:07:48 -0800124 void onDataReceived(const Data& data);
125
126 void onInterestReceived(const Name& name);
127
128 // A map from each fileManifest to corresponding file stream on disk and a bitmap of which Data
129 // packets this manager currently has
130 mutable std::unordered_map<Name,
131 std::pair<std::shared_ptr<fs::fstream>,
132 std::vector<bool>>> m_fileStates;
133 // The segments of the TorrentFile this manager has
134 std::vector<TorrentFile> m_torrentSegments;
135 // The FileManifests this manager has
136 std::vector<FileManifest> m_fileManifests;
137 // The name of the initial segment of the torrent file for this manager
138 Name m_torrentFileName;
139 // The path to the location on disk of the Data packet for this manager
140 std::string m_dataPath;
141};
142
143inline
144TorrentManager::TorrentManager(const ndn::Name& torrentFileName,
145 const std::string& dataPath)
146: m_fileStates()
147, m_torrentSegments()
148, m_fileManifests()
149, m_torrentFileName(torrentFileName)
150, m_dataPath(dataPath)
151{
152}
153
154} // end ntorrent
155} // end ndn
156
157#endif // INCLUDED_TORRENT_FILE_MANAGER_H