123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569 |
- /* <!-- copyright */
- /*
- * aria2 - a simple utility for downloading files faster
- *
- * 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
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- /* copyright --> */
- #include "TorrentMan.h"
- #include "Dictionary.h"
- #include "List.h"
- #include "ShaVisitor.h"
- #include "Util.h"
- #include "MetaFileUtil.h"
- #include "DlAbortEx.h"
- #include "File.h"
- #include "message.h"
- #include "PreAllocationDiskWriter.h"
- #include "DefaultDiskWriter.h"
- #include "MultiDiskWriter.h"
- #include "prefs.h"
- #include "CopyDiskAdaptor.h"
- #include "DirectDiskAdaptor.h"
- #include "MultiDiskAdaptor.h"
- #include "LogFactory.h"
- #include <errno.h>
- #include <libgen.h>
- #include <string.h>
- TorrentMan::TorrentMan():bitfield(NULL),
- peerEntryIdCounter(0), cuidCounter(0),
- downloadLength(0), uploadLength(0),
- preDownloadLength(0), preUploadLength(0),
- deltaDownloadLength(0), deltaUploadLength(0),
- storeDir("."),
- setupComplete(false),
- interval(DEFAULT_ANNOUNCE_INTERVAL),
- minInterval(DEFAULT_ANNOUNCE_MIN_INTERVAL),
- complete(0), incomplete(0),
- connections(0), diskAdaptor(NULL) {
- logger = LogFactory::getInstance();
- }
- TorrentMan::~TorrentMan() {
- if(bitfield != NULL) {
- delete bitfield;
- }
- for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
- delete *itr;
- }
- if(diskAdaptor != NULL) {
- delete diskAdaptor;
- }
- }
- // TODO do not use this method in application code
- void TorrentMan::updatePeers(const Peers& peers) {
- this->peers = peers;
- }
- bool TorrentMan::addPeer(Peer* peer, bool duplicate) {
- if(duplicate) {
- for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
- Peer* p = *itr;
- if(p->ipaddr == peer->ipaddr && p->port == peer->port && p->error > 0) {
- return false;
- }
- }
- } else {
- deleteOldErrorPeers(MAX_PEER_LIST_SIZE);
- for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
- Peer* p = *itr;
- if(p->ipaddr == peer->ipaddr && p->port == peer->port) {
- return false;
- }
- }
- }
- ++peerEntryIdCounter;
- peer->entryId = peerEntryIdCounter;
- peers.push_back(peer);
- return true;
- }
- /*
- void TorrentMan::updatePeer(const Peer& peer) {
- for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
- Peer& p = *itr;
- if(p.eid == peer.eid) {
- p = peer;
- break;
- }
- }
- }
- */
- bool TorrentMan::isPeerAvailable() const {
- return getPeer() != Peer::nullPeer;
- }
- int TorrentMan::deleteOldErrorPeers(int maxNum) {
- int counter = 0;
- for(Peers::iterator itr = peers.begin(); itr != peers.end();) {
- Peer* p = *itr;
- if(p->error >= MAX_PEER_ERROR && p->cuid == 0) {
- delete p;
- itr = peers.erase(itr);
- counter++;
- if(maxNum <= counter) {
- break;
- }
- } else {
- itr++;
- }
- }
- return counter;
- }
- Peer* TorrentMan::getPeer() const {
- for(Peers::const_iterator itr = peers.begin(); itr != peers.end(); itr++) {
- Peer* p = *itr;
- if(p->cuid == 0 && p->error < MAX_PEER_ERROR) {
- return p;
- }
- }
- return Peer::nullPeer;
- }
- bool TorrentMan::isEndGame() const {
- return bitfield->countMissingBlock() <= END_GAME_PIECE_NUM;
- }
- Piece TorrentMan::getMissingPiece(const Peer* peer) {
- int index = -1;
- if(isEndGame()) {
- index = bitfield->getMissingIndex(peer->getBitfield(), peer->getBitfieldLength());
- } else {
- index = bitfield->getMissingUnusedIndex(peer->getBitfield(), peer->getBitfieldLength());
- }
- if(index == -1) {
- return Piece::nullPiece;
- }
- bitfield->setUseBit(index);
- Piece piece = findUsedPiece(index);
- if(Piece::isNull(piece)) {
- Piece piece(index, bitfield->getBlockLength(index));
- addUsedPiece(piece);
- return piece;
- } else {
- return piece;
- }
- }
- int TorrentMan::deleteUsedPiecesByFillRate(int fillRate, int toDelete) {
- int deleted = 0;
- for(UsedPieces::iterator itr = usedPieces.begin();
- itr != usedPieces.end() && deleted < toDelete;) {
- Piece& piece = *itr;
- if(!bitfield->isUseBitSet(piece.getIndex()) &&
- piece.countCompleteBlock() <= piece.countBlock()*(fillRate/100.0)) {
- logger->debug("deleting used piece index=%d, fillRate(%%)=%d<=%d",
- piece.getIndex(),
- (piece.countCompleteBlock()*100)/piece.countBlock(),
- fillRate);
- itr = usedPieces.erase(itr);
- deleted++;
- } else {
- itr++;
- }
- }
- return deleted;
- }
- void TorrentMan::reduceUsedPieces(int max) {
- int toDelete = usedPieces.size()-max;
- if(toDelete <= 0) {
- return;
- }
- int fillRate = 10;
- while(fillRate < 50) {
- int deleted = deleteUsedPiecesByFillRate(fillRate, toDelete);
- if(deleted == 0) {
- break;
- }
- toDelete -= deleted;
- fillRate += 10;
- }
- }
- void TorrentMan::addUsedPiece(const Piece& piece) {
- usedPieces.push_back(piece);
- }
- Piece TorrentMan::findUsedPiece(int index) const {
- for(UsedPieces::const_iterator itr = usedPieces.begin(); itr != usedPieces.end(); itr++) {
- const Piece& piece = *itr;
- if(piece.getIndex() == index) {
- return piece;
- }
- }
- return Piece::nullPiece;
- }
- void TorrentMan::deleteUsedPiece(const Piece& piece) {
- if(Piece::isNull(piece)) {
- return;
- }
- for(UsedPieces::iterator itr = usedPieces.begin(); itr != usedPieces.end(); itr++) {
- if(itr->getIndex() == piece.getIndex()) {
- usedPieces.erase(itr);
- break;
- }
- }
- }
- void TorrentMan::completePiece(const Piece& piece) {
- if(Piece::isNull(piece)) {
- return;
- }
- if(!hasPiece(piece.getIndex())) {
- addDownloadLength(piece.getLength());
- }
- bitfield->setBit(piece.getIndex());
- bitfield->unsetUseBit(piece.getIndex());
- deleteUsedPiece(piece);
- if(!isEndGame()) {
- reduceUsedPieces(100);
- }
- }
- void TorrentMan::cancelPiece(const Piece& piece) {
- if(Piece::isNull(piece)) {
- return;
- }
- bitfield->unsetUseBit(piece.getIndex());
- if(!isEndGame()) {
- if(piece.countCompleteBlock() == 0) {
- deleteUsedPiece(piece);
- }
- }
- }
- void TorrentMan::updatePiece(const Piece& piece) {
- if(Piece::isNull(piece)) {
- return;
- }
- for(UsedPieces::iterator itr = usedPieces.begin(); itr != usedPieces.end(); itr++) {
- if(itr->getIndex() == piece.getIndex()) {
- *itr = piece;
- break;
- }
- }
- }
- void TorrentMan::syncPiece(Piece& piece) {
- if(Piece::isNull(piece)) {
- return;
- }
- for(UsedPieces::iterator itr = usedPieces.begin(); itr != usedPieces.end(); itr++) {
- if(itr->getIndex() == piece.getIndex()) {
- piece = *itr;
- return;
- }
- }
- // hasPiece(piece.getIndex()) is true, then set all bit of
- // piece.bitfield to 1
- if(hasPiece(piece.getIndex())) {
- piece.setAllBlock();
- }
- }
- void TorrentMan::initBitfield() {
- if(bitfield != NULL) {
- delete bitfield;
- }
- bitfield = new BitfieldMan(pieceLength, totalLength);
- }
- void TorrentMan::setBitfield(unsigned char* bitfield, int bitfieldLength) {
- if(this->bitfield == NULL) {
- initBitfield();
- }
- this->bitfield->setBitfield(bitfield, bitfieldLength);
- }
- bool TorrentMan::downloadComplete() const {
- return bitfield->isAllBitSet();
- }
- void TorrentMan::readFileEntry(FileEntries& fileEntries, Directory** pTopDir, const Dictionary* infoDic, const string& defaultName) {
- Data* topName = (Data*)infoDic->get("name");
- if(topName != NULL) {
- name = topName->toString();
- } else {
- char* basec = strdup(defaultName.c_str());
- name = string(basename(basec))+".file";
- free(basec);
- }
- List* files = (List*)infoDic->get("files");
- if(files == NULL) {
- // single-file mode;
- setFileMode(SINGLE);
- Data* length = (Data*)infoDic->get("length");
- totalLength = length->toLLInt();
- FileEntry fileEntry(name, totalLength, 0);
- fileEntries.push_back(fileEntry);
- } else {
- long long int length = 0;
- long long int offset = 0;
- // multi-file mode
- setFileMode(MULTI);
- *pTopDir = new Directory(name);
- const MetaList& metaList = files->getList();
- for(MetaList::const_iterator itr = metaList.begin(); itr != metaList.end();
- itr++) {
- Dictionary* fileDic = (Dictionary*)(*itr);
- Data* lengthData = (Data*)fileDic->get("length");
- length += lengthData->toLLInt();
- List* path = (List*)fileDic->get("path");
- const MetaList& paths = path->getList();
- Directory* parentDir = *pTopDir;
- string filePath = name;
- for(int i = 0; i < (int)paths.size()-1; i++) {
- Data* subpath = (Data*)paths.at(i);
- Directory* dir = new Directory(subpath->toString());
- parentDir->addFile(dir);
- parentDir = dir;
- filePath.append("/").append(subpath->toString());
- }
- Data* lastpath = (Data*)paths.back();
- filePath.append("/").append(lastpath->toString());
- FileEntry fileEntry(filePath, lengthData->toLLInt(), offset);
- fileEntries.push_back(fileEntry);
- offset += fileEntry.length;
- }
- totalLength = length;
- }
- }
- void TorrentMan::setup(string metaInfoFile, const Strings& targetFilePaths) {
- peerId = "-A2****-";
- for(int i = 0; i < 12; i++) {
- peerId += Util::itos((int)(((double)10)*random()/(RAND_MAX+1.0)));
- }
- uploadLength = 0;
- downloadLength = 0;
- Dictionary* topDic = (Dictionary*)MetaFileUtil::parseMetaFile(metaInfoFile);
- const Dictionary* infoDic = (const Dictionary*)topDic->get("info");
- ShaVisitor v;
- infoDic->accept(&v);
- unsigned char md[20];
- int len;
- v.getHash(md, len);
- setInfoHash(md);
- FileEntries fileEntries;
- Directory* topDir = NULL;
- readFileEntry(fileEntries, &topDir, infoDic, metaInfoFile);
- announce = ((Data*)topDic->get("announce"))->toString();
- pieceLength = ((Data*)infoDic->get("piece length"))->toInt();
- pieces = totalLength/pieceLength+(totalLength%pieceLength ? 1 : 0);
- Data* piecesHashData = (Data*)infoDic->get("pieces");
- if(piecesHashData->getLen() != pieces*20) {
- throw new DlAbortEx("the number of pieces is wrong.");
- }
- for(int index = 0; index < pieces; index++) {
- string hex = Util::toHex((unsigned char*)&piecesHashData->getData()[index*20], 20);
- pieceHashes.push_back(hex);
- logger->debug("piece #%d, hash:%s", index, hex.c_str());
- }
- initBitfield();
- delete topDic;
- if(option->get(PREF_DIRECT_FILE_MAPPING) == V_TRUE) {
- if(fileMode == SINGLE) {
- diskAdaptor = new DirectDiskAdaptor(new DefaultDiskWriter(totalLength));
- } else {
- diskAdaptor = new MultiDiskAdaptor(new MultiDiskWriter(pieceLength));
- }
- } else {
- diskAdaptor = new CopyDiskAdaptor(new DefaultDiskWriter(totalLength));
- ((CopyDiskAdaptor*)diskAdaptor)->setTempFilename(name+".a2tmp");
- }
- diskAdaptor->setStoreDir(storeDir);
- diskAdaptor->setTopDir(topDir);
- diskAdaptor->setFileEntries(fileEntries);
- setFileFilter(targetFilePaths);
- if(segmentFileExists()) {
- load();
- diskAdaptor->openExistingFile();
- } else {
- diskAdaptor->initAndOpenFile();
- }
- setupComplete = true;
- }
- void TorrentMan::setFileFilter(const Strings& filePaths) {
- if(fileMode != MULTI || filePaths.empty()) {
- return;
- }
- diskAdaptor->removeAllDownloadEntry();
- for(Strings::const_iterator pitr = filePaths.begin();
- pitr != filePaths.end(); pitr++) {
- if(!diskAdaptor->addDownloadEntry(*pitr)) {
- throw new DlAbortEx("no such file entry <%s>", (*pitr).c_str());
- }
- FileEntry fileEntry = diskAdaptor->getFileEntryFromPath(*pitr);
- bitfield->addFilter(fileEntry.offset, fileEntry.length);
- }
- bitfield->enableFilter();
- }
- FileEntries TorrentMan::readFileEntryFromMetaInfoFile(const string& metaInfoFile) {
- Dictionary* topDic = (Dictionary*)MetaFileUtil::parseMetaFile(metaInfoFile);
- const Dictionary* infoDic = (const Dictionary*)topDic->get("info");
- FileEntries fileEntries;
- Directory* topDir = NULL;
- readFileEntry(fileEntries, &topDir, infoDic, metaInfoFile);
- if(topDir != NULL) {
- delete topDir;
- }
- return fileEntries;
- }
- string TorrentMan::getName() const {
- return name;
- }
- bool TorrentMan::hasPiece(int index) const {
- return bitfield->isBitSet(index);
- }
- string TorrentMan::getPieceHash(int index) const {
- return pieceHashes.at(index);
- }
- string TorrentMan::getSegmentFilePath() const {
- return storeDir+"/"+name+".aria2";
- }
- bool TorrentMan::segmentFileExists() const {
- string segFilename = getSegmentFilePath();
- File f(segFilename);
- if(f.isFile()) {
- logger->info(MSG_SEGMENT_FILE_EXISTS, segFilename.c_str());
- return true;
- } else {
- logger->info(MSG_SEGMENT_FILE_DOES_NOT_EXIST, segFilename.c_str());
- return false;
- }
- }
- FILE* TorrentMan::openSegFile(string segFilename, string mode) const {
- FILE* segFile = fopen(segFilename.c_str(), mode.c_str());
- if(segFile == NULL) {
- throw new DlAbortEx(strerror(errno));
- }
- return segFile;
- }
- void TorrentMan::load() {
- string segFilename = getSegmentFilePath();
- logger->info(MSG_LOADING_SEGMENT_FILE, segFilename.c_str());
- FILE* segFile = openSegFile(segFilename, "r+");
- read(segFile);
- fclose(segFile);
- logger->info(MSG_LOADED_SEGMENT_FILE);
- }
- void TorrentMan::read(FILE* file) {
- assert(file != NULL);
- unsigned char savedInfoHash[INFO_HASH_LENGTH];
- if(fread(savedInfoHash, INFO_HASH_LENGTH, 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- if(Util::toHex(savedInfoHash, INFO_HASH_LENGTH) != Util::toHex(infoHash, INFO_HASH_LENGTH)) {
- throw new DlAbortEx("info hash mismatch");
- }
- unsigned char* savedBitfield = new unsigned char[bitfield->getBitfieldLength()];
- try {
- if(fread(savedBitfield, bitfield->getBitfieldLength(), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- setBitfield(savedBitfield, bitfield->getBitfieldLength());
- if(fread(&downloadLength, sizeof(downloadLength), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- if(fread(&uploadLength, sizeof(uploadLength), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- preDownloadLength = downloadLength;
- preUploadLength = uploadLength;
- delete [] savedBitfield;
- } catch(Exception* ex) {
- delete [] savedBitfield;
- throw;
- }
- }
- void TorrentMan::save() const {
- if(!setupComplete) {
- return;
- }
- string segFilename = getSegmentFilePath();
- logger->info(MSG_SAVING_SEGMENT_FILE, segFilename.c_str());
- FILE* file = openSegFile(segFilename, "w");
- if(fwrite(infoHash, INFO_HASH_LENGTH, 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- if(fwrite(bitfield->getBitfield(), bitfield->getBitfieldLength(), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- if(fwrite(&downloadLength, sizeof(downloadLength), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- if(fwrite(&uploadLength, sizeof(uploadLength), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- fclose(file);
- logger->info(MSG_SAVED_SEGMENT_FILE);
- }
- void TorrentMan::remove() const {
- if(segmentFileExists()) {
- File f(getSegmentFilePath());
- f.remove();
- }
- }
- bool TorrentMan::isSelectiveDownloadingMode() const {
- return bitfield->isFilterEnabled();
- }
- void TorrentMan::finishSelectiveDownloadingMode() {
- bitfield->clearFilter();
- diskAdaptor->addAllDownloadEntry();
- }
- long long int TorrentMan::getCompletedLength() const {
- return bitfield->getCompletedLength();
- }
- long long int TorrentMan::getSelectedTotalLength() const {
- return bitfield->getFilteredTotalLength();
- }
- void TorrentMan::onDownloadComplete() {
- save();
- diskAdaptor->onDownloadComplete();
- if(isSelectiveDownloadingMode()) {
- finishSelectiveDownloadingMode();
- }
- }
|