/* */ #include "PeerSessionResource.h" #include #include #include "BitfieldMan.h" #include "A2STR.h" #include "BtMessageDispatcher.h" #include "wallclock.h" namespace aria2 { PeerSessionResource::PeerSessionResource(size_t pieceLength, uint64_t totalLength): 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) {} PeerSessionResource::~PeerSessionResource() { delete bitfieldMan_; } void PeerSessionResource::amChoking(bool b) { amChoking_ = b; if(!b) { lastAmUnchoking_ = global::wallclock; } } void PeerSessionResource::amInterested(bool b) { amInterested_ = b; } void PeerSessionResource::peerChoking(bool b) { peerChoking_ = b; } void PeerSessionResource::peerInterested(bool b) { peerInterested_ = b; } void PeerSessionResource::chokingRequired(bool b) { chokingRequired_ = b; } void PeerSessionResource::optUnchoking(bool b) { optUnchoking_ = b; } bool PeerSessionResource::shouldBeChoking() const { if(optUnchoking_) { return false; } return chokingRequired_; } void PeerSessionResource::snubbing(bool b) { snubbing_ = b; if(snubbing_) { chokingRequired(true); optUnchoking(false); } } bool PeerSessionResource::hasAllPieces() const { return bitfieldMan_->isAllBitSet(); } void PeerSessionResource::updateBitfield(size_t index, int operation) { if(operation == 1) { bitfieldMan_->setBit(index); } else if(operation == 0) { bitfieldMan_->unsetBit(index); } } void PeerSessionResource::setBitfield(const unsigned char* bitfield, size_t bitfieldLength) { bitfieldMan_->setBitfield(bitfield, bitfieldLength); } const unsigned char* PeerSessionResource::getBitfield() const { return bitfieldMan_->getBitfield(); } size_t PeerSessionResource::getBitfieldLength() const { return bitfieldMan_->getBitfieldLength(); } bool PeerSessionResource::hasPiece(size_t index) const { return bitfieldMan_->isBitSet(index); } void PeerSessionResource::markSeeder() { bitfieldMan_->setAllBit(); } void PeerSessionResource::fastExtensionEnabled(bool b) { fastExtensionEnabled_ = b; } const std::vector& PeerSessionResource::peerAllowedIndexSet() const { return peerAllowedIndexSet_; } static void updateIndexSet(std::vector& c, size_t index) { std::vector::iterator i = std::lower_bound(c.begin(), c.end(), index); if(i == c.end() || (*i) != index) { c.insert(i, index); } } void PeerSessionResource::addPeerAllowedIndex(size_t index) { updateIndexSet(peerAllowedIndexSet_, index); } bool PeerSessionResource::peerAllowedIndexSetContains(size_t index) const { return std::binary_search(peerAllowedIndexSet_.begin(), peerAllowedIndexSet_.end(), index); } void PeerSessionResource::addAmAllowedIndex(size_t index) { updateIndexSet(amAllowedIndexSet_, index); } bool PeerSessionResource::amAllowedIndexSetContains(size_t index) const { return std::binary_search(amAllowedIndexSet_.begin(), amAllowedIndexSet_.end(), index); } void PeerSessionResource::extendedMessagingEnabled(bool b) { extendedMessagingEnabled_ = b; } uint8_t PeerSessionResource::getExtensionMessageID(const std::string& name) const { Extensions::const_iterator itr = extensions_.find(name); if(itr == extensions_.end()) { return 0; } else { return (*itr).second; } } std::string PeerSessionResource::getExtensionName(uint8_t id) const { for(Extensions::const_iterator itr = extensions_.begin(), eoi = extensions_.end(); itr != eoi; ++itr) { const Extensions::value_type& p = *itr; if(p.second == id) { return p.first; } } return A2STR::NIL; } void PeerSessionResource::addExtension(const std::string& name, uint8_t id) { extensions_[name] = id; } void PeerSessionResource::dhtEnabled(bool b) { dhtEnabled_ = b; } uint64_t PeerSessionResource::uploadLength() const { return peerStat_.getSessionUploadLength(); } void PeerSessionResource::updateUploadLength(size_t bytes) { peerStat_.updateUploadLength(bytes); } uint64_t PeerSessionResource::downloadLength() const { return peerStat_.getSessionDownloadLength(); } void PeerSessionResource::updateDownloadLength(size_t bytes) { peerStat_.updateDownloadLength(bytes); lastDownloadUpdate_ = global::wallclock; } uint64_t PeerSessionResource::getCompletedLength() const { return bitfieldMan_->getCompletedLength(); } void PeerSessionResource::setBtMessageDispatcher (const WeakHandle& dpt) { dispatcher_ = dpt; } size_t PeerSessionResource::countOutstandingUpload() const { assert(!dispatcher_.isNull()); return dispatcher_->countOutstandingUpload(); } void PeerSessionResource::reconfigure(size_t pieceLength, uint64_t totalLenth) { delete bitfieldMan_; bitfieldMan_ = new BitfieldMan(pieceLength, totalLenth); } } // namespace aria2