PeerAbstractCommand.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "PeerAbstractCommand.h"
  23. #include "DlAbortEx.h"
  24. #include "DlRetryEx.h"
  25. #include "Util.h"
  26. #include "message.h"
  27. #include "prefs.h"
  28. PeerAbstractCommand::PeerAbstractCommand(int cuid, const PeerHandle& peer,
  29. TorrentDownloadEngine* e,
  30. const SocketHandle& s)
  31. :Command(cuid), e(e), socket(s), peer(peer),
  32. checkSocketIsReadable(false), checkSocketIsWritable(false),
  33. uploadLimitCheck(false), uploadLimit(0), noCheck(false) {
  34. setReadCheckSocket(socket);
  35. timeout = e->option->getAsInt(PREF_TIMEOUT);
  36. e->torrentMan->connections++;
  37. }
  38. PeerAbstractCommand::~PeerAbstractCommand() {
  39. disableReadCheckSocket();
  40. disableWriteCheckSocket();
  41. e->torrentMan->connections--;
  42. }
  43. bool PeerAbstractCommand::execute() {
  44. if(e->torrentMan->isHalt()) {
  45. return true;
  46. }
  47. try {
  48. if(noCheck ||
  49. uploadLimitCheck && (uploadLimit == 0 ||
  50. e->getUploadSpeed() <= uploadLimit*1024) ||
  51. checkSocketIsReadable && readCheckTarget->isReadable(0) ||
  52. checkSocketIsWritable && writeCheckTarget->isWritable(0)) {
  53. checkPoint.reset();
  54. }
  55. if(checkPoint.elapsed(timeout)) {
  56. throw new DlRetryEx(EX_TIME_OUT);
  57. }
  58. return executeInternal();
  59. } catch(Exception* err) {
  60. logger->error(MSG_DOWNLOAD_ABORTED, err, cuid);
  61. onAbort(err);
  62. delete(err);
  63. return prepareForNextPeer(0);
  64. }
  65. }
  66. // TODO this method removed when PeerBalancerCommand is implemented
  67. bool PeerAbstractCommand::prepareForNextPeer(int wait) {
  68. return true;
  69. }
  70. bool PeerAbstractCommand::prepareForRetry(int wait) {
  71. return true;
  72. }
  73. void PeerAbstractCommand::onAbort(Exception* ex) {
  74. if(peer->isSeeder()) {
  75. peer->error++;
  76. } else {
  77. peer->error += MAX_PEER_ERROR;
  78. }
  79. peer->resetStatus();
  80. logger->debug("CUID#%d - Peer %s:%d banned.", cuid, peer->ipaddr.c_str(), peer->port);
  81. }
  82. void PeerAbstractCommand::disableReadCheckSocket() {
  83. if(checkSocketIsReadable) {
  84. e->deleteSocketForReadCheck(readCheckTarget, getUuid());
  85. checkSocketIsReadable = false;
  86. readCheckTarget = SocketHandle();
  87. }
  88. }
  89. void PeerAbstractCommand::setReadCheckSocket(const SocketHandle& socket) {
  90. if(!socket->isOpen()) {
  91. disableReadCheckSocket();
  92. } else {
  93. if(checkSocketIsReadable) {
  94. if(readCheckTarget != socket) {
  95. e->deleteSocketForReadCheck(readCheckTarget, getUuid());
  96. e->addSocketForReadCheck(socket, getUuid());
  97. readCheckTarget = socket;
  98. }
  99. } else {
  100. e->addSocketForReadCheck(socket, getUuid());
  101. checkSocketIsReadable = true;
  102. readCheckTarget = socket;
  103. }
  104. }
  105. }
  106. void PeerAbstractCommand::disableWriteCheckSocket() {
  107. if(checkSocketIsWritable) {
  108. e->deleteSocketForWriteCheck(writeCheckTarget, getUuid());
  109. checkSocketIsWritable = false;
  110. writeCheckTarget = SocketHandle();
  111. }
  112. }
  113. void PeerAbstractCommand::setWriteCheckSocket(const SocketHandle& socket) {
  114. if(!socket->isOpen()) {
  115. disableWriteCheckSocket();
  116. } else {
  117. if(checkSocketIsWritable) {
  118. if(writeCheckTarget != socket) {
  119. e->deleteSocketForWriteCheck(writeCheckTarget, getUuid());
  120. e->addSocketForWriteCheck(socket, getUuid());
  121. writeCheckTarget = socket;
  122. }
  123. } else {
  124. e->addSocketForWriteCheck(socket, getUuid());
  125. checkSocketIsWritable = true;
  126. writeCheckTarget = socket;
  127. }
  128. }
  129. }
  130. void PeerAbstractCommand::setUploadLimit(int uploadLimit) {
  131. this->uploadLimit = uploadLimit;
  132. }
  133. void PeerAbstractCommand::setUploadLimitCheck(bool check) {
  134. this->uploadLimitCheck = check;
  135. }
  136. void PeerAbstractCommand::setNoCheck(bool check) {
  137. this->noCheck = check;
  138. }