PeerAbstractCommand.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "PeerAbstractCommand.h"
  36. #include "Peer.h"
  37. #include "DownloadEngine.h"
  38. #include "Option.h"
  39. #include "DlAbortEx.h"
  40. #include "Socket.h"
  41. #include "Logger.h"
  42. #include "message.h"
  43. #include "prefs.h"
  44. #include "DownloadFailureException.h"
  45. #include "StringFormat.h"
  46. #include "wallclock.h"
  47. namespace aria2 {
  48. PeerAbstractCommand::PeerAbstractCommand(int32_t cuid,
  49. const SharedHandle<Peer>& peer,
  50. DownloadEngine* e,
  51. const SocketHandle& s):
  52. Command(cuid),
  53. e(e),
  54. socket(s),
  55. peer(peer),
  56. checkSocketIsReadable(false),
  57. checkSocketIsWritable(false),
  58. noCheck(false)
  59. {
  60. if(!socket.isNull() && socket->isOpen()) {
  61. setReadCheckSocket(socket);
  62. }
  63. // TODO referring global option
  64. timeout = e->option->getAsInt(PREF_BT_TIMEOUT);
  65. }
  66. PeerAbstractCommand::~PeerAbstractCommand()
  67. {
  68. disableReadCheckSocket();
  69. disableWriteCheckSocket();
  70. }
  71. bool PeerAbstractCommand::execute()
  72. {
  73. if(logger->debug()) {
  74. logger->debug("CUID#%d -"
  75. " socket: read:%d, write:%d, hup:%d, err:%d, noCheck:%d",
  76. cuid, _readEvent, _writeEvent, _hupEvent, _errorEvent,
  77. noCheck);
  78. }
  79. if(exitBeforeExecute()) {
  80. onAbort();
  81. return true;
  82. }
  83. try {
  84. if(noCheck ||
  85. (checkSocketIsReadable && _readEvent) ||
  86. (checkSocketIsWritable && _writeEvent) ||
  87. _hupEvent) {
  88. checkPoint = global::wallclock;
  89. } else if(_errorEvent) {
  90. throw DL_ABORT_EX
  91. (StringFormat(MSG_NETWORK_PROBLEM,
  92. socket->getSocketError().c_str()).str());
  93. }
  94. if(checkPoint.difference(global::wallclock) >= timeout) {
  95. throw DL_ABORT_EX(EX_TIME_OUT);
  96. }
  97. return executeInternal();
  98. } catch(DownloadFailureException& err) {
  99. logger->error(EX_DOWNLOAD_ABORTED, err);
  100. onAbort();
  101. onFailure();
  102. return true;
  103. } catch(RecoverableException& err) {
  104. if(logger->debug()) {
  105. logger->debug(MSG_TORRENT_DOWNLOAD_ABORTED, err, cuid);
  106. logger->debug(MSG_PEER_BANNED,
  107. cuid, peer->ipaddr.c_str(), peer->port);
  108. }
  109. onAbort();
  110. return prepareForNextPeer(0);
  111. }
  112. }
  113. // TODO this method removed when PeerBalancerCommand is implemented
  114. bool PeerAbstractCommand::prepareForNextPeer(time_t wait)
  115. {
  116. return true;
  117. }
  118. void PeerAbstractCommand::disableReadCheckSocket()
  119. {
  120. if(checkSocketIsReadable) {
  121. e->deleteSocketForReadCheck(readCheckTarget, this);
  122. checkSocketIsReadable = false;
  123. readCheckTarget = SocketHandle();
  124. }
  125. }
  126. void PeerAbstractCommand::setReadCheckSocket(const SocketHandle& socket)
  127. {
  128. if(!socket->isOpen()) {
  129. disableReadCheckSocket();
  130. } else {
  131. if(checkSocketIsReadable) {
  132. if(readCheckTarget != socket) {
  133. e->deleteSocketForReadCheck(readCheckTarget, this);
  134. e->addSocketForReadCheck(socket, this);
  135. readCheckTarget = socket;
  136. }
  137. } else {
  138. e->addSocketForReadCheck(socket, this);
  139. checkSocketIsReadable = true;
  140. readCheckTarget = socket;
  141. }
  142. }
  143. }
  144. void PeerAbstractCommand::disableWriteCheckSocket()
  145. {
  146. if(checkSocketIsWritable) {
  147. e->deleteSocketForWriteCheck(writeCheckTarget, this);
  148. checkSocketIsWritable = false;
  149. writeCheckTarget = SocketHandle();
  150. }
  151. }
  152. void PeerAbstractCommand::setWriteCheckSocket(const SocketHandle& socket)
  153. {
  154. if(!socket->isOpen()) {
  155. disableWriteCheckSocket();
  156. } else {
  157. if(checkSocketIsWritable) {
  158. if(writeCheckTarget != socket) {
  159. e->deleteSocketForWriteCheck(writeCheckTarget, this);
  160. e->addSocketForWriteCheck(socket, this);
  161. writeCheckTarget = socket;
  162. }
  163. } else {
  164. e->addSocketForWriteCheck(socket, this);
  165. checkSocketIsWritable = true;
  166. writeCheckTarget = socket;
  167. }
  168. }
  169. }
  170. void PeerAbstractCommand::setNoCheck(bool check)
  171. {
  172. this->noCheck = check;
  173. }
  174. void PeerAbstractCommand::updateKeepAlive()
  175. {
  176. checkPoint = global::wallclock;
  177. }
  178. } // namespace aria2