2008-02-09 17:14:40 +00:00
|
|
|
/* <!-- 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
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2008-02-09 17:14:40 +00:00
|
|
|
*
|
|
|
|
* 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 "PeerSessionResource.h"
|
2008-11-03 07:16:25 +00:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2008-02-09 17:14:40 +00:00
|
|
|
#include "BitfieldMan.h"
|
2008-05-13 14:15:23 +00:00
|
|
|
#include "A2STR.h"
|
2008-11-03 07:16:25 +00:00
|
|
|
#include "BtMessageDispatcher.h"
|
2010-03-06 14:30:05 +00:00
|
|
|
#include "wallclock.h"
|
2008-02-09 17:14:40 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
2012-06-25 14:35:24 +00:00
|
|
|
PeerSessionResource::PeerSessionResource(int32_t pieceLength, int64_t totalLength)
|
2011-12-07 15:03:25 +00:00
|
|
|
:
|
2012-10-07 13:14:06 +00:00
|
|
|
bitfieldMan_(new BitfieldMan(pieceLength, totalLength)),
|
|
|
|
lastDownloadUpdate_(0),
|
|
|
|
lastAmUnchoking_(0),
|
2013-08-20 16:57:17 +00:00
|
|
|
dispatcher_(nullptr),
|
2010-06-21 13:51:56 +00:00
|
|
|
amChoking_(true),
|
|
|
|
amInterested_(false),
|
|
|
|
peerChoking_(true),
|
|
|
|
peerInterested_(false),
|
|
|
|
chokingRequired_(true),
|
|
|
|
optUnchoking_(false),
|
|
|
|
snubbing_(false),
|
|
|
|
fastExtensionEnabled_(false),
|
|
|
|
extendedMessagingEnabled_(false),
|
2012-10-07 13:14:06 +00:00
|
|
|
dhtEnabled_(false)
|
2008-02-09 17:14:40 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
PeerSessionResource::~PeerSessionResource()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
delete bitfieldMan_;
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::amChoking(bool b)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
amChoking_ = b;
|
2008-04-13 01:25:36 +00:00
|
|
|
if(!b) {
|
2011-08-09 16:17:28 +00:00
|
|
|
lastAmUnchoking_ = global::wallclock();
|
2008-04-13 01:25:36 +00:00
|
|
|
}
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::amInterested(bool b)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
amInterested_ = b;
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::peerChoking(bool b)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
peerChoking_ = b;
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::peerInterested(bool b)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
peerInterested_ = b;
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
2012-10-01 14:52:22 +00:00
|
|
|
|
2008-02-09 17:14:40 +00:00
|
|
|
void PeerSessionResource::chokingRequired(bool b)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
chokingRequired_ = b;
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::optUnchoking(bool b)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
optUnchoking_ = b;
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PeerSessionResource::shouldBeChoking() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(optUnchoking_) {
|
2008-02-09 17:14:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
return chokingRequired_;
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::snubbing(bool b)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
snubbing_ = b;
|
|
|
|
if(snubbing_) {
|
2008-04-13 01:25:36 +00:00
|
|
|
chokingRequired(true);
|
|
|
|
optUnchoking(false);
|
|
|
|
}
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PeerSessionResource::hasAllPieces() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfieldMan_->isAllBitSet();
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
void PeerSessionResource::updateBitfield(size_t index, int operation)
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
|
|
|
if(operation == 1) {
|
2010-06-21 13:51:56 +00:00
|
|
|
bitfieldMan_->setBit(index);
|
2008-02-09 17:14:40 +00:00
|
|
|
} else if(operation == 0) {
|
2010-06-21 13:51:56 +00:00
|
|
|
bitfieldMan_->unsetBit(index);
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-07 15:03:25 +00:00
|
|
|
void PeerSessionResource::setBitfield
|
|
|
|
(const unsigned char* bitfield, size_t bitfieldLength)
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
bitfieldMan_->setBitfield(bitfield, bitfieldLength);
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned char* PeerSessionResource::getBitfield() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfieldMan_->getBitfield();
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t PeerSessionResource::getBitfieldLength() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfieldMan_->getBitfieldLength();
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
bool PeerSessionResource::hasPiece(size_t index) const
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfieldMan_->isBitSet(index);
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::markSeeder()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
bitfieldMan_->setAllBit();
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::fastExtensionEnabled(bool b)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
fastExtensionEnabled_ = b;
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 16:34:37 +00:00
|
|
|
const std::set<size_t>& PeerSessionResource::peerAllowedIndexSet() const
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return peerAllowedIndexSet_;
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
void PeerSessionResource::addPeerAllowedIndex(size_t index)
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2012-03-22 16:34:37 +00:00
|
|
|
peerAllowedIndexSet_.insert(index);
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
bool PeerSessionResource::peerAllowedIndexSetContains(size_t index) const
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2012-03-22 16:34:37 +00:00
|
|
|
return peerAllowedIndexSet_.count(index) == 1;
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
void PeerSessionResource::addAmAllowedIndex(size_t index)
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2012-03-22 16:34:37 +00:00
|
|
|
amAllowedIndexSet_.insert(index);
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
bool PeerSessionResource::amAllowedIndexSetContains(size_t index) const
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2012-03-22 16:34:37 +00:00
|
|
|
return amAllowedIndexSet_.count(index) == 1;
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::extendedMessagingEnabled(bool b)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
extendedMessagingEnabled_ = b;
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2012-09-26 13:02:48 +00:00
|
|
|
uint8_t PeerSessionResource::getExtensionMessageID(int key) const
|
|
|
|
{
|
|
|
|
return extreg_.getExtensionMessageID(key);
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2012-09-26 13:02:48 +00:00
|
|
|
const char* PeerSessionResource::getExtensionName(uint8_t id) const
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2012-09-26 13:02:48 +00:00
|
|
|
return extreg_.getExtensionName(id);
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2012-09-26 13:02:48 +00:00
|
|
|
void PeerSessionResource::addExtension(int key, uint8_t id)
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2012-09-26 13:02:48 +00:00
|
|
|
extreg_.setExtensionMessageID(key, id);
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::dhtEnabled(bool b)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
dhtEnabled_ = b;
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2012-06-25 14:35:24 +00:00
|
|
|
int64_t PeerSessionResource::uploadLength() const
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2012-10-25 14:33:45 +00:00
|
|
|
return netStat_.getSessionUploadLength();
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2011-12-07 15:03:25 +00:00
|
|
|
void PeerSessionResource::updateUploadLength(int32_t bytes)
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2012-10-25 14:33:45 +00:00
|
|
|
netStat_.updateUploadLength(bytes);
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2012-06-25 14:35:24 +00:00
|
|
|
int64_t PeerSessionResource::downloadLength() const
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2012-10-25 14:33:45 +00:00
|
|
|
return netStat_.getSessionDownloadLength();
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2011-12-07 15:03:25 +00:00
|
|
|
void PeerSessionResource::updateDownloadLength(int32_t bytes)
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2012-10-25 14:33:45 +00:00
|
|
|
netStat_.updateDownloadLength(bytes);
|
2011-08-09 16:17:28 +00:00
|
|
|
lastDownloadUpdate_ = global::wallclock();
|
2008-04-13 01:25:36 +00:00
|
|
|
}
|
|
|
|
|
2012-06-25 14:35:24 +00:00
|
|
|
int64_t PeerSessionResource::getCompletedLength() const
|
2008-05-17 05:12:14 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfieldMan_->getCompletedLength();
|
2008-05-17 05:12:14 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 12:48:48 +00:00
|
|
|
void PeerSessionResource::setBtMessageDispatcher(BtMessageDispatcher* dpt)
|
2008-11-03 07:16:25 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
dispatcher_ = dpt;
|
2008-11-03 07:16:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t PeerSessionResource::countOutstandingUpload() const
|
|
|
|
{
|
2010-11-12 12:48:48 +00:00
|
|
|
assert(dispatcher_);
|
2010-06-21 13:51:56 +00:00
|
|
|
return dispatcher_->countOutstandingUpload();
|
2008-11-03 07:16:25 +00:00
|
|
|
}
|
|
|
|
|
2012-06-25 14:35:24 +00:00
|
|
|
void PeerSessionResource::reconfigure(int32_t pieceLength, int64_t totalLenth)
|
2010-06-23 11:55:23 +00:00
|
|
|
{
|
|
|
|
delete bitfieldMan_;
|
|
|
|
bitfieldMan_ = new BitfieldMan(pieceLength, totalLenth);
|
|
|
|
}
|
|
|
|
|
2008-02-09 17:14:40 +00:00
|
|
|
} // namespace aria2
|