MockBtContext.h 3.2 KB

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