PeerAbstractCommand.cc 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/* <!-- copyright */
/*
 * aria2 - The high speed download utility
 *
 * Copyright (C) 2006 Tatsuhiro Tsujikawa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
/* copyright --> */
#include "PeerAbstractCommand.h"
#include "Peer.h"
#include "DownloadEngine.h"
#include "Option.h"
#include "DlAbortEx.h"
40
#include "SocketCore.h"
41 42 43 44 45 46 47 48 49 50 51
#include "Logger.h"
#include "LogFactory.h"
#include "message.h"
#include "prefs.h"
#include "DownloadFailureException.h"
#include "fmt.h"
#include "wallclock.h"
#include "util.h"

namespace aria2 {

52 53 54 55 56 57 58 59 60 61 62 63 64 65
PeerAbstractCommand::PeerAbstractCommand(cuid_t cuid,
                                         const std::shared_ptr<Peer>& peer,
                                         DownloadEngine* e,
                                         const std::shared_ptr<SocketCore>& s)
    : Command(cuid),
      checkPoint_(global::wallclock()),
      // TODO referring global option
      timeout_(std::chrono::seconds(e->getOption()->getAsInt(PREF_BT_TIMEOUT))),
      e_(e),
      socket_(s),
      peer_(peer),
      checkSocketIsReadable_(false),
      checkSocketIsWritable_(false),
      noCheck_(false)
66
{
67
  if (socket_ && socket_->isOpen()) {
68 69 70 71 72 73 74 75 76 77 78 79
    setReadCheckSocket(socket_);
  }
}

PeerAbstractCommand::~PeerAbstractCommand()
{
  disableReadCheckSocket();
  disableWriteCheckSocket();
}

bool PeerAbstractCommand::execute()
{
80
  A2_LOG_DEBUG(fmt("CUID#%" PRId64 " -"
81
                   " socket: read:%d, write:%d, hup:%d, err:%d, noCheck:%d",
82 83 84
                   getCuid(), readEventEnabled(), writeEventEnabled(),
                   hupEventEnabled(), errorEventEnabled(), noCheck_));
  if (exitBeforeExecute()) {
85 86 87 88
    onAbort();
    return true;
  }
  try {
89 90
    if (noCheck_ || (checkSocketIsReadable_ && readEventEnabled()) ||
        (checkSocketIsWritable_ && writeEventEnabled()) || hupEventEnabled()) {
91 92
      checkPoint_ = global::wallclock();
    }
93 94 95 96 97
    else if (errorEventEnabled()) {
      throw DL_ABORT_EX(
          fmt(MSG_NETWORK_PROBLEM, socket_->getSocketError().c_str()));
    }
    if (checkPoint_.difference(global::wallclock()) >= timeout_) {
98 99 100
      throw DL_ABORT_EX(EX_TIME_OUT);
    }
    return executeInternal();
101 102
  }
  catch (DownloadFailureException& err) {
103 104 105 106
    A2_LOG_ERROR_EX(EX_DOWNLOAD_ABORTED, err);
    onAbort();
    onFailure(err);
    return true;
107 108 109 110
  }
  catch (RecoverableException& err) {
    A2_LOG_DEBUG_EX(fmt(MSG_TORRENT_DOWNLOAD_ABORTED, getCuid()), err);
    A2_LOG_DEBUG(fmt(MSG_PEER_BANNED, getCuid(), peer_->getIPAddress().c_str(),
111 112 113 114 115 116 117
                     peer_->getPort()));
    onAbort();
    return prepareForNextPeer(0);
  }
}

// TODO this method removed when PeerBalancerCommand is implemented
118
bool PeerAbstractCommand::prepareForNextPeer(time_t wait) { return true; }
119 120 121

void PeerAbstractCommand::disableReadCheckSocket()
{
122
  if (checkSocketIsReadable_) {
123 124 125
    e_->deleteSocketForReadCheck(readCheckTarget_, this);
    checkSocketIsReadable_ = false;
    readCheckTarget_.reset();
126
  }
127 128
}

129 130
void PeerAbstractCommand::setReadCheckSocket(
    const std::shared_ptr<SocketCore>& socket)
131
{
132
  if (!socket->isOpen()) {
133
    disableReadCheckSocket();
134 135 136 137
  }
  else {
    if (checkSocketIsReadable_) {
      if (*readCheckTarget_ != *socket) {
138 139 140 141
        e_->deleteSocketForReadCheck(readCheckTarget_, this);
        e_->addSocketForReadCheck(socket, this);
        readCheckTarget_ = socket;
      }
142 143
    }
    else {
144 145 146 147 148 149 150 151 152
      e_->addSocketForReadCheck(socket, this);
      checkSocketIsReadable_ = true;
      readCheckTarget_ = socket;
    }
  }
}

void PeerAbstractCommand::disableWriteCheckSocket()
{
153
  if (checkSocketIsWritable_) {
154 155 156 157 158 159
    e_->deleteSocketForWriteCheck(writeCheckTarget_, this);
    checkSocketIsWritable_ = false;
    writeCheckTarget_.reset();
  }
}

160 161
void PeerAbstractCommand::setWriteCheckSocket(
    const std::shared_ptr<SocketCore>& socket)
162
{
163
  if (!socket->isOpen()) {
164
    disableWriteCheckSocket();
165 166 167 168
  }
  else {
    if (checkSocketIsWritable_) {
      if (*writeCheckTarget_ != *socket) {
169 170 171 172
        e_->deleteSocketForWriteCheck(writeCheckTarget_, this);
        e_->addSocketForWriteCheck(socket, this);
        writeCheckTarget_ = socket;
      }
173 174
    }
    else {
175 176 177 178 179 180 181
      e_->addSocketForWriteCheck(socket, this);
      checkSocketIsWritable_ = true;
      writeCheckTarget_ = socket;
    }
  }
}

182
void PeerAbstractCommand::setNoCheck(bool check) { noCheck_ = check; }
183 184 185 186 187 188 189 190

void PeerAbstractCommand::updateKeepAlive()
{
  checkPoint_ = global::wallclock();
}

void PeerAbstractCommand::createSocket()
{
191
  socket_ = std::make_shared<SocketCore>();
192 193
}

194 195 196 197 198
void PeerAbstractCommand::addCommandSelf()
{
  e_->addCommand(std::unique_ptr<Command>(this));
}

199
} // namespace aria2