2008-07-23 18:01:59 -04:00
|
|
|
/*
|
|
|
|
* libretroshare/src/ft: ftdatamultiplex.h
|
|
|
|
*
|
|
|
|
* 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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FT_DATA_MULTIPLEX_HEADER
|
|
|
|
#define FT_DATA_MULTIPLEX_HEADER
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ftDataMultiplexModule.
|
|
|
|
*
|
|
|
|
* This multiplexes the data from PQInterface to the ftTransferModules.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ftTransferModule;
|
|
|
|
class ftFileProvider;
|
|
|
|
class ftFileCreator;
|
|
|
|
class ftSearch;
|
2011-11-03 19:18:00 -04:00
|
|
|
class CRC32Thread;
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include "util/rsthreads.h"
|
|
|
|
|
|
|
|
#include "ft/ftdata.h"
|
2010-08-06 05:40:23 -04:00
|
|
|
#include "retroshare/rsfiles.h"
|
2008-08-09 13:03:24 -04:00
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
class ftClient
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
ftClient() :mModule(NULL), mCreator(NULL) { return; }
|
|
|
|
ftClient(ftTransferModule *module, ftFileCreator *creator);
|
|
|
|
|
|
|
|
ftTransferModule *mModule;
|
|
|
|
ftFileCreator *mCreator;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ftRequest
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
ftRequest(uint32_t type, std::string peerId, std::string hash, uint64_t size, uint64_t offset, uint32_t chunk, void *data);
|
|
|
|
|
|
|
|
ftRequest()
|
|
|
|
:mType(0), mSize(0), mOffset(0), mChunk(0), mData(NULL) { return; }
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
uint32_t mType;
|
|
|
|
std::string mPeerId;
|
|
|
|
std::string mHash;
|
2008-08-03 08:45:53 -04:00
|
|
|
uint64_t mSize;
|
2008-07-23 18:01:59 -04:00
|
|
|
uint64_t mOffset;
|
|
|
|
uint32_t mChunk;
|
|
|
|
void *mData;
|
|
|
|
};
|
|
|
|
|
2012-03-15 15:55:43 -04:00
|
|
|
typedef std::map<std::string,time_t> ChunkCheckSumSourceList ;
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2012-03-15 15:55:43 -04:00
|
|
|
class Sha1CacheEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Sha1Map _map ; // Map of available sha1 sums for every chunk.
|
|
|
|
time_t last_activity ; // This is used for removing unused entries.
|
|
|
|
std::vector<uint32_t> _received ; // received chunk ids. To bedispatched.
|
|
|
|
std::map<uint32_t,ChunkCheckSumSourceList> _to_ask ; // Chunks to ask to sources.
|
|
|
|
};
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
class ftDataMultiplex: public ftDataRecv, public RsQueueThread
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
ftDataMultiplex(std::string ownId, ftDataSend *server, ftSearch *search);
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
/* ftController Interface */
|
|
|
|
bool addTransferModule(ftTransferModule *mod, ftFileCreator *f);
|
|
|
|
bool removeTransferModule(std::string hash);
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
/* data interface */
|
|
|
|
/* get Details of File Transfers */
|
|
|
|
bool FileUploads(std::list<std::string> &hashs);
|
|
|
|
bool FileDownloads(std::list<std::string> &hashs);
|
2010-10-27 16:01:31 -04:00
|
|
|
bool FileDetails(const std::string &hash, uint32_t hintsflag, FileInfo &info);
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2010-02-15 15:44:37 -05:00
|
|
|
void deleteUnusedServers() ;
|
2012-03-15 15:55:43 -04:00
|
|
|
void handlePendingCrcRequests() ;
|
2009-02-09 15:26:12 -05:00
|
|
|
|
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
/*************** SEND INTERFACE (calls ftDataSend) *******************/
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
/* Client Send */
|
2010-01-11 11:00:42 -05:00
|
|
|
bool sendDataRequest(const std::string& peerId, const std::string& hash, uint64_t size, uint64_t offset, uint32_t chunksize);
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
/* Server Send */
|
2010-01-11 11:00:42 -05:00
|
|
|
bool sendData(const std::string& peerId, const std::string& hash, uint64_t size, uint64_t offset, uint32_t chunksize, void *data);
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2010-01-11 11:00:42 -05:00
|
|
|
/* Server/client Send */
|
2010-07-25 15:04:31 -04:00
|
|
|
bool sendChunkMapRequest(const std::string& peerId, const std::string& hash,bool is_client) ;
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2010-07-21 19:14:10 -04:00
|
|
|
/* Client Send */
|
2010-07-29 17:07:07 -04:00
|
|
|
bool sendCRC32MapRequest(const std::string& peerId, const std::string& hash) ;
|
2010-07-21 19:14:10 -04:00
|
|
|
|
2011-11-03 19:18:00 -04:00
|
|
|
/* called from a separate thread */
|
|
|
|
bool computeAndSendCRC32Map(const std::string& peerId, const std::string& hash) ;
|
|
|
|
|
2012-03-15 15:55:43 -04:00
|
|
|
/* called from a separate thread */
|
|
|
|
bool sendSingleChunkCRCRequests(const std::string& hash, const std::vector<std::pair<uint32_t, std::list<std::string > > >& to_ask) ;
|
|
|
|
|
|
|
|
bool dispatchReceivedChunkCheckSum() ;
|
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
/*************** RECV INTERFACE (provides ftDataRecv) ****************/
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
/* Client Recv */
|
2010-01-11 11:00:42 -05:00
|
|
|
virtual bool recvData(const std::string& peerId, const std::string& hash, uint64_t size, uint64_t offset, uint32_t chunksize, void *data);
|
2010-07-21 19:14:10 -04:00
|
|
|
/* Server Recv */
|
|
|
|
virtual bool recvDataRequest(const std::string& peerId, const std::string& hash, uint64_t size, uint64_t offset, uint32_t chunksize);
|
2010-01-11 11:00:42 -05:00
|
|
|
|
|
|
|
/// Receive a request for a chunk map
|
|
|
|
virtual bool recvChunkMapRequest(const std::string& peer_id,const std::string& hash,bool is_client) ;
|
|
|
|
/// Receive a chunk map
|
|
|
|
virtual bool recvChunkMap(const std::string& peer_id,const std::string& hash,const CompressedChunkMap& cmap,bool is_client) ;
|
2010-07-21 19:14:10 -04:00
|
|
|
/// Receive a CRC map
|
2010-07-29 17:07:07 -04:00
|
|
|
virtual bool recvCRC32Map(const std::string& peer_id,const std::string& hash,const CRC32Map& crc_map) ;
|
2010-07-21 19:14:10 -04:00
|
|
|
/// Receive a CRC map request
|
2010-07-29 17:07:07 -04:00
|
|
|
virtual bool recvCRC32MapRequest(const std::string& peer_id,const std::string& hash) ;
|
2012-03-15 15:55:43 -04:00
|
|
|
|
|
|
|
virtual bool recvSingleChunkCrcRequest(const std::string& peer_id,const std::string& hash,uint32_t chunk_id) ;
|
|
|
|
virtual bool recvSingleChunkCrc(const std::string& peer_id,const std::string& hash,uint32_t chunk_id,const Sha1CheckSum& sum) ;
|
2010-07-21 19:14:10 -04:00
|
|
|
|
2010-01-11 11:00:42 -05:00
|
|
|
// Returns the chunk map from the file uploading client. Also initiates a chunk map request if this
|
|
|
|
// map is too old. This supposes that the caller will ask again in a few seconds.
|
|
|
|
//
|
|
|
|
bool getClientChunkMap(const std::string& upload_hash,const std::string& peer_id,CompressedChunkMap& map) ;
|
2008-08-03 08:45:53 -04:00
|
|
|
|
|
|
|
protected:
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
/* Overloaded from RsQueueThread */
|
|
|
|
virtual bool workQueued();
|
|
|
|
virtual bool doWork();
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
/* Handling Job Queues */
|
2010-01-11 11:00:42 -05:00
|
|
|
bool handleRecvData(const std::string& peerId, const std::string& hash, uint64_t size, uint64_t offset, uint32_t chunksize, void *data);
|
|
|
|
bool handleRecvDataRequest(const std::string& peerId, const std::string& hash, uint64_t size, uint64_t offset, uint32_t chunksize);
|
|
|
|
bool handleSearchRequest(const std::string& peerId, const std::string& hash);
|
|
|
|
bool handleRecvClientChunkMapRequest(const std::string& peerId, const std::string& hash) ;
|
|
|
|
bool handleRecvServerChunkMapRequest(const std::string& peerId, const std::string& hash) ;
|
2010-07-29 17:07:07 -04:00
|
|
|
bool handleRecvCRC32MapRequest(const std::string& peerId, const std::string& hash) ;
|
2012-03-15 15:55:43 -04:00
|
|
|
bool handleRecvChunkCrcRequest(const std::string& peerId, const std::string& hash,uint32_t chunk_id) ;
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
/* We end up doing the actual server job here */
|
|
|
|
bool locked_handleServerRequest(ftFileProvider *provider, std::string peerId, std::string hash, uint64_t size, uint64_t offset, uint32_t chunksize);
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
RsMutex dataMtx;
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
std::map<std::string, ftClient> mClients;
|
|
|
|
std::map<std::string, ftFileProvider *> mServers;
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
std::list<ftRequest> mRequestQueue;
|
|
|
|
std::list<ftRequest> mSearchQueue;
|
2010-01-11 11:00:42 -05:00
|
|
|
// std::map<std::string, time_t> mUnknownHashs;
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2011-11-03 19:18:00 -04:00
|
|
|
std::list<CRC32Thread *> _crc32map_threads ;
|
|
|
|
std::map<std::string,std::pair<time_t,CRC32Map> > _cached_crc32maps ;
|
2012-03-15 15:55:43 -04:00
|
|
|
|
|
|
|
std::map<std::string,Sha1CacheEntry> _cached_sha1maps ; // one cache entry per file hash. Handled dynamically.
|
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
ftDataSend *mDataSend;
|
|
|
|
ftSearch *mSearch;
|
|
|
|
std::string mOwnId;
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2009-12-28 16:11:00 -05:00
|
|
|
friend class ftServer;
|
2008-07-23 18:01:59 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|