PeerAbstractCommand.cc 5.9 KB

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