- implemented a visualisation for currently handled chunks, availability maps, and transfer info

- implemented transfer protocol for chunk availability maps between peers (not enabled yet though)
- suppressed rsiface directory from retroshare-gui
- moved notifyqt.{h,cpp} to gui/

next moves: 
	- send availability maps to clients; 
	- connect turtle search to unfinished files;
	- test multisource download with unfinished files.



git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1939 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2009-12-28 21:11:00 +00:00
parent 1a86871556
commit f4a2eaecce
19 changed files with 622 additions and 747 deletions

View file

@ -212,16 +212,28 @@ bool ChunkMap::getDataChunk(const std::string& peer_id,uint32_t size_hint,ftChun
return true ;
}
#ifdef A_FAIRE
void setPeerAvailabilityMap(const std::string& peer_id,const std::vector<uint32_t>& peer_map)
void ChunkMap::setPeerAvailabilityMap(const std::string& peer_id,uint32_t chunk_size,uint32_t nb_chunks,const std::vector<uint32_t>& compressed_peer_map)
{
#ifdef DEBUG_FTCHUNK
std::cout << "Receiving new availability map for peer " << peer_id << std::endl ;
std::cout << "ChunkMap::Receiving new availability map for peer " << peer_id << std::endl ;
#endif
// Check that the parameters are the same. Otherwise we should convert the info into the local format.
// If all RS instances have the same policy for deciding the sizes of chunks, this should not happen.
_peers_chunks_availability[peer_id] = peer_map ;
}
if(chunk_size != _chunk_size || nb_chunks != _map.size())
{
std::cerr << "ChunkMap::setPeerAvailabilityMap: chunk size / number of chunks is not correct. Dropping the info." << std::endl ;
return ;
}
// sets the map.
//
_peers_chunks_availability[peer_id] = compressed_peer_map ;
#ifdef DEBUG_FTCHUNK
std::cerr << "ChunkMap::setPeerAvailabilityMap: Setting chunk availability info for peer " << peer_id << std::endl ;
#endif
}
uint32_t ChunkMap::sizeOfChunk(uint32_t cid) const
{
@ -231,11 +243,10 @@ uint32_t ChunkMap::sizeOfChunk(uint32_t cid) const
return _chunk_size ;
}
uint32_t ChunkMap::getAvailableChunk(uint32_t start_location,const std::string& peer_id) const
uint32_t ChunkMap::getAvailableChunk(uint32_t start_location,const std::string& peer_id)
{
// Very bold algorithm: checks for 1st availabe chunk for this peer starting
// from the given start location.
#ifdef A_FAIRE
std::map<std::string,std::vector<uint32_t> >::const_iterator it(_peers_chunks_availability.find(peer_id)) ;
// Do we have records for this file source ?
@ -243,18 +254,21 @@ uint32_t ChunkMap::getAvailableChunk(uint32_t start_location,const std::string&
if(it == _peers_chunks_availability.end())
{
#ifdef DEBUG_FTCHUNK
std::cout << "No chunk map for peer " << peer_id << ": returning false" << endl ;
std::cout << "No chunk map for peer " << peer_id << ": supposing full data." << std::endl ;
#endif
return _map.size() ;
std::vector<uint32_t>& pchunks(_peers_chunks_availability[peer_id]) ;
pchunks.resize( (_map.size() >> 5)+!!(_map.size() & 0x11111),~(uint32_t)0 ) ;
it = _peers_chunks_availability.find(peer_id) ;
}
const std::vector<uint32_t> peer_chunks(it->second) ;
#endif
const std::vector<uint32_t>& peer_chunks(it->second) ;
for(unsigned int i=0;i<_map.size();++i)
{
uint32_t j = (start_location+i)%_map.size() ;
uint32_t j = (start_location+i)%(int)_map.size() ; // index of the chunk
if(_map[j] == FileChunksInfo::CHUNK_OUTSTANDING /*&& peers_chunks[j] == CHUNK_DONE*/)
if(_map[j] == FileChunksInfo::CHUNK_OUTSTANDING && COMPRESSED_MAP_READ(peer_chunks,j))
{
#ifdef DEBUG_FTCHUNK
std::cerr << "ChunkMap::getAvailableChunk: returning chunk " << j << " for peer " << peer_id << std::endl;
@ -274,6 +288,16 @@ void ChunkMap::getChunksInfo(FileChunksInfo& info) const
info.file_size = _file_size ;
info.chunk_size = _chunk_size ;
info.chunks = _map ;
info.active_chunks.clear() ;
for(std::map<ChunkNumber,ChunkDownloadInfo>::const_iterator it(_slices_to_download.begin());it!=_slices_to_download.end();++it)
info.active_chunks.push_back(std::pair<uint32_t,uint32_t>(it->first,it->second._remains)) ;
info.compressed_peer_availability_maps.clear() ;
for(std::map<std::string,std::vector<uint32_t> >::const_iterator it(_peers_chunks_availability.begin());it!= _peers_chunks_availability.end();++it)
info.compressed_peer_availability_maps.push_back(std::pair<std::string,std::vector<uint32_t> >(it->first,it->second)) ;
}
void ChunkMap::buildAvailabilityMap(std::vector<uint32_t>& map,uint32_t& chunk_size,uint32_t& chunk_number,FileChunksInfo::ChunkStrategy& strategy) const

View file

@ -24,7 +24,8 @@
// corresponding acive slice. When asked a slice, ftChunkMap cuts out a slice from the remaining part of the chunk
// to download, sends the slice's coordinates and gives a unique slice id (such as the slice offset).
// This class handles a slice of a chunk, at the level of ftFileCreator
// This class handles a slice of a chunk of arbitrary uint32_t size, at the level of ftFileCreator
class ftChunk
{
@ -41,10 +42,10 @@ class ftChunk
time_t ts;
};
// This class handles a single chunk. Although each chunk is requested at once,
// This class handles a single fixed-sized chunk. Although each chunk is requested at once,
// it may be sent back into sub-chunks because of file transfer rate constraints.
// So the dataReceived function should be called to progressively complete the chunk,
// and the getChunk method should ask for a sub0chunk of a given size.
// and the getChunk method should ask for a sub-chunk of a given size.
//
class Chunk
{
@ -118,11 +119,9 @@ class ChunkMap
void buildAvailabilityMap(std::vector<uint32_t>& map,uint32_t& chunk_size,uint32_t& chunk_number,FileChunksInfo::ChunkStrategy& s) const ;
void loadAvailabilityMap(const std::vector<uint32_t>& map,uint32_t chunk_size,uint32_t chunk_number,FileChunksInfo::ChunkStrategy s) ;
#ifdef TO_DO
// Updates the peer's availablility map
//
void setPeerAvailabilityMap(const std::string& peer_id,const std::vector<uint32_t>& peer_map) ;
#endif
void setPeerAvailabilityMap(const std::string& peer_id,uint32_t chunk_size,uint32_t nb_chunks,const std::vector<uint32_t>& peer_map) ;
// Returns the total size of downloaded data in the file.
uint64_t getTotalReceived() const { return _total_downloaded ; }
@ -134,7 +133,7 @@ class ChunkMap
// Returns the first chunk available starting from start_location for this peer_id.
//
uint32_t getAvailableChunk(uint32_t start_location,const std::string& peer_id) const ;
uint32_t getAvailableChunk(uint32_t start_location,const std::string& peer_id) ;
private:
uint64_t _file_size ; // total size of the file in bytes.
@ -145,7 +144,7 @@ class ChunkMap
std::vector<FileChunksInfo::ChunkState> _map ; // vector of chunk state over the whole file
std::map<std::string,std::vector<uint32_t> > _peers_chunks_availability ; // what does each source peer have.
std::map<std::string,std::vector<uint32_t> > _peers_chunks_availability ; // what does each source peer have, stored in compressed format.
uint64_t _total_downloaded ;
};

View file

@ -599,6 +599,8 @@ bool ftController::handleAPendingRequest()
mPendingChunkMaps.erase(it) ;
}
}
return true ;
}
@ -750,7 +752,7 @@ bool ftController::FileRequest(std::string fname, std::string hash,
/* do a source search - for any extra sources */
if (mSearch->search(hash, size,
RS_FILE_HINTS_REMOTE |
RS_FILE_HINTS_TURTLE |
// RS_FILE_HINTS_TURTLE |
RS_FILE_HINTS_SPEC_ONLY, info))
{
/* do something with results */

View file

@ -328,6 +328,31 @@ bool ftDataMultiplex::doWork()
return true;
}
bool ftDataMultiplex::recvFileMap(const std::string& peerId, const std::string& hash, uint32_t chunk_size, uint32_t nb_chunks, const std::vector<uint32_t>& compressed_map)
{
RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/
std::map<std::string, ftClient>::iterator it;
if (mClients.end() == (it = mClients.find(hash)))
{
#ifdef MPLEX_DEBUG
std::cerr << "ftDataMultiplex::handleRecvMap() ERROR: No matching Client!";
std::cerr << std::endl;
#endif
/* error */
return false;
}
#ifdef MPLEX_DEBUG
std::cerr << "ftDataMultiplex::handleRecvMap() Passing map to FT Module";
std::cerr << std::endl;
#endif
(it->second).mCreator->setSourceMap(peerId, chunk_size, nb_chunks,compressed_map);
return true;
}
bool ftDataMultiplex::handleRecvData(std::string peerId,
std::string hash, uint64_t size,

View file

@ -84,81 +84,72 @@ class ftDataMultiplex: public ftDataRecv, public RsQueueThread
public:
ftDataMultiplex(std::string ownId, ftDataSend *server, ftSearch *search);
ftDataMultiplex(std::string ownId, ftDataSend *server, ftSearch *search);
/* ftController Interface */
bool addTransferModule(ftTransferModule *mod, ftFileCreator *f);
bool removeTransferModule(std::string hash);
/* ftController Interface */
bool addTransferModule(ftTransferModule *mod, ftFileCreator *f);
bool removeTransferModule(std::string hash);
/* data interface */
/* get Details of File Transfers */
bool FileUploads(std::list<std::string> &hashs);
bool FileDownloads(std::list<std::string> &hashs);
bool FileDetails(std::string hash, uint32_t hintsflag, FileInfo &info);
/* data interface */
/* get Details of File Transfers */
bool FileUploads(std::list<std::string> &hashs);
bool FileDownloads(std::list<std::string> &hashs);
bool FileDetails(std::string hash, uint32_t hintsflag, FileInfo &info);
void deleteServers(const std::list<std::string>& serv_hash) ;
void deleteServers(const std::list<std::string>& serv_hash) ;
/*************** SEND INTERFACE (calls ftDataSend) *******************/
/*************** SEND INTERFACE (calls ftDataSend) *******************/
/* Client Send */
bool sendDataRequest(std::string peerId, std::string hash, uint64_t size,
uint64_t offset, uint32_t chunksize);
/* Client Send */
bool sendDataRequest(std::string peerId, std::string hash, uint64_t size,
uint64_t offset, uint32_t chunksize);
/* Server Send */
bool sendData(std::string peerId, std::string hash, uint64_t size,
uint64_t offset, uint32_t chunksize, void *data);
/* Server Send */
bool sendData(std::string peerId, std::string hash, uint64_t size,
uint64_t offset, uint32_t chunksize, void *data);
/*************** RECV INTERFACE (provides ftDataRecv) ****************/
/*************** RECV INTERFACE (provides ftDataRecv) ****************/
/* Client Recv */
virtual bool recvData(std::string peerId, std::string hash, uint64_t size, uint64_t offset, uint32_t chunksize, void *data);
/* Client Recv */
virtual bool recvData(std::string peerId, std::string hash, uint64_t size, uint64_t offset, uint32_t chunksize, void *data);
virtual bool recvFileMap(const std::string& peerId, const std::string& hash, uint32_t chunk_size, uint32_t nb_chunks, const std::vector<uint32_t>& compressed_map) ;
/* Server Recv */
virtual bool recvDataRequest(std::string peerId, std::string hash, uint64_t size, uint64_t offset, uint32_t chunksize);
/* Server Recv */
virtual bool recvDataRequest(std::string peerId, std::string hash, uint64_t size, uint64_t offset, uint32_t chunksize);
protected:
/* Overloaded from RsQueueThread */
virtual bool workQueued();
virtual bool doWork();
/* Overloaded from RsQueueThread */
virtual bool workQueued();
virtual bool doWork();
private:
/* Handling Job Queues */
bool handleRecvData(std::string peerId,
std::string hash, uint64_t size,
uint64_t offset, uint32_t chunksize, void *data);
/* Handling Job Queues */
bool handleRecvData(std::string peerId, std::string hash, uint64_t size, uint64_t offset, uint32_t chunksize, void *data);
bool handleRecvDataRequest(std::string peerId, std::string hash, uint64_t size, uint64_t offset, uint32_t chunksize);
bool handleSearchRequest(std::string peerId, std::string hash, uint64_t size, uint64_t offset, uint32_t chunksize);
bool handleRecvDataRequest(std::string peerId,
std::string hash, uint64_t size,
uint64_t offset, uint32_t chunksize);
/* 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);
bool handleSearchRequest(std::string peerId,
std::string hash, uint64_t size,
uint64_t offset, uint32_t chunksize);
RsMutex dataMtx;
/* 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);
std::map<std::string, ftClient> mClients;
std::map<std::string, ftFileProvider *> mServers;
RsMutex dataMtx;
std::list<ftRequest> mRequestQueue;
std::list<ftRequest> mSearchQueue;
std::map<std::string, time_t> mUnknownHashs;
std::map<std::string, ftClient> mClients;
std::map<std::string, ftFileProvider *> mServers;
ftDataSend *mDataSend;
ftSearch *mSearch;
std::string mOwnId;
std::list<ftRequest> mRequestQueue;
std::list<ftRequest> mSearchQueue;
std::map<std::string, time_t> mUnknownHashs;
ftDataSend *mDataSend;
ftSearch *mSearch;
std::string mOwnId;
friend class ftServer;
friend class ftServer;
};
#endif

View file

@ -409,6 +409,7 @@ void ftFileCreator::loadAvailabilityMap(const std::vector<uint32_t>& map,uint32_
chunkMap = ChunkMap(mSize,map,chunk_size,chunk_number,FileChunksInfo::ChunkStrategy(strategy)) ;
}
void ftFileCreator::storeAvailabilityMap(std::vector<uint32_t>& map,uint32_t& chunk_size,uint32_t& chunk_number,uint32_t& strategy)
{
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
@ -418,4 +419,11 @@ void ftFileCreator::storeAvailabilityMap(std::vector<uint32_t>& map,uint32_t& ch
strategy = (uint32_t)strat ;
}
void ftFileCreator::setSourceMap(const std::string& peer_id,uint32_t chunk_size,uint32_t nb_chunks,const std::vector<uint32_t>& compressed_map)
{
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
chunkMap.setPeerAvailabilityMap(peer_id,chunk_size,nb_chunks,compressed_map) ;
}

View file

@ -38,48 +38,64 @@
class ftFileCreator: public ftFileProvider
{
public:
public:
ftFileCreator(std::string savepath, uint64_t size, std::string hash, uint64_t recvd);
~ftFileCreator();
ftFileCreator(std::string savepath, uint64_t size, std::string hash, uint64_t recvd);
/* overloaded from FileProvider */
virtual bool getFileData(uint64_t offset, uint32_t &chunk_size, void *data);
bool finished() { return getRecvd() == getFileSize(); }
uint64_t getRecvd();
void getChunkMap(FileChunksInfo& info) ;
void setChunkStrategy(FileChunksInfo::ChunkStrategy s) ;
~ftFileCreator();
/*
* creation functions for FileCreator
*/
/* overloaded from FileProvider */
virtual bool getFileData(uint64_t offset, uint32_t &chunk_size, void *data);
bool finished() { return getRecvd() == getFileSize(); }
uint64_t getRecvd();
bool getMissingChunk(const std::string& peer_id,uint32_t size_hint,uint64_t& offset, uint32_t& size);
bool addFileData(uint64_t offset, uint32_t chunk_size, void *data);
void getChunkMap(FileChunksInfo& info) ;
void setChunkStrategy(FileChunksInfo::ChunkStrategy s) ;
void loadAvailabilityMap(const std::vector<uint32_t>& map,uint32_t chunk_size,uint32_t chunk_number,uint32_t chunk_strategy) ;
void storeAvailabilityMap(std::vector<uint32_t>& map,uint32_t& chunk_size,uint32_t& chunk_number,uint32_t& chunk_strategy) ;
/*
* creation functions for FileCreator
*/
protected:
// Gets a new variable-sized chunk of size "size_hint" from the given peer id. The returned size, "size" is
// at most equal to size_hint.
//
bool getMissingChunk(const std::string& peer_id,uint32_t size_hint,uint64_t& offset, uint32_t& size);
virtual int initializeFileAttrs();
// actually store data in the file, and update chunks info
//
bool addFileData(uint64_t offset, uint32_t chunk_size, void *data);
private:
// Load/save the availability map for the file being downloaded, in a compact/compressed form.
// This is used for
// - loading and saving info about the current transfers
// - getting info about current chunks for the GUI
// - sending availability info to the peers for which we also are a source
//
void loadAvailabilityMap(const std::vector<uint32_t>& map,uint32_t chunk_size,uint32_t chunk_number,uint32_t chunk_strategy) ;
void storeAvailabilityMap(std::vector<uint32_t>& map,uint32_t& chunk_size,uint32_t& chunk_number,uint32_t& chunk_strategy) ;
bool locked_printChunkMap();
int locked_notifyReceived(uint64_t offset, uint32_t chunk_size);
/*
* structure to track missing chunks
*/
uint64_t mStart;
uint64_t mEnd;
// This is called when receiving the availability map from a source peer, for the file being handled.
//
void setSourceMap(const std::string& peer_id,uint32_t chunk_size,uint32_t nb_chunks,const std::vector<uint32_t>& map) ;
std::map<uint64_t, ftChunk> mChunks;
protected:
ChunkMap chunkMap ;
virtual int initializeFileAttrs();
private:
bool locked_printChunkMap();
int locked_notifyReceived(uint64_t offset, uint32_t chunk_size);
/*
* structure to track missing chunks
*/
uint64_t mStart;
uint64_t mEnd;
std::map<uint64_t, ftChunk> mChunks;
ChunkMap chunkMap ;
};
#endif // FT_FILE_CREATOR_HEADER

View file

@ -156,7 +156,7 @@ void ftServer::connectToTurtleRouter(p3turtle *fts)
{
mTurtleRouter = fts ;
mFtSearch->addSearchMode(fts, RS_FILE_HINTS_TURTLE);
// mFtSearch->addSearchMode(fts, RS_FILE_HINTS_TURTLE);
mFtController->setTurtleRouter(fts) ;
}

View file

@ -68,7 +68,7 @@ const uint32_t RS_FILE_HINTS_LOCAL = 0x00000004;
const uint32_t RS_FILE_HINTS_REMOTE = 0x00000008;
const uint32_t RS_FILE_HINTS_DOWNLOAD = 0x00000010;
const uint32_t RS_FILE_HINTS_UPLOAD = 0x00000020;
const uint32_t RS_FILE_HINTS_TURTLE = 0x00000040;
//const uint32_t RS_FILE_HINTS_TURTLE = 0x00000040; // unused
const uint32_t RS_FILE_HINTS_NETWORK_WIDE = 0x00000080; // anonymously shared over network
const uint32_t RS_FILE_HINTS_BROWSABLE = 0x00000100; // browsable by friends

View file

@ -62,45 +62,45 @@ class FileInfo
/* old BaseInfo Entries */
public:
FileInfo() :flags(0), mId(0) { return; }
RsCertId id; /* key for matching everything */
int flags; /* INFO_TAG above */
FileInfo() :flags(0), mId(0) { return; }
RsCertId id; /* key for matching everything */
int flags; /* INFO_TAG above */
/* allow this to be tweaked by the GUI Model */
mutable unsigned int mId; /* (GUI) Model Id -> unique number */
/* allow this to be tweaked by the GUI Model */
mutable unsigned int mId; /* (GUI) Model Id -> unique number */
/* Old FileInfo Entries */
/* Old FileInfo Entries */
public:
static const int kRsFiStatusNone = 0;
static const int kRsFiStatusStall = 1;
static const int kRsFiStatusProgress = 2;
static const int kRsFiStatusDone = 2;
static const int kRsFiStatusNone = 0;
static const int kRsFiStatusStall = 1;
static const int kRsFiStatusProgress = 2;
static const int kRsFiStatusDone = 2;
/* FileInfo(); */
/* FileInfo(); */
int searchId; /* 0 if none */
std::string path;
std::string fname;
std::string hash;
std::string ext;
int searchId; /* 0 if none */
std::string path;
std::string fname;
std::string hash;
std::string ext;
uint64_t size;
uint64_t avail; /* how much we have */
int status;
uint64_t size;
uint64_t avail; /* how much we have */
int status;
bool inRecommend;
bool inRecommend;
double rank;
int age;
double rank;
int age;
/* Transfer Stuff */
uint64_t transfered;
double tfRate; /* in kbytes */
uint32_t downloadStatus; /* 0 = Err, 1 = Ok, 2 = Done */
std::list<TransferInfo> peers;
/* Transfer Stuff */
uint64_t transfered;
double tfRate; /* in kbytes */
uint32_t downloadStatus; /* 0 = Err, 1 = Ok, 2 = Done */
std::list<TransferInfo> peers;
time_t lastTS;
time_t lastTS;
};
std::ostream &operator<<(std::ostream &out, const FileInfo &info);
@ -248,15 +248,29 @@ class FileDetail
enum DwlPriority { Low = 0, Normal, High, Auto };
// Macro to read a bits array for compressed chunk maps
//
#define COMPRESSED_MAP_READ(A,j) (A[j >> 5] & (1 << (j & 0x11111)))
class FileChunksInfo
{
public:
enum ChunkState { CHUNK_DONE, CHUNK_ACTIVE, CHUNK_OUTSTANDING } ;
enum ChunkStrategy { CHUNK_STRATEGY_STREAMING, CHUNK_STRATEGY_RANDOM } ;
enum ChunkState { CHUNK_DONE, CHUNK_ACTIVE, CHUNK_OUTSTANDING } ;
enum ChunkStrategy { CHUNK_STRATEGY_STREAMING, CHUNK_STRATEGY_RANDOM } ;
uint64_t file_size ; // real size of the file
uint32_t chunk_size ; // size of chunks
// dl state of chunks. Only the last chunk may have size < chunk_size
std::vector<ChunkState> chunks ;
// For each source peer, gives the compressed bit map of have/don't have sate
std::vector<std::pair<std::string, std::vector<uint32_t> > > compressed_peer_availability_maps ;
// For each chunk (by chunk number), gives the completion of the chunk.
//
std::vector<std::pair<uint32_t,uint32_t> > active_chunks ;
uint64_t file_size ; // real size of the file
uint32_t chunk_size ; // size of chunks
std::vector<ChunkState> chunks ; // dl state of chunks. Only the last chunk may have size < chunk_size
};
/* class which encapsulates download details */

