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 {
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
PeerSessionResource::PeerSessionResource(size_t pieceLength, uint64_t totalLength):
|
2010-06-21 13:51:56 +00:00
|
|
|
amChoking_(true),
|
|
|
|
amInterested_(false),
|
|
|
|
peerChoking_(true),
|
|
|
|
peerInterested_(false),
|
|
|
|
chokingRequired_(true),
|
|
|
|
optUnchoking_(false),
|
|
|
|
snubbing_(false),
|
|
|
|
bitfieldMan_(new BitfieldMan(pieceLength, totalLength)),
|
|
|
|
fastExtensionEnabled_(false),
|
|
|
|
extendedMessagingEnabled_(false),
|
|
|
|
dhtEnabled_(false),
|
|
|
|
lastDownloadUpdate_(0),
|
|
|
|
lastAmUnchoking_(0)
|
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) {
|
2010-06-21 13:51:56 +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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::setBitfield(const unsigned char* bitfield, size_t bitfieldLength)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
const std::vector<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
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
static void updateIndexSet(std::vector<size_t>& c, size_t index)
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<size_t>::iterator i = std::lower_bound(c.begin(), c.end(), index);
|
2008-05-16 14:51:14 +00:00
|
|
|
if(i == c.end() || (*i) != index) {
|
|
|
|
c.insert(i, index);
|
|
|
|
}
|
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
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
updateIndexSet(peerAllowedIndexSet_, 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
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return std::binary_search(peerAllowedIndexSet_.begin(),
|
|
|
|
peerAllowedIndexSet_.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
index);
|
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
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
updateIndexSet(amAllowedIndexSet_, 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
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return std::binary_search(amAllowedIndexSet_.begin(),
|
|
|
|
amAllowedIndexSet_.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
index);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t
|
|
|
|
PeerSessionResource::getExtensionMessageID(const std::string& name) const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
Extensions::const_iterator itr = extensions_.find(name);
|
|
|
|
if(itr == extensions_.end()) {
|
2008-02-09 17:14:40 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return (*itr).second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PeerSessionResource::getExtensionName(uint8_t id) const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
for(Extensions::const_iterator itr = extensions_.begin(),
|
|
|
|
eoi = extensions_.end(); itr != eoi; ++itr) {
|
2008-02-09 17:14:40 +00:00
|
|
|
const Extensions::value_type& p = *itr;
|
|
|
|
if(p.second == id) {
|
|
|
|
return p.first;
|
|
|
|
}
|
|
|
|
}
|
2008-05-13 14:15:23 +00:00
|
|
|
return A2STR::NIL;
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PeerSessionResource::addExtension(const std::string& name, uint8_t id)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
extensions_[name] = 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
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
uint64_t PeerSessionResource::uploadLength() const
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return peerStat_.getSessionUploadLength();
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
void PeerSessionResource::updateUploadLength(size_t bytes)
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
peerStat_.updateUploadLength(bytes);
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
uint64_t PeerSessionResource::downloadLength() const
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return peerStat_.getSessionDownloadLength();
|
2008-02-09 17:14:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-08 11:10:37 +00:00
|
|
|
void PeerSessionResource::updateDownloadLength(size_t bytes)
|
2008-02-09 17:14:40 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
peerStat_.updateDownloadLength(bytes);
|
2008-04-13 01:25:36 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
lastDownloadUpdate_ = global::wallclock;
|
2008-04-13 01:25:36 +00:00
|
|
|
}
|
|
|
|
|
2008-05-17 05:12:14 +00:00
|
|
|
uint64_t PeerSessionResource::getCompletedLength() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return bitfieldMan_->getCompletedLength();
|
2008-05-17 05:12:14 +00:00
|
|
|
}
|
|
|
|
|
2008-11-03 07:16:25 +00:00
|
|
|
void PeerSessionResource::setBtMessageDispatcher
|
|
|
|
(const WeakHandle<BtMessageDispatcher>& dpt)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
dispatcher_ = dpt;
|
2008-11-03 07:16:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t PeerSessionResource::countOutstandingUpload() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
assert(!dispatcher_.isNull());
|
|
|
|
return dispatcher_->countOutstandingUpload();
|
2008-11-03 07:16:25 +00:00
|
|
|
}
|
|
|
|
|
2010-06-23 11:55:23 +00:00
|
|
|
void PeerSessionResource::reconfigure(size_t pieceLength, uint64_t totalLenth)
|
|
|
|
{
|
|
|
|
delete bitfieldMan_;
|
|
|
|
bitfieldMan_ = new BitfieldMan(pieceLength, totalLenth);
|
|
|
|
}
|
|
|
|
|
2008-02-09 17:14:40 +00:00
|
|
|
} // namespace aria2
|