TorrentMan.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #ifndef _D_TORRENT_MAN_H_
  23. #define _D_TORRENT_MAN_H_
  24. #include "Peer.h"
  25. #include "common.h"
  26. #include "Logger.h"
  27. #include "BitfieldMan.h"
  28. #include "DiskWriter.h"
  29. #include "Piece.h"
  30. #include "Directory.h"
  31. #include <deque>
  32. #include <map>
  33. #include <string>
  34. using namespace std;
  35. #define DEFAULT_BLOCK_LEN 16*1024;
  36. #define MAX_BLOCK_LEN 128*1024;
  37. #define INFO_HASH_LENGTH 20
  38. #define IS_NULL_PIECE(X) (X.index == 0 && X.length == 0)
  39. #define DEFAULT_ANNOUNCE_INTERVAL 300
  40. #define DEFAULT_ANNOUNCE_MIN_INTERVAL 300
  41. #define MAX_PEERS 55
  42. #define MAX_PEER_UPDATE 15
  43. #define MAX_PEER_LIST_SIZE 250
  44. #define END_GAME_PIECE_NUM 20
  45. #define MAX_PEER_ERROR 5
  46. class FileEntry {
  47. public:
  48. string path;
  49. long long int length;
  50. FileEntry(string path, long long int length):path(path), length(length) {}
  51. ~FileEntry() {}
  52. };
  53. typedef deque<Peer*> Peers;
  54. typedef multimap<int, int> Haves;
  55. typedef deque<FileEntry> MultiFileEntries;
  56. typedef deque<Piece> UsedPieces;
  57. typedef deque<int> PieceIndexes;
  58. class TorrentMan {
  59. private:
  60. Peers peers;
  61. BitfieldMan* bitfield;
  62. unsigned char infoHash[INFO_HASH_LENGTH];
  63. deque<string> pieceHashes;
  64. int peerEntryIdCounter;
  65. int cuidCounter;
  66. long long int downloadLength;
  67. long long int uploadLength;
  68. long long int preDownloadLength;
  69. long long int preUploadLength;
  70. int deltaDownload;
  71. int deltaUpload;
  72. int fileMode;
  73. string storeDir;
  74. int port;
  75. Haves haves;
  76. UsedPieces usedPieces;
  77. Directory* multiFileTopDir;
  78. MultiFileEntries multiFileEntries;
  79. bool setupComplete;
  80. FILE* openSegFile(string segFilename, string mode) const;
  81. void read(FILE* file);
  82. Piece findUsedPiece(int index) const;
  83. void addUsedPiece(const Piece& piece);
  84. void deleteUsedPiece(const Piece& piece);
  85. int deleteUsedPiecesByFillRate(int fillRate, int toDelete);
  86. void reduceUsedPieces(int max);
  87. public:
  88. int pieceLength;
  89. int pieces;
  90. long long int totalSize;
  91. string peerId;
  92. string announce;
  93. string trackerId;
  94. string name;
  95. int interval;
  96. int minInterval;
  97. int complete;
  98. int incomplete;
  99. int connections;
  100. public:
  101. TorrentMan();
  102. ~TorrentMan();
  103. const Logger* logger;
  104. DiskWriter* diskWriter;
  105. int getNewCuid() { return ++cuidCounter; }
  106. // TODO do not use this method
  107. void updatePeers(const Peers& peers);
  108. bool addPeer(Peer* peer, bool duplicate = false);
  109. //void updatePeer(const Peer* peer);
  110. const Peers& getPeers() const { return peers; }
  111. Peer* getPeer() const;
  112. bool isPeerAvailable() const;
  113. int deleteOldErrorPeers(int maxNum);
  114. Piece getMissingPiece(const Peer* peer);
  115. void completePiece(const Piece& piece);
  116. void cancelPiece(const Piece& piece);
  117. void updatePiece(const Piece& piece);
  118. void syncPiece(Piece& piece);
  119. bool hasPiece(int index) const;
  120. void initBitfield();
  121. bool isEndGame() const;
  122. bool downloadComplete() const;
  123. void setBitfield(unsigned char* bitfield, int len);
  124. const unsigned char* getBitfield() const {
  125. return bitfield->getBitfield();
  126. }
  127. int getBitfieldLength() const { return bitfield->getBitfieldLength(); }
  128. void setInfoHash(const unsigned char* infoHash) {
  129. memcpy(this->infoHash, infoHash, INFO_HASH_LENGTH);
  130. }
  131. const unsigned char* getInfoHash() const {
  132. return infoHash;
  133. }
  134. void setup(string metaInfoFile);
  135. string getPieceHash(int index) const;
  136. void advertisePiece(int cuid, int index) {
  137. Haves::value_type vt(cuid, index);
  138. haves.insert(vt);
  139. }
  140. PieceIndexes getAdvertisedPieceIndexes(int myCuid) const {
  141. PieceIndexes indexes;
  142. for(Haves::const_iterator itr = haves.begin(); itr != haves.end(); itr++) {
  143. const Haves::value_type& have = *itr;
  144. if(have.first == myCuid) {
  145. continue;
  146. }
  147. indexes.push_back(have.second);
  148. }
  149. return indexes;
  150. }
  151. void unadvertisePiece(int cuid) {
  152. haves.erase(cuid);
  153. }
  154. void addDeltaDownload(int size) { deltaDownload += size; }
  155. int getDeltaDownload() const { return deltaDownload; }
  156. void resetDeltaDownload() { deltaDownload = 0; }
  157. void addDeltaUpload(int size) { deltaUpload += size; }
  158. int getDeltaUpload() const { return deltaUpload; }
  159. void resetDeltaUpload() { deltaUpload = 0; }
  160. void addDownloadLength(int deltaLength) { downloadLength += deltaLength; }
  161. long long int getDownloadLength() const { return downloadLength; }
  162. void setDownloadLength(long long int length) { downloadLength = length; }
  163. void addUploadLength(int deltaLength) { uploadLength += deltaLength; }
  164. long long int getUploadLength() const { return uploadLength; }
  165. void setUploadLength(long long int length) { uploadLength = length; }
  166. long long int getSessionDownloadedSize() const {
  167. return downloadLength-preDownloadLength;
  168. }
  169. long long int getSessionUploadedSize() const {
  170. return uploadLength-preUploadLength;
  171. }
  172. void setFileMode(int mode) {
  173. fileMode = mode;
  174. }
  175. int getFileMode() const {
  176. return fileMode;
  177. }
  178. string getStoreDir() const { return storeDir; }
  179. void setStoreDir(string dir) { storeDir = dir; }
  180. string getFilePath() const;
  181. string getTempFilePath() const;
  182. string getSegmentFilePath() const;
  183. bool segmentFileExists() const;
  184. void load();
  185. void save() const;
  186. void remove() const;
  187. void copySingleFile() const;
  188. void splitMultiFile() const;
  189. void fixFilename() const;
  190. void deleteTempFile() const;
  191. void setPort(int port) { this->port = port; }
  192. int getPort() const { return port; }
  193. int countUsedPiece() const { return usedPieces.size(); }
  194. int countAdvertisedPiece() const { return haves.size(); }
  195. enum FILE_MODE {
  196. SINGLE,
  197. MULTI
  198. };
  199. };
  200. #endif // _D_TORRENT_MAN_H_