View file

@ -205,7 +205,7 @@ void p3turtle::manageTunnels()
time_t now = time(NULL) ;
bool tunnel_campain = false ;
if(now > _last_tunnel_campaign_time+REGULAR_TUNNEL_DIGGING_TIME)
if(now > _last_tunnel_campaign_time + REGULAR_TUNNEL_DIGGING_TIME)
{
#ifdef P3TURTLE_DEBUG
std::cerr << " Tunnel management: flaging all hashes for tunnels digging." << std::endl ;
@ -819,14 +819,12 @@ void p3turtle::handleRecvFileData(RsTurtleFileDataItem *item)
// Only file data transfer updates tunnels time_stamp field, to avoid maintaining tunnel that are incomplete.
tunnel.time_stamp = time(NULL) ;
// also update the hash time stamp to show that it's actually being downloaded.
_incoming_file_hashes[tunnel.hash].time_stamp = time(NULL) ;
// Let's figure out whether this reuqest is for us or not.
if(tunnel.local_src == mConnMgr->getOwnId()) // Yes, we have to pass on the data to the multiplexer
{
std::map<TurtleFileHash,TurtleFileHashInfo>::const_iterator it( _incoming_file_hashes.find(tunnel.hash) ) ;
std::map<TurtleFileHash,TurtleFileHashInfo>::iterator it( _incoming_file_hashes.find(tunnel.hash) ) ;
#ifdef P3TURTLE_DEBUG
assert(!tunnel.hash.empty()) ;
#endif
@ -848,6 +846,9 @@ void p3turtle::handleRecvFileData(RsTurtleFileDataItem *item)
vpid = tunnel.vpid ;
hash = tunnel.hash ;
size = hash_info.size ;
// also update the hash time stamp to show that it's actually being downloaded.
it->second.time_stamp = time(NULL) ;
}
else // No, it's a request we should forward down the pipe.
{
@ -876,6 +877,73 @@ void p3turtle::handleRecvFileData(RsTurtleFileDataItem *item)
// down _ft_server->getMultiplexer()->recvData()...in ftTransferModule::recvFileData
}
void p3turtle::handleRecvFileMap(RsTurtleFileMapItem *item)
{
#ifdef P3TURTLE_DEBUG
std::cerr << "p3Turtle: received file Map item:" << std::endl ;
item->print(std::cerr,1) ;
#endif
{
RsStackMutex stack(mTurtleMtx); /********** STACK LOCKED MTX ******/
std::map<TurtleTunnelId,TurtleTunnel>::iterator it(_local_tunnels.find(item->tunnel_id)) ;
if(it == _local_tunnels.end())
{
#ifdef P3TURTLE_DEBUG
std::cerr << "p3turtle: got file map with unknown tunnel id " << (void*)item->tunnel_id << std::endl ;
#endif
return ;
}
TurtleTunnel& tunnel(it->second) ;
// Only file data transfer updates tunnels time_stamp field, to avoid maintaining tunnel that are incomplete.
tunnel.time_stamp = time(NULL) ;
// Let's figure out whether this reuqest is for us or not.
if(tunnel.local_src == mConnMgr->getOwnId()) // Yes, we have to pass on the data to the multiplexer
{
std::map<TurtleFileHash,TurtleFileHashInfo>::iterator it( _incoming_file_hashes.find(tunnel.hash) ) ;
#ifdef P3TURTLE_DEBUG
assert(!tunnel.hash.empty()) ;
#endif
if(it==_incoming_file_hashes.end())
{
#ifdef P3TURTLE_DEBUG
std::cerr << "No tunnel for incoming data. Maybe the tunnel is being closed." << std::endl ;
#endif
return ;
}
const TurtleFileHashInfo& hash_info(it->second) ;
#ifdef P3TURTLE_DEBUG
std::cerr << " This is an endpoint for this file map." << std::endl ;
std::cerr << " Forwarding data to the multiplexer." << std::endl ;
std::cerr << " using peer_id=" << tunnel.vpid << ", hash=" << tunnel.hash << std::endl ;
#endif
// also update the hash time stamp to show that it's actually being downloaded.
it->second.time_stamp = time(NULL) ;
// we should check that there is no backward call to the turtle router!
//
_ft_server->getMultiplexer()->recvFileMap(tunnel.vpid,tunnel.hash,item->chunk_size,item->nb_chunks,item->compressed_map) ;
}
else // No, it's a request we should forward down the pipe.
{
#ifdef P3TURTLE_DEBUG
std::cerr << " Forwarding file map to peer " << tunnel.local_src << std::endl ;
#endif
RsTurtleFileMapItem *res_item = new RsTurtleFileMapItem(*item) ;
res_item->PeerId(tunnel.local_src) ;
sendItem(res_item) ;
return ;
}
}
}
// Send a data request into the correct tunnel for the given file hash
void p3turtle::sendDataRequest(const std::string& peerId, const std::string& hash, uint64_t, uint64_t offset, uint32_t chunksize)
{
@ -953,61 +1021,61 @@ void p3turtle::sendFileData(const std::string& peerId, const std::string& hash,
sendItem(item) ;
}
bool p3turtle::search(std::string hash, uint64_t, uint32_t hintflags, FileInfo &info) const
{
if(! (hintflags & RS_FILE_HINTS_TURTLE)) // this should not happen, but it's a security.
return false;
RsStackMutex stack(mTurtleMtx); /********** STACK LOCKED MTX ******/
#ifdef P3TURTLE_DEBUG
std::cerr << "p3turtle: received file search request for hash " << hash << "." << std::endl ;
#endif
std::map<TurtleFileHash,TurtleFileHashInfo>::const_iterator it = _incoming_file_hashes.find(hash) ;
if(_incoming_file_hashes.end() != it)
{
info.fname = it->second.name;
info.size = it->second.size;
info.hash = it->first;
for(unsigned int i=0;i<it->second.tunnels.size();++i)
{
TransferInfo ti;
ti.peerId = getTurtlePeerId(it->second.tunnels[i]);
ti.name = "Distant peer for hash=" + hash ;
ti.tfRate = 0;
info.peers.push_back(ti);
}
#ifdef P3TURTLE_DEBUG
std::cerr << " Found these tunnels for that hash:. "<< std::endl ;
for(unsigned int i=0;i<it->second.tunnels.size();++i)
std::cerr << " " << (void*)it->second.tunnels[i] << std::endl ;
std::cerr << " answered yes. "<< std::endl ;
#endif
return true ;
}
else
{
#ifdef P3TURTLE_DEBUG
std::cerr << " responding false." << std::endl ;
#endif
return false ;
}
}
// This function is actually not needed: Search request to the turtle router are:
// - distant search requests, handled by the router
// - search requests over files being downloaded, handled by rsFiles !!
//
//bool p3turtle::search(std::string hash, uint64_t, uint32_t hintflags, FileInfo &info) const
//{
// if(! (hintflags & RS_FILE_HINTS_TURTLE)) // this should not happen, but it's a security.
// return false;
//
// RsStackMutex stack(mTurtleMtx); /********** STACK LOCKED MTX ******/
//
//#ifdef P3TURTLE_DEBUG
// std::cerr << "p3turtle: received file search request for hash " << hash << "." << std::endl ;
//#endif
//
// std::map<TurtleFileHash,TurtleFileHashInfo>::const_iterator it = _incoming_file_hashes.find(hash) ;
//
// if(_incoming_file_hashes.end() != it)
// {
// info.fname = it->second.name;
// info.size = it->second.size;
// info.hash = it->first;
//
// for(unsigned int i=0;i<it->second.tunnels.size();++i)
// {
// TransferInfo ti;
// ti.peerId = getTurtlePeerId(it->second.tunnels[i]);
// ti.name = "Distant peer for hash=" + hash ;
// ti.tfRate = 0;
// info.peers.push_back(ti);
// }
//
//#ifdef P3TURTLE_DEBUG
// std::cerr << " Found these tunnels for that hash:. "<< std::endl ;
// for(unsigned int i=0;i<it->second.tunnels.size();++i)
// std::cerr << " " << (void*)it->second.tunnels[i] << std::endl ;
//
// std::cerr << " answered yes. "<< std::endl ;
//#endif
// return true ;
// }
// else
// {
//#ifdef P3TURTLE_DEBUG
// std::cerr << " responding false." << std::endl ;
//#endif
// return false ;
// }
//}
bool p3turtle::isTurtlePeer(const std::string& peer_id) const
{
RsStackMutex stack(mTurtleMtx); /********** STACK LOCKED MTX ******/
return _virtual_peers.find(peer_id) != _virtual_peers.end() ;
// if(it->second.tunnels.empty())
// return false ;
//
}
std::string p3turtle::getTurtlePeerId(TurtleTunnelId tid) const
@ -1472,7 +1540,7 @@ void p3turtle::returnSearchResult(RsTurtleSearchResultItem *item)
bool p3turtle::performLocalHashSearch(const TurtleFileHash& hash,FileInfo& info)
{
return rsFiles->FileDetails(hash, RS_FILE_HINTS_LOCAL | RS_FILE_HINTS_SPEC_ONLY, info);
return rsFiles->FileDetails(hash, RS_FILE_HINTS_LOCAL | RS_FILE_HINTS_SPEC_ONLY | RS_FILE_HINTS_DOWNLOAD, info);
}
static std::string printNumber(uint64_t num,bool hex=false)
@ -1480,7 +1548,7 @@ static std::string printNumber(uint64_t num,bool hex=false)
if(hex)
{
char tmp[100] ;
sprintf(tmp,"0x%08lx",num) ;
sprintf(tmp,"0x%08x%08x", uint32_t(num >> 32),uint32_t(num & ( (1<<32)-1 ))) ;
return std::string(tmp) ;
}
else

View file

@ -131,6 +131,11 @@
//
// Questions:
// - should tunnels be re-used ? nope. The only useful case would be when two peers are exchanging files, which happens quite rarely.
//
// TODO:
// - derive most packets from RsTurtleTunnelPacketItem, that can be handled
// as raw data through a tunnel. This will avoid replicating most of the
// tunnel handling code.
#ifndef MRK_PQI_TURTLE_H
@ -152,6 +157,7 @@ class p3AuthMgr;
class p3ConnectMgr;
class ftDataMultiplex;
class RsSerialiser;
static const int TURTLE_MAX_SEARCH_DEPTH = 6 ;
// This class is used to keep trace of requests (searches and tunnels).
@ -197,7 +203,7 @@ class TurtleFileHashInfo
// p3Config | ConfigChanged() | used to load/save .cfg file for turtle variales.
// -----------+------------------+------------------------------------------------------
//
class p3turtle: public p3Service, public pqiMonitor, public RsTurtle, public ftSearch, public p3Config
class p3turtle: public p3Service, public pqiMonitor, public RsTurtle,/* public ftSearch */ public p3Config
{
public:
p3turtle(p3ConnectMgr *cm,ftServer *m);
@ -250,10 +256,10 @@ class p3turtle: public p3Service, public pqiMonitor, public RsTurtle, public ftS
//
virtual int tick();
/************* from ftSearch *******************/
// Search function. This function looks into the file hashes currently handled , and sends back info.
//
virtual bool search(std::string hash, uint64_t size, uint32_t hintflags, FileInfo &info) const ;
// /************* from ftSearch *******************/
// // Search function. This function looks into the file hashes currently handled , and sends back info.
// //
// virtual bool search(std::string hash, uint64_t size, uint32_t hintflags, FileInfo &info) const ;
/************* from p3Config *******************/
virtual RsSerialiser *setupSerialiser() ;
@ -304,6 +310,7 @@ class p3turtle: public p3Service, public pqiMonitor, public RsTurtle, public ftS
void handleTunnelResult(RsTurtleTunnelOkItem *item);
void handleRecvFileRequest(RsTurtleFileRequestItem *item);
void handleRecvFileData(RsTurtleFileDataItem *item);
void handleRecvFileMap(RsTurtleFileMapItem*);
//------ Functions connecting the turtle router to other components.----------//

View file

@ -113,6 +113,22 @@ uint32_t RsTurtleFileDataItem::serial_size()
return s ;
}
uint32_t RsTurtleFileMapItem::serial_size()
{
uint32_t s = 0 ;
s += 8 ; // header
s += 4 ; // tunnel id
s += 4 ; // chunk_size
s += 4 ; // nb_chunks
s += 4 ; // compressed_map.size()
s += 4 * compressed_map.size() ;
return s ;
}
//
// ---------------------------------- Serialization ----------------------------------//
//
@ -146,6 +162,7 @@ RsItem *RsTurtleSerialiser::deserialise(void *data, uint32_t *size)
case RS_TURTLE_SUBTYPE_TUNNEL_OK : return new RsTurtleTunnelOkItem(data,*size) ;
case RS_TURTLE_SUBTYPE_FILE_REQUEST : return new RsTurtleFileRequestItem(data,*size) ;
case RS_TURTLE_SUBTYPE_FILE_DATA : return new RsTurtleFileDataItem(data,*size) ;
case RS_TURTLE_SUBTYPE_FILE_MAP : return new RsTurtleFileMapItem(data,*size) ;
default:
std::cerr << "Unknown packet type in RsTurtle!" << std::endl ;
@ -162,6 +179,44 @@ RsItem *RsTurtleSerialiser::deserialise(void *data, uint32_t *size)
}
bool RsTurtleFileMapItem::serialize(void *data,uint32_t& pktsize)
{
uint32_t tlvsize = serial_size();
uint32_t offset = 0;
if (pktsize < tlvsize)
return false; /* not enough space */
pktsize = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data,tlvsize,PacketId(), tlvsize);
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= setRawUInt32(data, tlvsize, &offset, tunnel_id);
ok &= setRawUInt32(data, tlvsize, &offset, chunk_size);
ok &= setRawUInt32(data, tlvsize, &offset, nb_chunks);
ok &= setRawUInt32(data, tlvsize, &offset, compressed_map.size());
for(uint32_t i=0;i<compressed_map.size() && ok;++i)
ok &= setRawUInt32(data, tlvsize, &offset, compressed_map[i]);
if (offset != tlvsize)
{
ok = false;
#ifdef RSSERIAL_DEBUG
std::cerr << "RsFileConfigSerialiser::serialiseTransfer() Size Error! " << std::endl;
#endif
}
return ok;
}
bool RsTurtleStringSearchRequestItem::serialize(void *data,uint32_t& pktsize)
{
uint32_t tlvsize = serial_size();
@ -350,6 +405,30 @@ bool RsTurtleSearchResultItem::serialize(void *data,uint32_t& pktsize)
return ok;
}
RsTurtleFileMapItem::RsTurtleFileMapItem(void *data,uint32_t pktsize)
: RsTurtleItem(RS_TURTLE_SUBTYPE_FILE_MAP)
{
#ifdef P3TURTLE_DEBUG
std::cerr << " type = search result" << std::endl ;
#endif
uint32_t offset = 8; // skip the header
uint32_t rssize = getRsItemSize(data);
/* add mandatory parts first */
bool ok = true ;
uint32_t s ;
ok &= getRawUInt32(data, pktsize, &offset, &tunnel_id);
ok &= getRawUInt32(data, pktsize, &offset, &chunk_size);
ok &= getRawUInt32(data, pktsize, &offset, &nb_chunks) ;
ok &= getRawUInt32(data, pktsize, &offset, &s) ;
compressed_map.resize(s) ;
for(uint32_t i=0;i<s && ok;++i)
ok &= getRawUInt32(data, pktsize, &offset, &(compressed_map[i])) ;
}
RsTurtleSearchResultItem::RsTurtleSearchResultItem(void *data,uint32_t pktsize)
: RsTurtleItem(RS_TURTLE_SUBTYPE_SEARCH_RESULT)
{
@ -742,3 +821,18 @@ std::ostream& RsTurtleFileDataItem::print(std::ostream& o, uint16_t)
return o ;
}
std::ostream& RsTurtleFileMapItem::print(std::ostream& o, uint16_t)
{
o << "File map item:" << std::endl ;
o << " tunnel id : " << (void*)tunnel_id << std::endl ;
o << " chunk size: " << chunk_size << std::endl ;
o << " nb chunks : " << nb_chunks << std::endl ;
o << " map : " ;
for(uint32_t i=0;i<compressed_map.size();++i)
o << (void*)compressed_map[i] << std::endl ;
return o ;
}

