2012-10-09 19:07:51 -04:00
|
|
|
#ifdef WINDOWS_SYS
|
|
|
|
#include "util/rswin.h"
|
|
|
|
#endif // WINDOWS_SYS
|
|
|
|
|
2008-07-01 22:36:39 -04:00
|
|
|
#include "ftfileprovider.h"
|
2010-01-11 11:00:42 -05:00
|
|
|
#include "ftchunkmap.h"
|
2008-07-01 22:36:39 -04:00
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
#include "util/rsdir.h"
|
2008-11-02 08:53:17 -05:00
|
|
|
#include <stdlib.h>
|
2009-05-11 10:30:53 -04:00
|
|
|
#include <stdio.h>
|
2008-08-09 13:03:24 -04:00
|
|
|
|
2010-10-15 18:12:29 -04:00
|
|
|
/********
|
|
|
|
* #define DEBUG_FT_FILE_PROVIDER 1
|
2010-11-13 06:46:17 -05:00
|
|
|
* #define DEBUG_TRANSFERS 1 // TO GET TIMESTAMPS of DATA READING
|
2010-10-15 18:12:29 -04:00
|
|
|
********/
|
2010-05-16 19:26:45 -04:00
|
|
|
|
2010-11-11 15:51:17 -05:00
|
|
|
#ifdef DEBUG_TRANSFERS
|
|
|
|
#include "util/rsprint.h"
|
|
|
|
#include <iomanip>
|
|
|
|
#endif
|
|
|
|
|
2010-07-27 12:05:21 -04:00
|
|
|
static const time_t UPLOAD_CHUNK_MAPS_TIME = 20 ; // time to ask for a new chunkmap from uploaders in seconds.
|
2010-01-11 11:00:42 -05:00
|
|
|
|
2011-02-19 19:59:47 -05:00
|
|
|
ftFileProvider::ftFileProvider(const std::string& path, uint64_t size, const std::string& hash)
|
2011-07-04 18:59:39 -04:00
|
|
|
: mSize(size), hash(hash), file_name(path), fd(NULL), ftcMutex("ftFileProvider")
|
2008-10-22 14:12:58 -04:00
|
|
|
{
|
2010-02-15 13:03:27 -05:00
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
2010-07-27 12:05:21 -04:00
|
|
|
|
2009-02-08 08:04:32 -05:00
|
|
|
#ifdef DEBUG_FT_FILE_PROVIDER
|
2009-02-06 15:42:01 -05:00
|
|
|
std::cout << "Creating file provider for " << hash << std::endl ;
|
2009-02-08 08:04:32 -05:00
|
|
|
#endif
|
2008-07-01 22:36:39 -04:00
|
|
|
}
|
2008-07-02 00:05:58 -04:00
|
|
|
|
2011-01-08 18:27:33 -05:00
|
|
|
ftFileProvider::~ftFileProvider()
|
|
|
|
{
|
2012-11-11 05:29:03 -05:00
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
2009-02-08 08:04:32 -05:00
|
|
|
#ifdef DEBUG_FT_FILE_PROVIDER
|
2011-01-08 18:27:33 -05:00
|
|
|
std::cout << "ftFileProvider::~ftFileProvider(): Destroying file provider for " << hash << std::endl ;
|
2009-02-08 08:04:32 -05:00
|
|
|
#endif
|
2008-07-01 22:36:39 -04:00
|
|
|
if (fd!=NULL) {
|
|
|
|
fclose(fd);
|
2010-03-28 16:46:45 -04:00
|
|
|
fd = NULL ;
|
2011-01-08 18:27:33 -05:00
|
|
|
#ifdef DEBUG_FT_FILE_PROVIDER
|
|
|
|
std::cout << "ftFileProvider::~ftFileProvider(): closed file: " << hash << std::endl ;
|
|
|
|
#endif
|
2008-07-01 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-22 14:12:58 -04:00
|
|
|
bool ftFileProvider::fileOk()
|
|
|
|
{
|
2010-07-27 12:05:21 -04:00
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
2008-10-22 14:12:58 -04:00
|
|
|
return (fd != NULL);
|
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
std::string ftFileProvider::getHash()
|
|
|
|
{
|
2008-10-22 14:12:58 -04:00
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
2008-08-03 08:45:53 -04:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t ftFileProvider::getFileSize()
|
|
|
|
{
|
2008-10-22 14:12:58 -04:00
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
|
|
|
return mSize;
|
2008-08-03 08:45:53 -04:00
|
|
|
}
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
bool ftFileProvider::FileDetails(FileInfo &info)
|
|
|
|
{
|
2009-02-04 16:35:39 -05:00
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
2008-08-09 13:03:24 -04:00
|
|
|
info.hash = hash;
|
2008-10-22 14:12:58 -04:00
|
|
|
info.size = mSize;
|
2008-08-09 13:03:24 -04:00
|
|
|
info.path = file_name;
|
|
|
|
info.fname = RsDirUtil::getTopDir(file_name);
|
2010-07-27 12:05:21 -04:00
|
|
|
|
|
|
|
info.transfered = 0 ; // unused
|
|
|
|
info.lastTS = 0;
|
2009-02-04 16:35:39 -05:00
|
|
|
info.status = FT_STATE_DOWNLOADING ;
|
|
|
|
|
|
|
|
info.peers.clear() ;
|
2010-07-27 12:05:21 -04:00
|
|
|
float total_transfer_rate = 0.0f ;
|
|
|
|
|
|
|
|
for(std::map<std::string,PeerUploadInfo>::const_iterator it(uploading_peers.begin());it!=uploading_peers.end();++it)
|
|
|
|
{
|
|
|
|
TransferInfo inf ;
|
|
|
|
inf.peerId = it->first ;
|
|
|
|
inf.status = FT_STATE_DOWNLOADING ;
|
|
|
|
inf.name = info.fname ;
|
|
|
|
inf.transfered = it->second.req_loc ;
|
2009-02-04 16:35:39 -05:00
|
|
|
|
2010-07-27 12:05:21 -04:00
|
|
|
inf.tfRate = it->second.transfer_rate/1024.0 ;
|
|
|
|
total_transfer_rate += it->second.transfer_rate ;
|
|
|
|
info.lastTS = std::max(info.lastTS,it->second.lastTS);
|
2009-02-04 19:01:51 -05:00
|
|
|
|
2010-07-27 12:05:21 -04:00
|
|
|
info.peers.push_back(inf) ;
|
|
|
|
}
|
|
|
|
info.tfRate = total_transfer_rate/1024.0 ;
|
2009-02-04 16:35:39 -05:00
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
/* Use req_loc / req_size to estimate data rate */
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-27 12:05:21 -04:00
|
|
|
bool ftFileProvider::purgeOldPeers(time_t now,uint32_t max_duration)
|
|
|
|
{
|
2010-11-29 12:21:51 -05:00
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2011-01-08 18:27:33 -05:00
|
|
|
#ifdef DEBUG_FT_FILE_PROVIDER
|
|
|
|
std::cerr << "ftFileProvider::purgeOldPeers(): " << (void*)this << ": examining peers." << std::endl ;
|
|
|
|
#endif
|
2010-07-27 12:05:21 -04:00
|
|
|
bool ret = true ;
|
|
|
|
for(std::map<std::string,PeerUploadInfo>::iterator it(uploading_peers.begin());it!=uploading_peers.end();)
|
|
|
|
if( (*it).second.lastTS+max_duration < (uint32_t)now)
|
|
|
|
{
|
2011-01-08 18:27:33 -05:00
|
|
|
#ifdef DEBUG_FT_FILE_PROVIDER
|
|
|
|
std::cerr << "ftFileProvider::purgeOldPeers(): " << (void*)this << ": peer " << it->first << " is too old. Removing." << std::endl ;
|
|
|
|
#endif
|
2010-07-27 12:05:21 -04:00
|
|
|
std::map<std::string,PeerUploadInfo>::iterator tmp = it ;
|
|
|
|
++tmp ;
|
|
|
|
uploading_peers.erase(it) ;
|
|
|
|
it=tmp ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-08 18:27:33 -05:00
|
|
|
#ifdef DEBUG_FT_FILE_PROVIDER
|
|
|
|
std::cerr << "ftFileProvider::purgeOldPeers(): " << (void*)this << ": peer " << it->first << " will be kept." << std::endl ;
|
|
|
|
#endif
|
2010-07-27 12:05:21 -04:00
|
|
|
ret = false ;
|
|
|
|
++it ;
|
|
|
|
}
|
|
|
|
return ret ;
|
|
|
|
}
|
|
|
|
|
2010-01-11 11:00:42 -05:00
|
|
|
void ftFileProvider::getAvailabilityMap(CompressedChunkMap& cmap)
|
|
|
|
{
|
|
|
|
// We are here because the file we deal with is complete. So we return a plain map.
|
|
|
|
//
|
|
|
|
ChunkMap::buildPlainMap(mSize,cmap) ;
|
|
|
|
}
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
|
2010-07-27 12:05:21 -04:00
|
|
|
bool ftFileProvider::getFileData(const std::string& peer_id,uint64_t offset, uint32_t &chunk_size, void *data)
|
2008-07-01 22:36:39 -04:00
|
|
|
{
|
2008-10-22 14:12:58 -04:00
|
|
|
/* dodgey checking outside of mutex...
|
|
|
|
* much check again inside FileAttrs().
|
|
|
|
*/
|
|
|
|
if (fd == NULL)
|
|
|
|
if (!initializeFileAttrs())
|
|
|
|
return false;
|
|
|
|
|
2009-02-04 16:35:39 -05:00
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
2008-10-22 14:12:58 -04:00
|
|
|
|
2008-07-02 00:05:58 -04:00
|
|
|
/*
|
|
|
|
* FIXME: Warning of comparison between unsigned and signed int?
|
|
|
|
*/
|
2008-11-13 18:03:46 -05:00
|
|
|
|
2013-04-01 17:18:58 -04:00
|
|
|
if(offset >= mSize)
|
|
|
|
{
|
|
|
|
std::cerr << "ftFileProvider::getFileData(): request (" << offset << ") exceeds file size (" << mSize << "! " << std::endl;
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
2008-11-13 18:03:46 -05:00
|
|
|
uint32_t data_size = chunk_size;
|
|
|
|
uint64_t base_loc = offset;
|
2008-07-01 22:36:39 -04:00
|
|
|
|
2008-10-22 14:12:58 -04:00
|
|
|
if (base_loc + data_size > mSize)
|
2008-07-01 22:36:39 -04:00
|
|
|
{
|
2008-10-22 14:12:58 -04:00
|
|
|
data_size = mSize - base_loc;
|
2008-11-20 17:59:58 -05:00
|
|
|
chunk_size = mSize - base_loc;
|
2008-07-01 22:36:39 -04:00
|
|
|
std::cerr <<"Chunk Size greater than total file size, adjusting chunk size " << data_size << std::endl;
|
|
|
|
}
|
|
|
|
|
2011-11-02 09:58:33 -04:00
|
|
|
if(data_size > 0 && data != NULL)
|
2009-07-27 16:31:56 -04:00
|
|
|
{
|
2008-07-02 00:05:58 -04:00
|
|
|
/*
|
2011-11-02 09:58:33 -04:00
|
|
|
* seek for base_loc
|
|
|
|
*/
|
2009-08-29 14:55:13 -04:00
|
|
|
fseeko64(fd, base_loc, SEEK_SET);
|
2008-07-01 22:36:39 -04:00
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
// Data space allocated by caller.
|
|
|
|
//void *data = malloc(chunk_size);
|
2008-07-02 00:05:58 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* read the data
|
|
|
|
*/
|
|
|
|
|
2008-07-01 22:36:39 -04:00
|
|
|
if (1 != fread(data, data_size, 1, fd))
|
|
|
|
{
|
2010-02-08 09:06:43 -05:00
|
|
|
#ifdef DEBUG_FT_FILE_PROVIDER
|
|
|
|
std::cerr << "ftFileProvider::getFileData() Failed to get data. Data_size=" << data_size << ", base_loc=" << base_loc << " !" << std::endl;
|
|
|
|
#endif
|
2009-08-09 11:55:31 -04:00
|
|
|
//free(data); No!! It's already freed upwards in ftDataMultiplex::locked_handleServerRequest()
|
2008-07-01 22:36:39 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-02 00:05:58 -04:00
|
|
|
/*
|
|
|
|
* Update status of ftFileStatus to reflect last usage (for GUI display)
|
2008-07-01 22:36:39 -04:00
|
|
|
* We need to store.
|
|
|
|
* (a) Id,
|
|
|
|
* (b) Offset,
|
|
|
|
* (c) Size,
|
|
|
|
* (d) timestamp
|
|
|
|
*/
|
|
|
|
|
2010-07-27 12:05:21 -04:00
|
|
|
// This creates the peer info, and updates it.
|
|
|
|
//
|
|
|
|
time_t now = time(NULL) ;
|
|
|
|
uploading_peers[peer_id].updateStatus(offset,data_size,now) ;
|
2010-11-11 15:51:17 -05:00
|
|
|
|
|
|
|
#ifdef DEBUG_TRANSFERS
|
|
|
|
std::cerr << "ftFileProvider::getFileData() ";
|
|
|
|
std::cerr << " at " << RsUtil::AccurateTimeString();
|
|
|
|
std::cerr << " hash: " << hash;
|
|
|
|
std::cerr << " for peerId: " << peer_id;
|
|
|
|
std::cerr << " offset: " << offset;
|
|
|
|
std::cerr << " chunkSize: " << chunk_size;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2010-07-27 12:05:21 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-02 09:58:33 -04:00
|
|
|
std::cerr << "No data to read, or NULL buffer used" << std::endl;
|
2010-07-27 12:05:21 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2009-02-06 15:42:01 -05:00
|
|
|
|
2010-07-27 12:05:21 -04:00
|
|
|
void ftFileProvider::PeerUploadInfo::updateStatus(uint64_t offset,uint32_t data_size,time_t now)
|
|
|
|
{
|
|
|
|
lastTS = now ;
|
|
|
|
long int diff = (long int)now - (long int)lastTS_t ; // in bytes/s. Average over multiple samples
|
2009-02-06 15:42:01 -05:00
|
|
|
|
2009-02-08 08:04:32 -05:00
|
|
|
#ifdef DEBUG_FT_FILE_PROVIDER
|
2010-07-27 12:05:21 -04:00
|
|
|
std::cout << "diff = " << diff << std::endl ;
|
2009-02-08 08:04:32 -05:00
|
|
|
#endif
|
2009-02-06 15:42:01 -05:00
|
|
|
|
2010-07-27 12:05:21 -04:00
|
|
|
if(diff > 3)
|
|
|
|
{
|
|
|
|
transfer_rate = total_size / (float)diff ;
|
2009-02-06 15:42:01 -05:00
|
|
|
#ifdef DEBUG_FT_FILE_PROVIDER
|
2010-07-27 12:05:21 -04:00
|
|
|
std::cout << "updated TR = " << transfer_rate << ", total_size=" << total_size << std::endl ;
|
2009-02-06 15:42:01 -05:00
|
|
|
#endif
|
2010-07-27 12:05:21 -04:00
|
|
|
lastTS_t = now ;
|
|
|
|
total_size = 0 ;
|
2008-07-01 22:36:39 -04:00
|
|
|
}
|
2010-07-27 12:05:21 -04:00
|
|
|
|
|
|
|
req_loc = offset;
|
|
|
|
req_size = data_size;
|
|
|
|
total_size += req_size ;
|
2008-07-01 22:36:39 -04:00
|
|
|
}
|
|
|
|
|
2010-01-11 11:00:42 -05:00
|
|
|
void ftFileProvider::setClientMap(const std::string& peer_id,const CompressedChunkMap& cmap)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2010-07-27 12:05:21 -04:00
|
|
|
// Create by default.
|
|
|
|
uploading_peers[peer_id].client_chunk_map = cmap ;
|
|
|
|
uploading_peers[peer_id].client_chunk_map_stamp = time(NULL) ;
|
2010-01-11 11:00:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void ftFileProvider::getClientMap(const std::string& peer_id,CompressedChunkMap& cmap,bool& map_is_too_old)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2010-07-27 12:05:21 -04:00
|
|
|
PeerUploadInfo& pui(uploading_peers[peer_id]) ;
|
|
|
|
|
|
|
|
time_t now = time(NULL) ;
|
|
|
|
|
|
|
|
if(now - pui.client_chunk_map_stamp > UPLOAD_CHUNK_MAPS_TIME)
|
2010-01-11 11:00:42 -05:00
|
|
|
{
|
|
|
|
map_is_too_old = true ;
|
2010-07-27 12:05:21 -04:00
|
|
|
pui.client_chunk_map_stamp = now ; // to avoid re-asking before the TTL
|
2010-01-11 11:00:42 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
map_is_too_old = false ;
|
|
|
|
|
2010-07-27 12:05:21 -04:00
|
|
|
cmap = pui.client_chunk_map;
|
2010-01-11 11:00:42 -05:00
|
|
|
}
|
|
|
|
|
2008-07-01 22:36:39 -04:00
|
|
|
int ftFileProvider::initializeFileAttrs()
|
|
|
|
{
|
2010-03-07 09:42:13 -05:00
|
|
|
#ifdef DEBUG_FT_FILE_PROVIDER
|
|
|
|
std::cerr << "ftFileProvider::initializeFileAttrs() Filename: " << file_name << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
RsStackMutex stack(ftcMutex); /********** STACK LOCKED MTX ******/
|
2008-10-22 14:12:58 -04:00
|
|
|
if (fd)
|
|
|
|
return 1;
|
|
|
|
|
2008-07-02 00:05:58 -04:00
|
|
|
/*
|
2010-03-07 09:42:13 -05:00
|
|
|
* check if the file exists
|
|
|
|
*/
|
|
|
|
|
2008-07-01 22:36:39 -04:00
|
|
|
{
|
2010-03-07 09:42:13 -05:00
|
|
|
#ifdef DEBUG_FT_FILE_PROVIDER
|
|
|
|
std::cerr << "ftFileProvider::initializeFileAttrs() trying (r+b) " << std::endl;
|
|
|
|
#endif
|
2008-07-01 22:36:39 -04:00
|
|
|
}
|
|
|
|
|
2008-07-02 00:05:58 -04:00
|
|
|
/*
|
2010-03-07 09:42:13 -05:00
|
|
|
* attempt to open file
|
|
|
|
*/
|
|
|
|
|
2011-04-03 19:11:38 -04:00
|
|
|
fd = RsDirUtil::rs_fopen(file_name.c_str(), "r+b");
|
2008-07-01 22:36:39 -04:00
|
|
|
if (!fd)
|
|
|
|
{
|
2008-10-29 16:58:23 -04:00
|
|
|
std::cerr << "ftFileProvider::initializeFileAttrs() Failed to open (r+b): ";
|
|
|
|
std::cerr << file_name << std::endl;
|
2008-07-01 22:36:39 -04:00
|
|
|
|
2008-11-13 18:03:46 -05:00
|
|
|
/* try opening read only */
|
2011-04-03 19:11:38 -04:00
|
|
|
fd = RsDirUtil::rs_fopen(file_name.c_str(), "rb");
|
2008-11-13 18:03:46 -05:00
|
|
|
if (!fd)
|
|
|
|
{
|
|
|
|
std::cerr << "ftFileProvider::initializeFileAttrs() Failed to open (rb): ";
|
|
|
|
std::cerr << file_name << std::endl;
|
2010-03-07 09:42:13 -05:00
|
|
|
|
2008-11-13 18:03:46 -05:00
|
|
|
/* try opening read only */
|
|
|
|
return 0;
|
|
|
|
}
|
2008-07-01 22:36:39 -04:00
|
|
|
}
|
2011-01-08 18:27:33 -05:00
|
|
|
#ifdef DEBUG_FT_FILE_PROVIDER
|
|
|
|
std::cerr << "ftFileProvider:: openned file " << file_name << std::endl ;
|
|
|
|
#endif
|
2008-07-01 22:36:39 -04:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2010-07-21 19:14:10 -04:00
|
|
|
|
|
|
|
|