2008-07-23 18:01:59 -04:00
|
|
|
/*
|
|
|
|
* libretroshare/src/ft: ftcontroller.cc
|
|
|
|
*
|
|
|
|
* 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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-09 13:03:24 -04:00
|
|
|
#include "ft/ftcontroller.h"
|
|
|
|
|
|
|
|
#include "ft/ftfilecreator.h"
|
|
|
|
#include "ft/fttransfermodule.h"
|
|
|
|
#include "ft/ftsearch.h"
|
|
|
|
#include "ft/ftdatamultiplex.h"
|
|
|
|
|
|
|
|
#include "util/rsdir.h"
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
#include "pqi/p3connmgr.h"
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
|
2008-08-17 11:23:11 -04:00
|
|
|
#define CONTROL_DEBUG 1
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
#warning CONFIG_FT_CONTROL Not defined in p3cfgmgr.h
|
|
|
|
|
|
|
|
const uint32_t CONFIG_FT_CONTROL = 1;
|
|
|
|
|
2008-08-17 11:23:11 -04:00
|
|
|
ftFileControl::ftFileControl()
|
|
|
|
:mTransfer(NULL), mCreator(NULL),
|
|
|
|
mState(0), mSize(0), mFlags(0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
ftFileControl::ftFileControl(std::string fname,
|
|
|
|
std::string tmppath, std::string dest,
|
|
|
|
uint64_t size, std::string hash, uint32_t flags,
|
|
|
|
ftFileCreator *fc, ftTransferModule *tm, uint32_t cb)
|
|
|
|
:mName(fname), mCurrentPath(tmppath), mDestination(dest),
|
|
|
|
mTransfer(tm), mCreator(fc), mState(0), mHash(hash),
|
|
|
|
mSize(size), mFlags(0), mDoCallback(false), mCallbackCode(cb)
|
2008-08-17 11:23:11 -04:00
|
|
|
{
|
2008-10-29 16:58:23 -04:00
|
|
|
if (cb)
|
|
|
|
mDoCallback = true;
|
2008-08-17 11:23:11 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
ftController::ftController(CacheStrapper *cs, ftDataMultiplex *dm, std::string configDir)
|
2008-08-17 11:23:11 -04:00
|
|
|
:CacheTransfer(cs), p3Config(CONFIG_FT_CONTROL), mDataplex(dm)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
/* TODO */
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
void ftController::setFtSearch(ftSearch *search)
|
|
|
|
{
|
|
|
|
mSearch = search;
|
|
|
|
}
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
void ftController::run()
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
|
|
|
/* check the queues */
|
2008-08-17 11:23:11 -04:00
|
|
|
|
2008-08-21 17:30:59 -04:00
|
|
|
while(1)
|
2008-08-17 11:23:11 -04:00
|
|
|
{
|
2008-08-25 16:03:39 -04:00
|
|
|
#ifdef WIN32
|
|
|
|
Sleep(1000);
|
|
|
|
#else
|
2008-08-21 17:30:59 -04:00
|
|
|
sleep(1);
|
2008-08-25 16:03:39 -04:00
|
|
|
#endif
|
2008-08-21 17:30:59 -04:00
|
|
|
|
2008-11-02 06:38:11 -05:00
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
//std::cerr << "ftController::run()";
|
|
|
|
//std::cerr << std::endl;
|
|
|
|
#endif
|
2008-08-17 11:23:11 -04:00
|
|
|
|
2008-08-21 17:30:59 -04:00
|
|
|
/* tick the transferModules */
|
2008-10-29 16:58:23 -04:00
|
|
|
std::list<std::string> done;
|
|
|
|
std::list<std::string>::iterator it;
|
2008-08-21 17:30:59 -04:00
|
|
|
{
|
2008-10-29 16:58:23 -04:00
|
|
|
RsStackMutex stack(ctrlMutex); /******* LOCKED ********/
|
|
|
|
|
|
|
|
std::map<std::string, ftFileControl>::iterator it;
|
|
|
|
for(it = mDownloads.begin(); it != mDownloads.end(); it++)
|
|
|
|
{
|
2008-08-21 17:30:59 -04:00
|
|
|
std::cerr << "\tTicking: " << it->first;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
if (it->second.mTransfer)
|
|
|
|
(it->second.mTransfer)->tick();
|
|
|
|
}
|
2008-08-21 17:30:59 -04:00
|
|
|
}
|
2008-10-29 16:58:23 -04:00
|
|
|
|
|
|
|
RsStackMutex stack2(doneMutex);
|
|
|
|
for(it = mDone.begin(); it != mDone.end(); it++)
|
|
|
|
{
|
|
|
|
completeFile(*it);
|
|
|
|
}
|
|
|
|
mDone.clear();
|
2008-08-17 11:23:11 -04:00
|
|
|
}
|
2008-10-29 16:58:23 -04:00
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Called every 10 seconds or so */
|
|
|
|
void ftController::checkDownloadQueue()
|
|
|
|
{
|
|
|
|
/* */
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
bool ftController::FlagFileComplete(std::string hash)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
2008-10-29 16:58:23 -04:00
|
|
|
RsStackMutex stack2(doneMutex);
|
|
|
|
mDone.push_back(hash);
|
2008-08-09 13:03:24 -04:00
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
std::cerr << "ftController:FlagFileComplete(" << hash << ")";
|
|
|
|
std::cerr << std::endl;
|
2008-08-09 13:03:24 -04:00
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ftController::completeFile(std::string hash)
|
|
|
|
{
|
2008-07-23 18:01:59 -04:00
|
|
|
RsStackMutex stack(ctrlMutex); /******* LOCKED ********/
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
std::cerr << "ftController:completeFile(" << hash << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
std::map<std::string, ftFileControl>::iterator it;
|
|
|
|
it = mDownloads.find(hash);
|
|
|
|
if (it == mDownloads.end())
|
|
|
|
{
|
2008-10-29 16:58:23 -04:00
|
|
|
std::cerr << "ftController:completeFile(" << hash << ")";
|
|
|
|
std::cerr << " Not Found!";
|
|
|
|
std::cerr << std::endl;
|
2008-07-23 18:01:59 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if finished */
|
2008-10-29 16:58:23 -04:00
|
|
|
if (!(it->second).mCreator->finished())
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
|
|
|
/* not done! */
|
2008-10-29 16:58:23 -04:00
|
|
|
std::cerr << "ftController:completeFile(" << hash << ")";
|
|
|
|
std::cerr << " Transfer Not Done";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
std::cerr << "FileSize: ";
|
|
|
|
std::cerr << (it->second).mCreator->getFileSize();
|
|
|
|
std::cerr << " and Recvd: ";
|
|
|
|
std::cerr << (it->second).mCreator->getRecvd();
|
|
|
|
|
|
|
|
return false;
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
2008-09-11 06:33:43 -04:00
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
ftFileControl *fc = &(it->second);
|
|
|
|
|
|
|
|
/* done - cleanup */
|
2008-10-29 16:58:23 -04:00
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
mDataplex->removeTransferModule(fc->mTransfer->hash());
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
if (fc->mTransfer)
|
|
|
|
{
|
|
|
|
delete fc->mTransfer;
|
|
|
|
fc->mTransfer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fc->mCreator)
|
|
|
|
{
|
|
|
|
delete fc->mCreator;
|
|
|
|
fc->mCreator = NULL;
|
|
|
|
}
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2008-09-11 06:33:43 -04:00
|
|
|
fc->mState = ftFileControl::COMPLETED;
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
/* Move to Correct Location */
|
|
|
|
if (0 == rename(fc->mCurrentPath.c_str(), fc->mDestination.c_str()))
|
|
|
|
{
|
|
|
|
/* correct the file_name */
|
|
|
|
fc->mCurrentPath = fc->mDestination;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fc->mState = ftFileControl::ERROR_COMPLETION;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* If it has a callback - do it now */
|
|
|
|
if (fc->mDoCallback)
|
|
|
|
{
|
2008-11-04 18:12:53 -05:00
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::completeFile() doing Callback";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
switch (fc->mCallbackCode)
|
|
|
|
{
|
|
|
|
case CB_CODE_CACHE:
|
|
|
|
/* callback */
|
|
|
|
if (fc->mState == ftFileControl::COMPLETED)
|
|
|
|
{
|
2008-11-04 18:12:53 -05:00
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::completeFile() doing Callback : Success";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
CompletedCache(fc->mHash);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-04 18:12:53 -05:00
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::completeFile() Cache Callback : Failed";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-10-29 16:58:23 -04:00
|
|
|
FailedCache(fc->mHash);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CB_CODE_MEDIA:
|
2008-11-04 18:12:53 -05:00
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::completeFile() NULL MEDIA callback";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-10-29 16:58:23 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-11-04 18:12:53 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::completeFile() No callback";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2008-10-29 16:58:23 -04:00
|
|
|
|
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
/* switch map */
|
2008-08-09 13:03:24 -04:00
|
|
|
mCompleted[fc->mHash] = *fc;
|
2008-07-23 18:01:59 -04:00
|
|
|
mDownloads.erase(it);
|
|
|
|
|
|
|
|
return true;
|
2008-08-09 13:03:24 -04:00
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************/
|
|
|
|
/********************** Controller Access **********************/
|
|
|
|
/***************************************************************/
|
|
|
|
|
2008-11-04 18:12:53 -05:00
|
|
|
const uint32_t FT_CNTRL_STANDARD_RATE = 100 * 1024;
|
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
bool ftController::FileRequest(std::string fname, std::string hash,
|
|
|
|
uint64_t size, std::string dest, uint32_t flags,
|
2008-08-09 13:03:24 -04:00
|
|
|
std::list<std::string> &srcIds)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
|
|
|
/* check if we have the file */
|
2008-08-09 13:03:24 -04:00
|
|
|
FileInfo info;
|
2008-08-17 11:23:11 -04:00
|
|
|
std::list<std::string>::iterator it;
|
2008-11-02 06:38:11 -05:00
|
|
|
std::list<TransferInfo>::iterator pit;
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2008-08-17 11:23:11 -04:00
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::FileRequest(" << fname << ",";
|
|
|
|
std::cerr << hash << "," << size << "," << dest << ",";
|
|
|
|
std::cerr << flags << ",<";
|
|
|
|
|
|
|
|
for(it = srcIds.begin(); it != srcIds.end(); it++)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
2008-08-17 11:23:11 -04:00
|
|
|
std::cerr << *it << ",";
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
2008-08-17 11:23:11 -04:00
|
|
|
std::cerr << ">)";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
bool doCallback = false;
|
|
|
|
uint32_t callbackCode = 0;
|
|
|
|
if (flags & RS_FILE_HINTS_NO_SEARCH)
|
2008-08-17 11:23:11 -04:00
|
|
|
{
|
2008-10-29 16:58:23 -04:00
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::FileRequest() Flags for NO_SEARCH ";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-08-17 11:23:11 -04:00
|
|
|
/* no search */
|
2008-10-29 16:58:23 -04:00
|
|
|
if (flags & RS_FILE_HINTS_CACHE)
|
|
|
|
{
|
|
|
|
doCallback = true;
|
|
|
|
callbackCode = CB_CODE_CACHE;
|
|
|
|
}
|
2008-08-17 11:23:11 -04:00
|
|
|
}
|
|
|
|
else
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
2008-08-17 11:23:11 -04:00
|
|
|
if (mSearch->search(hash, size,
|
|
|
|
RS_FILE_HINTS_LOCAL |
|
|
|
|
RS_FILE_HINTS_EXTRA |
|
|
|
|
RS_FILE_HINTS_SPEC_ONLY, info))
|
|
|
|
{
|
|
|
|
/* have it already */
|
|
|
|
/* add in as completed transfer */
|
2008-10-29 16:58:23 -04:00
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::FileRequest() Matches Local File";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "\tNo need to download";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-08-17 11:23:11 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do a source search - for any extra sources */
|
|
|
|
if (mSearch->search(hash, size,
|
|
|
|
RS_FILE_HINTS_REMOTE |
|
|
|
|
RS_FILE_HINTS_SPEC_ONLY, info))
|
|
|
|
{
|
|
|
|
/* do something with results */
|
2008-10-29 16:58:23 -04:00
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::FileRequest() Found Other Sources";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* if the sources don't exist already - add in */
|
2008-11-02 06:38:11 -05:00
|
|
|
for(pit = info.peers.begin(); pit != info.peers.end(); pit++)
|
2008-10-29 16:58:23 -04:00
|
|
|
{
|
2008-11-02 06:38:11 -05:00
|
|
|
std::cerr << "\tSource: " << pit->peerId;
|
2008-10-29 16:58:23 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
if (srcIds.end() == std::find(
|
2008-11-02 06:38:11 -05:00
|
|
|
srcIds.begin(), srcIds.end(), pit->peerId))
|
2008-10-29 16:58:23 -04:00
|
|
|
{
|
2008-11-02 06:38:11 -05:00
|
|
|
srcIds.push_back(pit->peerId);
|
2008-10-29 16:58:23 -04:00
|
|
|
|
2008-11-02 06:38:11 -05:00
|
|
|
std::cerr << "\tAdding in: " << pit->peerId;
|
2008-10-29 16:58:23 -04:00
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & RS_FILE_HINTS_MEDIA)
|
|
|
|
{
|
|
|
|
doCallback = true;
|
|
|
|
callbackCode = CB_CODE_MEDIA;
|
2008-08-17 11:23:11 -04:00
|
|
|
}
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
//std::map<std::string, ftTransferModule *> mTransfers;
|
|
|
|
//std::map<std::string, ftFileCreator *> mFileCreators;
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
/* add in new item for download */
|
2008-10-29 16:58:23 -04:00
|
|
|
std::string savepath = mPartialsPath + "/" + hash;
|
|
|
|
std::string destination = dest + "/" + fname;
|
|
|
|
|
|
|
|
/* if no destpath - send to download directory */
|
|
|
|
if (dest == "")
|
|
|
|
{
|
|
|
|
destination = mDownloadPath + "/" + fname;
|
|
|
|
}
|
|
|
|
|
2008-10-22 14:12:58 -04:00
|
|
|
ftFileCreator *fc = new ftFileCreator(savepath, size, hash, 0);
|
2008-09-08 10:04:10 -04:00
|
|
|
ftTransferModule *tm = new ftTransferModule(fc, mDataplex,this);
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
/* add into maps */
|
2008-10-29 16:58:23 -04:00
|
|
|
ftFileControl ftfc(fname, savepath, destination,
|
|
|
|
size, hash, flags, fc, tm, callbackCode);
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
/* add to ClientModule */
|
2008-08-09 13:03:24 -04:00
|
|
|
mDataplex->addTransferModule(tm, fc);
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
/* now add source peers (and their current state) */
|
|
|
|
tm->setFileSources(srcIds);
|
|
|
|
|
|
|
|
/* get current state for transfer module */
|
2008-08-21 17:30:59 -04:00
|
|
|
std::string ownId = mConnMgr->getOwnId();
|
2008-07-23 18:01:59 -04:00
|
|
|
for(it = srcIds.begin(); it != srcIds.end(); it++)
|
|
|
|
{
|
2008-08-21 17:30:59 -04:00
|
|
|
if (*it == ownId)
|
|
|
|
{
|
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::FileRequest()";
|
|
|
|
std::cerr << *it << " is Self - set high rate";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
//tm->setPeerState(*it, RS_FILE_RATE_FAST |
|
|
|
|
// RS_FILE_PEER_ONLINE, 100000);
|
2008-11-04 18:12:53 -05:00
|
|
|
//tm->setPeerState(*it, PQIPEER_IDLE, 10000);
|
|
|
|
tm->setPeerState(*it, PQIPEER_IDLE, FT_CNTRL_STANDARD_RATE);
|
2008-08-21 17:30:59 -04:00
|
|
|
}
|
|
|
|
else if (mConnMgr->isOnline(*it))
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
2008-08-17 11:23:11 -04:00
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::FileRequest()";
|
|
|
|
std::cerr << *it << " is Online";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-08-21 17:30:59 -04:00
|
|
|
//tm->setPeerState(*it, RS_FILE_RATE_TRICKLE |
|
|
|
|
// RS_FILE_PEER_ONLINE, 10000);
|
2008-11-04 18:12:53 -05:00
|
|
|
//tm->setPeerState(*it, PQIPEER_IDLE, 10000);
|
|
|
|
tm->setPeerState(*it, PQIPEER_IDLE, FT_CNTRL_STANDARD_RATE);
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-08-17 11:23:11 -04:00
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::FileRequest()";
|
|
|
|
std::cerr << *it << " is Offline";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-08-21 17:30:59 -04:00
|
|
|
//tm->setPeerState(*it, RS_FILE_PEER_OFFLINE, 10000);
|
2008-11-04 18:12:53 -05:00
|
|
|
tm->setPeerState(*it, PQIPEER_NOT_ONLINE, FT_CNTRL_STANDARD_RATE);
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* only need to lock before to fiddle with own variables */
|
|
|
|
RsStackMutex stack(ctrlMutex); /******* LOCKED ********/
|
|
|
|
mDownloads[hash] = ftfc;
|
|
|
|
mSlowQueue.push_back(hash);
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
return true;
|
2008-08-09 13:03:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-04 18:12:53 -05:00
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
bool ftController::FileCancel(std::string hash)
|
|
|
|
{
|
2008-09-08 04:44:37 -04:00
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::FileCancel" << std::endl;
|
|
|
|
#endif
|
|
|
|
/*check if the file in the download map*/
|
|
|
|
std::map<std::string,ftFileControl>::iterator mit;
|
|
|
|
mit=mDownloads.find(hash);
|
|
|
|
if (mit==mDownloads.end())
|
|
|
|
{
|
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr<<"ftController::FileCancel file is not found in mDownloads"<<std::endl;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*find the point to transfer module*/
|
|
|
|
ftTransferModule* ft=(mit->second).mTransfer;
|
|
|
|
ft->cancelTransfer();
|
|
|
|
return true;
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
bool ftController::FileControl(std::string hash, uint32_t flags)
|
|
|
|
{
|
2008-09-08 04:44:37 -04:00
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::FileControl(" << hash << ",";
|
|
|
|
std::cerr << flags << ")"<<std::endl;
|
|
|
|
#endif
|
|
|
|
/*check if the file in the download map*/
|
|
|
|
std::map<std::string,ftFileControl>::iterator mit;
|
|
|
|
mit=mDownloads.find(hash);
|
|
|
|
if (mit==mDownloads.end())
|
|
|
|
{
|
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr<<"ftController::FileControl file is not found in mDownloads"<<std::endl;
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*find the point to transfer module*/
|
|
|
|
ftTransferModule* ft=(mit->second).mTransfer;
|
|
|
|
switch (flags)
|
|
|
|
{
|
|
|
|
case RS_FILE_CTRL_PAUSE:
|
2008-09-09 09:08:22 -04:00
|
|
|
ft->pauseTransfer();
|
2008-09-08 04:44:37 -04:00
|
|
|
break;
|
|
|
|
case RS_FILE_CTRL_START:
|
|
|
|
ft->resumeTransfer();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2008-08-09 13:03:24 -04:00
|
|
|
}
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
bool ftController::FileClearCompleted()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
/* get Details of File Transfers */
|
|
|
|
bool ftController::FileDownloads(std::list<std::string> &hashs)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(ctrlMutex); /******* LOCKED ********/
|
|
|
|
|
|
|
|
std::map<std::string, ftFileControl>::iterator it;
|
|
|
|
for(it = mDownloads.begin(); it != mDownloads.end(); it++)
|
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
hashs.push_back(it->second.mHash);
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Directory Handling */
|
|
|
|
bool ftController::setDownloadDirectory(std::string path)
|
|
|
|
{
|
2008-08-29 21:07:24 -04:00
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::setDownloadDirectory(" << path << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-07-23 18:01:59 -04:00
|
|
|
/* check if it exists */
|
|
|
|
if (RsDirUtil::checkCreateDirectory(path))
|
|
|
|
{
|
|
|
|
RsStackMutex stack(ctrlMutex); /******* LOCKED ********/
|
|
|
|
|
|
|
|
mDownloadPath = path;
|
2008-08-29 21:07:24 -04:00
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::setDownloadDirectory() Okay!";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-07-23 18:01:59 -04:00
|
|
|
return true;
|
|
|
|
}
|
2008-08-29 21:07:24 -04:00
|
|
|
|
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::setDownloadDirectory() Failed";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-07-23 18:01:59 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
bool ftController::setPartialsDirectory(std::string path)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
/* check it is not a subdir of download / shared directories (BAD) - TODO */
|
|
|
|
|
|
|
|
/* check if it exists */
|
|
|
|
|
|
|
|
if (RsDirUtil::checkCreateDirectory(path))
|
|
|
|
{
|
|
|
|
RsStackMutex stack(ctrlMutex); /******* LOCKED ********/
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
mPartialsPath = path;
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
#if 0 /*** FIX ME !!!**************/
|
2008-07-23 18:01:59 -04:00
|
|
|
/* move all existing files! */
|
|
|
|
std::map<std::string, ftFileControl>::iterator it;
|
|
|
|
for(it = mDownloads.begin(); it != mDownloads.end(); it++)
|
|
|
|
{
|
|
|
|
(it->second).mCreator->changePartialDirectory(mPartialPath);
|
|
|
|
}
|
2008-10-29 16:58:23 -04:00
|
|
|
#endif
|
2008-07-23 18:01:59 -04:00
|
|
|
return true;
|
|
|
|
}
|
2008-08-09 13:03:24 -04:00
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ftController::getDownloadDirectory()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(ctrlMutex); /******* LOCKED ********/
|
|
|
|
|
|
|
|
return mDownloadPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ftController::getPartialsDirectory()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(ctrlMutex); /******* LOCKED ********/
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
return mPartialsPath;
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ftController::FileDetails(std::string hash, FileInfo &info)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(ctrlMutex); /******* LOCKED ********/
|
|
|
|
|
|
|
|
std::map<std::string, ftFileControl>::iterator it;
|
|
|
|
it = mDownloads.find(hash);
|
|
|
|
if (it == mDownloads.end())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* extract details */
|
2008-11-02 06:38:11 -05:00
|
|
|
info.hash = hash;
|
|
|
|
info.fname = it->second.mName;
|
|
|
|
|
|
|
|
/* get list of sources from transferModule */
|
|
|
|
std::list<std::string> peerIds;
|
|
|
|
std::list<std::string>::iterator pit;
|
|
|
|
|
|
|
|
it->second.mTransfer->getFileSources(peerIds);
|
|
|
|
|
|
|
|
double totalRate;
|
|
|
|
uint32_t tfRate;
|
|
|
|
uint32_t state;
|
|
|
|
|
|
|
|
bool isDownloading = false;
|
|
|
|
bool isSuspended = false;
|
|
|
|
|
|
|
|
for(pit = peerIds.begin(); pit != peerIds.end(); pit++)
|
|
|
|
{
|
|
|
|
if (it->second.mTransfer->getPeerState(*pit, state, tfRate))
|
|
|
|
{
|
|
|
|
TransferInfo ti;
|
|
|
|
switch(state)
|
|
|
|
{
|
|
|
|
case PQIPEER_INIT:
|
|
|
|
ti.status = FT_STATE_OKAY;
|
|
|
|
break;
|
|
|
|
case PQIPEER_NOT_ONLINE:
|
|
|
|
ti.status = FT_STATE_WAITING;
|
|
|
|
break;
|
|
|
|
case PQIPEER_DOWNLOADING:
|
|
|
|
isDownloading = true;
|
|
|
|
ti.status = FT_STATE_DOWNLOADING;
|
|
|
|
break;
|
|
|
|
case PQIPEER_IDLE:
|
|
|
|
ti.status = FT_STATE_OKAY;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case PQIPEER_SUSPEND:
|
|
|
|
isSuspended = true;
|
|
|
|
ti.status = FT_STATE_FAILED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ti.tfRate = tfRate / 1024.0;
|
|
|
|
ti.peerId = *pit;
|
|
|
|
info.peers.push_back(ti);
|
|
|
|
totalRate += tfRate / 1024.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((it->second).mCreator->finished())
|
|
|
|
{
|
|
|
|
info.downloadStatus = FT_STATE_COMPLETE;
|
|
|
|
}
|
|
|
|
else if (isDownloading)
|
|
|
|
{
|
|
|
|
info.downloadStatus = FT_STATE_DOWNLOADING;
|
|
|
|
}
|
|
|
|
else if (isSuspended)
|
|
|
|
{
|
|
|
|
info.downloadStatus = FT_STATE_FAILED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info.downloadStatus = FT_STATE_WAITING;
|
|
|
|
}
|
|
|
|
info.tfRate = totalRate;
|
|
|
|
info.size = (it->second).mSize;
|
|
|
|
info.transfered = (it->second).mCreator->getRecvd();
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************/
|
|
|
|
/********************** Controller Access **********************/
|
|
|
|
/***************************************************************/
|
|
|
|
|
|
|
|
/* pqiMonitor callback:
|
|
|
|
* Used to tell TransferModules new available peers
|
|
|
|
*/
|
|
|
|
void ftController::statusChange(const std::list<pqipeer> &plist)
|
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
|
|
|
|
#if 0 /*** FIX ME !!!**************/
|
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
RsStackMutex stack(ctrlMutex); /******* LOCKED ********/
|
|
|
|
|
|
|
|
/* add online to all downloads */
|
|
|
|
std::map<std::string, ftFileControl>::iterator it;
|
|
|
|
std::list<pqipeer>::const_iterator pit;
|
|
|
|
|
|
|
|
for(it = mDownloads.begin(); it != mDownloads.end(); it++)
|
|
|
|
{
|
|
|
|
for(pit = plist.begin(); pit != plist.end(); pit++)
|
|
|
|
{
|
|
|
|
if (pit->actions | RS_PEER_CONNECTED)
|
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
((it->second).mTransfer)->setPeer(RS_FILE_PEER_ONLINE | RS_FILE_RATE_TRICKLE);
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
else if (pit->actions | RS_PEER_DISCONNECTED)
|
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
((it->second).mTransfer)->setPeer(RS_FILE_PEER_OFFLINE);
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* modify my list of peers */
|
|
|
|
for(pit = plist.begin(); pit != plist.end(); pit++)
|
|
|
|
{
|
|
|
|
if (pit->actions | RS_PEER_CONNECTED)
|
|
|
|
{
|
|
|
|
/* add in */
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
((it->second).mTransfer)->setPeer(RS_FILE_PEER_ONLINE | RS_FILE_RATE_TRICKLE);
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
else if (pit->actions | RS_PEER_DISCONNECTED)
|
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
((it->second).mTransfer)->setPeer(RS_FILE_PEER_OFFLINE);
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
#endif
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
}
|
2008-07-23 18:01:59 -04:00
|
|
|
/* p3Config Interface */
|
2008-08-09 13:03:24 -04:00
|
|
|
RsSerialiser *ftController::setupSerialiser()
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
std::list<RsItem *> ftController::saveList(bool &cleanup)
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
std::list<RsItem *> emptyList;
|
|
|
|
return emptyList;
|
|
|
|
}
|
2008-07-23 18:01:59 -04:00
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
|
|
|
|
bool ftController::loadList(std::list<RsItem *> load)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
|
2008-08-17 11:23:11 -04:00
|
|
|
/* Cache Interface */
|
|
|
|
bool ftController::RequestCacheFile(RsPeerId id, std::string path, std::string hash, uint64_t size)
|
|
|
|
{
|
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::RequestCacheFile(" << id << ",";
|
|
|
|
std::cerr << path << "," << hash << "," << size << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Request File */
|
|
|
|
std::list<std::string> ids;
|
|
|
|
ids.push_back(id);
|
|
|
|
|
|
|
|
FileRequest(hash, hash, size, path,
|
|
|
|
RS_FILE_HINTS_CACHE | RS_FILE_HINTS_NO_SEARCH, ids);
|
2008-10-29 16:58:23 -04:00
|
|
|
|
|
|
|
return true;
|
2008-08-17 11:23:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ftController::CancelCacheFile(RsPeerId id, std::string path, std::string hash, uint64_t size)
|
|
|
|
{
|
|
|
|
#ifdef CONTROL_DEBUG
|
|
|
|
std::cerr << "ftController::CancelCacheFile(" << id << ",";
|
|
|
|
std::cerr << path << "," << hash << "," << size << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
return true;
|
2008-08-17 11:23:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|