DefaultBtContext.h 5.3 KB

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