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