PeerAbstractCommand.cc 6.1 KB

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