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
|
|
|
*/
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
/*******
|
|
|
|
* #define FT_DEBUG 1
|
|
|
|
******/
|
2008-09-10 10:12:13 -04:00
|
|
|
|
2008-11-02 06:38:11 -05:00
|
|
|
#define FT_DEBUG 1
|
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
#include "fttransfermodule.h"
|
|
|
|
|
2008-09-09 09:08:22 -04:00
|
|
|
ftTransferModule::ftTransferModule(ftFileCreator *fc, ftDataMultiplex *dm, ftController *c)
|
|
|
|
:mFileCreator(fc), mMultiplexor(dm), mFtController(c), mFlag(0)
|
2008-09-08 04:44:37 -04:00
|
|
|
{
|
2008-11-02 06:38:11 -05:00
|
|
|
RsStackMutex stack(tfMtx); /******* STACK LOCKED ******/
|
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
mHash = mFileCreator->getHash();
|
|
|
|
mSize = mFileCreator->getFileSize();
|
2008-09-09 09:08:22 -04:00
|
|
|
mFileStatus.hash = mHash;
|
2008-09-08 04:44:37 -04:00
|
|
|
|
|
|
|
// Dummy for Testing (should be handled independantly for
|
|
|
|
// each peer.
|
|
|
|
//mChunkSize = 10000;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-29 11:18:27 -04:00
|
|
|
ftTransferModule::~ftTransferModule()
|
|
|
|
{}
|
|
|
|
|
2008-11-02 06:38:11 -05:00
|
|
|
|
2008-07-29 11:18:27 -04:00
|
|
|
bool ftTransferModule::setFileSources(std::list<std::string> peerIds)
|
|
|
|
{
|
2008-11-02 06:38:11 -05:00
|
|
|
RsStackMutex stack(tfMtx); /******* STACK LOCKED ******/
|
|
|
|
|
2008-09-08 10:04:10 -04:00
|
|
|
mFileSources.clear();
|
|
|
|
|
2008-09-10 10:12:13 -04:00
|
|
|
#ifdef FT_DEBUG
|
|
|
|
std::cerr << "ftTransferModule::setFileSources()";
|
|
|
|
std::cerr << " List of peers: " ;
|
|
|
|
#endif
|
|
|
|
|
2008-07-29 11:18:27 -04:00
|
|
|
std::list<std::string>::iterator it;
|
|
|
|
for(it = peerIds.begin(); it != peerIds.end(); it++)
|
|
|
|
{
|
2008-09-10 10:12:13 -04:00
|
|
|
|
|
|
|
#ifdef FT_DEBUG
|
|
|
|
std::cerr << " \t" << *it;
|
|
|
|
#endif
|
|
|
|
|
2008-09-09 09:08:22 -04:00
|
|
|
peerInfo pInfo(*it);
|
|
|
|
mFileSources.insert(std::pair<std::string,peerInfo>(*it,pInfo));
|
2008-07-29 11:18:27 -04:00
|
|
|
}
|
|
|
|
|
2008-09-10 10:12:13 -04:00
|
|
|
#ifdef FT_DEBUG
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-09-08 10:04:10 -04:00
|
|
|
return true;
|
2008-07-29 11:18:27 -04:00
|
|
|
}
|
|
|
|
|
2008-11-02 06:38:11 -05:00
|
|
|
bool ftTransferModule::getFileSources(std::list<std::string> &peerIds)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(tfMtx); /******* STACK LOCKED ******/
|
|
|
|
std::map<std::string,peerInfo>::iterator it;
|
|
|
|
for(it = mFileSources.begin(); it != mFileSources.end(); it++)
|
|
|
|
{
|
|
|
|
peerIds.push_back(it->first);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-11-02 06:38:11 -05:00
|
|
|
RsStackMutex stack(tfMtx); /******* STACK LOCKED ******/
|
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
|
|
|
}
|
|
|
|
|
2008-11-02 06:38:11 -05:00
|
|
|
|
|
|
|
bool ftTransferModule::getPeerState(std::string peerId,uint32_t &state,uint32_t &tfRate)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(tfMtx); /******* STACK LOCKED ******/
|
|
|
|
std::map<std::string,peerInfo>::iterator mit;
|
|
|
|
mit = mFileSources.find(peerId);
|
|
|
|
|
|
|
|
if (mit == mFileSources.end()) return false;
|
|
|
|
|
|
|
|
state = (mit->second).state;
|
|
|
|
tfRate = (uint32_t) (mit->second).actualRate;
|
|
|
|
|
|
|
|
#ifdef FT_DEBUG
|
|
|
|
std::cerr << "ftTransferModule::getPeerState()";
|
|
|
|
std::cerr << " peerId: " << peerId;
|
|
|
|
std::cerr << " state: " << state;
|
|
|
|
std::cerr << " tfRate: " << tfRate << std::endl;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-07-29 11:18:27 -04:00
|
|
|
uint32_t ftTransferModule::getDataRate(std::string peerId)
|
|
|
|
{
|
2008-11-02 06:38:11 -05:00
|
|
|
RsStackMutex stack(tfMtx); /******* STACK LOCKED ******/
|
2008-07-29 11:18:27 -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())
|
|
|
|
{
|
|
|
|
#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;
|
2008-10-29 16:58:23 -04:00
|
|
|
std::cerr << " data: " << data;
|
2008-09-08 04:44:37 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-09-08 10:04:10 -04:00
|
|
|
|
2008-11-02 06:38:11 -05:00
|
|
|
{
|
|
|
|
RsStackMutex stack(tfMtx); /******* STACK LOCKED ******/
|
|
|
|
|
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-10-22 14:12:58 -04:00
|
|
|
{
|
|
|
|
#ifdef FT_DEBUG
|
|
|
|
std::cerr << "ftTransferModule::recvFileData()";
|
|
|
|
std::cerr << " peer not found in sources";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-08 10:04:10 -04:00
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
if ((mit->second).state != PQIPEER_DOWNLOADING)
|
2008-10-22 14:12:58 -04:00
|
|
|
{
|
|
|
|
#ifdef FT_DEBUG
|
|
|
|
std::cerr << "ftTransferModule::recvFileData()";
|
|
|
|
std::cerr << " peer not downloading???";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
//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-10-22 14:12:58 -04:00
|
|
|
#ifdef FT_DEBUG
|
|
|
|
std::cerr << "ftTransferModule::recvFileData()";
|
|
|
|
std::cerr << " offset != offset + recvdSize";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
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-11-02 06:38:11 -05:00
|
|
|
} /***** STACK MUTEX END ****/
|
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
|
|
|
{
|
2008-11-02 06:38:11 -05:00
|
|
|
/* NB: Not sure about this lock... might cause deadlock.
|
|
|
|
*/
|
|
|
|
RsStackMutex stack(tfMtx); /******* STACK LOCKED ******/
|
|
|
|
|
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:
|
|
|
|
break;
|
|
|
|
|
|
|
|
//file request has been sent to peer side, but no response received yet
|
|
|
|
case PQIPEER_DOWNLOADING:
|
2008-10-29 16:58:23 -04:00
|
|
|
if (ts - (pInfo->lastTS) < PQIPEER_DOWNLOAD_CHECK)
|
|
|
|
{
|
|
|
|
/* if not timed out yet.... ignore */
|
|
|
|
actualRate += pInfo->actualRate;
|
|
|
|
break;
|
|
|
|
}
|
2008-09-08 10:04:10 -04:00
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
/* otherwise fall through to request it again (with getChunk);
|
|
|
|
*/
|
2008-08-06 00:12:36 -04:00
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
//file response received or peer side is just ready for download
|
2008-07-29 11:18:27 -04:00
|
|
|
case PQIPEER_IDLE:
|
2008-09-08 10:04:10 -04:00
|
|
|
pInfo->actualRate = pInfo->chunkSize/(ts-(pInfo->lastTS));
|
2008-11-02 06:38:11 -05:00
|
|
|
|
|
|
|
if (pInfo->actualRate < pInfo->desiredRate)
|
|
|
|
{
|
|
|
|
if (pInfo->actualRate < pInfo->desiredRate/2)
|
|
|
|
{
|
|
|
|
req_size = pInfo->chunkSize * 2 ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
req_size = (uint32_t ) (pInfo->chunkSize * 1.1) ;
|
|
|
|
}
|
2008-08-06 00:12:36 -04:00
|
|
|
}
|
2008-11-02 06:38:11 -05: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
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
if (getChunk(req_offset,req_size))
|
|
|
|
{
|
|
|
|
if (req_size > 0)
|
|
|
|
{
|
2008-09-08 10:04:10 -04:00
|
|
|
pInfo->offset = req_offset;
|
|
|
|
pInfo->chunkSize = req_size;
|
|
|
|
pInfo->lastTS = ts;
|
|
|
|
pInfo->state = PQIPEER_DOWNLOADING;
|
2008-10-29 16:58:23 -04:00
|
|
|
pInfo->receivedSize = 0;
|
2008-09-08 10:04:10 -04:00
|
|
|
requestData(peerId,req_offset,req_size);
|
2008-10-29 16:58:23 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "transfermodule::Waiting for data to be available";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
2008-09-08 10:04:10 -04:00
|
|
|
}
|
|
|
|
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-11-02 06:38:11 -05:00
|
|
|
RsStackMutex stack(tfMtx); /******* STACK LOCKED ******/
|
|
|
|
|
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-11-02 06:38:11 -05:00
|
|
|
RsStackMutex stack(tfMtx); /******* STACK LOCKED ******/
|
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-11-02 06:38:11 -05:00
|
|
|
RsStackMutex stack(tfMtx); /******* STACK LOCKED ******/
|
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-10-29 16:58:23 -04:00
|
|
|
std::cerr << "ftTransferModule::completeFileTransfer()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
if (mFtController)
|
|
|
|
mFtController->FlagFileComplete(mHash);
|
2008-09-08 04:44:37 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-07-29 11:18:27 -04:00
|
|
|
int ftTransferModule::tick()
|
|
|
|
{
|
2008-09-10 10:12:13 -04:00
|
|
|
#ifdef FT_DEBUG
|
|
|
|
std::cerr << "ftTransferModule::tick()";
|
|
|
|
std::cerr << " mFlag: " << mFlag;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-07-29 11:18:27 -04:00
|
|
|
queryInactive();
|
2008-11-02 06:38:11 -05:00
|
|
|
|
|
|
|
uint32_t flags = 0;
|
|
|
|
{
|
|
|
|
RsStackMutex stack(tfMtx); /******* STACK LOCKED ******/
|
|
|
|
flags = mFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (flags)
|
2008-09-09 03:24:06 -04:00
|
|
|
{
|
|
|
|
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-11-02 06:38:11 -05:00
|
|
|
RsStackMutex stack(tfMtx); /******* STACK LOCKED ******/
|
|
|
|
|
2008-08-06 00:12:36 -04:00
|
|
|
std::map<std::string,peerInfo>::iterator mit;
|
2008-11-02 06:38:11 -05:00
|
|
|
|
|
|
|
#ifdef FT_DEBUG
|
|
|
|
std::cerr << "ftTransferModule::adjustSpeed()";
|
|
|
|
std::cerr << " Initial Desired Rate: " << desiredRate << " Actual Rate: " << actualRate;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
2008-11-02 06:38:11 -05:00
|
|
|
|
|
|
|
#ifdef FT_DEBUG
|
|
|
|
std::cerr << "ftTransferModule::adjustSpeed()";
|
|
|
|
std::cerr << "\t" << mit->first << " Desired Rate: " << desiredRate << " Actual Rate: " << actualRate;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-02 06:38:11 -05:00
|
|
|
#ifdef FT_DEBUG
|
|
|
|
std::cerr << "ftTransferModule::adjustSpeed()";
|
|
|
|
std::cerr << " Initial Desired Rate: " << desiredRate << " Actual Rate: " << actualRate;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-09-08 04:44:37 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|