View file

@ -17,6 +17,7 @@ const uint8_t RS_TURTLE_SUBTYPE_TUNNEL_CLOSED = 0x06 ;
const uint8_t RS_TURTLE_SUBTYPE_FILE_REQUEST = 0x07 ;
const uint8_t RS_TURTLE_SUBTYPE_FILE_DATA = 0x08 ;
const uint8_t RS_TURTLE_SUBTYPE_REGEXP_SEARCH_REQUEST = 0x09 ;
const uint8_t RS_TURTLE_SUBTYPE_FILE_MAP = 0x10 ;
/***********************************************************************************/
/* Basic Turtle Item Class */
@ -142,36 +143,6 @@ class RsTurtleTunnelOkItem: public RsTurtleItem
virtual uint32_t serial_size() ;
};
#ifdef A_VIRER
class RsTurtleCloseTunnelItem: public RsTurtleItem
{
public:
RsTurtleCloseTunnelItem() : RsTurtleItem(RS_TURTLE_SUBTYPE_CLOSE_TUNNEL) {}
RsTurtleCloseTunnelItem(void *data,uint32_t size) ; // deserialization
uint32_t tunnel_id ; // id of the tunnel to close.
virtual std::ostream& print(std::ostream& o, uint16_t) ;
protected:
virtual bool serialize(void *data,uint32_t& size) ;
virtual uint32_t serial_size() ;
};
class RsTurtleTunnelClosedItem: public RsTurtleItem
{
public:
RsTurtleTunnelClosedItem() : RsTurtleItem(RS_TURTLE_SUBTYPE_TUNNEL_CLOSED) {}
RsTurtleTunnelClosedItem(void *data,uint32_t size) ; // deserialization
uint32_t tunnel_id ; // id of the tunnel to close.
virtual std::ostream& print(std::ostream& o, uint16_t) ;
protected:
virtual bool serialize(void *data,uint32_t& size) ;
virtual uint32_t serial_size() ;
};
#endif
/***********************************************************************************/
/* Turtle File Transfer item classes */
/***********************************************************************************/
@ -210,6 +181,27 @@ class RsTurtleFileDataItem: public RsTurtleItem
virtual uint32_t serial_size() ;
};
class RsTurtleFileMapItem: public RsTurtleItem
{
public:
RsTurtleFileMapItem() : RsTurtleItem(RS_TURTLE_SUBTYPE_FILE_MAP) {}
RsTurtleFileMapItem(void *data,uint32_t size) ; // deserialization
uint32_t tunnel_id ; // id of the tunnel to travel through. Also used for identifying the file source
uint32_t chunk_size ; // fixed size of chunks, as seen from the source, for the given map.
uint32_t nb_chunks ; // number of chunks in the file. The last two infos are redundant, as we can recompute
// this info from the file size, but this allows a security check.
std::vector<uint32_t> compressed_map ; // Map info for the file in compressed format. Each *bit* in the array uint's says "I have" or "I don't have"
// by default, we suppose the peer has all the chunks. This info will thus be and-ed
// with the default file map for this source.
virtual std::ostream& print(std::ostream& o, uint16_t) ;
virtual bool serialize(void *data,uint32_t& size) ;
virtual uint32_t serial_size() ;
};
/***********************************************************************************/
/* Turtle Serialiser class */
/***********************************************************************************/

