TorrentMan.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef _D_TORRENT_MAN_H_
  36. #define _D_TORRENT_MAN_H_
  37. #include "Peer.h"
  38. #include "common.h"
  39. #include "Logger.h"
  40. #include "BitfieldMan.h"
  41. #include "DiskWriter.h"
  42. #include "Piece.h"
  43. #include "Dictionary.h"
  44. #include "Option.h"
  45. #include "FileEntry.h"
  46. #include "DiskAdaptor.h"
  47. #include "Request.h"
  48. #include "TimeA2.h"
  49. #include <deque>
  50. #include <map>
  51. #include <string>
  52. #include <algorithm>
  53. using namespace std;
  54. #define INFO_HASH_LENGTH 20
  55. #define PEER_ID_LENGTH 20
  56. #define DEFAULT_ANNOUNCE_INTERVAL 1800
  57. #define DEFAULT_ANNOUNCE_MIN_INTERVAL 1800
  58. #define MAX_PEERS 55
  59. #define MIN_PEERS 15
  60. #define MAX_PEER_LIST_SIZE 100
  61. #define END_GAME_PIECE_NUM 20
  62. #define MAX_PEER_ERROR 5
  63. class TransferStat {
  64. public:
  65. int downloadSpeed;
  66. int uploadSpeed;
  67. long long int sessionDownloadLength;
  68. long long int sessionUploadLength;
  69. public:
  70. TransferStat():downloadSpeed(0), uploadSpeed(0),
  71. sessionDownloadLength(0), sessionUploadLength(0) {}
  72. };
  73. class HaveEntry {
  74. public:
  75. int cuid;
  76. int index;
  77. Time registeredTime;
  78. HaveEntry(int cuid, int index):
  79. cuid(cuid),
  80. index(index) {}
  81. };
  82. typedef deque<PeerHandle> Peers;
  83. typedef deque<HaveEntry> Haves;
  84. typedef deque<int> PieceIndexes;
  85. typedef deque<Piece> Pieces;
  86. class TorrentMan {
  87. private:
  88. Peers peers;
  89. BitfieldMan* bitfield;
  90. unsigned char infoHash[INFO_HASH_LENGTH];
  91. deque<string> pieceHashes;
  92. int peerEntryIdCounter;
  93. int cuidCounter;
  94. long long int totalLength;
  95. long long int downloadLength;
  96. long long int uploadLength;
  97. long long int preDownloadLength;
  98. long long int preUploadLength;
  99. int fileMode;
  100. string storeDir;
  101. int port;
  102. Haves haves;
  103. Pieces usedPieces;
  104. bool setupComplete;
  105. const Logger* logger;
  106. Peers activePeers;
  107. bool halt;
  108. FILE* openSegFile(const string& segFilename, const string& mode) const;
  109. void read(FILE* file);
  110. Piece findUsedPiece(int index) const;
  111. void addUsedPiece(const Piece& piece);
  112. void deleteUsedPiece(const Piece& piece);
  113. int deleteUsedPiecesByFillRate(int fillRate, int toDelete);
  114. void reduceUsedPieces(int max);
  115. void readFileEntry(FileEntries& fileEntries, Directory** pTopDir, const Dictionary* infoDic, const string& defaultName);
  116. void setFileFilter(const Strings& filePaths);
  117. void setupInternal1(const string& metaInfoFile);
  118. void setupInternal2();
  119. Piece checkOutPiece(int index);
  120. public:
  121. int pieceLength;
  122. int pieces;
  123. // TODO type char* would be better
  124. string peerId;
  125. string key;
  126. string announce;
  127. string trackerId;
  128. string name;
  129. int interval;
  130. int minInterval;
  131. int complete;
  132. int incomplete;
  133. int connections;
  134. // The number of tracker request command currently in the command queue.
  135. int trackers;
  136. // tracker request
  137. Request* req;
  138. public:
  139. TorrentMan();
  140. ~TorrentMan();
  141. DiskAdaptor* diskAdaptor;
  142. const Option* option;
  143. int getNewCuid() { return ++cuidCounter; }
  144. // TODO do not use this method
  145. void updatePeers(const Peers& peers);
  146. bool addPeer(const PeerHandle& peer);
  147. //void updatePeer(const Peer* peer);
  148. const Peers& getPeers() const { return peers; }
  149. PeerHandle getPeer() const;
  150. bool isPeerAvailable() const;
  151. void deleteUnusedPeer(int delSize);
  152. bool hasMissingPiece(const PeerHandle& peer) const;
  153. int getMissingPieceIndex(const PeerHandle& peer) const;
  154. int getMissingFastPieceIndex(const PeerHandle& peer) const;
  155. Piece getMissingPiece(const PeerHandle& peer);
  156. Piece getMissingFastPiece(const PeerHandle& peer);
  157. void completePiece(const Piece& piece);
  158. void cancelPiece(const Piece& piece);
  159. void updatePiece(const Piece& piece);
  160. void syncPiece(Piece& piece);
  161. bool hasPiece(int index) const;
  162. void initBitfield();
  163. /**
  164. * Returns true if the number of missing block is less than or equal to
  165. * END_GAME_PIECE_NUM.
  166. * If file filter is enabled, only a range specified by the filter is
  167. * concerned.
  168. */
  169. bool isEndGame() const;
  170. /**
  171. * Returns true if download has completed. If file filter is enabled,
  172. * returns true if download of a range specified by the filter has completed.
  173. */
  174. bool downloadComplete() const;
  175. bool hasAllPieces() const;
  176. void setBitfield(unsigned char* bitfield, int len);
  177. const unsigned char* getBitfield() const {
  178. return bitfield->getBitfield();
  179. }
  180. int getBitfieldLength() const { return bitfield->getBitfieldLength(); }
  181. int getPieceLength(int index) const {
  182. return bitfield->getBlockLength(index);
  183. }
  184. int getPieceLength() const { return bitfield->getBlockLength(); }
  185. void setInfoHash(const unsigned char* infoHash) {
  186. memcpy(this->infoHash, infoHash, INFO_HASH_LENGTH);
  187. }
  188. const unsigned char* getInfoHash() const {
  189. return infoHash;
  190. }
  191. void setup(const string& metaInfoFile, const Strings& targetFilePaths);
  192. void setup(const string& metaInfoFile, const Integers& targetFileIndexes);
  193. string getPieceHash(int index) const;
  194. // Adds piece index to advertise to other commands. They send have message
  195. // based on this information.
  196. void advertisePiece(int cuid, int index);
  197. // Returns piece index which is not advertised by the caller command and
  198. // newer than lastCheckTime.
  199. PieceIndexes getAdvertisedPieceIndexes(int myCuid, const Time& lastCheckTime) const;
  200. // Removes have entry if specified seconds have elapsed since its registration.
  201. void removeAdvertisedPiece(int elapsed);
  202. long long int getTotalLength() const { return totalLength; }
  203. void setTotalLength(long long int length) { totalLength = length; }
  204. void addDownloadLength(int deltaLength) { downloadLength += deltaLength; }
  205. long long int getDownloadLength() const { return downloadLength; }
  206. void setDownloadLength(long long int length) { downloadLength = length; }
  207. void addUploadLength(int deltaLength) { uploadLength += deltaLength; }
  208. long long int getUploadLength() const { return uploadLength; }
  209. void setUploadLength(long long int length) { uploadLength = length; }
  210. long long int getSessionDownloadLength() const {
  211. return downloadLength-preDownloadLength;
  212. }
  213. long long int getSessionUploadLength() const {
  214. return uploadLength-preUploadLength;
  215. }
  216. void setFileMode(int mode) {
  217. fileMode = mode;
  218. }
  219. int getFileMode() const {
  220. return fileMode;
  221. }
  222. string getStoreDir() const { return storeDir; }
  223. void setStoreDir(const string& dir) { storeDir = dir; }
  224. string getSegmentFilePath() const;
  225. bool segmentFileExists() const;
  226. void load();
  227. void save() const;
  228. void remove() const;
  229. void copySingleFile() const;
  230. void splitMultiFile();
  231. void fixFilename();
  232. void deleteTempFile() const;
  233. void setPort(int port) { this->port = port; }
  234. int getPort() const { return port; }
  235. int countUsedPiece() const { return usedPieces.size(); }
  236. int countAdvertisedPiece() const { return haves.size(); }
  237. FileEntries readFileEntryFromMetaInfoFile(const string& metaInfoFile);
  238. string getName() const;
  239. void finishSelectiveDownloadingMode();
  240. bool isSelectiveDownloadingMode() const;
  241. long long int getCompletedLength() const;
  242. long long int getSelectedTotalLength() const;
  243. void onDownloadComplete();
  244. void addActivePeer(const PeerHandle& peer) {
  245. peer->activate();
  246. activePeers.push_back(peer);
  247. }
  248. Peers& getActivePeers() { return this->activePeers; }
  249. void deleteActivePeer(const PeerHandle& peer) {
  250. Peers::iterator itr = find(activePeers.begin(), activePeers.end(), peer);
  251. if(itr != activePeers.end()) {
  252. peer->deactivate();
  253. activePeers.erase(itr);
  254. }
  255. }
  256. bool isHalt() const { return halt; }
  257. void setHalt(bool halt) {
  258. this->halt = halt;
  259. }
  260. enum FILE_MODE {
  261. SINGLE,
  262. MULTI
  263. };
  264. TransferStat calculateStat();
  265. };
  266. #endif // _D_TORRENT_MAN_H_