PeerAbstractCommand.cc 4.9 KB

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