MockBtContext.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #ifndef _D_MOCK_BT_CONTEXT_H_
  2. #define _D_MOCK_BT_CONTEXT_H_
  3. #include "BtContext.h"
  4. #include "Util.h"
  5. #include "AnnounceTier.h"
  6. #include <cstring>
  7. namespace aria2 {
  8. class MockBtContext : public BtContext {
  9. private:
  10. unsigned char infoHash[20];
  11. std::deque<std::string> pieceHashes;
  12. uint64_t totalLength;
  13. FILE_MODE fileMode;
  14. std::string name;
  15. size_t pieceLength;
  16. size_t numPieces;
  17. unsigned char peerId[20];
  18. std::deque<SharedHandle<FileEntry> > fileEntries;
  19. std::deque<SharedHandle<AnnounceTier> > announceTiers;
  20. std::deque<std::pair<std::string, uint16_t> > _nodes;
  21. std::deque<size_t> fastSet;
  22. public:
  23. MockBtContext():totalLength(0),
  24. pieceLength(0),
  25. numPieces(0) {}
  26. virtual ~MockBtContext() {}
  27. virtual const unsigned char* getInfoHash() const {
  28. return infoHash;
  29. }
  30. void setInfoHash(const unsigned char* infoHash) {
  31. memcpy(this->infoHash, infoHash, sizeof(this->infoHash));
  32. }
  33. virtual size_t getInfoHashLength() const {
  34. return sizeof(infoHash);
  35. }
  36. virtual std::string getInfoHashAsString() const {
  37. return Util::toHex(infoHash, sizeof(infoHash));
  38. }
  39. virtual std::string getPieceHash(size_t index) const {
  40. return pieceHashes.at(index);
  41. }
  42. virtual const std::deque<std::string>& getPieceHashes() const {
  43. return pieceHashes;
  44. }
  45. void addPieceHash(const std::string& pieceHash) {
  46. pieceHashes.push_back(pieceHash);
  47. }
  48. virtual uint64_t getTotalLength() const {
  49. return totalLength;
  50. }
  51. void setTotalLength(uint64_t length) {
  52. this->totalLength = length;
  53. }
  54. virtual FILE_MODE getFileMode() const {
  55. return fileMode;
  56. }
  57. void setFileMode(FILE_MODE fileMode) {
  58. this->fileMode = fileMode;
  59. }
  60. virtual std::deque<SharedHandle<FileEntry> > getFileEntries() const {
  61. return fileEntries;
  62. }
  63. void addFileEntry(const SharedHandle<FileEntry>& fileEntry) {
  64. fileEntries.push_back(fileEntry);
  65. }
  66. virtual std::deque<SharedHandle<AnnounceTier> > getAnnounceTiers() const {
  67. return announceTiers;
  68. }
  69. void addAnnounceTier(const SharedHandle<AnnounceTier>& announceTier) {
  70. announceTiers.push_back(announceTier);
  71. }
  72. virtual void load(const std::string& torrentFile) {}
  73. virtual std::string getName() const {
  74. return name;
  75. }
  76. void setName(const std::string& name) {
  77. this->name = name;
  78. }
  79. virtual size_t getPieceLength() const {
  80. return pieceLength;
  81. }
  82. void setPieceLength(size_t pieceLength) {
  83. this->pieceLength = pieceLength;
  84. }
  85. virtual size_t getNumPieces() const {
  86. return numPieces;
  87. }
  88. void setNumPieces(size_t numPieces) {
  89. this->numPieces = numPieces;
  90. }
  91. virtual const unsigned char* getPeerId() {
  92. return peerId;
  93. }
  94. void setPeerId(const unsigned char* peerId) {
  95. memcpy(this->peerId, peerId, sizeof(this->peerId));
  96. }
  97. virtual std::deque<size_t> computeFastSet(const std::string& ipaddr, size_t fastSetSize)
  98. {
  99. return fastSet;
  100. }
  101. void setFastSet(const std::deque<size_t>& fastSet)
  102. {
  103. this->fastSet = fastSet;
  104. }
  105. virtual std::string getPieceHashAlgo() const
  106. {
  107. return "sha1";
  108. }
  109. virtual std::string getActualBasePath() const
  110. {
  111. return _dir+"/"+name;
  112. }
  113. virtual RequestGroup* getOwnerRequestGroup()
  114. {
  115. return 0;
  116. }
  117. virtual std::deque<std::pair<std::string, uint16_t> >& getNodes()
  118. {
  119. return _nodes;
  120. }
  121. void setNodes(const std::deque<std::pair<std::string, uint16_t> >& nodes)
  122. {
  123. _nodes = nodes;
  124. }
  125. };
  126. } // namespace aria2
  127. #endif // _D_MOCK_BT_CONTEXT_H_