📄 btdocument.cpp
字号:
#include "BTDocument.h"
#include "BTSession.h"
#include "BTPeer.h"
#include "SimEventScheduler.h"
#include "Sysparameter.h"
#include "Debug.h"
#include "Statistic.h"
map<string, BTDocument*> BTDocument::mDocumentListBykey;
map<int, BTDocument*> BTDocument::mDocumentListByID;
int BTDocument::mNextAvailableId =0;
int BTDocument::getNextAvailableId(void){
return mNextAvailableId++;
};
BTDocument* BTDocument::getDocumentByKey(string key){
map<string, BTDocument*>::iterator aIt = mDocumentListBykey.find(key);
if( aIt != mDocumentListBykey.end())
return aIt->second;
else
return NULL;
}
BTDocument* BTDocument::getDocumentByID(int id){
map<int, BTDocument*>::iterator aIt = mDocumentListByID.find(id);
if( aIt != mDocumentListByID.end())
return aIt->second;
else
return NULL;
}
BTDocument::BTDocument(string key, unsigned int size, int popularity )
:mSize(size),
mkey(key),
mPopularity(popularity),
mState(empty),
mPieceLength( PIECE_LENTH ),
mBlockLength( BLOCK_LENTH ),
mSession(NULL)
{
mId = getNextAvailableId();
unsigned int num = mSize/mPieceLength;
unsigned int len = mSize%mPieceLength;
if( len){
mLastPieceLength = len;
mPieceNum = num+1;
}else{
mLastPieceLength = mPieceLength;
mPieceNum = num;
}
mPieces = new vector<bool>( mPieceNum );
for( unsigned int i =0; i< mPieceNum; i++)
(*mPieces)[i] = false;
if( mDocumentListBykey.find(key) == mDocumentListBykey.end())
mDocumentListBykey.insert( map<string, BTDocument*>::value_type( key, this));
mDocumentListByID.insert( map<int, BTDocument*>::value_type( mId, this));
mLeftPieceNum = mPieceNum;
}
BTDocument::~BTDocument( void){
delete mPieces;
}
void BTDocument::setSession( BTSession* session){
mSession = session;
}
bool BTDocument::finishPiece( unsigned int index){
if( !inPartialList(index) ||!inDownLoadingSet( index ))
return false;
DEBUG(DOCDUBLEVEL) printf("%f peer %d fininsh piece %u left piecenum %u \n", Scheduler.getCurrent(), mSession->getAgent()->getID(), index, mLeftPieceNum);
if( mState == empty)
mState = partial;
mLeftPieceNum--;
mDownloadingSet.erase(index);
mPartialList.erase(index);
(*mPieces)[index] =true;
bool finished = true;
for( unsigned int i = 0; i< mPieceNum; i++){
if( !(*mPieces)[i]){
finished = false;
break;
}
}
if( finished){
mState = whole;
statistic.addFileDownLoadTime(mkey, Scheduler.getCurrent() - mSession->mAgent->mStartTime);
DEBUG(DOCDUBLEVEL) printf("%f peer %d has finished download file%s, time %f, speed %f kB/s uploadnum %fM upbandwidth %fk now %d peers finished\n", Scheduler.getCurrent(), mSession->getAgent()->getID(), mkey.c_str(), Scheduler.getCurrent()-mSession->mStartTime, ((double)mSize/1024.0)/(Scheduler.getCurrent()-mSession->mStartTime), (double)mSession->mOutPutNum *16/1024.0, (double)mSession->mAgent->getOutBandwidth()/1024.0, finishPeerNum++);
}
return true;
};
bool BTDocument::addPieceProgress( unsigned int index, unsigned int addlen){
if( !inDownLoadingSet(index) || !inPartialList( index))
return false;
unsigned int len = getPieceProgress(index);
unsigned int offset = len + addlen;
if( offset > getPieceLength(index)){
finishPiece( index);
return true;
}
setPieceProgress(index, offset);
return true;
}
unsigned int BTDocument::getLeftSize(void){
unsigned int num = getLeftPieceNum();
if( (*mPieces)[mPieceNum -1])
return num*mPieceLength;
else
return (num-1)*mPieceLength + mLastPieceLength;
}
double BTDocument::getFinishedPercent(void){
return (double)(mSize - getLeftSize()) /(double)mSize;
}
void BTDocument::setWhole(void){
for(unsigned int i =0; i< mPieceNum; i++)
(*mPieces)[i] = true;
mState = whole;
mLeftPieceNum = 0;
};
unsigned int BTDocument::getPieceLength( unsigned int index){
if( index > mPieceNum -1)
return 0;
if( index == mPieceNum -1)
return mLastPieceLength;
else
return mPieceLength;
};
vector< unsigned int>* BTDocument::morePieces(vector < bool > * peerPieces){
vector< unsigned int>* piecelist = new vector< unsigned int>;
piecelist->clear();
for( unsigned int i = 0; i < mPieceNum; i++){
if((*peerPieces)[i] && mSession->pieceInKnowledge( i ))
if( !(*mPieces)[i])
piecelist->push_back(i);
}
return piecelist;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -