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
|
|
|
|
|
|
|
#warning CONFIG_FT_CONTROL Not defined in p3cfgmgr.h
|
|
|
|
|
|
|
|
const uint32_t CONFIG_FT_CONTROL = 1;
|
|
|
|
|
|
|
|
ftController::ftController(CacheStrapper *cs, ftDataMultiplex *dm, std::string configDir)
|
|
|
|
:CacheTransfer(cs), p3Config(CONFIG_FT_CONTROL)
|
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 */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Called every 10 seconds or so */
|
|
|
|
void ftController::checkDownloadQueue()
|
|
|
|
{
|
|
|
|
/* */
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ftController::completeFile(std::string hash)
|
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
RsStackMutex stack(ctrlMutex); /******* LOCKED ********/
|
|
|
|
|
|
|
|
std::map<std::string, ftFileControl>::iterator it;
|
|
|
|
it = mDownloads.find(hash);
|
|
|
|
if (it == mDownloads.end())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if finished */
|
|
|
|
if (!(it->second).mCreator->finished())
|
|
|
|
{
|
|
|
|
/* not done! */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ftFileControl *fc = &(it->second);
|
|
|
|
|
|
|
|
/* done - cleanup */
|
|
|
|
fc->mTransfer->done();
|
2008-08-09 13:03:24 -04:00
|
|
|
mDataplex->removeTransferModule(fc->mTransfer->hash());
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
delete fc->mTransfer;
|
|
|
|
fc->mTransfer = NULL;
|
|
|
|
|
|
|
|
delete fc->mCreator;
|
|
|
|
fc->mCreator = NULL;
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
fc->mState = COMPLETE;
|
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
|
|
|
|
|
|
|
#endif
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************/
|
|
|
|
/********************** Controller Access **********************/
|
|
|
|
/***************************************************************/
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
|
|
|
|
#if 0 /*** FIX ME !!!**************/
|
|
|
|
|
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-07-23 18:01:59 -04:00
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
if (mSearch->search(hash, size,
|
|
|
|
RS_FILE_HINTS_LOCAL |
|
|
|
|
RS_FILE_HINTS_EXTRA |
|
|
|
|
RS_FILE_HINTS_SPEC_ONLY, info))
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
|
|
|
/* have it already */
|
|
|
|
/* add in as completed transfer */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do a source search - for any extra sources */
|
2008-08-09 13:03:24 -04:00
|
|
|
if (mSearch->search(hash, size,
|
|
|
|
RS_FILE_HINTS_REMOTE |
|
|
|
|
RS_FILE_HINTS_SPEC_ONLY, info))
|
2008-07-23 18:01:59 -04:00
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
/* do something with results */
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, ftTransferModule *> mTransfers;
|
|
|
|
std::map<std::string, ftFileCreator *> mFileCreators;
|
|
|
|
|
|
|
|
/* add in new item for download */
|
|
|
|
std::string savepath = mDownloadPath + "/" + fname;
|
|
|
|
std::string chunker = "";
|
|
|
|
ftFileCreator *fc = new ftFileCreator(savepath, size, hash, chunker);
|
2008-08-09 13:03:24 -04:00
|
|
|
ftTransferModule *tm = new ftTransferModule(fc, mDataplex);
|
2008-07-23 18:01:59 -04:00
|
|
|
|
|
|
|
/* add into maps */
|
|
|
|
ftFileControl ftfc(fname, size, hash, flags, fc, tm);
|
|
|
|
|
|
|
|
/* 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-09 13:03:24 -04:00
|
|
|
std::list<std::string>::iterator it;
|
2008-07-23 18:01:59 -04:00
|
|
|
for(it = srcIds.begin(); it != srcIds.end(); it++)
|
|
|
|
{
|
|
|
|
if (mConnMgr->isOnline(*it))
|
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
tm->setPeer(*it, RS_FILE_RATE_TRICKLE | RS_FILE_PEER_ONLINE);
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
tm->setPeer(*it, RS_FILE_PEER_OFFLINE);
|
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-08-09 13:03:24 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ftController::FileCancel(std::string hash)
|
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
return false;
|
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)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
/* check if it exists */
|
|
|
|
if (RsDirUtil::checkCreateDirectory(path))
|
|
|
|
{
|
|
|
|
RsStackMutex stack(ctrlMutex); /******* LOCKED ********/
|
|
|
|
|
|
|
|
mDownloadPath = path;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
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
|
|
|
#if 0 /*** FIX ME !!!**************/
|
|
|
|
|
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 ********/
|
|
|
|
|
|
|
|
mPartialPath = path;
|
|
|
|
|
|
|
|
/* move all existing files! */
|
|
|
|
std::map<std::string, ftFileControl>::iterator it;
|
|
|
|
for(it = mDownloads.begin(); it != mDownloads.end(); it++)
|
|
|
|
{
|
|
|
|
(it->second).mCreator->changePartialDirectory(mPartialPath);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2008-08-09 13:03:24 -04:00
|
|
|
|
2008-07-23 18:01:59 -04:00
|
|
|
return false;
|
2008-08-09 13:03:24 -04:00
|
|
|
#endif
|
2008-07-23 18:01:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ftController::getDownloadDirectory()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(ctrlMutex); /******* LOCKED ********/
|
|
|
|
|
|
|
|
return mDownloadPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ftController::getPartialsDirectory()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(ctrlMutex); /******* LOCKED ********/
|
|
|
|
|
|
|
|
return mPartialPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|