PeerAbstractCommand.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "PeerAbstractCommand.h"
  23. #include "DlAbortEx.h"
  24. #include "DlRetryEx.h"
  25. #include "Util.h"
  26. #include "message.h"
  27. #include "prefs.h"
  28. PeerAbstractCommand::PeerAbstractCommand(int cuid, Peer* peer, TorrentDownloadEngine* e, const Socket* s):
  29. Command(cuid), e(e), peer(peer),
  30. checkSocketIsReadable(false), checkSocketIsWritable(false),
  31. uploadLimitCheck(false), uploadLimit(0) {
  32. if(s != NULL) {
  33. socket = new Socket(*s);
  34. setReadCheckSocket(socket);
  35. } else {
  36. socket = NULL;
  37. }
  38. timeout = e->option->getAsInt(PREF_TIMEOUT);
  39. e->torrentMan->connections++;
  40. }
  41. PeerAbstractCommand::~PeerAbstractCommand() {
  42. setReadCheckSocket(NULL);
  43. setWriteCheckSocket(NULL);
  44. if(socket != NULL) {
  45. delete(socket);
  46. }
  47. e->torrentMan->connections--;
  48. }
  49. bool PeerAbstractCommand::execute() {
  50. if(e->torrentMan->isHalt()) {
  51. return true;
  52. }
  53. try {
  54. if(uploadLimitCheck && (uploadLimit == 0 ||
  55. e->getUploadSpeed() <= uploadLimit*1024) ||
  56. checkSocketIsReadable && readCheckTarget->isReadable(0) ||
  57. checkSocketIsWritable && writeCheckTarget->isWritable(0)) {
  58. checkPoint.reset();
  59. }
  60. if(checkPoint.elapsed(timeout)) {
  61. throw new DlRetryEx(EX_TIME_OUT);
  62. }
  63. return executeInternal();
  64. } catch(Exception* err) {
  65. logger->error(MSG_DOWNLOAD_ABORTED, err, cuid);
  66. onAbort(err);
  67. delete(err);
  68. return prepareForNextPeer(0);
  69. }
  70. }
  71. // TODO this method removed when PeerBalancerCommand is implemented
  72. bool PeerAbstractCommand::prepareForNextPeer(int wait) {
  73. return true;
  74. }
  75. bool PeerAbstractCommand::prepareForRetry(int wait) {
  76. return true;
  77. }
  78. void PeerAbstractCommand::onAbort(Exception* ex) {
  79. if(peer->isSeeder()) {
  80. peer->error++;
  81. } else {
  82. peer->error += MAX_PEER_ERROR;
  83. }
  84. peer->resetStatus();
  85. logger->debug("CUID#%d - Peer %s:%d banned.", cuid, peer->ipaddr.c_str(), peer->port);
  86. }
  87. void PeerAbstractCommand::setReadCheckSocket(Socket* socket) {
  88. if(socket == NULL) {
  89. if(checkSocketIsReadable) {
  90. e->deleteSocketForReadCheck(readCheckTarget);
  91. checkSocketIsReadable = false;
  92. readCheckTarget = NULL;
  93. }
  94. } else {
  95. if(checkSocketIsReadable) {
  96. if(readCheckTarget != socket) {
  97. e->deleteSocketForReadCheck(readCheckTarget);
  98. e->addSocketForReadCheck(socket, this);
  99. readCheckTarget = socket;
  100. }
  101. } else {
  102. e->addSocketForReadCheck(socket, this);
  103. checkSocketIsReadable = true;
  104. readCheckTarget = socket;
  105. }
  106. }
  107. }
  108. void PeerAbstractCommand::setWriteCheckSocket(Socket* socket) {
  109. if(socket == NULL) {
  110. if(checkSocketIsWritable) {
  111. e->deleteSocketForWriteCheck(writeCheckTarget);
  112. checkSocketIsWritable = false;
  113. writeCheckTarget = NULL;
  114. }
  115. } else {
  116. if(checkSocketIsWritable) {
  117. if(writeCheckTarget != socket) {
  118. e->deleteSocketForWriteCheck(writeCheckTarget);
  119. e->addSocketForWriteCheck(socket, this);
  120. writeCheckTarget = socket;
  121. }
  122. } else {
  123. e->addSocketForWriteCheck(socket, this);
  124. checkSocketIsWritable = true;
  125. writeCheckTarget = socket;
  126. }
  127. }
  128. }
  129. void PeerAbstractCommand::setUploadLimit(int uploadLimit) {
  130. this->uploadLimit = uploadLimit;
  131. }
  132. void PeerAbstractCommand::setUploadLimitCheck(bool check) {
  133. this->uploadLimitCheck = check;
  134. }