2008-07-03 09:11:20 -04:00
|
|
|
/*
|
|
|
|
* Retroshare file transfer module: ftTransferModule.h
|
|
|
|
*
|
|
|
|
* 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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FT_TRANSFER_MODULE_HEADER
|
|
|
|
#define FT_TRANSFER_MODULE_HEADER
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FUNCTION DESCRIPTION
|
|
|
|
*
|
|
|
|
* Each Transfer Module is paired up with a single File Creator, and responsible for the transfer of one file.
|
|
|
|
* The Transfer Module is responsible for sending requests to peers at the correct data rates, and storing the returned data
|
|
|
|
* in a FileCreator.
|
|
|
|
* There are multiple Transfer Modules in the File Transfer system. Their requests are multiplexed through the Client Module. * The Transfer Module contains all the algorithms for sensible Data Requests.
|
|
|
|
* It must be able to cope with varying data rates and dropped peers without flooding the system with too many requests.
|
|
|
|
*
|
|
|
|
*/
|
2008-09-08 04:44:37 -04:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <list>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "ft/ftfilecreator.h"
|
|
|
|
#include "ft/ftdatamultiplex.h"
|
2008-09-09 09:08:22 -04:00
|
|
|
#include "ft/ftcontroller.h"
|
2008-09-08 04:44:37 -04:00
|
|
|
|
|
|
|
#include "util/rsthreads.h"
|
|
|
|
|
2008-11-13 18:03:46 -05:00
|
|
|
const uint32_t PQIPEER_INIT = 0x0000;
|
|
|
|
const uint32_t PQIPEER_NOT_ONLINE = 0x0001;
|
|
|
|
const uint32_t PQIPEER_DOWNLOADING = 0x0002;
|
|
|
|
const uint32_t PQIPEER_IDLE = 0x0004;
|
|
|
|
const uint32_t PQIPEER_SUSPEND = 0x0010;
|
2008-07-03 09:11:20 -04:00
|
|
|
|
2010-07-09 17:04:29 -04:00
|
|
|
class HashThread ;
|
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
class peerInfo
|
|
|
|
{
|
|
|
|
public:
|
2008-09-08 10:04:10 -04:00
|
|
|
peerInfo(std::string peerId_in):peerId(peerId_in),state(PQIPEER_NOT_ONLINE),desiredRate(0),actualRate(0),
|
2009-12-08 17:29:52 -05:00
|
|
|
// offset(0),chunkSize(0),receivedSize(0),
|
|
|
|
lastTS(0),
|
2008-11-22 12:07:11 -05:00
|
|
|
recvTS(0), lastTransfers(0), nResets(0),
|
|
|
|
rtt(0), rttActive(false), rttStart(0), rttOffset(0),
|
|
|
|
mRateIncrease(1)
|
2008-09-08 10:04:10 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2008-09-08 04:44:37 -04:00
|
|
|
peerInfo(std::string peerId_in,uint32_t state_in,uint32_t maxRate_in):
|
|
|
|
peerId(peerId_in),state(state_in),desiredRate(maxRate_in),actualRate(0),
|
2009-12-08 17:29:52 -05:00
|
|
|
// offset(0),chunkSize(0),receivedSize(0),
|
|
|
|
lastTS(0),
|
2008-11-18 20:12:21 -05:00
|
|
|
recvTS(0), lastTransfers(0), nResets(0),
|
|
|
|
rtt(0), rttActive(false), rttStart(0), rttOffset(0),
|
|
|
|
mRateIncrease(1)
|
2008-09-08 04:44:37 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string peerId;
|
|
|
|
uint32_t state;
|
|
|
|
double desiredRate;
|
|
|
|
double actualRate;
|
|
|
|
|
|
|
|
//current file data request
|
2009-12-08 17:29:52 -05:00
|
|
|
// uint64_t offset;
|
|
|
|
// uint32_t chunkSize;
|
2008-09-08 04:44:37 -04:00
|
|
|
|
2008-09-09 03:24:06 -04:00
|
|
|
//already received data size for current request
|
2009-12-08 17:29:52 -05:00
|
|
|
// uint32_t receivedSize;
|
2008-09-08 04:44:37 -04:00
|
|
|
|
2008-11-04 18:12:53 -05:00
|
|
|
time_t lastTS; /* last Request */
|
|
|
|
time_t recvTS; /* last Recv */
|
|
|
|
uint32_t lastTransfers; /* data recvd in last second */
|
2008-11-13 18:03:46 -05:00
|
|
|
uint32_t nResets; /* count to disable non-existant files */
|
2008-11-18 20:12:21 -05:00
|
|
|
|
|
|
|
/* rrt rate control */
|
|
|
|
uint32_t rtt; /* last rtt */
|
|
|
|
bool rttActive; /* have we initialised an rtt measurement */
|
|
|
|
time_t rttStart; /* ts of request */
|
|
|
|
uint64_t rttOffset; /* end of request */
|
|
|
|
float mRateIncrease; /* current rate */
|
2008-09-08 04:44:37 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class ftFileStatus
|
|
|
|
{
|
2008-09-09 09:08:22 -04:00
|
|
|
public:
|
2008-09-08 04:44:37 -04:00
|
|
|
enum Status {
|
|
|
|
PQIFILE_INIT,
|
|
|
|
PQIFILE_NOT_ONLINE,
|
|
|
|
PQIFILE_DOWNLOADING,
|
|
|
|
PQIFILE_COMPLETE,
|
2010-07-06 01:04:11 -04:00
|
|
|
PQIFILE_CHECKING,
|
2008-09-08 04:44:37 -04:00
|
|
|
PQIFILE_FAIL,
|
|
|
|
PQIFILE_FAIL_CANCEL,
|
|
|
|
PQIFILE_FAIL_NOT_AVAIL,
|
|
|
|
PQIFILE_FAIL_NOT_OPEN,
|
|
|
|
PQIFILE_FAIL_NOT_SEEK,
|
|
|
|
PQIFILE_FAIL_NOT_WRITE,
|
|
|
|
PQIFILE_FAIL_NOT_READ,
|
|
|
|
PQIFILE_FAIL_BAD_PATH
|
|
|
|
};
|
2008-09-09 09:08:22 -04:00
|
|
|
|
2008-09-10 10:12:13 -04:00
|
|
|
ftFileStatus():hash(""),stat(PQIFILE_INIT) {}
|
|
|
|
ftFileStatus(std::string hash_in):hash(hash_in),stat(PQIFILE_INIT) {}
|
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
std::string hash;
|
|
|
|
Status stat;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ftTransferModule
|
|
|
|
{
|
|
|
|
public:
|
2008-11-02 08:53:17 -05:00
|
|
|
ftTransferModule(ftFileCreator *fc, ftDataMultiplex *dm, ftController *c);
|
2008-09-08 04:44:37 -04:00
|
|
|
~ftTransferModule();
|
|
|
|
|
|
|
|
//interface to download controller
|
|
|
|
bool setFileSources(std::list<std::string> peerIds);
|
2008-11-13 18:03:46 -05:00
|
|
|
bool addFileSource(std::string peerId);
|
2009-06-03 14:47:14 -04:00
|
|
|
bool removeFileSource(std::string peerId);
|
2008-09-08 04:44:37 -04:00
|
|
|
bool setPeerState(std::string peerId,uint32_t state,uint32_t maxRate); //state = ONLINE/OFFLINE
|
2008-11-02 06:38:11 -05:00
|
|
|
bool getFileSources(std::list<std::string> &peerIds);
|
|
|
|
bool getPeerState(std::string peerId,uint32_t &state,uint32_t &tfRate);
|
2008-09-08 04:44:37 -04:00
|
|
|
uint32_t getDataRate(std::string peerId);
|
|
|
|
bool cancelTransfer();
|
2010-07-06 01:04:11 -04:00
|
|
|
bool cancelFileTransferUpward();
|
2008-09-08 04:44:37 -04:00
|
|
|
bool completeFileTransfer();
|
2010-07-06 01:04:11 -04:00
|
|
|
bool isCheckingHash() ;
|
2010-07-21 19:14:10 -04:00
|
|
|
void forceCheck() ;
|
|
|
|
void addCRC32Map(const CRC32Map& map) ;
|
2008-09-08 04:44:37 -04:00
|
|
|
|
|
|
|
//interface to multiplex module
|
|
|
|
bool recvFileData(std::string peerId, uint64_t offset,
|
|
|
|
uint32_t chunk_size, void *data);
|
|
|
|
void requestData(std::string peerId, uint64_t offset, uint32_t chunk_size);
|
|
|
|
|
|
|
|
//interface to file creator
|
2009-12-08 17:29:52 -05:00
|
|
|
bool getChunk(const std::string& peer_id,uint32_t size_hint,uint64_t &offset, uint32_t &chunk_size);
|
2008-09-08 04:44:37 -04:00
|
|
|
bool storeData(uint64_t offset, uint32_t chunk_size, void *data);
|
|
|
|
|
|
|
|
int tick();
|
|
|
|
|
|
|
|
std::string hash() { return mHash; }
|
|
|
|
uint64_t size() { return mSize; }
|
|
|
|
|
|
|
|
//internal used functions
|
2008-09-09 03:24:06 -04:00
|
|
|
bool queryInactive();
|
2008-09-08 04:44:37 -04:00
|
|
|
void adjustSpeed();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2008-11-04 18:12:53 -05:00
|
|
|
bool locked_tickPeerTransfer(peerInfo &info);
|
|
|
|
bool locked_recvPeerData(peerInfo &info, uint64_t offset,
|
|
|
|
uint32_t chunk_size, void *data);
|
|
|
|
|
2010-07-06 01:04:11 -04:00
|
|
|
bool checkFile() ;
|
|
|
|
bool checkCRC() ;
|
2008-11-04 18:12:53 -05:00
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
/* These have independent Mutexes / are const locally (no Mutex protection)*/
|
|
|
|
ftFileCreator *mFileCreator;
|
|
|
|
ftDataMultiplex *mMultiplexor;
|
2008-09-08 10:04:10 -04:00
|
|
|
ftController *mFtController;
|
2008-09-08 04:44:37 -04:00
|
|
|
|
|
|
|
std::string mHash;
|
|
|
|
uint64_t mSize;
|
|
|
|
|
|
|
|
RsMutex tfMtx; /* below is mutex protected */
|
|
|
|
|
2008-09-08 10:04:10 -04:00
|
|
|
std::list<std::string> mOnlinePeers;
|
|
|
|
std::map<std::string,peerInfo> mFileSources;
|
2008-09-08 04:44:37 -04:00
|
|
|
|
2010-07-06 01:04:11 -04:00
|
|
|
uint16_t mFlag; //2:file canceled, 1:transfer complete, 0: not complete, 3: checking hash, 4: checking chunks
|
2008-09-08 04:44:37 -04:00
|
|
|
double desiredRate;
|
|
|
|
double actualRate;
|
|
|
|
|
2010-07-21 19:14:10 -04:00
|
|
|
CRC32Map _crcmap ;
|
|
|
|
uint32_t _crcmap_state ;
|
|
|
|
time_t _crcmap_last_asked_time ;
|
2010-07-06 01:04:11 -04:00
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
ftFileStatus mFileStatus; //used for pause/resume file transfer
|
2010-07-09 17:04:29 -04:00
|
|
|
|
|
|
|
HashThread *_hash_thread ;
|
2008-07-03 09:11:20 -04:00
|
|
|
};
|
|
|
|
|
2008-09-08 04:44:37 -04:00
|
|
|
#endif //FT_TRANSFER_MODULE_HEADER
|