DefaultBtContext.h 5.6 KB

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