PeerAbstractCommand.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "LogFactory.h"
  43. #include "message.h"
  44. #include "prefs.h"
  45. #include "DownloadFailureException.h"
  46. #include "fmt.h"
  47. #include "wallclock.h"
  48. #include "util.h"
  49. namespace aria2 {
  50. PeerAbstractCommand::PeerAbstractCommand
  51. (cuid_t cuid,
  52. const SharedHandle<Peer>& peer,
  53. DownloadEngine* e,
  54. const SocketHandle& s)
  55. : Command(cuid),
  56. checkPoint_(global::wallclock()),
  57. // TODO referring global option
  58. timeout_(e->getOption()->getAsInt(PREF_BT_TIMEOUT)),
  59. e_(e),
  60. socket_(s),
  61. peer_(peer),
  62. checkSocketIsReadable_(false),
  63. checkSocketIsWritable_(false),
  64. noCheck_(false)
  65. {
  66. if(socket_ && socket_->isOpen()) {
  67. setReadCheckSocket(socket_);
  68. }
  69. }
  70. PeerAbstractCommand::~PeerAbstractCommand()
  71. {
  72. disableReadCheckSocket();
  73. disableWriteCheckSocket();
  74. }
  75. bool PeerAbstractCommand::execute()
  76. {
  77. A2_LOG_DEBUG(fmt("CUID#%" PRId64 " -"
  78. " socket: read:%d, write:%d, hup:%d, err:%d, noCheck:%d",
  79. getCuid(),
  80. readEventEnabled(), writeEventEnabled(),
  81. hupEventEnabled(), errorEventEnabled(),
  82. noCheck_));
  83. if(exitBeforeExecute()) {
  84. onAbort();
  85. return true;
  86. }
  87. try {
  88. if(noCheck_ ||
  89. (checkSocketIsReadable_ && readEventEnabled()) ||
  90. (checkSocketIsWritable_ && writeEventEnabled()) ||
  91. hupEventEnabled()) {
  92. checkPoint_ = global::wallclock();
  93. } else if(errorEventEnabled()) {
  94. throw DL_ABORT_EX
  95. (fmt(MSG_NETWORK_PROBLEM,
  96. socket_->getSocketError().c_str()));
  97. }
  98. if(checkPoint_.difference(global::wallclock()) >= timeout_) {
  99. throw DL_ABORT_EX(EX_TIME_OUT);
  100. }
  101. return executeInternal();
  102. } catch(DownloadFailureException& err) {
  103. A2_LOG_ERROR_EX(EX_DOWNLOAD_ABORTED, err);
  104. onAbort();
  105. onFailure(err);
  106. return true;
  107. } catch(RecoverableException& err) {
  108. A2_LOG_DEBUG_EX(fmt(MSG_TORRENT_DOWNLOAD_ABORTED,
  109. getCuid()),
  110. err);
  111. A2_LOG_DEBUG(fmt(MSG_PEER_BANNED,
  112. getCuid(),
  113. peer_->getIPAddress().c_str(),
  114. peer_->getPort()));
  115. onAbort();
  116. return prepareForNextPeer(0);
  117. }
  118. }
  119. // TODO this method removed when PeerBalancerCommand is implemented
  120. bool PeerAbstractCommand::prepareForNextPeer(time_t wait)
  121. {
  122. return true;
  123. }
  124. void PeerAbstractCommand::disableReadCheckSocket()
  125. {
  126. if(checkSocketIsReadable_) {
  127. e_->deleteSocketForReadCheck(readCheckTarget_, this);
  128. checkSocketIsReadable_ = false;
  129. readCheckTarget_.reset();
  130. }
  131. }
  132. void PeerAbstractCommand::setReadCheckSocket(const SocketHandle& socket)
  133. {
  134. if(!socket->isOpen()) {
  135. disableReadCheckSocket();
  136. } else {
  137. if(checkSocketIsReadable_) {
  138. if(*readCheckTarget_ != *socket) {
  139. e_->deleteSocketForReadCheck(readCheckTarget_, this);
  140. e_->addSocketForReadCheck(socket, this);
  141. readCheckTarget_ = socket;
  142. }
  143. } else {
  144. e_->addSocketForReadCheck(socket, this);
  145. checkSocketIsReadable_ = true;
  146. readCheckTarget_ = socket;
  147. }
  148. }
  149. }
  150. void PeerAbstractCommand::disableWriteCheckSocket()
  151. {
  152. if(checkSocketIsWritable_) {
  153. e_->deleteSocketForWriteCheck(writeCheckTarget_, this);
  154. checkSocketIsWritable_ = false;
  155. writeCheckTarget_.reset();
  156. }
  157. }
  158. void PeerAbstractCommand::setWriteCheckSocket(const SocketHandle& socket)
  159. {
  160. if(!socket->isOpen()) {
  161. disableWriteCheckSocket();
  162. } else {
  163. if(checkSocketIsWritable_) {
  164. if(*writeCheckTarget_ != *socket) {
  165. e_->deleteSocketForWriteCheck(writeCheckTarget_, this);
  166. e_->addSocketForWriteCheck(socket, this);
  167. writeCheckTarget_ = socket;
  168. }
  169. } else {
  170. e_->addSocketForWriteCheck(socket, this);
  171. checkSocketIsWritable_ = true;
  172. writeCheckTarget_ = socket;
  173. }
  174. }
  175. }
  176. void PeerAbstractCommand::setNoCheck(bool check)
  177. {
  178. noCheck_ = check;
  179. }
  180. void PeerAbstractCommand::updateKeepAlive()
  181. {
  182. checkPoint_ = global::wallclock();
  183. }
  184. void PeerAbstractCommand::createSocket()
  185. {
  186. socket_.reset(new SocketCore());
  187. }
  188. } // namespace aria2