2008-07-29 11:18:27 -04:00
|
|
|
/*
|
|
|
|
* libretroshare/src/ft: fttransfermodule.cc
|
|
|
|
*
|
|
|
|
* File Transfer for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2008 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
2008-09-08 04:44:37 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fttransfermodule.h"
|
|
|
|
|
2008-09-08 10:04:10 -04:00
|
|
|
ftTransferModule::ftTransferModule(ftFileCreator *fc, ftDataMultiplex *dm, ftController *fc)
|
|
|
|
:mFileCreator(fc), mMultiplexor(dm), mFtController(fc), mFlag(0)
|
2008-09-08 04:44:37 -04:00
|
|
|
{
|
|
|
|
mHash = mFileCreator->getHash();
|
|
|
|
mSize = mFileCreator->getFileSize();
|
|
|
|
|
|
|
|
// Dummy for Testing (should be handled independantly for
|
|
|
|
// each peer.
|
|
|
|
//mChunkSize = 10000;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-29 11:18:27 -04:00
|
|
|
ftTransferModule::~ftTransferModule()
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool ftTransferModule::setFileSources(std::list<std::string> peerIds)
|
|
|
|
{
|
2008-09-08 10:04:10 -04:00
|
|
|
mFileSources.clear();
|
|
|
|
|
2008-07-29 11:18:27 -04:00
|
|
|
std::list<std::string>::iterator it;
|
|
|
|
for(it = peerIds.begin(); it != peerIds.end(); it++)
|
|
|
|
{
|
2008-09-08 10:04:10 -04:00
|
|
|
peerInfo pInfo;
|
|
|
|
mFileSources.insert(pair<std::string,peerInfo>(*it,pInfo));
|
2008-07-29 11:18:27 -04:00
|
|
|
}
|
|
|
|
|
2008-09-08 10:04:10 -04:00
|
|
|
return true;
|
2008-07-29 11:18:27 -04:00
|
|
|
}
|
|
|
|
|
2008-09-09 03:24:06 -04:00
|
|
|
bool ftTransferModule::setPeerState(std::string peerId,uint32_t state,uint32_t maxRate)
|
2008-07-29 11:18:27 -04:00
|
|
|
{
|
2008-09-09 03:24:06 -04:00
|
|
|
#ifdef FT_DEBUG
|
|
|
|
std::cerr << "ftTransferModule::setPeerState()";
|
|
|
|
std::cerr << " peerId: " << peerId;
|
|
|
|
std::cerr << " state: " << state;
|
|
|
|
std::cerr << " maxRate: " << maxRate << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-09-08 10:04:10 -04:00
|
|
|
std::map<std::string,peerInfo>::iterator mit;
|
|
|
|
mit = mFileSources.find(peerId);
|
2008-07-29 11:18:27 -04:00
|
|
|
|
2008-09-08 10:04:10 -04:00
|
|
|
if (mit == mFileSources.end()) return false;
|
2008-07-29 11:18:27 -04:00
|
|
|
|
2008-09-08 10:04:10 -04:00
|
|
|
(mit->second).state=state;
|
|
|
|
(mit->second).desiredRate=maxRate;
|
2008-07-29 11:18:27 -04:00
|
|
|
|
2008-09-09 03:24:06 -04:00
|
|
|
std::list<std::string>::iterator it;
|
|
|
|
it=mOnlinePeers.begin();
|
|
|
|
while((it!=mOnlinePeers.end())&&(*it!=peerId)) it++;
|
|
|
|
|
|
|
|
if (state!=PQIPEER_NOT_ONLINE)
|
|
|
|
{
|
|
|
|
//change to online, add peerId in online peer list
|
|
|
|
if (it==mOnlinePeers.end()) mOnlinePeers.push_back(peerId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//change to offline, remove peerId in online peer list
|
|
|
|
if (it!=mOnlinePeers.end()) mOnlinePeers.erase(it);
|
|
|
|
}
|
2008-09-08 10:04:10 -04:00
|
|
|
|
|
|
|
return true;
|
2008-07-29 11:18:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ftTransferModule::getDataRate(std::string peerId)
|
|
|
|
{
|
|
|
|
std::map<std::string,peerInfo>::iterator mit;
|
2008-09-08 10:04:10 -04:00
|
|
|
mit = mFileSources.find(peerId);
|
|
|
|
if (mit == mFileSources.end())
|
|
|
|
{
|
|
|
|
#ifdef FT_DEBUG
|
|
|
|
std::cerr << "ftTransferModule::getDataRate()";
|
|
|
|
std::cerr << " peerId: " << peerId;
|
|
|
|
std::cerr << " peer not exist in file sources " << std::endl;
|
|
|
|
#endif
|
2008-07-29 11:18:27 -04:00
|
|
|
return 0;
|
2008-09-08 10:04:10 -04:00
|
|
|
}
|
2008-07-29 11:18:27 -04:00
|
|
|
else
|
2008-08-17 11:23:11 -04:00
|
|
|
return (uint32_t) (mit->second).actualRate;
|
2008-07-29 11:18:27 -04:00
|
|
|
}
|
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
|
|
|
|
//interface to client module
|
|
|
|
bool ftTransferModule::recvFileData(std::string peerId, uint64_t offset,
|
|
|
|
uint32_t chunk_size, void *data)
|
|
|
|
{
|
|
|
|
#ifdef FT_DEBUG
|
|
|
|
std::cerr << "ftTransferModule::recvFileData()";
|
|
|
|
std::cerr << " peerId: " << peerId;
|
|
|
|
std::cerr << " offset: " << offset;
|
|
|
|
std::cerr << " chunksize: " << chunk_size;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-09-08 10:04:10 -04:00
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
std::map<std::string,peerInfo>::iterator mit;
|
2008-09-08 10:04:10 -04:00
|
|
|
mit = mFileSources.find(peerId);
|
|
|
|
|
|
|
|
if (mit == mFileSources.end())
|
2008-09-08 04:44:37 -04:00
|
|
|
return false;
|
2008-09-08 10:04:10 -04:00
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
if ((mit->second).state != PQIPEER_DOWNLOADING)
|
|
|
|
return false;
|
2008-09-08 10:04:10 -04:00
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
if (offset != ((mit->second).offset + (mit->second).receivedSize))
|
2008-09-08 10:04:10 -04:00
|
|
|
{
|
|
|
|
//fix me
|
|
|
|
//received data not expected
|
2008-09-08 04:44:37 -04:00
|
|
|
return false;
|
2008-09-08 10:04:10 -04:00
|
|
|
}
|
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
(mit->second).receivedSize += chunk_size;
|
|
|
|
(mit->second).state = PQIPEER_IDLE;
|
|
|
|
|
2008-09-08 10:04:10 -04:00
|
|
|
return storeData(offset, chunk_size, data);
|
2008-09-08 04:44:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ftTransferModule::requestData(std::string peerId, uint64_t offset, uint32_t chunk_size)
|
|
|
|
{
|
2008-09-08 10:04:10 -04:00
|
|
|
#ifdef FT_DEBUG
|
2008-09-08 04:44:37 -04:00
|
|
|
std::cerr << "ftTransferModule::requestData()";
|
|
|
|
std::cerr << " peerId: " << peerId;
|
2008-09-08 10:04:10 -04:00
|
|
|
std::cerr << " hash: " << mHash;
|
|
|
|
std::cerr << " size: " << mSize;
|
2008-09-08 04:44:37 -04:00
|
|
|
std::cerr << " offset: " << offset;
|
|
|
|
std::cerr << " chunk_size: " << chunk_size;
|
|
|
|
std::cerr << std::endl;
|
2008-09-08 10:04:10 -04:00
|
|
|
#endif
|
2008-09-08 04:44:37 -04:00
|
|
|
|
|
|
|
mMultiplexor->sendDataRequest(peerId, mHash, mSize, offset,chunk_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ftTransferModule::getChunk(uint64_t &offset, uint32_t &chunk_size)
|
|
|
|
{
|
2008-09-08 10:04:10 -04:00
|
|
|
#ifdef FT_DEBUG
|
2008-09-08 04:44:37 -04:00
|
|
|
std::cerr << "ftTransferModule::getChunk()";
|
2008-09-08 10:04:10 -04:00
|
|
|
std::cerr << " hash: " << mHash;
|
|
|
|
std::cerr << " size: " << mSize;
|
|
|
|
std::cerr << " offset: " << offset;
|
2008-09-08 04:44:37 -04:00
|
|
|
std::cerr << " chunk_size: " << chunk_size;
|
|
|
|
std::cerr << std::endl;
|
2008-09-08 10:04:10 -04:00
|
|
|
#endif
|
2008-09-08 04:44:37 -04:00
|
|
|
|
|
|
|
bool val = mFileCreator->getMissingChunk(offset, chunk_size);
|
|
|
|
|
2008-09-08 10:04:10 -04:00
|
|
|
#ifdef FT_DEBUG
|
2008-09-08 04:44:37 -04:00
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
std::cerr << "ftTransferModule::getChunk()";
|
2008-09-08 10:04:10 -04:00
|
|
|
std::cerr << " Answer: Chunk Available";
|
|
|
|
std::cerr << " hash: " << mHash;
|
|
|
|
std::cerr << " size: " << mSize;
|
|
|
|
std::cerr << " offset: " << offset;
|
2008-09-08 04:44:37 -04:00
|
|
|
std::cerr << " chunk_size: " << chunk_size;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "ftTransferModule::getChunk()";
|
|
|
|
std::cerr << " Answer: No Chunk Available";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
2008-09-08 10:04:10 -04:00
|
|
|
#endif
|
2008-09-08 04:44:37 -04:00
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ftTransferModule::storeData(uint64_t offset, uint32_t chunk_size,void *data)
|
|
|
|
{
|
2008-09-08 10:04:10 -04:00
|
|
|
#ifdef FT_DEBUG
|
2008-09-08 04:44:37 -04:00
|
|
|
std::cerr << "ftTransferModule::storeData()";
|
2008-09-08 10:04:10 -04:00
|
|
|
std::cerr << " hash: " << mHash;
|
|
|
|
std::cerr << " size: " << mSize;
|
2008-09-08 04:44:37 -04:00
|
|
|
std::cerr << " offset: " << offset;
|
|
|
|
std::cerr << " chunk_size: " << chunk_size;
|
|
|
|
std::cerr << std::endl;
|
2008-09-08 10:04:10 -04:00
|
|
|
#endif
|
2008-09-08 04:44:37 -04:00
|
|
|
|
|
|
|
return mFileCreator -> addFileData(offset, chunk_size, data);
|
|
|
|
}
|
|
|
|
|
2008-09-09 03:24:06 -04:00
|
|
|
bool ftTransferModule::queryInactive()
|
2008-07-29 11:18:27 -04:00
|
|
|
{
|
|
|
|
#ifdef FT_DEBUG
|
2008-09-08 10:04:10 -04:00
|
|
|
std::cerr << "ftTransferModule::queryInactive()" << std::endl;
|
2008-07-29 11:18:27 -04:00
|
|
|
#endif
|
2008-09-08 10:04:10 -04:00
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
if (mFileStatus.stat == ftFileStatus::PQIFILE_INIT)
|
|
|
|
mFileStatus.stat = ftFileStatus::PQIFILE_DOWNLOADING;
|
|
|
|
|
|
|
|
if (mFileStatus.stat != ftFileStatus::PQIFILE_DOWNLOADING)
|
2008-09-09 03:24:06 -04:00
|
|
|
{
|
|
|
|
if (mFileStatus.stat == ftFileStatus::PQIFILE_FAIL_CANCEL)
|
|
|
|
mFlag = 2; //file canceled by user
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-08 04:44:37 -04:00
|
|
|
|
|
|
|
int ts = time(NULL);
|
2008-09-08 10:04:10 -04:00
|
|
|
uint64_t req_offset;
|
|
|
|
uint32_t req_size;
|
2008-09-08 04:44:37 -04:00
|
|
|
int delta;
|
2008-07-29 11:18:27 -04:00
|
|
|
|
|
|
|
std::map<std::string,peerInfo>::iterator mit;
|
2008-09-08 10:04:10 -04:00
|
|
|
for(mit = mFileSources.begin(); mit != mFileSources.end(); mit++)
|
2008-07-29 11:18:27 -04:00
|
|
|
{
|
2008-09-08 10:04:10 -04:00
|
|
|
std::string peerId = mit->first;
|
|
|
|
peerInfo* pInfo = &mit->second;
|
|
|
|
switch (pInfo->state)
|
2008-07-29 11:18:27 -04:00
|
|
|
{
|
|
|
|
//Peer side has change from online to offline during transfer
|
|
|
|
case PQIPEER_NOT_ONLINE:
|
2008-09-08 10:04:10 -04:00
|
|
|
/*
|
|
|
|
if (ts - (pInfo->lastTS) > PQIPEER_OFFLINE_CHECK)
|
2008-07-29 11:18:27 -04:00
|
|
|
{//start to request data
|
2008-09-08 10:04:10 -04:00
|
|
|
req_size = TRANSFER_START_MIN;
|
|
|
|
if (getChunk(req_offset,req_size))
|
|
|
|
{
|
|
|
|
pInfo->offset = req_offset;
|
|
|
|
pInfo->chunkSize = req_size;
|
|
|
|
pInfo->lastTS = ts;
|
|
|
|
pInfo->state = PQIPEER_DOWNLOADING;
|
|
|
|
requestData(peerId, req_offset,req_size);
|
|
|
|
}
|
2008-07-29 11:18:27 -04:00
|
|
|
else mFlag = 1;
|
|
|
|
}
|
2008-09-08 10:04:10 -04:00
|
|
|
*/
|
2008-07-29 11:18:27 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
//file request has been sent to peer side, but no response received yet
|
|
|
|
case PQIPEER_DOWNLOADING:
|
2008-09-08 10:04:10 -04:00
|
|
|
if (ts - (pInfo->lastTS) > PQIPEER_DOWNLOAD_CHECK)
|
|
|
|
requestData(peerId, pInfo->offset,pInfo->chunkSize); //give a push
|
|
|
|
|
|
|
|
actualRate += pInfo->actualRate;
|
2008-08-06 00:12:36 -04:00
|
|
|
|
2008-07-29 11:18:27 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
//file response has been received or peer side is just ready for download
|
|
|
|
case PQIPEER_IDLE:
|
2008-09-08 10:04:10 -04:00
|
|
|
pInfo->actualRate = pInfo->chunkSize/(ts-(pInfo->lastTS));
|
|
|
|
if (pInfo->actualRate < pInfo->desiredRate/2)
|
2008-08-06 00:12:36 -04:00
|
|
|
{
|
2008-09-08 10:04:10 -04:00
|
|
|
req_size = pInfo->chunkSize * 2 ;
|
2008-08-06 00:12:36 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-08 10:04:10 -04:00
|
|
|
req_size = (uint32_t ) (pInfo->chunkSize * 0.9) ;
|
2008-08-06 00:12:36 -04:00
|
|
|
}
|
2008-09-08 10:04:10 -04:00
|
|
|
|
|
|
|
if (getChunk(req_offset,req_size))
|
|
|
|
{
|
|
|
|
pInfo->offset = req_offset;
|
|
|
|
pInfo->chunkSize = req_size;
|
|
|
|
pInfo->lastTS = ts;
|
|
|
|
pInfo->state = PQIPEER_DOWNLOADING;
|
|
|
|
requestData(peerId,req_offset,req_size);
|
|
|
|
}
|
|
|
|
else mFlag = 1;
|
|
|
|
|
|
|
|
actualRate += pInfo->actualRate;
|
2008-07-29 11:18:27 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
//file transfer has been stopped
|
|
|
|
case PQIPEER_SUSPEND:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}//switch
|
|
|
|
}//for
|
2008-09-09 03:24:06 -04:00
|
|
|
|
|
|
|
return true;
|
2008-07-29 11:18:27 -04:00
|
|
|
}
|
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
bool ftTransferModule::pauseTransfer()
|
2008-07-29 11:18:27 -04:00
|
|
|
{
|
2008-09-08 04:44:37 -04:00
|
|
|
/*
|
2008-07-29 11:18:27 -04:00
|
|
|
std::map<std::string,peerInfo>::iterator mit;
|
|
|
|
for(mit = mOnlinePeers.begin(); mit != mOnlinePeers.end(); mit++)
|
|
|
|
{
|
|
|
|
(mit->second).state = PQIPEER_SUSPEND;
|
|
|
|
}
|
2008-09-08 04:44:37 -04:00
|
|
|
*/
|
|
|
|
mFileStatus.stat=ftFileStatus::PQIFILE_PAUSE;
|
|
|
|
|
2008-07-29 11:18:27 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ftTransferModule::resumeTransfer()
|
|
|
|
{
|
2008-09-08 04:44:37 -04:00
|
|
|
/*
|
2008-07-29 11:18:27 -04:00
|
|
|
std::map<std::string,peerInfo>::iterator mit;
|
|
|
|
for(mit = mOnlinePeers.begin(); mit != mOnlinePeers.end(); mit++)
|
|
|
|
{
|
|
|
|
(mit->second).state = PQIPEER_IDLE;
|
|
|
|
}
|
2008-09-08 04:44:37 -04:00
|
|
|
*/
|
|
|
|
mFileStatus.stat=ftFileStatus::PQIFILE_DOWNLOADING;
|
2008-07-29 11:18:27 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
bool ftTransferModule::cancelTransfer()
|
|
|
|
{
|
2008-09-08 10:04:10 -04:00
|
|
|
mFileStatus.stat=ftFileStatus::PQIFILE_FAIL_CANCEL;
|
|
|
|
|
|
|
|
return 1;
|
2008-09-08 04:44:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ftTransferModule::completeFileTransfer()
|
|
|
|
{
|
2008-09-08 10:04:10 -04:00
|
|
|
mFtController->completeFile(mHash);
|
2008-09-08 04:44:37 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-07-29 11:18:27 -04:00
|
|
|
int ftTransferModule::tick()
|
|
|
|
{
|
|
|
|
queryInactive();
|
2008-09-09 03:24:06 -04:00
|
|
|
switch (mFlag)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
adjustSpeed();
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
completeFileTransfer();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
/* tell me what to do here */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-07-29 11:18:27 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-09-08 04:44:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
void ftTransferModule::adjustSpeed()
|
|
|
|
{
|
2008-08-06 00:12:36 -04:00
|
|
|
std::map<std::string,peerInfo>::iterator mit;
|
2008-09-08 10:04:10 -04:00
|
|
|
for(mit = mFileSources.begin(); mit != mFileSources.end(); mit++)
|
2008-09-08 04:44:37 -04:00
|
|
|
{
|
|
|
|
if (((mit->second).state == PQIPEER_DOWNLOADING)
|
|
|
|
|| ((mit->second).state == PQIPEER_IDLE))
|
|
|
|
{
|
|
|
|
if ((actualRate < desiredRate) && ((mit->second).actualRate >= (mit->second).desiredRate))
|
|
|
|
{
|
|
|
|
(mit->second).desiredRate *= 1.1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((actualRate > desiredRate) && ((mit->second).actualRate < (mit->second).desiredRate))
|
|
|
|
{
|
|
|
|
(mit->second).desiredRate *= 0.9;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|