PeerSessionResource.h 5.1 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_PEER_SESSION_RESOURCE_H_
  36. #define _D_PEER_SESSION_RESOURCE_H_
  37. #include "common.h"
  38. #include "BtConstants.h"
  39. #include "PeerStat.h"
  40. #include "TimeA2.h"
  41. #include <string>
  42. #include <deque>
  43. namespace aria2 {
  44. class BitfieldMan;
  45. class PeerSessionResource {
  46. private:
  47. // localhost is choking this peer
  48. bool _amChoking;
  49. // localhost is interested in this peer
  50. bool _amInterested;
  51. // this peer is choking localhost
  52. bool _peerChoking;
  53. // this peer is interested in localhost
  54. bool _peerInterested;
  55. // choking this peer is requested
  56. bool _chokingRequired;
  57. // this peer is eligible for *optional* unchoking.
  58. bool _optUnchoking;
  59. // this peer is snubbing.
  60. bool _snubbing;
  61. BitfieldMan* _bitfieldMan;
  62. bool _fastExtensionEnabled;
  63. // fast index set which a peer has sent to localhost.
  64. std::deque<size_t> _peerAllowedIndexSet;
  65. // fast index set which localhost has sent to a peer.
  66. std::deque<size_t> _amAllowedIndexSet;
  67. bool _extendedMessagingEnabled;
  68. Extensions _extensions;
  69. bool _dhtEnabled;
  70. PeerStat _peerStat;
  71. unsigned int _latency;
  72. uint64_t _uploadLength;
  73. uint64_t _downloadLength;
  74. Time _lastDownloadUpdate;
  75. Time _lastAmUnchoking;
  76. public:
  77. PeerSessionResource(size_t pieceLength, uint64_t totalLength);
  78. ~PeerSessionResource();
  79. // localhost is choking this peer
  80. bool amChoking() const;
  81. void amChoking(bool b);
  82. // localhost is interested in this peer
  83. bool amInterested() const;
  84. void amInterested(bool b);
  85. // this peer is choking localhost
  86. bool peerChoking() const;
  87. void peerChoking(bool b);
  88. // this peer is interested in localhost
  89. bool peerInterested() const;
  90. void peerInterested(bool b);
  91. // this peer should be choked
  92. bool chokingRequired() const;
  93. void chokingRequired(bool b);
  94. // this peer is eligible for unchoking optionally.
  95. bool optUnchoking() const;
  96. void optUnchoking(bool b);
  97. bool shouldBeChoking() const;
  98. // this peer is snubbing.
  99. bool snubbing() const;
  100. void snubbing(bool b);
  101. bool hasAllPieces() const;
  102. void updateBitfield(size_t index, int operation);
  103. void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
  104. const unsigned char* getBitfield() const;
  105. size_t getBitfieldLength() const;
  106. bool hasPiece(size_t index) const;
  107. void markSeeder();
  108. bool fastExtensionEnabled() const;
  109. void fastExtensionEnabled(bool b);
  110. // fast index set which a peer has sent to localhost.
  111. const std::deque<size_t>& peerAllowedIndexSet() const;
  112. void addPeerAllowedIndex(size_t index);
  113. bool peerAllowedIndexSetContains(size_t index) const;
  114. // fast index set which localhost has sent to a peer.
  115. const std::deque<size_t>& amAllowedIndexSet() const;
  116. void addAmAllowedIndex(size_t index);
  117. bool amAllowedIndexSetContains(size_t index) const;
  118. bool extendedMessagingEnabled() const;
  119. void extendedMessagingEnabled(bool b);
  120. uint8_t getExtensionMessageID(const std::string& name) const;
  121. std::string getExtensionName(uint8_t id) const;
  122. void addExtension(const std::string& name, uint8_t id);
  123. bool dhtEnabled() const;
  124. void dhtEnabled(bool b);
  125. PeerStat& getPeerStat();
  126. unsigned int latency() const;
  127. void updateLatency(unsigned int latency);
  128. uint64_t uploadLength() const;
  129. void updateUploadLength(size_t bytes);
  130. uint64_t downloadLength() const;
  131. void updateDownloadLength(size_t bytes);
  132. const Time& getLastDownloadUpdate() const;
  133. const Time& getLastAmUnchoking() const;
  134. uint64_t getCompletedLength() const;
  135. };
  136. } // namespace aria2
  137. #endif // _D_PEER_SESSION_RESOURCE_H_