PeerAbstractCommand.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <sys/time.h>
  26. #include "Util.h"
  27. #include "message.h"
  28. #include "prefs.h"
  29. PeerAbstractCommand::PeerAbstractCommand(int cuid, Peer* peer, TorrentDownloadEngine* e, const Socket* s):
  30. Command(cuid), e(e), peer(peer), checkSocketIsReadable(false), checkSocketIsWritable(false) {
  31. if(s != NULL) {
  32. socket = new Socket(*s);
  33. setReadCheckSocket(socket);
  34. } else {
  35. socket = NULL;
  36. }
  37. this->checkPoint.tv_sec = 0;
  38. this->checkPoint.tv_usec = 0;
  39. timeout = e->option->getAsInt(PREF_TIMEOUT);
  40. e->torrentMan->connections++;
  41. }
  42. PeerAbstractCommand::~PeerAbstractCommand() {
  43. setReadCheckSocket(NULL);
  44. setWriteCheckSocket(NULL);
  45. if(socket != NULL) {
  46. delete(socket);
  47. }
  48. e->torrentMan->connections--;
  49. }
  50. void PeerAbstractCommand::updateCheckPoint() {
  51. gettimeofday(&checkPoint, NULL);
  52. }
  53. bool PeerAbstractCommand::isTimeoutDetected() {
  54. struct timeval now;
  55. gettimeofday(&now, NULL);
  56. if(checkPoint.tv_sec == 0 && checkPoint.tv_usec == 0) {
  57. checkPoint = now;
  58. return false;
  59. } else {
  60. long long int elapsed = Util::difftv(now, checkPoint);
  61. if(elapsed >= ((long long int)timeout)*1000000) {
  62. return true;
  63. } else {
  64. return false;
  65. }
  66. }
  67. }
  68. bool PeerAbstractCommand::execute() {
  69. try {
  70. beforeSocketCheck();
  71. if(checkSocketIsReadable && !readCheckTarget->isReadable(0)
  72. || checkSocketIsWritable && !writeCheckTarget->isWritable(0)) {
  73. if(isTimeoutDetected()) {
  74. // TODO
  75. checkPoint.tv_sec = 0;
  76. checkPoint.tv_usec = 0;
  77. throw new DlRetryEx(EX_TIME_OUT);
  78. }
  79. e->commands.push(this);
  80. return false;
  81. }
  82. updateCheckPoint();
  83. bool returnValue = executeInternal();
  84. //e->torrentMan->updatePeer(peer);
  85. return returnValue;
  86. } catch(Exception* err) {
  87. e->logger->error(MSG_DOWNLOAD_ABORTED, err, cuid);
  88. onAbort(err);
  89. delete(err);
  90. return prepareForNextPeer(0);
  91. }
  92. /*catch(DlRetryEx* err) {
  93. e->logger->error(MSG_RESTARTING_DOWNLOAD, err, cuid);
  94. peer->tryCount++;
  95. bool isAbort = e->option->getAsInt(PREF_MAX_TRIES) != 0 &&
  96. peer->tryCount >= e->option->getAsInt(PREF_MAX_TRIES);
  97. int tryCount = peer->tryCount;
  98. if(isAbort) {
  99. onAbort(err);
  100. }
  101. delete(err);
  102. if(isAbort) {
  103. e->logger->error(MSG_MAX_TRY, cuid, tryCount);
  104. return true;
  105. } else {
  106. return prepareForRetry(e->option->getAsInt(PREF_RETRY_WAIT));
  107. }
  108. }
  109. */
  110. }
  111. // TODO this method removed when PeerBalancerCommand is implemented
  112. bool PeerAbstractCommand::prepareForNextPeer(int wait) {
  113. return true;
  114. }
  115. bool PeerAbstractCommand::prepareForRetry(int wait) {
  116. return true;
  117. }
  118. void PeerAbstractCommand::onAbort(Exception* ex) {
  119. if(peer->isSeeder()) {
  120. peer->error++;
  121. } else {
  122. peer->error += MAX_PEER_ERROR;
  123. }
  124. peer->tryCount = 0;
  125. peer->cuid = 0;
  126. peer->amChocking = true;
  127. peer->amInterested = false;
  128. peer->peerChoking = true;
  129. peer->peerInterested = false;
  130. e->logger->debug("CUID#%d - peer %s:%d banned.", cuid, peer->ipaddr.c_str(), peer->port);
  131. }
  132. void PeerAbstractCommand::setReadCheckSocket(Socket* socket) {
  133. if(socket == NULL) {
  134. if(checkSocketIsReadable) {
  135. e->deleteSocketForReadCheck(readCheckTarget);
  136. checkSocketIsReadable = false;
  137. readCheckTarget = NULL;
  138. }
  139. } else {
  140. if(checkSocketIsReadable) {
  141. if(readCheckTarget != socket) {
  142. e->deleteSocketForReadCheck(readCheckTarget);
  143. e->addSocketForReadCheck(socket);
  144. readCheckTarget = socket;
  145. }
  146. } else {
  147. e->addSocketForReadCheck(socket);
  148. checkSocketIsReadable = true;
  149. readCheckTarget = socket;
  150. }
  151. }
  152. }
  153. void PeerAbstractCommand::setWriteCheckSocket(Socket* socket) {
  154. if(socket == NULL) {
  155. if(checkSocketIsWritable) {
  156. e->deleteSocketForWriteCheck(writeCheckTarget);
  157. checkSocketIsWritable = false;
  158. writeCheckTarget = NULL;
  159. }
  160. } else {
  161. if(checkSocketIsWritable) {
  162. if(writeCheckTarget != socket) {
  163. e->deleteSocketForWriteCheck(writeCheckTarget);
  164. e->addSocketForWriteCheck(socket);
  165. writeCheckTarget = socket;
  166. }
  167. } else {
  168. e->addSocketForWriteCheck(socket);
  169. checkSocketIsWritable = true;
  170. writeCheckTarget = socket;
  171. }
  172. }
  173. }