DefaultBtContext.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_DEFAULT_BT_CONTEXT_H_
  36. #define _D_DEFAULT_BT_CONTEXT_H_
  37. #include "BtContext.h"
  38. #include <iosfwd>
  39. namespace aria2 {
  40. class Randomizer;
  41. class Logger;
  42. class MetaEntry;
  43. class Dictionary;
  44. class List;
  45. class Data;
  46. #define INFO_HASH_LENGTH 20
  47. #define PIECE_HASH_LENGTH 20
  48. class DefaultBtContext : public BtContext {
  49. private:
  50. unsigned char infoHash[INFO_HASH_LENGTH];
  51. std::string infoHashString;
  52. std::deque<std::string> pieceHashes;
  53. std::deque<SharedHandle<FileEntry> > fileEntries;
  54. FILE_MODE fileMode;
  55. int64_t totalLength;
  56. int32_t pieceLength;
  57. std::string name;
  58. int32_t numPieces;
  59. std::string peerId;
  60. std::string _peerIdPrefix;
  61. std::deque<SharedHandle<AnnounceTier> > announceTiers;
  62. std::deque<std::pair<std::string, uint16_t> > _nodes;
  63. SharedHandle<Randomizer> _randomizer;
  64. RequestGroup* _ownerRequestGroup;
  65. const Logger* _logger;
  66. void clear();
  67. void extractPieceHash(const unsigned char* hashData,
  68. int32_t hashDataLength,
  69. int32_t hashLength);
  70. void extractFileEntries(const Dictionary* infoDic,
  71. const std::string& defaultName,
  72. const std::deque<std::string>& urlList);
  73. void extractAnnounce(const Data* announceData);
  74. void extractAnnounceList(const List* announceListData);
  75. std::deque<std::string> extractUrlList(const MetaEntry* obj);
  76. void extractNodes(const List* nodes);
  77. void processRootDictionary(const Dictionary* rootDic, const std::string& defaultName);
  78. public:
  79. DefaultBtContext();
  80. virtual ~DefaultBtContext();
  81. virtual const unsigned char* getInfoHash() const;
  82. virtual int32_t getInfoHashLength() const;
  83. virtual std::string getInfoHashAsString() const;
  84. virtual std::string getPieceHash(int32_t index) const;
  85. virtual const std::deque<std::string>& getPieceHashes() const
  86. {
  87. return pieceHashes;
  88. }
  89. virtual int64_t getTotalLength() const;
  90. virtual FILE_MODE getFileMode() const;
  91. virtual std::deque<SharedHandle<FileEntry> > getFileEntries() const;
  92. virtual std::string getPieceHashAlgo() const
  93. {
  94. return "sha1";
  95. }
  96. virtual std::deque<SharedHandle<AnnounceTier> > getAnnounceTiers() const;
  97. virtual void load(const std::string& torrentFile);
  98. void loadFromMemory(const char* content, int32_t length, const std::string& defaultName);
  99. virtual std::string getName() const;
  100. virtual int32_t getPieceLength() const;
  101. virtual int32_t getNumPieces() const;
  102. virtual std::string getActualBasePath() const;
  103. virtual const unsigned char* getPeerId() {
  104. if(peerId == "") {
  105. peerId = generatePeerId();
  106. }
  107. return (const unsigned char*)peerId.c_str();
  108. }
  109. virtual std::deque<int32_t> computeFastSet(const std::string& ipaddr, int32_t fastSetSize);
  110. virtual RequestGroup* getOwnerRequestGroup()
  111. {
  112. return _ownerRequestGroup;
  113. }
  114. virtual std::deque<std::pair<std::string, uint16_t> >& getNodes();
  115. std::string generatePeerId() const;
  116. void setPeerIdPrefix(const std::string& peerIdPrefix)
  117. {
  118. _peerIdPrefix = peerIdPrefix;
  119. }
  120. // for unit test
  121. void setInfoHash(const unsigned char* infoHash)
  122. {
  123. memcpy(this->infoHash, infoHash, sizeof(this->infoHash));
  124. }
  125. void setNumPieces(int32_t numPieces)
  126. {
  127. this->numPieces = numPieces;
  128. }
  129. void setOwnerRequestGroup(RequestGroup* owner)
  130. {
  131. _ownerRequestGroup = owner;
  132. }
  133. void setRandomizer(const SharedHandle<Randomizer>& randomizer);
  134. friend std::ostream& operator<<(std::ostream& o, const DefaultBtContext& ctx);
  135. };
  136. typedef SharedHandle<DefaultBtContext> DefaultBtContextHandle;
  137. } // namespace aria2
  138. #endif // _D_DEFAULT_BT_CONTEXT_H_