PeerAbstractCommand.cc 5.6 KB

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