|
@@ -58,7 +58,9 @@
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
|
-DefaultBtContext::DefaultBtContext():_peerIdPrefix("-aria2-"),
|
|
|
+const std::string DefaultBtContext::DEFAULT_PEER_ID_PREFIX("-aria2-");
|
|
|
+
|
|
|
+DefaultBtContext::DefaultBtContext():_peerIdPrefix(DEFAULT_PEER_ID_PREFIX),
|
|
|
_randomizer(SimpleRandomizer::getInstance()),
|
|
|
_ownerRequestGroup(0),
|
|
|
_logger(LogFactory::getInstance()) {}
|
|
@@ -113,13 +115,13 @@ void DefaultBtContext::extractPieceHash(const unsigned char* hashData,
|
|
|
void DefaultBtContext::extractFileEntries(const Dictionary* infoDic,
|
|
|
const std::string& defaultName,
|
|
|
const std::deque<std::string>& urlList) {
|
|
|
- const Data* nameData = dynamic_cast<const Data*>(infoDic->get("name"));
|
|
|
+ const Data* nameData = dynamic_cast<const Data*>(infoDic->get(BtContext::NAME));
|
|
|
if(nameData) {
|
|
|
name = nameData->toString();
|
|
|
} else {
|
|
|
name = File(defaultName).getBasename()+".file";
|
|
|
}
|
|
|
- const List* files = dynamic_cast<const List*>(infoDic->get("files"));
|
|
|
+ const List* files = dynamic_cast<const List*>(infoDic->get(BtContext::FILES));
|
|
|
if(files) {
|
|
|
uint64_t length = 0;
|
|
|
off_t offset = 0;
|
|
@@ -132,14 +134,16 @@ void DefaultBtContext::extractFileEntries(const Dictionary* infoDic,
|
|
|
if(!fileDic) {
|
|
|
continue;
|
|
|
}
|
|
|
- const Data* lengthData = dynamic_cast<const Data*>(fileDic->get("length"));
|
|
|
+ const Data* lengthData =
|
|
|
+ dynamic_cast<const Data*>(fileDic->get(BtContext::LENGTH));
|
|
|
if(lengthData) {
|
|
|
length += lengthData->toLLInt();
|
|
|
} else {
|
|
|
throw DlAbortEx
|
|
|
(StringFormat(MSG_SOMETHING_MISSING_IN_TORRENT, "file length").str());
|
|
|
}
|
|
|
- const List* pathList = dynamic_cast<const List*>(fileDic->get("path"));
|
|
|
+ const List* pathList =
|
|
|
+ dynamic_cast<const List*>(fileDic->get(BtContext::PATH));
|
|
|
if(!pathList) {
|
|
|
throw DlAbortEx
|
|
|
(StringFormat(MSG_SOMETHING_MISSING_IN_TORRENT, "file path list").str());
|
|
@@ -177,7 +181,8 @@ void DefaultBtContext::extractFileEntries(const Dictionary* infoDic,
|
|
|
} else {
|
|
|
// single-file mode;
|
|
|
fileMode = BtContext::SINGLE;
|
|
|
- const Data* length = dynamic_cast<const Data*>(infoDic->get("length"));
|
|
|
+ const Data* length =
|
|
|
+ dynamic_cast<const Data*>(infoDic->get(BtContext::LENGTH));
|
|
|
if(length) {
|
|
|
totalLength = length->toLLInt();
|
|
|
} else {
|
|
@@ -290,7 +295,8 @@ void DefaultBtContext::load(const std::string& torrentFile) {
|
|
|
void DefaultBtContext::processRootDictionary(const Dictionary* rootDic, const std::string& defaultName)
|
|
|
{
|
|
|
clear();
|
|
|
- const Dictionary* infoDic = dynamic_cast<const Dictionary*>(rootDic->get("info"));
|
|
|
+ const Dictionary* infoDic =
|
|
|
+ dynamic_cast<const Dictionary*>(rootDic->get(BtContext::INFO));
|
|
|
if(!infoDic) {
|
|
|
throw DlAbortEx
|
|
|
(StringFormat(MSG_SOMETHING_MISSING_IN_TORRENT, "info directory").str());
|
|
@@ -303,7 +309,8 @@ void DefaultBtContext::processRootDictionary(const Dictionary* rootDic, const st
|
|
|
v.getBencodedData().size());
|
|
|
infoHashString = Util::toHex(infoHash, INFO_HASH_LENGTH);
|
|
|
// calculate the number of pieces
|
|
|
- const Data* pieceHashData = dynamic_cast<const Data*>(infoDic->get("pieces"));
|
|
|
+ const Data* pieceHashData =
|
|
|
+ dynamic_cast<const Data*>(infoDic->get(BtContext::PIECES));
|
|
|
if(!pieceHashData) {
|
|
|
throw DlAbortEx
|
|
|
(StringFormat(MSG_SOMETHING_MISSING_IN_TORRENT, "pieces").str());
|
|
@@ -316,7 +323,8 @@ void DefaultBtContext::processRootDictionary(const Dictionary* rootDic, const st
|
|
|
throw DlAbortEx("The number of pieces is 0.");
|
|
|
}
|
|
|
// retrieve piece length
|
|
|
- const Data* pieceLengthData = dynamic_cast<const Data*>(infoDic->get("piece length"));
|
|
|
+ const Data* pieceLengthData =
|
|
|
+ dynamic_cast<const Data*>(infoDic->get(BtContext::PIECE_LENGTH));
|
|
|
if(!pieceLengthData) {
|
|
|
throw DlAbortEx
|
|
|
(StringFormat(MSG_SOMETHING_MISSING_IN_TORRENT, "piece length").str());
|
|
@@ -325,9 +333,10 @@ void DefaultBtContext::processRootDictionary(const Dictionary* rootDic, const st
|
|
|
// retrieve piece hashes
|
|
|
extractPieceHash(pieceHashData->getData(), pieceHashData->getLen(),
|
|
|
PIECE_HASH_LENGTH);
|
|
|
- const Data* privateFlag = dynamic_cast<const Data*>(infoDic->get("private"));
|
|
|
+ const Data* privateFlag =
|
|
|
+ dynamic_cast<const Data*>(infoDic->get(BtContext::PRIVATE));
|
|
|
if(privateFlag) {
|
|
|
- if(privateFlag->toString() == "1") {
|
|
|
+ if(privateFlag->toString() == BtContext::PRIVATE_ON) {
|
|
|
_private = true;
|
|
|
}
|
|
|
}
|
|
@@ -335,22 +344,25 @@ void DefaultBtContext::processRootDictionary(const Dictionary* rootDic, const st
|
|
|
// This implemantation obeys HTTP-Seeding specification:
|
|
|
// see http://www.getright.com/seedtorrent.html
|
|
|
std::deque<std::string> urlList;
|
|
|
- extractUrlList(urlList, rootDic->get("url-list"));
|
|
|
+ extractUrlList(urlList, rootDic->get(BtContext::URL_LIST));
|
|
|
// retrieve file entries
|
|
|
extractFileEntries(infoDic, defaultName, urlList);
|
|
|
if((totalLength+pieceLength-1)/pieceLength != numPieces) {
|
|
|
throw DlAbortEx("Too few/many piece hash.");
|
|
|
}
|
|
|
// retrieve announce
|
|
|
- const Data* announceData = dynamic_cast<const Data*>(rootDic->get("announce"));
|
|
|
- const List* announceListData = dynamic_cast<const List*>(rootDic->get("announce-list"));
|
|
|
+ const Data* announceData =
|
|
|
+ dynamic_cast<const Data*>(rootDic->get(BtContext::ANNOUNCE));
|
|
|
+ const List* announceListData =
|
|
|
+ dynamic_cast<const List*>(rootDic->get(BtContext::ANNOUNCE_LIST));
|
|
|
if(announceListData) {
|
|
|
extractAnnounceList(announceListData);
|
|
|
} else if(announceData) {
|
|
|
extractAnnounce(announceData);
|
|
|
}
|
|
|
// retrieve nodes
|
|
|
- const List* nodes = dynamic_cast<const List*>(rootDic->get("nodes"));
|
|
|
+ const List* nodes =
|
|
|
+ dynamic_cast<const List*>(rootDic->get(BtContext::NODES));
|
|
|
if(nodes) {
|
|
|
extractNodes(nodes);
|
|
|
}
|