2018-05-22 16:03:11 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
* libretroshare/src/ft: ftcontroller.h *
|
|
|
|
* *
|
|
|
|
* libretroshare: retroshare core library *
|
|
|
|
* *
|
|
|
|
* Copyright 2008 by Robert Fernie <drbob@lunamutt.com> *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
2018-05-28 16:03:39 -04:00
|
|
|
* it under the terms of the GNU Lesser General Public License as *
|
2018-05-22 16:03:11 -04:00
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program 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 *
|
2018-05-28 16:03:39 -04:00
|
|
|
* GNU Lesser General Public License for more details. *
|
2018-05-22 16:03:11 -04:00
|
|
|
* *
|
2018-05-28 16:03:39 -04:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
2018-05-22 16:03:11 -04:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
2008-07-02 09:19:59 -04:00
|
|
|
|
|
|
|
#ifndef FT_CONTROLLER_HEADER
|
|
|
|
#define FT_CONTROLLER_HEADER
|
|
|
|
|
2009-07-12 09:22:31 -04:00
|
|
|
/*
|
2008-07-02 09:19:59 -04:00
|
|
|
* ftController
|
|
|
|
*
|
|
|
|
* Top level download controller.
|
|
|
|
*
|
|
|
|
* inherits configuration (save downloading files)
|
|
|
|
* inherits pqiMonitor (knows which peers are online).
|
|
|
|
* inherits CacheTransfer (transfers cache files too)
|
|
|
|
* inherits RsThread (to control transfers)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
class ftFileCreator;
|
|
|
|
class ftTransferModule;
|
|
|
|
class ftFileProvider;
|
2008-08-09 13:03:24 -04:00
|
|
|
class ftSearch;
|
2013-04-06 05:21:01 -04:00
|
|
|
class ftServer;
|
2008-11-09 17:17:20 -05:00
|
|
|
class ftExtraList;
|
2008-08-09 13:03:24 -04:00
|
|
|
class ftDataMultiplex;
|
2009-05-26 17:42:45 -04:00
|
|
|
class p3turtle ;
|
2014-03-28 23:57:44 -04:00
|
|
|
class p3ServiceControl;
|
2008-08-09 13:03:24 -04:00
|
|
|
|
|
|
|
#include "util/rsthreads.h"
|
2014-03-28 23:57:44 -04:00
|
|
|
#include "pqi/pqiservicemonitor.h"
|
2008-08-09 13:03:24 -04:00
|
|
|
#include "pqi/p3cfgmgr.h"
|
|
|
|
|
2010-08-06 05:40:23 -04:00
|
|
|
#include "retroshare/rsfiles.h"
|
2017-04-24 16:47:08 -04:00
|
|
|
#include "rsitems/rsconfigitems.h"
|
2008-08-09 13:03:24 -04:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
|
|
|
|
const uint32_t FC_TRANSFER_COMPLETE = 0x0001;
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
class ftFileControl
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
enum { DOWNLOADING = 0,
|
|
|
|
COMPLETED = 1,
|
|
|
|
ERROR_COMPLETION = 2,
|
2010-03-15 08:57:08 -04:00
|
|
|
QUEUED = 3,
|
2010-07-09 17:04:29 -04:00
|
|
|
PAUSED = 4,
|
|
|
|
CHECKING_HASH = 5
|
2010-03-06 18:29:47 -05:00
|
|
|
};
|
2010-01-26 15:40:21 -05:00
|
|
|
|
|
|
|
ftFileControl();
|
2018-05-06 12:53:29 -04:00
|
|
|
ftFileControl( const std::string& fname, const std::string& tmppath, const std::string& dest
|
|
|
|
, uint64_t size, const RsFileHash& hash, TransferRequestFlags flags
|
|
|
|
, ftFileCreator *fc, ftTransferModule *tm);
|
2010-01-26 15:40:21 -05:00
|
|
|
|
|
|
|
std::string mName;
|
|
|
|
std::string mCurrentPath; /* current full path (including name) */
|
|
|
|
std::string mDestination; /* final full path (including name) */
|
|
|
|
ftTransferModule * mTransfer;
|
|
|
|
ftFileCreator * mCreator;
|
|
|
|
uint32_t mState;
|
2014-03-17 16:56:06 -04:00
|
|
|
RsFileHash mHash;
|
2010-01-26 15:40:21 -05:00
|
|
|
uint64_t mSize;
|
2012-11-02 09:52:29 -04:00
|
|
|
TransferRequestFlags mFlags;
|
2010-01-26 15:40:21 -05:00
|
|
|
time_t mCreateTime;
|
2010-03-06 18:29:47 -05:00
|
|
|
uint32_t mQueuePriority ;
|
|
|
|
uint32_t mQueuePosition ;
|
2008-08-09 13:03:24 -04:00
|
|
|
};
|
|
|
|
|
2008-11-20 19:10:59 -05:00
|
|
|
class ftPendingRequest
|
|
|
|
{
|
|
|
|
public:
|
2014-03-17 16:56:06 -04:00
|
|
|
ftPendingRequest(const std::string& fname, const RsFileHash& hash,
|
2012-11-02 09:52:29 -04:00
|
|
|
uint64_t size, const std::string& dest, TransferRequestFlags flags,
|
2014-03-17 16:56:06 -04:00
|
|
|
const std::list<RsPeerId> &srcIds, uint16_t state)
|
2008-11-20 19:10:59 -05:00
|
|
|
: mName(fname), mHash(hash), mSize(size),
|
2013-10-06 08:56:37 -04:00
|
|
|
mDest(dest), mFlags(flags), mSrcIds(srcIds), mState(state) { return; }
|
2008-11-20 19:10:59 -05:00
|
|
|
|
2013-10-06 08:56:37 -04:00
|
|
|
ftPendingRequest() : mSize(0), mFlags(0), mState(0) { return; }
|
2008-11-20 19:10:59 -05:00
|
|
|
|
|
|
|
std::string mName;
|
2014-03-17 16:56:06 -04:00
|
|
|
RsFileHash mHash;
|
2008-11-20 19:10:59 -05:00
|
|
|
uint64_t mSize;
|
|
|
|
std::string mDest;
|
2012-11-02 09:52:29 -04:00
|
|
|
TransferRequestFlags mFlags;
|
2014-03-17 16:56:06 -04:00
|
|
|
std::list<RsPeerId> mSrcIds;
|
2013-10-06 08:56:37 -04:00
|
|
|
uint16_t mState;
|
2008-11-20 19:10:59 -05:00
|
|
|
};
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
|
2016-07-30 15:52:42 -04:00
|
|
|
class ftController: public RsTickingThread, public pqiServiceMonitor, public p3Config
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
/* Setup */
|
2016-07-30 15:52:42 -04:00
|
|
|
ftController(ftDataMultiplex *dm, p3ServiceControl *sc, uint32_t ftServiceId);
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
void setFtSearchNExtra(ftSearch *, ftExtraList *);
|
|
|
|
void setTurtleRouter(p3turtle *) ;
|
2013-04-06 05:21:01 -04:00
|
|
|
void setFtServer(ftServer *) ;
|
2010-03-06 18:29:47 -05:00
|
|
|
bool activate();
|
|
|
|
bool isActiveAndNoPending();
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2015-05-22 16:54:38 -04:00
|
|
|
virtual void data_tick();
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
/***************************************************************/
|
|
|
|
/********************** Controller Access **********************/
|
|
|
|
/***************************************************************/
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
bool FileRequest(const std::string& fname, const RsFileHash& hash,
|
2012-11-02 09:52:29 -04:00
|
|
|
uint64_t size, const std::string& dest, TransferRequestFlags flags,
|
2014-03-17 16:56:06 -04:00
|
|
|
const std::list<RsPeerId> &sourceIds, uint16_t state = ftFileControl::DOWNLOADING);
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
/// Do we already have this file, either in download or in file lists ?
|
2014-03-17 16:56:06 -04:00
|
|
|
bool alreadyHaveFile(const RsFileHash& hash, FileInfo &info);
|
2010-01-16 10:42:26 -05:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
bool setChunkStrategy(const RsFileHash& hash,FileChunksInfo::ChunkStrategy s);
|
2010-03-21 17:07:12 -04:00
|
|
|
void setDefaultChunkStrategy(FileChunksInfo::ChunkStrategy s);
|
2016-10-29 12:18:02 -04:00
|
|
|
void setDefaultEncryptionPolicy(uint32_t s);
|
|
|
|
FileChunksInfo::ChunkStrategy defaultChunkStrategy();
|
2010-05-21 16:49:48 -04:00
|
|
|
uint32_t freeDiskSpaceLimit() const ;
|
|
|
|
void setFreeDiskSpaceLimit(uint32_t size_in_mb) ;
|
2016-10-29 12:18:02 -04:00
|
|
|
uint32_t defaultEncryptionPolicy();
|
2009-12-08 17:29:52 -05:00
|
|
|
|
2017-05-08 16:00:51 -04:00
|
|
|
void setMaxUploadsPerFriend(uint32_t m) ;
|
|
|
|
uint32_t getMaxUploadsPerFriend() ;
|
|
|
|
|
2017-06-26 17:35:01 -04:00
|
|
|
void setFilePermDirectDL(uint32_t perm) ;
|
|
|
|
uint32_t filePermDirectDL() ;
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
bool FileCancel(const RsFileHash& hash);
|
|
|
|
bool FileControl(const RsFileHash& hash, uint32_t flags);
|
2010-03-06 18:29:47 -05:00
|
|
|
bool FileClearCompleted();
|
2014-03-17 16:56:06 -04:00
|
|
|
bool FlagFileComplete(const RsFileHash& hash);
|
|
|
|
bool getFileDownloadChunksDetails(const RsFileHash& hash,FileChunksInfo& info);
|
|
|
|
bool setDestinationName(const RsFileHash& hash,const std::string& dest_name) ;
|
|
|
|
bool setDestinationDirectory(const RsFileHash& hash,const std::string& dest_name) ;
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
// Download speed
|
2014-03-17 16:56:06 -04:00
|
|
|
bool getPriority(const RsFileHash& hash,DwlSpeed& p);
|
|
|
|
void setPriority(const RsFileHash& hash,DwlSpeed p);
|
2010-01-26 15:40:21 -05:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
// Action on queue position
|
|
|
|
//
|
2014-03-17 16:56:06 -04:00
|
|
|
void moveInQueue(const RsFileHash& hash,QueueMove mv) ;
|
2010-03-06 18:29:47 -05:00
|
|
|
void clearQueue() ;
|
|
|
|
void setQueueSize(uint32_t size) ;
|
|
|
|
uint32_t getQueueSize() ;
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
/* get Details of File Transfers */
|
2015-03-08 09:46:07 -04:00
|
|
|
void FileDownloads(std::list<RsFileHash> &hashs);
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
/* Directory Handling */
|
2018-05-12 08:31:17 -04:00
|
|
|
bool setDownloadDirectory(std::string path);
|
|
|
|
bool setPartialsDirectory(std::string path);
|
2010-03-06 18:29:47 -05:00
|
|
|
std::string getDownloadDirectory();
|
|
|
|
std::string getPartialsDirectory();
|
2014-03-17 16:56:06 -04:00
|
|
|
bool FileDetails(const RsFileHash &hash, FileInfo &info);
|
2008-11-15 15:00:29 -05:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
/***************************************************************/
|
|
|
|
/********************** Cache Transfer *************************/
|
|
|
|
/***************************************************************/
|
2010-01-26 15:40:21 -05:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
/// Returns true is full source availability can be assumed for this peer.
|
|
|
|
///
|
2014-03-17 16:56:06 -04:00
|
|
|
bool assumeAvailability(const RsPeerId& peer_id) const ;
|
2008-08-17 11:23:11 -04:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
/* pqiMonitor callback (also provided mConnMgr pointer!) */
|
2014-03-28 23:57:44 -04:00
|
|
|
virtual void statusChange(const std::list<pqiServicePeer> &plist);
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
void addFileSource(const RsFileHash& hash,const RsPeerId& peer_id) ;
|
|
|
|
void removeFileSource(const RsFileHash& hash,const RsPeerId& peer_id) ;
|
2008-08-17 11:23:11 -04:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
protected:
|
2010-01-26 15:40:21 -05:00
|
|
|
|
2012-11-19 17:45:37 -05:00
|
|
|
void searchForDirectSources() ;
|
2010-03-06 18:29:47 -05:00
|
|
|
void tickTransfers() ;
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
/***************************************************************/
|
|
|
|
/********************** Controller Access **********************/
|
|
|
|
/***************************************************************/
|
2008-07-02 09:19:59 -04:00
|
|
|
|
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
/* p3Config Interface */
|
|
|
|
virtual RsSerialiser *setupSerialiser();
|
2010-12-18 14:35:07 -05:00
|
|
|
virtual bool saveList(bool &cleanup, std::list<RsItem *>&);
|
|
|
|
virtual bool loadList(std::list<RsItem *>& load);
|
2010-03-06 18:29:47 -05:00
|
|
|
bool loadConfigMap(std::map<std::string, std::string> &configMap);
|
2008-11-15 15:00:29 -05:00
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
private:
|
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
/* RunTime Functions */
|
|
|
|
void checkDownloadQueue(); // check the whole queue for inactive files
|
|
|
|
|
2010-09-15 15:48:13 -04:00
|
|
|
void locked_addToQueue(ftFileControl*,int strategy) ;// insert this one into the queue
|
2010-03-06 18:29:47 -05:00
|
|
|
void locked_bottomQueue(uint32_t pos) ; // bottom queue file which is at this position
|
|
|
|
void locked_topQueue(uint32_t pos) ; // top queue file which is at this position
|
|
|
|
void locked_checkQueueElement(uint32_t pos) ; // check the state of this element in the queue
|
|
|
|
void locked_queueRemove(uint32_t pos) ; // delete this element from the queue
|
|
|
|
void locked_swapQueue(uint32_t pos1,uint32_t pos2) ; // swap position of the two elements
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
bool completeFile(const RsFileHash& hash);
|
2010-03-06 18:29:47 -05:00
|
|
|
bool handleAPendingRequest();
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
bool setPeerState(ftTransferModule *tm, const RsPeerId& id,
|
2010-03-06 18:29:47 -05:00
|
|
|
uint32_t maxrate, bool online);
|
2008-11-13 18:03:46 -05:00
|
|
|
|
2009-12-10 17:55:27 -05:00
|
|
|
time_t last_save_time ;
|
2010-01-21 07:31:00 -05:00
|
|
|
time_t last_clean_time ;
|
2010-03-06 18:29:47 -05:00
|
|
|
/* pointers to other components */
|
|
|
|
|
|
|
|
ftSearch *mSearch;
|
|
|
|
ftDataMultiplex *mDataplex;
|
|
|
|
ftExtraList *mExtraList;
|
|
|
|
p3turtle *mTurtle ;
|
2013-04-06 05:21:01 -04:00
|
|
|
ftServer *mFtServer ;
|
2014-03-28 23:57:44 -04:00
|
|
|
p3ServiceControl *mServiceCtrl;
|
2016-11-22 17:19:34 -05:00
|
|
|
uint32_t mFtServiceType;
|
2017-06-26 17:35:01 -04:00
|
|
|
uint32_t mDefaultEncryptionPolicy;
|
|
|
|
uint32_t mFilePermDirectDLPolicy;
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2015-05-22 16:54:38 -04:00
|
|
|
uint32_t cnt ;
|
2010-03-06 18:29:47 -05:00
|
|
|
RsMutex ctrlMutex;
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
std::map<RsFileHash, ftFileControl*> mCompleted;
|
|
|
|
std::map<RsFileHash, ftFileControl*> mDownloads;
|
2016-11-22 17:19:34 -05:00
|
|
|
std::vector<ftFileControl*> mDownloadQueue ;
|
2008-08-09 13:03:24 -04:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
std::string mConfigPath;
|
|
|
|
std::string mDownloadPath;
|
|
|
|
std::string mPartialsPath;
|
2008-08-17 11:23:11 -04:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
/**** SPEED QUEUES ****/
|
2008-10-29 16:58:23 -04:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
/* callback list (for File Completion) */
|
|
|
|
RsMutex doneMutex;
|
2014-03-17 16:56:06 -04:00
|
|
|
std::list<RsFileHash> mDone;
|
2008-11-20 19:10:59 -05:00
|
|
|
|
2010-03-06 18:29:47 -05:00
|
|
|
/* List to Pause File transfers until Caches are properly loaded */
|
|
|
|
bool mFtActive;
|
|
|
|
bool mFtPendingDone;
|
|
|
|
std::list<ftPendingRequest> mPendingRequests;
|
2014-03-17 16:56:06 -04:00
|
|
|
std::map<RsFileHash,RsFileTransfer*> mPendingChunkMaps ;
|
2009-07-12 09:22:31 -04:00
|
|
|
|
2010-03-21 17:07:12 -04:00
|
|
|
FileChunksInfo::ChunkStrategy mDefaultChunkStrategy ;
|
2010-01-26 15:40:21 -05:00
|
|
|
|
2017-05-08 16:00:51 -04:00
|
|
|
uint32_t _max_active_downloads ; // maximum number of simultaneous downloads
|
|
|
|
uint32_t _max_uploads_per_friend ; // maximum number of uploads per friend. 0 means unlimited.
|
2008-07-02 09:19:59 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|