DefaultBtContext.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 const std::string& getInfoHashAsString() const;
  85. virtual const 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 const std::string& getPieceHashAlgo() const;
  94. virtual std::deque<SharedHandle<AnnounceTier> > getAnnounceTiers() const;
  95. virtual void load(const std::string& torrentFile);
  96. void loadFromMemory(const unsigned char* content, size_t length,
  97. const std::string& defaultName);
  98. void loadFromMemory(const std::string& context, const std::string& defaultName)
  99. {
  100. loadFromMemory(reinterpret_cast<const unsigned char*>(context.c_str()),
  101. context.size(), defaultName);
  102. }
  103. virtual const std::string& getName() const;
  104. virtual size_t getPieceLength() const;
  105. virtual size_t getNumPieces() const;
  106. virtual std::string getActualBasePath() const;
  107. virtual const unsigned char* getPeerId() {
  108. if(peerId.empty()) {
  109. peerId = generatePeerId();
  110. }
  111. return reinterpret_cast<const unsigned char*>(peerId.c_str());
  112. }
  113. virtual void computeFastSet
  114. (std::deque<size_t>& fastSet, const std::string& ipaddr, size_t fastSetSize);
  115. virtual RequestGroup* getOwnerRequestGroup()
  116. {
  117. return _ownerRequestGroup;
  118. }
  119. virtual std::deque<std::pair<std::string, uint16_t> >& getNodes();
  120. std::string generatePeerId() const;
  121. void setPeerIdPrefix(const std::string& peerIdPrefix)
  122. {
  123. _peerIdPrefix = peerIdPrefix;
  124. }
  125. // for unit test
  126. void setInfoHash(const unsigned char* infoHash);
  127. void setNumPieces(size_t numPieces)
  128. {
  129. this->numPieces = numPieces;
  130. }
  131. void setOwnerRequestGroup(RequestGroup* owner)
  132. {
  133. _ownerRequestGroup = owner;
  134. }
  135. void setRandomizer(const SharedHandle<Randomizer>& randomizer);
  136. friend std::ostream& operator<<(std::ostream& o, const DefaultBtContext& ctx);
  137. static const std::string DEFAULT_PEER_ID_PREFIX;
  138. };
  139. typedef SharedHandle<DefaultBtContext> DefaultBtContextHandle;
  140. } // namespace aria2
  141. #endif // _D_DEFAULT_BT_CONTEXT_H_