DefaultBtContext.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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::string& overrideName,
  74. const std::deque<std::string>& urlList);
  75. void extractAnnounce(const Data* announceData);
  76. void extractAnnounceList(const List* announceListData);
  77. void extractUrlList(std::deque<std::string>& uris, const MetaEntry* obj);
  78. void extractNodes(const List* nodes);
  79. void processRootDictionary(const Dictionary* rootDic,
  80. const std::string& defaultName,
  81. const std::string& overrideName);
  82. public:
  83. DefaultBtContext();
  84. virtual ~DefaultBtContext();
  85. virtual const unsigned char* getInfoHash() const;
  86. virtual size_t getInfoHashLength() const;
  87. virtual const std::string& getInfoHashAsString() const;
  88. virtual const std::string& getPieceHash(size_t index) const;
  89. virtual const std::deque<std::string>& getPieceHashes() const
  90. {
  91. return pieceHashes;
  92. }
  93. virtual uint64_t getTotalLength() const;
  94. virtual FILE_MODE getFileMode() const;
  95. virtual std::deque<SharedHandle<FileEntry> > getFileEntries() const;
  96. virtual const std::string& getPieceHashAlgo() const;
  97. virtual std::deque<SharedHandle<AnnounceTier> > getAnnounceTiers() const;
  98. virtual void load(const std::string& torrentFile,
  99. const std::string& overrideName = "");
  100. void loadFromMemory(const unsigned char* content, size_t length,
  101. const std::string& defaultName,
  102. const std::string& overrideName = "");
  103. void loadFromMemory(const std::string& context,
  104. const std::string& defaultName,
  105. const std::string& overrideName = "")
  106. {
  107. loadFromMemory(reinterpret_cast<const unsigned char*>(context.c_str()),
  108. context.size(), defaultName, overrideName);
  109. }
  110. virtual const std::string& getName() const;
  111. virtual size_t getPieceLength() const;
  112. virtual size_t getNumPieces() const;
  113. virtual std::string getActualBasePath() const;
  114. virtual const unsigned char* getPeerId() {
  115. if(peerId.empty()) {
  116. peerId = generatePeerId();
  117. }
  118. return reinterpret_cast<const unsigned char*>(peerId.c_str());
  119. }
  120. virtual void computeFastSet
  121. (std::deque<size_t>& fastSet, const std::string& ipaddr, size_t fastSetSize);
  122. virtual RequestGroup* getOwnerRequestGroup()
  123. {
  124. return _ownerRequestGroup;
  125. }
  126. virtual std::deque<std::pair<std::string, uint16_t> >& getNodes();
  127. std::string generatePeerId() const;
  128. void setPeerIdPrefix(const std::string& peerIdPrefix)
  129. {
  130. _peerIdPrefix = peerIdPrefix;
  131. }
  132. // for unit test
  133. void setInfoHash(const unsigned char* infoHash);
  134. void setNumPieces(size_t numPieces)
  135. {
  136. this->numPieces = numPieces;
  137. }
  138. void setOwnerRequestGroup(RequestGroup* owner)
  139. {
  140. _ownerRequestGroup = owner;
  141. }
  142. void setRandomizer(const SharedHandle<Randomizer>& randomizer);
  143. friend std::ostream& operator<<(std::ostream& o, const DefaultBtContext& ctx);
  144. static const std::string DEFAULT_PEER_ID_PREFIX;
  145. };
  146. typedef SharedHandle<DefaultBtContext> DefaultBtContextHandle;
  147. } // namespace aria2
  148. #endif // _D_DEFAULT_BT_CONTEXT_H_