DefaultBtInteractive.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #include "DefaultBtInteractive.h"
  36. #include "prefs.h"
  37. #include "message.h"
  38. #include "BtHandshakeMessage.h"
  39. #include "Util.h"
  40. #include "BtKeepAliveMessage.h"
  41. #include "BtChokeMessage.h"
  42. #include "BtUnchokeMessage.h"
  43. #include "DlAbortEx.h"
  44. void DefaultBtInteractive::initiateHandshake() {
  45. BtMessageHandle message = messageFactory->createHandshakeMessage(btContext->getInfoHash(),
  46. btContext->getPeerId());
  47. dispatcher->addMessageToQueue(message);
  48. dispatcher->sendMessages();
  49. }
  50. BtMessageHandle DefaultBtInteractive::receiveHandshake(bool quickReply) {
  51. BtHandshakeMessageHandle message =
  52. btMessageReceiver->receiveHandshake(quickReply);
  53. if(message.isNull()) {
  54. return 0;
  55. }
  56. peer->setPeerId(message->getPeerId());
  57. logger->info(MSG_RECEIVE_PEER_MESSAGE, cuid,
  58. peer->ipaddr.c_str(), peer->port,
  59. message->toString().c_str());
  60. return message;
  61. }
  62. BtMessageHandle DefaultBtInteractive::receiveAndSendHandshake() {
  63. return receiveHandshake(true);
  64. }
  65. void DefaultBtInteractive::doPostHandshakeProcessing() {
  66. // TODO where is the valid place to rest haveCheckTime?
  67. haveCheckPoint.reset();
  68. keepAliveCheckPoint.reset();
  69. floodingCheckPoint.reset();
  70. addBitfieldMessageToQueue();
  71. addAllowedFastMessageToQueue();
  72. sendPendingMessage();
  73. }
  74. void DefaultBtInteractive::addBitfieldMessageToQueue() {
  75. if(peer->isFastExtensionEnabled()) {
  76. if(pieceStorage->downloadFinished()) {
  77. dispatcher->addMessageToQueue(messageFactory->createHaveAllMessage());
  78. } else if(pieceStorage->getCompletedLength() > 0) {
  79. dispatcher->addMessageToQueue(messageFactory->createBitfieldMessage());
  80. } else {
  81. dispatcher->addMessageToQueue(messageFactory->createHaveNoneMessage());
  82. }
  83. } else {
  84. if(pieceStorage->getCompletedLength() > 0) {
  85. dispatcher->addMessageToQueue(messageFactory->createBitfieldMessage());
  86. }
  87. }
  88. }
  89. void DefaultBtInteractive::addAllowedFastMessageToQueue() {
  90. if(peer->isFastExtensionEnabled()) {
  91. Integers fastSet = Util::computeFastSet(peer->ipaddr,
  92. btContext->getInfoHash(),
  93. btContext->getNumPieces(),
  94. allowedFastSetSize);
  95. for(Integers::const_iterator itr = fastSet.begin();
  96. itr != fastSet.end(); itr++) {
  97. dispatcher->addMessageToQueue(messageFactory->createAllowedFastMessage(*itr));
  98. }
  99. }
  100. }
  101. void DefaultBtInteractive::decideChoking() {
  102. if(peer->shouldBeChoking()) {
  103. if(!peer->amChoking) {
  104. dispatcher->addMessageToQueue(messageFactory->createChokeMessage());
  105. }
  106. } else {
  107. if(peer->amChoking) {
  108. dispatcher->addMessageToQueue(messageFactory->createUnchokeMessage());
  109. }
  110. }
  111. }
  112. void DefaultBtInteractive::checkHave() {
  113. Integers indexes =
  114. pieceStorage->getAdvertisedPieceIndexes(cuid, haveCheckPoint);
  115. haveCheckPoint.reset();
  116. if(indexes.size() >= 20) {
  117. if(peer->isFastExtensionEnabled() && pieceStorage->downloadFinished()) {
  118. dispatcher->addMessageToQueue(messageFactory->createHaveAllMessage());
  119. } else {
  120. dispatcher->addMessageToQueue(messageFactory->createBitfieldMessage());
  121. }
  122. } else {
  123. for(Integers::iterator itr = indexes.begin(); itr != indexes.end(); itr++) {
  124. dispatcher->addMessageToQueue(messageFactory->createHaveMessage(*itr));
  125. }
  126. }
  127. }
  128. void DefaultBtInteractive::sendKeepAlive() {
  129. if(keepAliveCheckPoint.elapsed(keepAliveInterval)) {
  130. dispatcher->addMessageToQueue(messageFactory->createKeepAliveMessage());
  131. dispatcher->sendMessages();
  132. keepAliveCheckPoint.reset();
  133. }
  134. }
  135. void DefaultBtInteractive::receiveMessages() {
  136. for(int i = 0; i < 50; i++) {
  137. if(maxDownloadSpeedLimit > 0) {
  138. TransferStat stat = peerStorage->calculateStat();
  139. if(maxDownloadSpeedLimit < stat.downloadSpeed) {
  140. break;
  141. }
  142. }
  143. BtMessageHandle message = btMessageReceiver->receiveMessage();
  144. if(message.isNull()) {
  145. break;
  146. }
  147. logger->info(MSG_RECEIVE_PEER_MESSAGE, cuid,
  148. peer->ipaddr.c_str(), peer->port,
  149. message->toString().c_str());
  150. message->doReceivedAction();
  151. switch(message->getId()) {
  152. case BtKeepAliveMessage::ID:
  153. floodingStat.incKeepAliveCount();
  154. break;
  155. case BtChokeMessage::ID:
  156. if(!peer->peerChoking) {
  157. floodingStat.incChokeUnchokeCount();
  158. }
  159. break;
  160. case BtUnchokeMessage::ID:
  161. if(peer->peerChoking) {
  162. floodingStat.incChokeUnchokeCount();
  163. }
  164. break;
  165. }
  166. }
  167. }
  168. void DefaultBtInteractive::decideInterest() {
  169. if(pieceStorage->hasMissingPiece(peer)) {
  170. if(!peer->amInterested) {
  171. logger->debug("CUID#%d - Interested in the peer", cuid);
  172. dispatcher->
  173. addMessageToQueue(messageFactory->createInterestedMessage());
  174. }
  175. } else {
  176. if(peer->amInterested) {
  177. logger->debug("CUID#%d - Not interested in the peer", cuid);
  178. dispatcher->
  179. addMessageToQueue(messageFactory->createNotInterestedMessage());
  180. }
  181. }
  182. }
  183. void DefaultBtInteractive::fillPiece(int maxPieceNum) {
  184. if(pieceStorage->hasMissingPiece(peer)) {
  185. if(peer->peerChoking) {
  186. if(peer->isFastExtensionEnabled()) {
  187. while(btRequestFactory->countTargetPiece() < maxPieceNum) {
  188. PieceHandle piece = pieceStorage->getMissingFastPiece(peer);
  189. if(piece.isNull()) {
  190. break;
  191. } else {
  192. btRequestFactory->addTargetPiece(piece);
  193. }
  194. }
  195. }
  196. } else {
  197. while(btRequestFactory->countTargetPiece() < maxPieceNum) {
  198. PieceHandle piece = pieceStorage->getMissingPiece(peer);
  199. if(piece.isNull()) {
  200. break;
  201. } else {
  202. btRequestFactory->addTargetPiece(piece);
  203. }
  204. }
  205. }
  206. }
  207. }
  208. void DefaultBtInteractive::addRequests() {
  209. uint32_t MAX_PENDING_REQUEST;
  210. if(peer->getLatency() < 500) {
  211. MAX_PENDING_REQUEST = 24;
  212. } else if(peer->getLatency() < 1500) {
  213. MAX_PENDING_REQUEST = 12;
  214. } else {
  215. MAX_PENDING_REQUEST = 6;
  216. }
  217. uint32_t pieceNum;
  218. if(pieceStorage->isEndGame()) {
  219. pieceNum = 1;
  220. } else {
  221. uint32_t blocks = DIV_FLOOR(btContext->getPieceLength(), BLOCK_LENGTH);
  222. pieceNum = DIV_FLOOR(MAX_PENDING_REQUEST, blocks);
  223. }
  224. fillPiece(pieceNum);
  225. uint32_t reqNumToCreate =
  226. MAX_PENDING_REQUEST <= dispatcher->countOutstandingRequest() ?
  227. 0 : MAX_PENDING_REQUEST-dispatcher->countOutstandingRequest();
  228. if(reqNumToCreate > 0) {
  229. //logger->debug("CUID#%d - %u requets to go.", cuid, reqNumToCreate);
  230. BtMessages requests;
  231. if(pieceStorage->isEndGame()) {
  232. requests = btRequestFactory->createRequestMessagesOnEndGame(reqNumToCreate);
  233. } else {
  234. requests = btRequestFactory->createRequestMessages(reqNumToCreate);
  235. }
  236. dispatcher->addMessageToQueue(requests);
  237. }
  238. }
  239. void DefaultBtInteractive::cancelAllPiece() {
  240. btRequestFactory->removeAllTargetPiece();
  241. }
  242. void DefaultBtInteractive::sendPendingMessage() {
  243. dispatcher->sendMessages();
  244. }
  245. void DefaultBtInteractive::detectMessageFlooding() {
  246. if(floodingCheckPoint.elapsed(FLOODING_CHECK_INTERVAL)) {
  247. if(floodingStat.getChokeUnchokeCount() >= 2 ||
  248. floodingStat.getKeepAliveCount() >= 2) {
  249. throw new DlAbortEx("Flooding detected.");
  250. } else {
  251. floodingStat.reset();
  252. }
  253. floodingCheckPoint.reset();
  254. }
  255. }
  256. void DefaultBtInteractive::doInteractionProcessing() {
  257. decideChoking();
  258. detectMessageFlooding();
  259. dispatcher->checkRequestSlotAndDoNecessaryThing();
  260. checkHave();
  261. sendKeepAlive();
  262. receiveMessages();
  263. btRequestFactory->removeCompletedPiece();
  264. decideInterest();
  265. if(!pieceStorage->downloadFinished()) {
  266. addRequests();
  267. }
  268. sendPendingMessage();
  269. }