View file

@ -32,18 +32,57 @@
/* RsIface Thread Wrappers */
#undef RSTHREAD_SELF_LOCKING_GUARD
class RsMutex
{
public:
RsMutex() { pthread_mutex_init(&realMutex, NULL); }
~RsMutex() { pthread_mutex_destroy(&realMutex); }
void lock() { pthread_mutex_lock(&realMutex); }
void unlock() { pthread_mutex_unlock(&realMutex); }
bool trylock() { return (0 == pthread_mutex_trylock(&realMutex)); }
RsMutex()
{
pthread_mutex_init(&realMutex, NULL);
#ifdef RSTHREAD_SELF_LOCKING_GUARD
_thread_id = 0 ;
#endif
}
~RsMutex()
{
pthread_mutex_destroy(&realMutex);
}
void lock()
{
#ifdef RSTHREAD_SELF_LOCKING_GUARD
if(!trylock())
if(!pthread_equal(_thread_id,pthread_self()))
#endif
pthread_mutex_lock(&realMutex);
#ifdef RSTHREAD_SELF_LOCKING_GUARD
_thread_id = pthread_self() ;
++_cnt ;
#endif
}
void unlock()
{
#ifdef RSTHREAD_SELF_LOCKING_GUARD
if(--_cnt == 0)
{
#endif
pthread_mutex_unlock(&realMutex);
#ifdef RSTHREAD_SELF_LOCKING_GUARD
_thread_id = 0 ;
}
#endif
}
bool trylock() { return (0 == pthread_mutex_trylock(&realMutex)); }
private:
pthread_mutex_t realMutex;
pthread_mutex_t realMutex;
#ifdef RSTHREAD_SELF_LOCKING_GUARD
pthread_t _thread_id ;
uint32_t _cnt ;
#endif
};
class RsStackMutex