DefaultBtInteractive.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_INTERACTIVE_H_
  36. #define _D_DEFAULT_BT_INTERACTIVE_H_
  37. #include "BtInteractive.h"
  38. #include "Peer.h"
  39. #include "BtContext.h"
  40. #include "PieceStorage.h"
  41. #include "PeerStorage.h"
  42. #include "BtMessageReceiver.h"
  43. #include "BtMessageDispatcher.h"
  44. #include "BtRequestFactory.h"
  45. #include "PeerConnection.h"
  46. #include "BtRegistry.h"
  47. #include "Logger.h"
  48. #include "LogFactory.h"
  49. #include "TimeA2.h"
  50. class FloodingStat {
  51. private:
  52. uint32_t chokeUnchokeCount;
  53. uint32_t keepAliveCount;
  54. public:
  55. FloodingStat():chokeUnchokeCount(0), keepAliveCount(0) {}
  56. void incChokeUnchokeCount() {
  57. if(chokeUnchokeCount < UINT32_MAX) {
  58. chokeUnchokeCount++;
  59. }
  60. }
  61. void incKeepAliveCount() {
  62. if(keepAliveCount < UINT32_MAX) {
  63. keepAliveCount++;
  64. }
  65. }
  66. uint32_t getChokeUnchokeCount() const {
  67. return chokeUnchokeCount;
  68. }
  69. uint32_t getKeepAliveCount() const {
  70. return keepAliveCount;
  71. }
  72. void reset() {
  73. chokeUnchokeCount = 0;
  74. keepAliveCount = 0;
  75. }
  76. };
  77. class DefaultBtInteractive : public BtInteractive {
  78. private:
  79. int32_t cuid;
  80. PeerHandle peer;
  81. BtContextHandle btContext;
  82. PeerStorageHandle peerStorage;
  83. PieceStorageHandle pieceStorage;
  84. BtMessageReceiverHandle btMessageReceiver;
  85. BtMessageDispatcherHandle dispatcher;
  86. BtRequestFactoryHandle btRequestFactory;
  87. PeerConnectionHandle peerConnection;
  88. const Logger* logger;
  89. uint32_t allowedFastSetSize;
  90. Time haveCheckPoint;
  91. Time keepAliveCheckPoint;
  92. Time floodingCheckPoint;
  93. FloodingStat floodingStat;
  94. const Option* option;
  95. static const uint32_t FLOODING_CHECK_INTERVAL = 5;
  96. void addBitfieldMessageToQueue();
  97. void addAllowedFastMessageToQueue();
  98. void decideChoking();
  99. void checkHave();
  100. void sendKeepAlive();
  101. void decideInterest();
  102. void fillPiece(int maxPieceNum);
  103. void addRequests();
  104. void detectMessageFlooding();
  105. public:
  106. DefaultBtInteractive():peer(0),
  107. btContext(0),
  108. peerStorage(0),
  109. pieceStorage(0),
  110. btMessageReceiver(0),
  111. dispatcher(0),
  112. btRequestFactory(0),
  113. peerConnection(0),
  114. logger(LogFactory::getInstance()),
  115. allowedFastSetSize(10)
  116. {
  117. logger->debug("DefaultBtInteractive instantiated.");
  118. }
  119. virtual ~DefaultBtInteractive() {
  120. logger->debug("DefaultBtInteractive deleted.");
  121. }
  122. virtual void initiateHandshake();
  123. virtual BtMessageHandle receiveHandshake(bool quickReply = false);
  124. virtual BtMessageHandle receiveAndSendHandshake();
  125. virtual void doPostHandshakeProcessing();
  126. virtual void doInteractionProcessing();
  127. virtual void cancelAllPiece();
  128. virtual void sendPendingMessage();
  129. void receiveMessages();
  130. virtual uint32_t countPendingMessage() {
  131. return dispatcher->countMessageInQueue();
  132. }
  133. virtual bool isSendingMessageInProgress() {
  134. return dispatcher->isSendingInProgress();
  135. }
  136. void setCuid(int32_t cuid) {
  137. this->cuid = cuid;
  138. }
  139. void setPeer(const PeerHandle& peer) {
  140. this->peer = peer;
  141. }
  142. void setBtContext(const BtContextHandle& btContext) {
  143. this->btContext = btContext;
  144. this->peerStorage = PEER_STORAGE(btContext);
  145. this->pieceStorage = PIECE_STORAGE(btContext);
  146. }
  147. void setBtMessageReceiver(const BtMessageReceiverHandle& receiver) {
  148. this->btMessageReceiver = receiver;
  149. }
  150. void setDispatcher(const BtMessageDispatcherHandle& dispatcher) {
  151. this->dispatcher = dispatcher;
  152. }
  153. void setBtRequestFactory(const BtRequestFactoryHandle& factory) {
  154. this->btRequestFactory = factory;
  155. }
  156. void setPeerConnection(const PeerConnectionHandle& peerConnection) {
  157. this->peerConnection = peerConnection;
  158. }
  159. void setOption(const Option* option) {
  160. this->option = option;
  161. }
  162. };
  163. typedef SharedHandle<DefaultBtInteractive> DefaultBtInteractiveHandle;
  164. #endif // _D_DEFAULT_BT_INTERACTIVE_H_