MockBtContext.h 3.4 KB

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