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-08-03 08:45:53 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fttransfermodule.h"
|
|
|
|
|
|
|
|
const uint32_t PQIPEER_OFFLINE_CHECK = 120; /* check every 2 minutes */
|
|
|
|
const uint32_t PQIPEER_DOWNLOAD_TIMEOUT = 60; /* time it out, -> offline after 60 secs */
|
|
|
|
const uint32_t PQIPEER_DOWNLOAD_CHECK = 10; /* desired delta = 10 secs */
|
|
|
|
const uint32_t PQIPEER_DOWNLOAD_TOO_FAST = 8; /* 8 secs */
|
|
|
|
const uint32_t PQIPEER_DOWNLOAD_TOO_SLOW = 12; /* 12 secs */
|
|
|
|
const uint32_t PQIPEER_DOWNLOAD_MIN_DELTA = 5; /* 5 secs */
|
|
|
|
|
2008-08-23 17:28:08 -04:00
|
|
|
const uint32_t TRANSFER_START_MIN = 10000; /* 10000 byte min limit */
|
2008-08-06 00:12:36 -04:00
|
|
|
const uint32_t TRANSFER_START_MAX = 10000; /* 10000 byte max limit */
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
ftTransferModule::ftTransferModule(ftFileCreator *fc, ftDataMultiplex *dm)
|
|
|
|
:mFileCreator(fc), mMultiplexor(dm), mFlag(0)
|
|
|
|
{
|
|
|
|
mHash = mFileCreator->getHash();
|
|
|
|
mSize = mFileCreator->getFileSize();
|
|
|
|
|
|
|
|
// Dummy for Testing (should be handled independantly for
|
|
|
|
// each peer.
|
2008-08-17 11:23:11 -04:00
|
|
|
//mChunkSize = 10000;
|
2008-08-03 08:45:53 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-29 11:18:27 -04:00
|
|
|
ftTransferModule::~ftTransferModule()
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool ftTransferModule::setFileSources(std::list<std::string> peerIds)
|
|
|
|
{
|
|
|
|
std::list<std::string>::iterator it;
|
|
|
|
for(it = peerIds.begin(); it != peerIds.end(); it++)
|
|
|
|
{
|
|
|
|
mFileSources.push_back(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftTransferModule::setPeerState(std::string peerId,uint32_t state,uint32_t maxRate) //state = ONLINE/OFFLINE
|
2008-07-29 11:18:27 -04:00
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
std::list<std::string>::iterator it;
|
2008-08-03 08:45:53 -04:00
|
|
|
it = mFileSources.begin();
|
|
|
|
while (( it != mFileSources.end())&&(!found))
|
2008-07-29 11:18:27 -04:00
|
|
|
{
|
|
|
|
if ((*it) == peerId)
|
|
|
|
found = true;
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
2008-08-23 17:28:08 -04:00
|
|
|
if (!found) mFileSources.push_back(peerId);
|
2008-07-29 11:18:27 -04:00
|
|
|
|
|
|
|
std::map<std::string,peerInfo>::iterator mit;
|
2008-08-23 17:28:08 -04:00
|
|
|
mit = mOnlinePeers.find(peerId);
|
|
|
|
if (mit == mOnlinePeers.end())
|
|
|
|
{
|
|
|
|
peerInfo pInfo;
|
|
|
|
/* Initialise it properly */
|
|
|
|
pInfo.peerId = peerId;
|
|
|
|
pInfo.state = state;
|
|
|
|
pInfo.offset = 0;
|
|
|
|
pInfo.chunkSize = TRANSFER_START_MIN;
|
|
|
|
pInfo.receivedSize = 0;
|
|
|
|
pInfo.lastTS = 0;
|
|
|
|
pInfo.actualRate = 0;
|
|
|
|
pInfo.desiredRate = maxRate;
|
|
|
|
mOnlinePeers[peerId] = pInfo;
|
|
|
|
|
2008-07-29 11:18:27 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
(mit->second).state = state;
|
|
|
|
(mit->second).desiredRate = maxRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ftTransferModule::getDataRate(std::string peerId)
|
|
|
|
{
|
|
|
|
std::map<std::string,peerInfo>::iterator mit;
|
|
|
|
mit = mOnlinePeers.find(peerId);
|
2008-08-03 08:45:53 -04:00
|
|
|
if (mit == mOnlinePeers.end())
|
2008-07-29 11:18:27 -04:00
|
|
|
return 0;
|
|
|
|
else
|
2008-08-17 11:23:11 -04:00
|
|
|
return (uint32_t) (mit->second).actualRate;
|
2008-07-29 11:18:27 -04:00
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -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-08-06 00:12:36 -04:00
|
|
|
std::map<std::string,peerInfo>::iterator mit;
|
|
|
|
mit = mOnlinePeers.find(peerId);
|
|
|
|
if (mit == mOnlinePeers.end())
|
|
|
|
return false;
|
|
|
|
if ((mit->second).state != PQIPEER_DOWNLOADING)
|
|
|
|
return false;
|
|
|
|
if (offset != ((mit->second).offset + (mit->second).receivedSize))
|
|
|
|
return false;
|
|
|
|
(mit->second).receivedSize += chunk_size;
|
|
|
|
(mit->second).state = PQIPEER_IDLE;
|
2008-08-03 08:45:53 -04:00
|
|
|
|
|
|
|
return storeData(offset, chunk_size, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ftTransferModule::requestData(std::string peerId, uint64_t offset, uint32_t chunk_size)
|
|
|
|
{
|
2008-08-21 17:30:59 -04:00
|
|
|
std::cerr << "ftTransferModule::requestData()";
|
|
|
|
std::cerr << " peerId: " << peerId;
|
|
|
|
std::cerr << " offset: " << offset;
|
|
|
|
std::cerr << " chunk_size: " << chunk_size;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
mMultiplexor->sendDataRequest(peerId, mHash, mSize, offset,chunk_size);
|
|
|
|
}
|
|
|
|
|
2008-08-29 21:07:24 -04:00
|
|
|
bool ftTransferModule::getChunk(uint64_t &offset, uint32_t &chunk_size)
|
2008-08-03 08:45:53 -04:00
|
|
|
{
|
2008-08-21 17:30:59 -04:00
|
|
|
std::cerr << "ftTransferModule::getChunk()";
|
2008-08-23 17:28:08 -04:00
|
|
|
std::cerr << " Request: offset: " << offset;
|
|
|
|
std::cerr << " chunk_size: " << chunk_size;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
bool val = mFileCreator->getMissingChunk(offset, chunk_size);
|
|
|
|
|
2008-08-29 21:07:24 -04:00
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
std::cerr << "ftTransferModule::getChunk()";
|
|
|
|
std::cerr << " Answer: offset: " << offset;
|
|
|
|
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-08-21 17:30:59 -04:00
|
|
|
|
2008-08-23 17:28:08 -04:00
|
|
|
return val;
|
2008-08-03 08:45:53 -04:00
|
|
|
}
|
2008-08-17 11:23:11 -04:00
|
|
|
|
|
|
|
bool ftTransferModule::storeData(uint64_t offset, uint32_t chunk_size,void *data)
|
|
|
|
{
|
2008-08-21 17:30:59 -04:00
|
|
|
std::cerr << "ftTransferModule::storeData()";
|
|
|
|
std::cerr << " offset: " << offset;
|
|
|
|
std::cerr << " chunk_size: " << chunk_size;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2008-08-17 11:23:11 -04:00
|
|
|
return mFileCreator -> addFileData(offset, chunk_size, data);
|
|
|
|
}
|
|
|
|
|
2008-07-29 11:18:27 -04:00
|
|
|
void ftTransferModule::queryInactive()
|
|
|
|
{
|
|
|
|
#ifdef FT_DEBUG
|
|
|
|
std::ostringstream out;
|
|
|
|
out<<"ftTransferModule::queryInactive()";
|
|
|
|
out<<std:endl;
|
2008-08-21 17:30:59 -04:00
|
|
|
std::cerr << out.str();
|
2008-07-29 11:18:27 -04:00
|
|
|
#endif
|
|
|
|
|
2008-08-17 11:23:11 -04:00
|
|
|
int ts = time(NULL);
|
|
|
|
uint64_t offset;
|
|
|
|
uint32_t size;
|
|
|
|
int delta;
|
|
|
|
|
2008-07-29 11:18:27 -04:00
|
|
|
std::map<std::string,peerInfo>::iterator mit;
|
|
|
|
for(mit = mOnlinePeers.begin(); mit != mOnlinePeers.end(); mit++)
|
|
|
|
{
|
|
|
|
switch ((mit->second).state)
|
|
|
|
{
|
|
|
|
//Peer side has change from online to offline during transfer
|
|
|
|
case PQIPEER_NOT_ONLINE:
|
|
|
|
if (ts - ((mit->second).lastTS) > PQIPEER_OFFLINE_CHECK)
|
|
|
|
{//start to request data
|
2008-08-06 00:12:36 -04:00
|
|
|
size = TRANSFER_START_MIN;
|
|
|
|
if (getChunk(offset,size))
|
2008-07-29 11:18:27 -04:00
|
|
|
{
|
2008-08-06 00:12:36 -04:00
|
|
|
(mit->second).offset = offset;
|
|
|
|
(mit->second).chunkSize = size;
|
2008-07-29 11:18:27 -04:00
|
|
|
(mit->second).lastTS = ts;
|
|
|
|
(mit->second).state = PQIPEER_DOWNLOADING;
|
2008-08-06 00:12:36 -04:00
|
|
|
requestData(mit->first, offset,size);
|
2008-07-29 11:18:27 -04:00
|
|
|
}
|
|
|
|
else mFlag = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
//file request has been sent to peer side, but no response received yet
|
|
|
|
case PQIPEER_DOWNLOADING:
|
|
|
|
if (ts - ((mit->second).lastTS) > PQIPEER_DOWNLOAD_CHECK)
|
2008-08-17 11:23:11 -04:00
|
|
|
requestData(mit->first, (mit->second).offset,(mit->second).chunkSize); //give a push
|
2008-08-06 00:12:36 -04:00
|
|
|
|
|
|
|
actualRate += (mit->second).actualRate;
|
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-08-17 11:23:11 -04:00
|
|
|
(mit->second).actualRate = (mit->second).chunkSize/(ts-(mit->second).lastTS);
|
|
|
|
if ((mit->second).actualRate < (mit->second).desiredRate)
|
2008-08-06 00:12:36 -04:00
|
|
|
{
|
2008-08-17 11:23:11 -04:00
|
|
|
size = (mit->second).chunkSize * 2 ;
|
2008-08-06 00:12:36 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-08-17 11:23:11 -04:00
|
|
|
size = (uint32_t ) ((mit->second).chunkSize * 0.9) ;
|
2008-08-06 00:12:36 -04:00
|
|
|
}
|
|
|
|
if (getChunk(offset,size))
|
2008-07-29 11:18:27 -04:00
|
|
|
{
|
2008-08-06 00:12:36 -04:00
|
|
|
(mit->second).offset = offset;
|
2008-08-17 11:23:11 -04:00
|
|
|
(mit->second).chunkSize = size;
|
2008-07-29 11:18:27 -04:00
|
|
|
(mit->second).lastTS = ts;
|
|
|
|
(mit->second).state = PQIPEER_DOWNLOADING;
|
2008-08-06 00:12:36 -04:00
|
|
|
requestData(mit->first,offset,size);
|
2008-07-29 11:18:27 -04:00
|
|
|
}
|
2008-08-06 00:12:36 -04:00
|
|
|
else mFlag = 1;
|
|
|
|
|
|
|
|
actualRate += (mit->second).actualRate;
|
2008-07-29 11:18:27 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
//file transfer has been stopped
|
|
|
|
case PQIPEER_SUSPEND:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}//switch
|
|
|
|
}//for
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ftTransferModule::stopTransfer()
|
|
|
|
{
|
|
|
|
std::map<std::string,peerInfo>::iterator mit;
|
|
|
|
for(mit = mOnlinePeers.begin(); mit != mOnlinePeers.end(); mit++)
|
|
|
|
{
|
|
|
|
(mit->second).state = PQIPEER_SUSPEND;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ftTransferModule::resumeTransfer()
|
|
|
|
{
|
|
|
|
std::map<std::string,peerInfo>::iterator mit;
|
|
|
|
for(mit = mOnlinePeers.begin(); mit != mOnlinePeers.end(); mit++)
|
|
|
|
{
|
|
|
|
(mit->second).state = PQIPEER_IDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-08-17 11:23:11 -04:00
|
|
|
bool ftTransferModule::completeFileTransfer()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-07-29 11:18:27 -04:00
|
|
|
int ftTransferModule::tick()
|
|
|
|
{
|
|
|
|
queryInactive();
|
|
|
|
if (mFlag != 1) adjustSpeed();
|
|
|
|
else
|
|
|
|
completeFileTransfer();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-08-03 08:45:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
void ftTransferModule::adjustSpeed()
|
|
|
|
{
|
2008-08-06 00:12:36 -04:00
|
|
|
std::map<std::string,peerInfo>::iterator mit;
|
|
|
|
for(mit = mOnlinePeers.begin(); mit != mOnlinePeers.end(); mit++)
|
|
|
|
{
|
|
|
|
if (((mit->second).state == PQIPEER_DOWNLOADING)
|
|
|
|
|| ((mit->second).state == PQIPEER_IDLE))
|
|
|
|
{
|
2008-08-17 11:23:11 -04:00
|
|
|
if ((actualRate < desiredRate) && ((mit->second).actualRate >= (mit->second).desiredRate))
|
2008-08-06 00:12:36 -04:00
|
|
|
{
|
|
|
|
(mit->second).desiredRate *= 1.1;
|
|
|
|
}
|
|
|
|
|
2008-08-17 11:23:11 -04:00
|
|
|
if ((actualRate > desiredRate) && ((mit->second).actualRate < (mit->second).desiredRate))
|
2008-08-06 00:12:36 -04:00
|
|
|
{
|
|
|
|
(mit->second).desiredRate *= 0.9;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-08-03 08:45:53 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|