diff --git a/src/BtDependency.cc b/src/BtDependency.cc index 967fa845..a952feae 100644 --- a/src/BtDependency.cc +++ b/src/BtDependency.cc @@ -100,7 +100,7 @@ bool BtDependency::resolve() dependee->getPieceStorage()->getDiskAdaptor(); diskAdaptor->openExistingFile(); std::string content = util::toString(diskAdaptor); - if(dependee->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)) { + if(dependee->getDownloadContext()->hasAttribute(CTX_ATTR_BT)) { SharedHandle attrs = bittorrent::getTorrentAttrs(dependee->getDownloadContext()); bittorrent::loadFromMemory diff --git a/src/BtSetup.cc b/src/BtSetup.cc index d49b5985..cd975762 100644 --- a/src/BtSetup.cc +++ b/src/BtSetup.cc @@ -94,7 +94,7 @@ void BtSetup::setup(std::vector& commands, DownloadEngine* e, const Option* option) { - if(!requestGroup->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)){ + if(!requestGroup->getDownloadContext()->hasAttribute(CTX_ATTR_BT)){ return; } SharedHandle torrentAttrs = diff --git a/src/ConsoleStatCalc.cc b/src/ConsoleStatCalc.cc index 735a6a84..95e569e5 100644 --- a/src/ConsoleStatCalc.cc +++ b/src/ConsoleStatCalc.cc @@ -110,7 +110,7 @@ void printProgress << "#" << rg->getGID() << " "; #ifdef ENABLE_BITTORRENT - if(rg->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT) && + if(rg->getDownloadContext()->hasAttribute(CTX_ATTR_BT) && !bittorrent::getTorrentAttrs(rg->getDownloadContext())->metadata.empty() && rg->downloadFinished()) { o << "SEEDING" << "(" << "ratio:"; diff --git a/src/ContextAttribute.cc b/src/ContextAttribute.cc new file mode 100644 index 00000000..b106903e --- /dev/null +++ b/src/ContextAttribute.cc @@ -0,0 +1,49 @@ +/* */ +#include "ContextAttribute.h" + +namespace aria2 { + +const char* strContextAttributeType(ContextAttributeType key) +{ + switch(key) { + case CTX_ATTR_BT: + return "BitTorrent"; + default: + return "UNKNOWN"; + } +} + +} // namespace aria2 diff --git a/src/ContextAttribute.h b/src/ContextAttribute.h index 02bda128..10b9f129 100644 --- a/src/ContextAttribute.h +++ b/src/ContextAttribute.h @@ -43,6 +43,17 @@ struct ContextAttribute { virtual ~ContextAttribute() {} }; +enum ContextAttributeType { + // For BitTorrent + CTX_ATTR_BT, + // Max value of attribute type to use allocate vector to hold + // attributes. + MAX_CTX_ATTR +}; + +// Returns human readable string representation of type |key| +const char* strContextAttributeType(ContextAttributeType key); + } // namespace aria2 #endif // D_CONTEXT_ATTRIBUTE_H diff --git a/src/DefaultPieceStorage.cc b/src/DefaultPieceStorage.cc index 9dcb154b..a59a20a1 100644 --- a/src/DefaultPieceStorage.cc +++ b/src/DefaultPieceStorage.cc @@ -465,7 +465,7 @@ void DefaultPieceStorage::completePiece(const SharedHandle& piece) A2_LOG_INFO(MSG_DOWNLOAD_COMPLETED); } #ifdef ENABLE_BITTORRENT - if(downloadContext_->hasAttribute(bittorrent::BITTORRENT)) { + if(downloadContext_->hasAttribute(CTX_ATTR_BT)) { SharedHandle torrentAttrs = bittorrent::getTorrentAttrs(downloadContext_); if(!torrentAttrs->metadata.empty()) { diff --git a/src/DownloadCommand.cc b/src/DownloadCommand.cc index 45671be6..2ed595b5 100644 --- a/src/DownloadCommand.cc +++ b/src/DownloadCommand.cc @@ -227,7 +227,7 @@ bool DownloadCommand::executeInternal() { if( #ifdef ENABLE_BITTORRENT (!getPieceStorage()->isEndGame() || - !getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)) && + !getDownloadContext()->hasAttribute(CTX_ATTR_BT)) && #endif // ENABLE_BITTORRENT segment->isHashCalculated()) { A2_LOG_DEBUG(fmt("Hash is available! index=%lu", diff --git a/src/DownloadContext.cc b/src/DownloadContext.cc index 10c03ca6..16ddd159 100644 --- a/src/DownloadContext.cc +++ b/src/DownloadContext.cc @@ -43,7 +43,6 @@ #include "DlAbortEx.h" #include "a2functional.h" #include "Signature.h" -#include "ContextAttribute.h" namespace aria2 { @@ -52,6 +51,7 @@ DownloadContext::DownloadContext(): checksumVerified_(false), knowsTotalLength_(true), ownerRequestGroup_(0), + attrs_(MAX_CTX_ATTR), downloadStartTime_(0), downloadStopTime_(downloadStartTime_), metalinkServerContacted_(false) {} @@ -63,6 +63,7 @@ DownloadContext::DownloadContext(int32_t pieceLength, checksumVerified_(false), knowsTotalLength_(true), ownerRequestGroup_(0), + attrs_(MAX_CTX_ATTR), downloadStartTime_(0), downloadStopTime_(0), metalinkServerContacted_(false) @@ -151,32 +152,29 @@ void DownloadContext::setFileFilter(SegList& sgl) } void DownloadContext::setAttribute -(const std::string& key, const SharedHandle& value) +(ContextAttributeType key, const SharedHandle& value) { - std::map >::value_type p = - std::make_pair(key, value); - std::pair >::iterator, - bool> r = attrs_.insert(p); - if(!r.second) { - (*r.first).second = value; - } + assert(key < MAX_CTX_ATTR); + attrs_[key] = value; } const SharedHandle& DownloadContext::getAttribute -(const std::string& key) +(ContextAttributeType key) { - std::map >::const_iterator itr = - attrs_.find(key); - if(itr == attrs_.end()) { - throw DL_ABORT_EX(fmt("No attribute named %s", key.c_str())); + assert(key < MAX_CTX_ATTR); + const SharedHandle& attr = attrs_[key]; + if(attr) { + return attr; } else { - return (*itr).second; + throw DL_ABORT_EX(fmt("No attribute named %s", + strContextAttributeType(key))); } } -bool DownloadContext::hasAttribute(const std::string& key) const +bool DownloadContext::hasAttribute(ContextAttributeType key) const { - return attrs_.count(key) == 1; + assert(key < MAX_CTX_ATTR); + return attrs_[key]; } void DownloadContext::releaseRuntimeResource() diff --git a/src/DownloadContext.h b/src/DownloadContext.h index 810671bd..29d4840a 100644 --- a/src/DownloadContext.h +++ b/src/DownloadContext.h @@ -46,13 +46,13 @@ #include "A2STR.h" #include "ValueBase.h" #include "SegList.h" +#include "ContextAttribute.h" namespace aria2 { class RequestGroup; class Signature; class FileEntry; -struct ContextAttribute; class DownloadContext { @@ -77,7 +77,7 @@ private: RequestGroup* ownerRequestGroup_; - std::map > attrs_; + std::vector > attrs_; Timer downloadStartTime_; @@ -203,11 +203,11 @@ public: } void setAttribute - (const std::string& key, const SharedHandle& value); + (ContextAttributeType key, const SharedHandle& value); - const SharedHandle& getAttribute(const std::string& key); + const SharedHandle& getAttribute(ContextAttributeType key); - bool hasAttribute(const std::string& key) const; + bool hasAttribute(ContextAttributeType key) const; void resetDownloadStartTime(); diff --git a/src/Makefile.am b/src/Makefile.am index b04d78c6..a4a88790 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -189,7 +189,7 @@ SRCS = Socket.h\ Event.h\ timespec.h\ ValueBase.cc ValueBase.h\ - ContextAttribute.h\ + ContextAttribute.cc ContextAttribute.h\ TorrentAttribute.cc TorrentAttribute.h\ AdaptiveFileAllocationIterator.cc AdaptiveFileAllocationIterator.h\ TruncFileAllocationIterator.cc TruncFileAllocationIterator.h\ diff --git a/src/RequestGroup.cc b/src/RequestGroup.cc index 4f55ee2e..6b95beab 100644 --- a/src/RequestGroup.cc +++ b/src/RequestGroup.cc @@ -283,7 +283,7 @@ void RequestGroup::createInitialCommand downloadContext_->resetDownloadStartTime(); #ifdef ENABLE_BITTORRENT { - if(downloadContext_->hasAttribute(bittorrent::BITTORRENT)) { + if(downloadContext_->hasAttribute(CTX_ATTR_BT)) { SharedHandle torrentAttrs = bittorrent::getTorrentAttrs(downloadContext_); bool metadataGetMode = torrentAttrs->metadata.empty(); @@ -584,14 +584,14 @@ void RequestGroup::initPieceStorage() // content-length = 0. Google's dl server used this before. (downloadContext_->getTotalLength() > 0 #ifdef ENABLE_BITTORRENT - || downloadContext_->hasAttribute(bittorrent::BITTORRENT) + || downloadContext_->hasAttribute(CTX_ATTR_BT) #endif // ENABLE_BITTORRENT )) { #ifdef ENABLE_BITTORRENT DefaultPieceStorage* ps = new DefaultPieceStorage(downloadContext_, option_.get()); SharedHandle psHolder(ps); - if(downloadContext_->hasAttribute(bittorrent::BITTORRENT)) { + if(downloadContext_->hasAttribute(CTX_ATTR_BT)) { if(isUriSuppliedForRequsetFileEntry (downloadContext_->getFileEntries().begin(), downloadContext_->getFileEntries().end())) { @@ -1179,7 +1179,7 @@ DownloadResultHandle RequestGroup::createDownloadResult() const } } #ifdef ENABLE_BITTORRENT - if(downloadContext_->hasAttribute(bittorrent::BITTORRENT)) { + if(downloadContext_->hasAttribute(CTX_ATTR_BT)) { const unsigned char* p = bittorrent::getInfoHash(downloadContext_); res->infoHash.assign(p, p+INFO_HASH_LENGTH); } @@ -1196,7 +1196,7 @@ void RequestGroup::reportDownloadFinished() downloadContext_->getBasePath().c_str())); uriSelector_->resetCounters(); #ifdef ENABLE_BITTORRENT - if(downloadContext_->hasAttribute(bittorrent::BITTORRENT)) { + if(downloadContext_->hasAttribute(CTX_ATTR_BT)) { TransferStat stat = calculateStat(); int64_t completedLength = getCompletedLength(); double shareRatio = completedLength == 0 ? 0.0 : @@ -1309,7 +1309,7 @@ void RequestGroup::setDownloadContext bool RequestGroup::p2pInvolved() const { #ifdef ENABLE_BITTORRENT - return downloadContext_->hasAttribute(bittorrent::BITTORRENT); + return downloadContext_->hasAttribute(CTX_ATTR_BT); #else // !ENABLE_BITTORRENT return false; #endif // !ENABLE_BITTORRENT diff --git a/src/RequestGroupMan.cc b/src/RequestGroupMan.cc index 19262793..4206aac9 100644 --- a/src/RequestGroupMan.cc +++ b/src/RequestGroupMan.cc @@ -423,7 +423,7 @@ public: // we don't remove it. if(group->getOption()->getAsBool(PREF_BT_REMOVE_UNSELECTED_FILE) && !group->inMemoryDownload() && - dctx->hasAttribute(bittorrent::BITTORRENT)) { + dctx->hasAttribute(CTX_ATTR_BT)) { A2_LOG_INFO(fmt(MSG_REMOVING_UNSELECTED_FILE, group->getGID())); const std::vector >& files = dctx->getFileEntries(); diff --git a/src/RpcMethodImpl.cc b/src/RpcMethodImpl.cc index ee421812..32c362e3 100644 --- a/src/RpcMethodImpl.cc +++ b/src/RpcMethodImpl.cc @@ -801,7 +801,7 @@ void gatherProgress { gatherProgressCommon(entryDict, group, keys); #ifdef ENABLE_BITTORRENT - if(group->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)) { + if(group->getDownloadContext()->hasAttribute(CTX_ATTR_BT)) { SharedHandle torrentAttrs = bittorrent::getTorrentAttrs(group->getDownloadContext()); const SharedHandle& btObject = @@ -1117,7 +1117,7 @@ void changeOption if(option.defined(PREF_DIR) || option.defined(PREF_OUT)) { if(dctx->getFileEntries().size() == 1 #ifdef ENABLE_BITTORRENT - && !dctx->hasAttribute(bittorrent::BITTORRENT) + && !dctx->hasAttribute(CTX_ATTR_BT) #endif // ENABLE_BITTORRENT ) { dctx->getFirstFileEntry()->setPath @@ -1127,7 +1127,7 @@ void changeOption } #ifdef ENABLE_BITTORRENT if(option.defined(PREF_DIR) || option.defined(PREF_INDEX_OUT)) { - if(dctx->hasAttribute(bittorrent::BITTORRENT)) { + if(dctx->hasAttribute(CTX_ATTR_BT)) { std::istringstream indexOutIn(grOption->get(PREF_INDEX_OUT)); std::vector > indexPaths = util::createIndexPaths(indexOutIn); diff --git a/src/UTMetadataPostDownloadHandler.cc b/src/UTMetadataPostDownloadHandler.cc index 13623fc1..0557a1b1 100644 --- a/src/UTMetadataPostDownloadHandler.cc +++ b/src/UTMetadataPostDownloadHandler.cc @@ -58,7 +58,7 @@ bool UTMetadataPostDownloadHandler::Criteria::match { const SharedHandle& dctx = requestGroup->getDownloadContext(); - if(dctx->hasAttribute(bittorrent::BITTORRENT)) { + if(dctx->hasAttribute(CTX_ATTR_BT)) { SharedHandle attrs = bittorrent::getTorrentAttrs(dctx); if(attrs->metadata.empty()) { return true; diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index ea7f1c5b..e47e4143 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -107,8 +107,6 @@ const std::string C_CREATED_BY("created by"); const std::string DEFAULT_PEER_ID_PREFIX("aria2-"); } // namespace -const std::string BITTORRENT("bittorrent"); - const std::string MULTI("multi"); const std::string SINGLE("single"); @@ -515,7 +513,7 @@ void processRootDictionary torrent->createdBy = util::encodeNonUtf8(createdBy->s()); } - ctx->setAttribute(BITTORRENT, torrent); + ctx->setAttribute(CTX_ATTR_BT, torrent); } } // namespace @@ -626,7 +624,7 @@ void loadFromMemory(const SharedHandle& torrent, SharedHandle getTorrentAttrs (const SharedHandle& dctx) { - return static_pointer_cast(dctx->getAttribute(BITTORRENT)); + return static_pointer_cast(dctx->getAttribute(CTX_ATTR_BT)); } const unsigned char* @@ -943,7 +941,7 @@ void loadMagnet (const std::string& magnet, const SharedHandle& dctx) { SharedHandle attrs = parseMagnet(magnet); - dctx->setAttribute(BITTORRENT, attrs); + dctx->setAttribute(CTX_ATTR_BT, attrs); } std::string metadata2Torrent diff --git a/src/bittorrent_helper.h b/src/bittorrent_helper.h index 5f4f17fe..7690d205 100644 --- a/src/bittorrent_helper.h +++ b/src/bittorrent_helper.h @@ -64,9 +64,6 @@ extern const std::string SINGLE; extern const std::string MULTI; -extern const std::string BITTORRENT; - - void load(const std::string& torrentFile, const SharedHandle& ctx, const SharedHandle