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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ftDataMultiplexModule.
|
|
|
|
*
|
|
|
|
* This multiplexes the data from PQInterface to the ftTransferModules.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ft/ftdatamultiplex.h"
|
|
|
|
#include "ft/fttransfermodule.h"
|
|
|
|
#include "ft/ftfilecreator.h"
|
|
|
|
#include "ft/ftfileprovider.h"
|
|
|
|
#include "ft/ftsearch.h"
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
/* For Thread Behaviour */
|
|
|
|
const uint32_t DMULTIPLEX_MIN = 10; /* 1ms sleep */
|
|
|
|
const uint32_t DMULTIPLEX_MAX = 1000; /* 1 sec sleep */
|
|
|
|
const double DMULTIPLEX_RELAX = 0.5; /* ??? */
|
2008-08-23 17:28:08 -04:00
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
/******
|
|
|
|
* #define MPLEX_DEBUG 1
|
|
|
|
*****/
|
2008-08-03 08:45:53 -04:00
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
ftClient::ftClient(ftTransferModule *module, ftFileCreator *creator)
|
|
|
|
:mModule(module), mCreator(creator)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t FT_DATA = 0x0001;
|
|
|
|
const uint32_t FT_DATA_REQ = 0x0002;
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
ftRequest::ftRequest(uint32_t type, std::string peerId, std::string hash, uint64_t size, uint64_t offset, uint32_t chunk, void *data)
|
|
|
|
:mType(type), mPeerId(peerId), mHash(hash), mSize(size),
|
2008-07-23 18:01:59 -04:00
|
|
|
mOffset(offset), mChunk(chunk), mData(data)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-29 21:07:24 -04:00
|
|
|
ftDataMultiplex::ftDataMultiplex(std::string ownId, ftDataSend *server, ftSearch *search)
|
2008-08-03 08:45:53 -04:00
|
|
|
:RsQueueThread(DMULTIPLEX_MIN, DMULTIPLEX_MAX, DMULTIPLEX_RELAX),
|
2008-08-29 21:07:24 -04:00
|
|
|
mDataSend(server), mSearch(search), mOwnId(ownId)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ftDataMultiplex::addTransferModule(ftTransferModule *mod, ftFileCreator *f)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/
|
|
|
|
std::map<std::string, ftClient>::iterator it;
|
|
|
|
if (mClients.end() != (it = mClients.find(mod->hash())))
|
|
|
|
{
|
|
|
|
/* error */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mClients[mod->hash()] = ftClient(mod, f);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
bool ftDataMultiplex::removeTransferModule(std::string hash)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/
|
|
|
|
std::map<std::string, ftClient>::iterator it;
|
2008-08-09 13:03:24 -04:00
|
|
|
if (mClients.end() == (it = mClients.find(hash)))
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
|
|
|
/* error */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mClients.erase(it);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
bool ftDataMultiplex::FileUploads(std::list<std::string> &hashs)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/
|
|
|
|
std::map<std::string, ftFileProvider *>::iterator sit;
|
|
|
|
for(sit = mServers.begin(); sit != mServers.end(); sit++)
|
|
|
|
{
|
|
|
|
hashs.push_back(sit->first);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ftDataMultiplex::FileDownloads(std::list<std::string> &hashs)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/
|
|
|
|
std::map<std::string, ftClient>::iterator cit;
|
|
|
|
for(cit = mClients.begin(); cit != mClients.end(); cit++)
|
|
|
|
{
|
|
|
|
hashs.push_back(cit->first);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ftDataMultiplex::FileDetails(std::string hash, uint32_t hintsflag, FileInfo &info)
|
|
|
|
{
|
2008-11-04 18:12:53 -05:00
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::FileDetails(";
|
|
|
|
std::cerr << hash << ", " << hintsflag << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/
|
|
|
|
std::map<std::string, ftFileProvider *>::iterator sit;
|
|
|
|
sit = mServers.find(hash);
|
|
|
|
if (sit != mServers.end())
|
|
|
|
{
|
2008-11-04 18:12:53 -05:00
|
|
|
|
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::FileDetails()";
|
|
|
|
std::cerr << " Found ftFileProvider!";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
(sit->second)->FileDetails(info);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, ftClient>::iterator cit;
|
|
|
|
if (mClients.end() != (cit = mClients.find(hash)))
|
|
|
|
{
|
2008-11-04 18:12:53 -05:00
|
|
|
|
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::FileDetails()";
|
|
|
|
std::cerr << " Found ftFileCreator!";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
//(cit->second).mModule->FileDetails(info);
|
|
|
|
(cit->second).mCreator->FileDetails(info);
|
|
|
|
return true;
|
|
|
|
}
|
2008-11-04 18:12:53 -05:00
|
|
|
|
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::FileDetails()";
|
|
|
|
std::cerr << " Found nothing";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-08-09 13:03:24 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
/* data interface */
|
|
|
|
|
|
|
|
/*************** SEND INTERFACE (calls ftDataSend) *******************/
|
|
|
|
|
|
|
|
/* Client Send */
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftDataMultiplex::sendDataRequest(std::string peerId,
|
|
|
|
std::string hash, uint64_t size, uint64_t offset, uint32_t chunksize)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
2008-08-23 17:28:08 -04:00
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::sendDataRequest() Client Send";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-08-03 08:45:53 -04:00
|
|
|
return mDataSend->sendDataRequest(peerId,hash,size,offset,chunksize);
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Server Send */
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftDataMultiplex::sendData(std::string peerId,
|
|
|
|
std::string hash, uint64_t size,
|
|
|
|
uint64_t offset, uint32_t chunksize, void *data)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
2008-08-23 17:28:08 -04:00
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::sendData() Server Send";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-08-03 08:45:53 -04:00
|
|
|
return mDataSend->sendData(peerId,hash,size,offset,chunksize,data);
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************** RECV INTERFACE (provides ftDataRecv) ****************/
|
|
|
|
|
|
|
|
/* Client Recv */
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftDataMultiplex::recvData(std::string peerId,
|
|
|
|
std::string hash, uint64_t size,
|
|
|
|
uint64_t offset, uint32_t chunksize, void *data)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
2008-08-23 17:28:08 -04:00
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::recvData() Client Recv";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-07-23 18:01:59 -04:00
|
|
|
/* Store in Queue */
|
|
|
|
RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/
|
|
|
|
mRequestQueue.push_back(
|
2008-08-03 08:45:53 -04:00
|
|
|
ftRequest(FT_DATA,peerId,hash,size,offset,chunksize,data));
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Server Recv */
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftDataMultiplex::recvDataRequest(std::string peerId,
|
|
|
|
std::string hash, uint64_t size,
|
|
|
|
uint64_t offset, uint32_t chunksize)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
2008-08-23 17:28:08 -04:00
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::recvDataRequest() Server Recv";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-07-23 18:01:59 -04:00
|
|
|
/* Store in Queue */
|
|
|
|
RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/
|
|
|
|
mRequestQueue.push_back(
|
2008-08-03 08:45:53 -04:00
|
|
|
ftRequest(FT_DATA_REQ,peerId,hash,size,offset,chunksize,NULL));
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*********** BACKGROUND THREAD OPERATIONS ***********/
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftDataMultiplex::workQueued()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/
|
|
|
|
if (mRequestQueue.size() > 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mSearchQueue.size() > 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ftDataMultiplex::doWork()
|
|
|
|
{
|
|
|
|
bool doRequests = true;
|
|
|
|
|
|
|
|
/* Handle All the current Requests */
|
|
|
|
while(doRequests)
|
|
|
|
{
|
|
|
|
ftRequest req;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/
|
|
|
|
if (mRequestQueue.size() == 0)
|
|
|
|
{
|
|
|
|
doRequests = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = mRequestQueue.front();
|
|
|
|
mRequestQueue.pop_front();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* MUTEX FREE */
|
|
|
|
|
|
|
|
switch(req.mType)
|
|
|
|
{
|
|
|
|
case FT_DATA:
|
2008-08-29 21:07:24 -04:00
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::doWork() Handling FT_DATA";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-08-03 08:45:53 -04:00
|
|
|
handleRecvData(req.mPeerId, req.mHash, req.mSize,
|
|
|
|
req.mOffset, req.mChunk, req.mData);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FT_DATA_REQ:
|
2008-08-29 21:07:24 -04:00
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::doWork() Handling FT_DATA_REQ";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-08-03 08:45:53 -04:00
|
|
|
handleRecvDataRequest(req.mPeerId, req.mHash,
|
|
|
|
req.mSize, req.mOffset, req.mChunk);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2008-08-29 21:07:24 -04:00
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::doWork() Ignoring UNKNOWN";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-08-03 08:45:53 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only Handle One Search Per Period....
|
|
|
|
* Lower Priority
|
|
|
|
*/
|
|
|
|
ftRequest req;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/
|
|
|
|
if (mSearchQueue.size() == 0)
|
|
|
|
{
|
|
|
|
/* Finished */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
req = mSearchQueue.front();
|
|
|
|
mSearchQueue.pop_front();
|
|
|
|
}
|
|
|
|
|
2008-08-29 21:07:24 -04:00
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::doWork() Handling Search Request";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-08-03 08:45:53 -04:00
|
|
|
handleSearchRequest(req.mPeerId, req.mHash, req.mSize,
|
|
|
|
req.mOffset, req.mChunk);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
|
|
|
|
bool ftDataMultiplex::handleRecvData(std::string peerId,
|
2008-08-03 08:45:53 -04:00
|
|
|
std::string hash, uint64_t size,
|
|
|
|
uint64_t offset, uint32_t chunksize, void *data)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/
|
|
|
|
std::map<std::string, ftClient>::iterator it;
|
|
|
|
if (mClients.end() == (it = mClients.find(hash)))
|
|
|
|
{
|
2008-08-29 21:07:24 -04:00
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::handleRecvData() ERROR: No matching Client!";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-07-23 18:01:59 -04:00
|
|
|
/* error */
|
|
|
|
return false;
|
|
|
|
}
|
2008-08-29 21:07:24 -04:00
|
|
|
|
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::handleRecvData() Passing to Module";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
(it->second).mModule->recvFileData(peerId, offset, chunksize, data);
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* called by ftTransferModule */
|
|
|
|
bool ftDataMultiplex::handleRecvDataRequest(std::string peerId,
|
2008-08-03 08:45:53 -04:00
|
|
|
std::string hash, uint64_t size,
|
|
|
|
uint64_t offset, uint32_t chunksize)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
|
|
|
/**** Find Files *****/
|
|
|
|
|
|
|
|
RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/
|
|
|
|
std::map<std::string, ftClient>::iterator cit;
|
2008-08-29 21:07:24 -04:00
|
|
|
if (mOwnId == peerId)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
2008-08-29 21:07:24 -04:00
|
|
|
/* own requests must be passed to Servers */
|
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::handleRecvData() OwnId, so skip Clients...";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if (mClients.end() != (cit = mClients.find(hash)))
|
|
|
|
{
|
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::handleRecvData() Matched to a Client.";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-07-23 18:01:59 -04:00
|
|
|
locked_handleServerRequest((cit->second).mCreator,
|
2008-08-03 08:45:53 -04:00
|
|
|
peerId, hash, size, offset, chunksize);
|
2008-07-23 18:01:59 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, ftFileProvider *>::iterator sit;
|
|
|
|
if (mServers.end() != (sit = mServers.find(hash)))
|
|
|
|
{
|
2008-08-29 21:07:24 -04:00
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::handleRecvData() Matched to a Provider.";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-07-23 18:01:59 -04:00
|
|
|
locked_handleServerRequest(sit->second,
|
2008-08-03 08:45:53 -04:00
|
|
|
peerId, hash, size, offset, chunksize);
|
2008-07-23 18:01:59 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-08-29 21:07:24 -04:00
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::handleRecvData() No Match... adding to Search Queue.";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
/* Add to Search Queue */
|
|
|
|
mSearchQueue.push_back(
|
2008-08-03 08:45:53 -04:00
|
|
|
ftRequest(FT_DATA_REQ, peerId, hash,
|
|
|
|
size, offset, chunksize, NULL));
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ftDataMultiplex::locked_handleServerRequest(ftFileProvider *provider,
|
2008-08-03 08:45:53 -04:00
|
|
|
std::string peerId, std::string hash, uint64_t size,
|
|
|
|
uint64_t offset, uint32_t chunksize)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
2009-05-26 17:42:45 -04:00
|
|
|
void *data = malloc(chunksize);
|
2008-10-29 16:58:23 -04:00
|
|
|
|
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::locked_handleServerRequest()";
|
|
|
|
std::cerr << "\t peer: " << peerId << " hash: " << hash;
|
|
|
|
std::cerr << " size: " << size;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\t offset: " << offset;
|
|
|
|
std::cerr << " chunksize: " << chunksize << " data: " << data;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
if (provider->getFileData(offset, chunksize, data))
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
2009-02-04 16:35:39 -05:00
|
|
|
// setup info
|
|
|
|
provider->setPeerId(peerId) ;
|
2008-07-23 18:01:59 -04:00
|
|
|
/* send data out */
|
2008-08-03 08:45:53 -04:00
|
|
|
sendData(peerId, hash, size, offset, chunksize, data);
|
2008-07-23 18:01:59 -04:00
|
|
|
return true;
|
|
|
|
}
|
2008-10-29 16:58:23 -04:00
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::locked_handleServerRequest()";
|
|
|
|
std::cerr << " FAILED";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-11-04 18:12:53 -05:00
|
|
|
free(data);
|
2008-10-29 16:58:23 -04:00
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-09 15:26:12 -05:00
|
|
|
void ftDataMultiplex::deleteServers(const std::list<std::string>& serv)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/
|
|
|
|
|
|
|
|
for(std::list<std::string>::const_iterator it=serv.begin();it != serv.end(); it++)
|
|
|
|
{
|
|
|
|
std::map<std::string,ftFileProvider *>::iterator sit = mServers.find(*it);
|
|
|
|
|
|
|
|
if(mServers.end() != sit)
|
|
|
|
{
|
|
|
|
delete sit->second;
|
|
|
|
mServers.erase(sit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
bool ftDataMultiplex::handleSearchRequest(std::string peerId,
|
2008-08-03 08:45:53 -04:00
|
|
|
std::string hash, uint64_t size,
|
|
|
|
uint64_t offset, uint32_t chunksize)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
|
|
|
|
2008-11-04 18:12:53 -05:00
|
|
|
|
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::handleSearchRequest(";
|
|
|
|
std::cerr << peerId << ", " << hash << ", " << size << "...)";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/
|
|
|
|
|
|
|
|
/* Check for bad requests */
|
|
|
|
std::map<std::string, time_t>::iterator bit;
|
|
|
|
if (mUnknownHashs.end() != (bit = mUnknownHashs.find(hash)))
|
|
|
|
{
|
2008-11-04 18:12:53 -05:00
|
|
|
|
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::handleSearchRequest(";
|
|
|
|
std::cerr << " Found Ignore Hash ... done";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
/* We've previously rejected this one, so ignore */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do Actual search
|
|
|
|
* Could be Cache File, Local or Extra
|
|
|
|
* (anywhere but remote really)
|
|
|
|
*/
|
|
|
|
|
2008-11-04 18:12:53 -05:00
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
FileInfo info;
|
|
|
|
uint32_t hintflags = (RS_FILE_HINTS_CACHE |
|
|
|
|
RS_FILE_HINTS_EXTRA |
|
|
|
|
RS_FILE_HINTS_LOCAL |
|
|
|
|
RS_FILE_HINTS_SPEC_ONLY);
|
|
|
|
|
|
|
|
if (mSearch->search(hash, size, hintflags, info))
|
|
|
|
{
|
|
|
|
|
2008-11-04 18:12:53 -05:00
|
|
|
#ifdef MPLEX_DEBUG
|
|
|
|
std::cerr << "ftDataMultiplex::handleSearchRequest(";
|
|
|
|
std::cerr << " Found Local File, sharing...";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
/* setup a new provider */
|
|
|
|
RsStackMutex stack(dataMtx); /******* LOCK MUTEX ******/
|
|
|
|
|
|
|
|
ftFileProvider *provider =
|
|
|
|
new ftFileProvider(info.path, size, hash);
|
|
|
|
|
|
|
|
mServers[hash] = provider;
|
|
|
|
|
|
|
|
/* handle request finally */
|
|
|
|
locked_handleServerRequest(provider,
|
2008-08-03 08:45:53 -04:00
|
|
|
peerId, hash, size, offset, chunksize);
|
2008-11-04 18:12:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
/* now we should should check if any further requests for the same
|
|
|
|
* file exists ... (can happen with caches!)
|
|
|
|
*
|
|
|
|
* but easier to check pre-search....
|
|
|
|
*/
|
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|