Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 1 | /* -*- 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 Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 24 | #include "file-manifest.hpp" |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 25 | #include "torrent-file.hpp" |
spirosmastorakis | 4ff8c87 | 2016-04-14 09:51:38 -0700 | [diff] [blame] | 26 | #include "update-handler.hpp" |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 27 | |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 28 | #include <ndn-cxx/data.hpp> |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 29 | #include <ndn-cxx/face.hpp> |
| 30 | #include <ndn-cxx/interest.hpp> |
| 31 | #include <ndn-cxx/link.hpp> |
| 32 | #include <ndn-cxx/name.hpp> |
| 33 | #include <ndn-cxx/security/key-chain.hpp> |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 34 | |
| 35 | #include <boost/filesystem/fstream.hpp> |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 36 | |
| 37 | #include <functional> |
| 38 | #include <memory> |
| 39 | #include <string> |
| 40 | #include <unordered_map> |
| 41 | #include <vector> |
| 42 | |
| 43 | namespace fs = boost::filesystem; |
| 44 | |
| 45 | namespace ndn { |
| 46 | namespace ntorrent { |
| 47 | |
Mickey Sweatt | 599bfef | 2016-04-05 19:11:20 -0700 | [diff] [blame] | 48 | class TorrentManager : noncopyable { |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 49 | /** |
| 50 | * \class TorrentManager |
| 51 | * |
| 52 | * \brief A class to manage the interaction with the system in seeding/leaching a torrent |
| 53 | */ |
| 54 | public: |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 55 | typedef std::function<void(const ndn::Name&)> DataReceivedCallback; |
| 56 | typedef std::function<void(const std::vector<ndn::Name>&)> ManifestReceivedCallback; |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 57 | typedef std::function<void(const std::vector<ndn::Name>&)> TorrentFileReceivedCallback; |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 58 | typedef std::function<void(const ndn::Name&, const std::string&)> FailedCallback; |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 59 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 60 | /* |
| 61 | * \brief Create a new Torrent manager with the specified parameters. |
| 62 | * @param torrentFileName The full name of the initial segment of the torrent file |
| 63 | * @param dataPath The path to the location on disk to use for the torrent data |
| 64 | * @param face Optional face object to be used for data retrieval |
| 65 | * |
| 66 | * The behavior is undefined unless Initialize() is called before calling any other method on a |
| 67 | * TorrentManger object. |
| 68 | */ |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 69 | TorrentManager(const ndn::Name& torrentFileName, |
| 70 | const std::string& dataPath, |
| 71 | std::shared_ptr<Face> face = nullptr); |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 72 | |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 73 | /* |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 74 | * @brief Initialize the state of this object. |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 75 | * |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 76 | * Read and validate from disk all torrent file segments, file manifests, and data packets for |
| 77 | * the torrent file managed by this object initializing all state in this manager respectively. |
| 78 | * Also seeds all validated data. |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 79 | */ |
| 80 | void |
| 81 | Initialize(); |
| 82 | |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 83 | /** |
| 84 | * brief Return 'true' if all segments of the torrent file downloaded, 'false' otherwise. |
| 85 | */ |
| 86 | bool |
| 87 | hasAllTorrentSegments() const; |
| 88 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 89 | /* |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 90 | * \brief Given a data packet name, find whether we have already downloaded this packet |
| 91 | * @param dataName The name of the data packet to download |
| 92 | * @return True if we have already downloaded this packet, false otherwise |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 93 | * |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 94 | */ |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 95 | bool |
| 96 | hasDataPacket(const Name& dataName) const; |
| 97 | |
| 98 | /* |
| 99 | * \brief Find the torrent file segment that we should download |
| 100 | * (either we have nothing or we have them all) |
| 101 | * @return A shared_ptr to the name of the segment to download or |
| 102 | * nullptr if we have all the segments |
| 103 | * |
| 104 | */ |
| 105 | shared_ptr<Name> |
| 106 | findTorrentFileSegmentToDownload() const; |
| 107 | |
| 108 | /* |
| 109 | * \brief Given a file manifest segment name, find the next file manifest segment |
| 110 | * that we should download |
| 111 | * @param manifestName The name of the file manifest segment that we want to download |
| 112 | * @return A shared_ptr to the name of the segment to download or |
| 113 | * nullptr if we have all the segments |
| 114 | * |
| 115 | */ |
| 116 | shared_ptr<Name> |
| 117 | findManifestSegmentToDownload(const Name& manifestName) const; |
| 118 | |
| 119 | /* |
| 120 | * \brief Find the segments of all the file manifests that we are missing |
| 121 | * @param manifestNames The name of the manifest file segments to download (currently missing) |
| 122 | * This parameter is used as an output vector of names |
| 123 | */ |
| 124 | void |
| 125 | findFileManifestsToDownload(std::vector<Name>& manifestNames) const; |
| 126 | |
| 127 | /* |
| 128 | * \brief Find the names of the data packets of a file manifest that we are currently missing |
| 129 | * @param manifestName The name of the manifest |
| 130 | * @param packetNames The name of the data packets to be downloaded |
| 131 | * (used as an output vector of names) |
| 132 | * |
| 133 | * No matter what segment of a manifest file the manifestName parameter might refer to, the |
| 134 | * missing data packets starting from the first segment of this manifest file would be returned |
| 135 | * |
| 136 | */ |
| 137 | void |
| 138 | findDataPacketsToDownload(const Name& manifestName, std::vector<Name>& packetNames) const; |
| 139 | |
| 140 | /* |
| 141 | * \brief Find all the data packets that we are currently missing |
| 142 | * @param packetNames The name of the data packets to be downloaded |
| 143 | * (used as an output vector of names) |
| 144 | * |
| 145 | */ |
| 146 | void |
| 147 | findAllMissingDataPackets(std::vector<Name>& packetNames) const; |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 148 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 149 | /* |
| 150 | * @brief Download the torrent file |
| 151 | * @param path The path to write the downloaded segments |
| 152 | * @param onSuccess Callback to be called if we successfully download all the |
| 153 | * segments of the torrent file. It passes the name of the file manifest |
| 154 | * to be downloaded to the callback |
| 155 | * @param onFailed Callaback to be called if we fail to download a segment of |
| 156 | * the torrent file. It passes the name of the torrent file segment that |
| 157 | * failed to download and a failure reason to the callback |
| 158 | * |
| 159 | * This method provides non-blocking downloading of all the torrent file segments |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 160 | */ |
| 161 | void |
| 162 | downloadTorrentFile(const std::string& path, |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 163 | TorrentFileReceivedCallback onSuccess = {}, |
| 164 | FailedCallback onFailed = {}); |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 165 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 166 | /* |
| 167 | * @brief Download a file manifest |
| 168 | * @param manifestName The name of the manifest file to be downloaded |
| 169 | * @param path The path to write the downloaded segments |
| 170 | * @param onSuccess Callback to be called if we successfully download all the |
| 171 | * segments of the file manifest. It passes the names of the data packets |
| 172 | * to be downloaded to the callback |
| 173 | * @param onFailed Callaback to be called if we fail to download a segment of |
| 174 | * the file manifest. It passes the name of the data packet that failed |
| 175 | * to download and a failure reason |
| 176 | * |
| 177 | * This method provides non-blocking downloading of all the file manifest segments |
| 178 | * |
| 179 | */ |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 180 | void |
| 181 | download_file_manifest(const Name& manifestName, |
| 182 | const std::string& path, |
| 183 | ManifestReceivedCallback onSuccess, |
| 184 | FailedCallback onFailed); |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 185 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 186 | /* |
| 187 | * @brief Download a data packet |
| 188 | * @param packetName The name of the data packet to be downloaded |
| 189 | * @param onSuccess Callback to be called if we successfully download the data packet. |
| 190 | * It passes the name of the data packet to the callback |
| 191 | * @param onFailed Callaback to be called if we fail to download the requested data packet |
| 192 | * It passes the name of the data packet to the callback and a failure reason |
| 193 | * |
| 194 | * This method writes the downloaded data packet to m_dataPath on disk |
| 195 | * |
| 196 | */ |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 197 | void |
| 198 | download_data_packet(const Name& packetName, |
| 199 | DataReceivedCallback onSuccess, |
| 200 | FailedCallback onFailed); |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 201 | |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 202 | // Seed the specified 'data' to the network. |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 203 | void |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 204 | seed(const Data& data); |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 205 | |
Mickey Sweatt | b7ee19c | 2016-04-21 12:18:15 -0700 | [diff] [blame] | 206 | /** |
| 207 | * @brief Process any data to receive or call timeout callbacks and update prefix list (if needed) |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 208 | * By default this blocks until all operations are complete. |
Mickey Sweatt | b7ee19c | 2016-04-21 12:18:15 -0700 | [diff] [blame] | 209 | */ |
| 210 | void |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 211 | processEvents(const time::milliseconds& timeout = time::milliseconds(0)); |
Mickey Sweatt | b7ee19c | 2016-04-21 12:18:15 -0700 | [diff] [blame] | 212 | |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 213 | protected: |
Mickey Sweatt | b7ee19c | 2016-04-21 12:18:15 -0700 | [diff] [blame] | 214 | /** |
Mickey Sweatt | 599bfef | 2016-04-05 19:11:20 -0700 | [diff] [blame] | 215 | * \brief Write @p packet composed of torrent date to disk. |
| 216 | * @param packet The data packet to be written to the disk |
| 217 | * Write the Data packet to disk, return 'true' if data successfully written to disk 'false' |
| 218 | * otherwise. Behavior is undefined unless the corresponding file manifest has already been |
| 219 | * downloaded. |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 220 | */ |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 221 | bool |
| 222 | writeData(const Data& packet); |
Mickey Sweatt | 599bfef | 2016-04-05 19:11:20 -0700 | [diff] [blame] | 223 | |
Mickey Sweatt | 599bfef | 2016-04-05 19:11:20 -0700 | [diff] [blame] | 224 | /* |
| 225 | * \brief Write the @p segment torrent segment to disk at the specified path. |
| 226 | * @param segment The torrent file segment to be written to disk |
| 227 | * @param path The path at which to write the torrent file segment |
| 228 | * Write the segment to disk, return 'true' if data successfully written to disk 'false' |
| 229 | * otherwise. Behavior is undefined unless @segment is a correct segment for the torrent file of |
| 230 | * this manager and @p path is the directory used for all segments of this torrent file. |
| 231 | */ |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 232 | bool |
| 233 | writeTorrentSegment(const TorrentFile& segment, const std::string& path); |
Mickey Sweatt | 599bfef | 2016-04-05 19:11:20 -0700 | [diff] [blame] | 234 | |
Mickey Sweatt | 599bfef | 2016-04-05 19:11:20 -0700 | [diff] [blame] | 235 | /* |
| 236 | * \brief Write the @p manifest file manifest to disk at the specified @p path. |
| 237 | * @param manifest The file manifest to be written to disk |
| 238 | * @param path The path at which to write the file manifest |
| 239 | * Write the file manifest to disk, return 'true' if data successfully written to disk 'false' |
| 240 | * otherwise. Behavior is undefined unless @manifest is a correct file manifest for a file in the |
| 241 | * torrent file of this manager and @p path is the directory used for all file manifests of this |
| 242 | * torrent file. |
| 243 | */ |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 244 | bool |
| 245 | writeFileManifest(const FileManifest& manifest, const std::string& path); |
Mickey Sweatt | afda1f1 | 2016-04-04 17:15:11 -0700 | [diff] [blame] | 246 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 247 | /* |
| 248 | * \brief Download the segments of the torrent file |
| 249 | * @param name The name of the torrent file to be downloaded |
| 250 | * @param path The path to write the torrent file on disk |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 251 | * @param onSuccess Optional callback to be called when all the segments of the torrent file |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 252 | * have been downloaded. The default value is an empty callback. |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 253 | * @param onFailed Optional callback to be called when we fail to download a segment of the |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 254 | * torrent file. The default value is an empty callback. |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 255 | * |
| 256 | */ |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 257 | void |
| 258 | downloadTorrentFileSegment(const ndn::Name& name, |
| 259 | const std::string& path, |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 260 | TorrentFileReceivedCallback onSuccess, |
| 261 | FailedCallback onFailed); |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 262 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 263 | /* |
| 264 | * \brief Download the segments of a file manifest |
| 265 | * @param manifestName The name of the file manifest to be downloaded |
| 266 | * @param path The path to write the file manifest on disk |
| 267 | * @param packetNames A vector containing the name of the data packets in the file manifest |
| 268 | * @param onSuccess Callback to be called when all the segments of the file manifest have been |
| 269 | * downloaded |
| 270 | * @param onFailed Callback to be called when we fail to download a file manifest segment |
| 271 | * |
| 272 | */ |
spirosmastorakis | 50642f8 | 2016-04-08 12:11:18 -0700 | [diff] [blame] | 273 | void |
| 274 | downloadFileManifestSegment(const Name& manifestName, |
| 275 | const std::string& path, |
| 276 | std::shared_ptr<std::vector<Name>> packetNames, |
| 277 | ManifestReceivedCallback onSuccess, |
| 278 | FailedCallback onFailed); |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 279 | |
| 280 | enum { |
| 281 | // Number of times to retry if a routable prefix fails to retrieve data |
| 282 | MAX_NUM_OF_RETRIES = 5, |
| 283 | // Number of Interests to be sent before sorting the stats table |
| 284 | SORTING_INTERVAL = 100 |
| 285 | }; |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 286 | |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 287 | void onDataReceived(const Data& data); |
| 288 | |
| 289 | void |
| 290 | onInterestReceived(const InterestFilter& filter, const Interest& interest); |
| 291 | |
| 292 | void |
| 293 | onRegisterFailed(const Name& prefix, const std::string& reason); |
| 294 | |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 295 | protected: |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 296 | // A map from each fileManifest to corresponding file stream on disk and a bitmap of which Data |
| 297 | // packets this manager currently has |
| 298 | mutable std::unordered_map<Name, |
| 299 | std::pair<std::shared_ptr<fs::fstream>, |
| 300 | std::vector<bool>>> m_fileStates; |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 301 | // A map for each initial manifest to the size for the sub-manifest |
| 302 | std::unordered_map<std::string, size_t> m_subManifestSizes; |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 303 | // The segments of the TorrentFile this manager has |
| 304 | std::vector<TorrentFile> m_torrentSegments; |
| 305 | // The FileManifests this manager has |
| 306 | std::vector<FileManifest> m_fileManifests; |
| 307 | // The name of the initial segment of the torrent file for this manager |
| 308 | Name m_torrentFileName; |
| 309 | // The path to the location on disk of the Data packet for this manager |
| 310 | std::string m_dataPath; |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 311 | |
| 312 | private: |
| 313 | shared_ptr<Interest> |
| 314 | createInterest(Name name); |
| 315 | |
| 316 | // Stats table where routable prefixes are stored |
| 317 | StatsTable m_statsTable; |
| 318 | // Face used for network communication |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 319 | std::shared_ptr<Face> m_face; |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 320 | // Iterator to the routable prefix that we currently use |
| 321 | StatsTable::iterator m_stats_table_iter; |
| 322 | // Number of retries per routable prefix |
| 323 | uint64_t m_retries; |
| 324 | // Number of Interests sent since last sorting |
| 325 | uint64_t m_sortingCounter; |
| 326 | // Keychain instance |
spirosmastorakis | 4ff8c87 | 2016-04-14 09:51:38 -0700 | [diff] [blame] | 327 | shared_ptr<KeyChain> m_keyChain; |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 328 | // TODO(spyros) Fix and reintegrate update handler |
| 329 | // // Update Handler instance |
| 330 | // shared_ptr<UpdateHandler> m_updateHandler; |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | inline |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 334 | TorrentManager::TorrentManager(const ndn::Name& torrentFileName, |
| 335 | const std::string& dataPath, |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 336 | std::shared_ptr<Face> face) |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 337 | : m_fileStates() |
| 338 | , m_torrentSegments() |
| 339 | , m_fileManifests() |
| 340 | , m_torrentFileName(torrentFileName) |
| 341 | , m_dataPath(dataPath) |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 342 | , m_face(face) |
| 343 | , m_retries(0) |
| 344 | , m_sortingCounter(0) |
| 345 | , m_keyChain(new KeyChain()) |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 346 | { |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 347 | if(face == nullptr) { |
spirosmastorakis | 4ff8c87 | 2016-04-14 09:51:38 -0700 | [diff] [blame] | 348 | m_face = make_shared<Face>(); |
Mickey Sweatt | e908a5c | 2016-04-08 14:10:45 -0700 | [diff] [blame] | 349 | } |
spirosmastorakis | 4ff8c87 | 2016-04-14 09:51:38 -0700 | [diff] [blame] | 350 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 351 | // Hardcoded prefixes for now |
| 352 | // TODO(Spyros): Think of something more clever to bootstrap... |
| 353 | m_statsTable.insert("/ucla"); |
| 354 | m_statsTable.insert("/arizona"); |
| 355 | m_stats_table_iter = m_statsTable.begin(); |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Mickey Sweatt | b7ee19c | 2016-04-21 12:18:15 -0700 | [diff] [blame] | 358 | inline |
| 359 | void |
| 360 | TorrentManager::processEvents(const time::milliseconds& timeout) |
| 361 | { |
| 362 | m_face->processEvents(timeout); |
| 363 | } |
| 364 | |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame^] | 365 | inline |
| 366 | bool |
| 367 | TorrentManager::hasAllTorrentSegments() const |
| 368 | { |
| 369 | return findTorrentFileSegmentToDownload() == nullptr; |
| 370 | } |
| 371 | |
Mickey Sweatt | 527b049 | 2016-03-02 11:07:48 -0800 | [diff] [blame] | 372 | } // end ntorrent |
| 373 | } // end ndn |
| 374 | |
spirosmastorakis | a46eee4 | 2016-04-05 14:24:45 -0700 | [diff] [blame] | 375 | #endif // INCLUDED_TORRENT_FILE_MANAGER_H |