2008-07-02 09:19:59 -04:00
|
|
|
/*
|
|
|
|
* libretroshare/src/ft: ftserver.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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
#include "util/rsdebug.h"
|
|
|
|
const int ftserverzone = 29539;
|
|
|
|
|
|
|
|
#include "ft/ftserver.h"
|
|
|
|
#include "ft/ftextralist.h"
|
|
|
|
#include "ft/ftfilesearch.h"
|
|
|
|
#include "ft/ftcontroller.h"
|
2009-01-22 16:06:54 -05:00
|
|
|
#include "ft/ftfileprovider.h"
|
2008-08-03 08:45:53 -04:00
|
|
|
#include "ft/ftdatamultiplex.h"
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
|
|
|
|
// Includes CacheStrapper / FiMonitor / FiStore for us.
|
|
|
|
|
|
|
|
#include "ft/ftdbase.h"
|
2008-08-03 08:45:53 -04:00
|
|
|
|
|
|
|
#include "pqi/pqi.h"
|
|
|
|
#include "pqi/p3connmgr.h"
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
#include "serialiser/rsserviceids.h"
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
2008-08-17 11:23:11 -04:00
|
|
|
/***
|
|
|
|
* #define SERVER_DEBUG 1
|
2008-12-07 09:53:57 -05:00
|
|
|
* #define DEBUG_TICK 1
|
2008-08-17 11:23:11 -04:00
|
|
|
***/
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
/* Setup */
|
2008-08-09 13:03:24 -04:00
|
|
|
ftServer::ftServer(p3AuthMgr *authMgr, p3ConnectMgr *connMgr)
|
2008-12-07 12:51:11 -05:00
|
|
|
: mP3iface(NULL),
|
|
|
|
mAuthMgr(authMgr), mConnMgr(connMgr),
|
|
|
|
mCacheStrapper(NULL),
|
|
|
|
mFiStore(NULL), mFiMon(NULL),
|
|
|
|
mFtController(NULL), mFtExtra(NULL),
|
|
|
|
mFtDataplex(NULL), mFtSearch(NULL)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
mCacheStrapper = new ftCacheStrapper(authMgr, connMgr);
|
2008-08-03 08:45:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ftServer::setConfigDirectory(std::string path)
|
|
|
|
{
|
|
|
|
mConfigPath = path;
|
2008-08-17 11:23:11 -04:00
|
|
|
|
|
|
|
/* Must update the sub classes ... if they exist
|
|
|
|
* TODO.
|
|
|
|
*/
|
|
|
|
|
|
|
|
std::string localcachedir = mConfigPath + "/cache/local";
|
|
|
|
std::string remotecachedir = mConfigPath + "/cache/remote";
|
|
|
|
|
|
|
|
//mFiStore -> setCacheDir(remotecachedir);
|
|
|
|
//mFiMon -> setCacheDir(localcachedir);
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
}
|
|
|
|
|
2008-08-17 11:23:11 -04:00
|
|
|
void ftServer::setP3Interface(P3Interface *pqi)
|
2008-08-03 08:45:53 -04:00
|
|
|
{
|
2008-08-17 11:23:11 -04:00
|
|
|
mP3iface = pqi;
|
2008-08-03 08:45:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Control Interface */
|
|
|
|
|
|
|
|
/* add Config Items (Extra, Controller) */
|
|
|
|
void ftServer::addConfigComponents(p3ConfigMgr *mgr)
|
|
|
|
{
|
|
|
|
/* NOT SURE ABOUT THIS ONE */
|
|
|
|
}
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
std::string ftServer::OwnId()
|
|
|
|
{
|
|
|
|
std::string ownId;
|
|
|
|
if (mConnMgr)
|
|
|
|
ownId = mConnMgr->getOwnId();
|
|
|
|
return ownId;
|
|
|
|
}
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
/* Final Setup (once everything is assigned) */
|
2008-08-09 13:03:24 -04:00
|
|
|
void ftServer::SetupFtServer(NotifyBase *cb)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
/* setup FiStore/Monitor */
|
2008-08-09 13:03:24 -04:00
|
|
|
std::string localcachedir = mConfigPath + "/cache/local";
|
|
|
|
std::string remotecachedir = mConfigPath + "/cache/remote";
|
2008-07-02 09:19:59 -04:00
|
|
|
std::string ownId = mConnMgr->getOwnId();
|
|
|
|
|
2008-08-17 11:23:11 -04:00
|
|
|
/* search/extras List */
|
|
|
|
mFtExtra = new ftExtraList();
|
|
|
|
mFtSearch = new ftFileSearch();
|
|
|
|
|
|
|
|
/* Transport */
|
2008-08-29 21:07:24 -04:00
|
|
|
mFtDataplex = new ftDataMultiplex(ownId, this, mFtSearch);
|
2008-08-17 11:23:11 -04:00
|
|
|
|
|
|
|
/* make Controller */
|
|
|
|
mFtController = new ftController(mCacheStrapper, mFtDataplex, mConfigPath);
|
2008-11-09 17:17:20 -05:00
|
|
|
mFtController -> setFtSearchNExtra(mFtSearch, mFtExtra);
|
2008-08-29 21:07:24 -04:00
|
|
|
std::string tmppath = ".";
|
2008-08-17 11:23:11 -04:00
|
|
|
mFtController->setPartialsDirectory(tmppath);
|
|
|
|
mFtController->setDownloadDirectory(tmppath);
|
|
|
|
|
|
|
|
|
|
|
|
/* Make Cache Source/Store */
|
2008-08-09 13:03:24 -04:00
|
|
|
mFiStore = new ftFiStore(mCacheStrapper, mFtController, cb, ownId, remotecachedir);
|
2009-01-22 16:06:54 -05:00
|
|
|
mFiMon = new ftFiMonitor(mCacheStrapper,cb, localcachedir, ownId);
|
2008-07-02 09:19:59 -04:00
|
|
|
|
|
|
|
/* now add the set to the cachestrapper */
|
|
|
|
CachePair cp(mFiMon, mFiStore, CacheId(RS_SERVICE_TYPE_FILE_INDEX, 0));
|
|
|
|
mCacheStrapper -> addCachePair(cp);
|
|
|
|
|
2008-08-17 11:23:11 -04:00
|
|
|
/* complete search setup */
|
2008-08-09 13:03:24 -04:00
|
|
|
mFtSearch->addSearchMode(mCacheStrapper, RS_FILE_HINTS_CACHE);
|
2008-08-03 08:45:53 -04:00
|
|
|
mFtSearch->addSearchMode(mFtExtra, RS_FILE_HINTS_EXTRA);
|
2008-08-09 13:03:24 -04:00
|
|
|
mFtSearch->addSearchMode(mFiMon, RS_FILE_HINTS_LOCAL);
|
|
|
|
mFtSearch->addSearchMode(mFiStore, RS_FILE_HINTS_REMOTE);
|
|
|
|
|
|
|
|
mConnMgr->addMonitor(mFtController);
|
2008-08-17 11:23:11 -04:00
|
|
|
mConnMgr->addMonitor(mCacheStrapper);
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
void ftServer::StartupThreads()
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
2008-08-03 08:45:53 -04:00
|
|
|
/* start up order - important for dependencies */
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
/* self contained threads */
|
|
|
|
/* startup ExtraList Thread */
|
|
|
|
mFtExtra->start();
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
/* startup Monitor Thread */
|
|
|
|
/* startup the FileMonitor (after cache load) */
|
|
|
|
mFiMon->setPeriod(600); /* 10 minutes */
|
|
|
|
/* start it up */
|
|
|
|
//mFiMon->setSharedDirectories(dbase_dirs);
|
|
|
|
mFiMon->start();
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
/* Controller thread */
|
|
|
|
mFtController->start();
|
2008-08-17 11:23:11 -04:00
|
|
|
|
2008-08-29 21:07:24 -04:00
|
|
|
/* Dataplex */
|
|
|
|
mFtDataplex->start();
|
|
|
|
|
2008-08-17 11:23:11 -04:00
|
|
|
/* start own thread */
|
|
|
|
start();
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
CacheStrapper *ftServer::getCacheStrapper()
|
|
|
|
{
|
|
|
|
return mCacheStrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheTransfer *ftServer::getCacheTransfer()
|
|
|
|
{
|
|
|
|
return mFtController;
|
|
|
|
}
|
|
|
|
|
2008-08-17 11:23:11 -04:00
|
|
|
void ftServer::run()
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
2009-01-17 23:00:24 -05:00
|
|
|
//scan the uploads list in ftdatamultiplex and delete the items which time out
|
|
|
|
time_t now = time(NULL);
|
|
|
|
FileInfo info;
|
|
|
|
std::list<std::string> toDels;
|
|
|
|
std::map<std::string, ftFileProvider *>::iterator sit;
|
|
|
|
for(sit = mFtDataplex->mServers.begin(); sit != mFtDataplex->mServers.end(); sit++)
|
|
|
|
{
|
|
|
|
if (FileDetails(sit->first,RS_FILE_HINTS_UPLOAD,info))
|
|
|
|
{
|
2009-02-06 15:42:01 -05:00
|
|
|
if ((now - info.lastTS) > 10)
|
2009-01-17 23:00:24 -05:00
|
|
|
{
|
2009-02-06 15:42:01 -05:00
|
|
|
#ifdef SERVER_DEBUG
|
|
|
|
std::cout << "info.lastTS = " << info.lastTS << ", now=" << now << std::endl ;
|
|
|
|
#endif
|
2009-01-17 23:00:24 -05:00
|
|
|
toDels.push_back(sit->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-09 15:26:12 -05:00
|
|
|
if(!toDels.empty())
|
|
|
|
mFtDataplex->deleteServers(toDels) ;
|
|
|
|
|
2008-08-25 16:03:39 -04:00
|
|
|
#ifdef WIN32
|
|
|
|
Sleep(1000);
|
|
|
|
#else
|
2008-08-17 11:23:11 -04:00
|
|
|
sleep(1);
|
2008-08-25 16:03:39 -04:00
|
|
|
#endif
|
2008-08-17 11:23:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
/***************************************************************/
|
|
|
|
/********************** RsFiles Interface **********************/
|
|
|
|
/***************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************/
|
|
|
|
/********************** Controller Access **********************/
|
|
|
|
/***************************************************************/
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
bool ftServer::FileRequest(std::string fname, std::string hash, uint64_t size,
|
2008-08-09 13:03:24 -04:00
|
|
|
std::string dest, uint32_t flags, std::list<std::string> srcIds)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
2009-02-06 15:42:01 -05:00
|
|
|
std::cerr << "Requesting " << fname << std::endl ;
|
2008-08-09 13:03:24 -04:00
|
|
|
return mFtController->FileRequest(fname, hash, size,
|
|
|
|
dest, flags, srcIds);
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftServer::FileCancel(std::string hash)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
|
|
|
return mFtController->FileCancel(hash);
|
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftServer::FileControl(std::string hash, uint32_t flags)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
|
|
|
return mFtController->FileControl(hash, flags);
|
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftServer::FileClearCompleted()
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
|
|
|
return mFtController->FileClearCompleted();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Directory Handling */
|
|
|
|
void ftServer::setDownloadDirectory(std::string path)
|
|
|
|
{
|
2008-08-03 08:45:53 -04:00
|
|
|
mFtController->setDownloadDirectory(path);
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ftServer::getDownloadDirectory()
|
|
|
|
{
|
|
|
|
return mFtController->getDownloadDirectory();
|
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
void ftServer::setPartialsDirectory(std::string path)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
2008-08-03 08:45:53 -04:00
|
|
|
mFtController->setPartialsDirectory(path);
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
std::string ftServer::getPartialsDirectory()
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
|
|
|
return mFtController->getPartialsDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************/
|
|
|
|
/************************* Other Access ************************/
|
|
|
|
/***************************************************************/
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
bool ftServer::FileDownloads(std::list<std::string> &hashs)
|
|
|
|
{
|
2008-11-30 12:17:28 -05:00
|
|
|
return mFtController->FileDownloads(hashs);
|
|
|
|
/* this only contains downloads.... not completed */
|
|
|
|
//return mFtDataplex->FileDownloads(hashs);
|
2008-08-09 13:03:24 -04:00
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftServer::FileUploads(std::list<std::string> &hashs)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
return mFtDataplex->FileUploads(hashs);
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftServer::FileDetails(std::string hash, uint32_t hintflags, FileInfo &info)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
|
|
|
bool found = false;
|
2008-11-30 12:17:28 -05:00
|
|
|
if (hintflags & RS_FILE_HINTS_DOWNLOAD)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
2008-11-09 17:17:20 -05:00
|
|
|
//found = mFtDataplex->FileDetails(hash, hintflags, info);
|
|
|
|
//
|
|
|
|
// Use Controller for download searches.
|
|
|
|
found = mFtController->FileDetails(hash, info);
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
2008-11-30 12:17:28 -05:00
|
|
|
else if (hintflags & RS_FILE_HINTS_UPLOAD)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
found = mFtDataplex->FileDetails(hash, hintflags, info);
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
2008-08-21 17:30:59 -04:00
|
|
|
found = mFtSearch->search(hash, 0, hintflags, info);
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************/
|
|
|
|
/******************* ExtraFileList Access **********************/
|
|
|
|
/***************************************************************/
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
bool ftServer::ExtraFileAdd(std::string fname, std::string hash, uint64_t size,
|
2008-07-02 09:19:59 -04:00
|
|
|
uint32_t period, uint32_t flags)
|
|
|
|
{
|
|
|
|
return mFtExtra->addExtraFile(fname, hash, size, period, flags);
|
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftServer::ExtraFileRemove(std::string hash, uint32_t flags)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
|
|
|
return mFtExtra->removeExtraFile(hash, flags);
|
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftServer::ExtraFileHash(std::string localpath, uint32_t period, uint32_t flags)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
|
|
|
return mFtExtra->hashExtraFile(localpath, period, flags);
|
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftServer::ExtraFileStatus(std::string localpath, FileInfo &info)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
|
|
|
return mFtExtra->hashExtraFileDone(localpath, info);
|
|
|
|
}
|
|
|
|
|
2008-11-27 16:23:46 -05:00
|
|
|
bool ftServer::ExtraFileMove(std::string fname, std::string hash, uint64_t size,
|
|
|
|
std::string destpath)
|
|
|
|
{
|
|
|
|
return mFtExtra->moveExtraFile(fname, hash, size, destpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-02 09:19:59 -04:00
|
|
|
/***************************************************************/
|
|
|
|
/******************** Directory Listing ************************/
|
|
|
|
/***************************************************************/
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
int ftServer::RequestDirDetails(std::string uid, std::string path, DirDetails &details)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
2008-12-07 12:51:11 -05:00
|
|
|
#ifdef SERVER_DEBUG
|
|
|
|
std::cerr << "ftServer::RequestDirDetails(uid:" << uid;
|
|
|
|
std::cerr << ", path:" << path << ", ...) -> mFiStore";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
if (!mFiStore)
|
|
|
|
{
|
|
|
|
std::cerr << "mFiStore not SET yet = FAIL";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
#endif
|
2008-07-02 09:19:59 -04:00
|
|
|
return mFiStore->RequestDirDetails(uid, path, details);
|
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
int ftServer::RequestDirDetails(void *ref, DirDetails &details, uint32_t flags)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
2008-12-07 12:51:11 -05:00
|
|
|
#ifdef SERVER_DEBUG
|
|
|
|
std::cerr << "ftServer::RequestDirDetails(ref:" << ref;
|
|
|
|
std::cerr << ", flags:" << flags << ", ...) -> mFiStore";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
if (!mFiStore)
|
|
|
|
{
|
|
|
|
std::cerr << "mFiStore not SET yet = FAIL";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2008-07-02 09:19:59 -04:00
|
|
|
return mFiStore->RequestDirDetails(ref, details, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************/
|
|
|
|
/******************** Search Interface *************************/
|
|
|
|
/***************************************************************/
|
|
|
|
|
|
|
|
|
2009-03-15 18:45:40 -04:00
|
|
|
int ftServer::SearchKeywords(std::list<std::string> keywords, std::list<FileDetail> &results,uint32_t flags)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
2008-12-07 12:51:11 -05:00
|
|
|
#ifdef SERVER_DEBUG
|
|
|
|
std::cerr << "ftServer::SearchKeywords()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
if (!mFiStore)
|
|
|
|
{
|
|
|
|
std::cerr << "mFiStore not SET yet = FAIL";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2009-03-15 18:45:40 -04:00
|
|
|
return mFiStore->SearchKeywords(keywords, results,flags);
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
int ftServer::SearchBoolExp(Expression * exp, std::list<FileDetail> &results)
|
2008-07-02 09:19:59 -04:00
|
|
|
{
|
|
|
|
return mFiStore->searchBoolExp(exp, results);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************/
|
|
|
|
/*************** Local Shared Dir Interface ********************/
|
|
|
|
/***************************************************************/
|
|
|
|
|
|
|
|
bool ftServer::ConvertSharedFilePath(std::string path, std::string &fullpath)
|
|
|
|
{
|
|
|
|
return mFiMon->convertSharedFilePath(path, fullpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ftServer::ForceDirectoryCheck()
|
|
|
|
{
|
|
|
|
mFiMon->forceDirectoryCheck();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ftServer::InDirectoryCheck()
|
|
|
|
{
|
2008-08-03 08:45:53 -04:00
|
|
|
return mFiMon->inDirectoryCheck();
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ftServer::getSharedDirectories(std::list<std::string> &dirs)
|
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
mFiMon->getSharedDirectories(dirs);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ftServer::setSharedDirectories(std::list<std::string> &dirs)
|
|
|
|
{
|
|
|
|
mFiMon->setSharedDirectories(dirs);
|
|
|
|
return true;
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ftServer::addSharedDirectory(std::string dir)
|
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
std::list<std::string> dirList;
|
|
|
|
mFiMon->getSharedDirectories(dirList);
|
|
|
|
dirList.push_back(dir);
|
|
|
|
|
|
|
|
mFiMon->setSharedDirectories(dirList);
|
|
|
|
return true;
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ftServer::removeSharedDirectory(std::string dir)
|
|
|
|
{
|
2008-08-09 13:03:24 -04:00
|
|
|
std::list<std::string> dirList;
|
|
|
|
std::list<std::string>::iterator it;
|
|
|
|
|
2008-11-15 15:00:29 -05:00
|
|
|
#ifdef SERVER_DEBUG
|
|
|
|
std::cerr << "ftServer::removeSharedDirectory(" << dir << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
mFiMon->getSharedDirectories(dirList);
|
|
|
|
|
2008-11-15 15:00:29 -05:00
|
|
|
#ifdef SERVER_DEBUG
|
|
|
|
for(it = dirList.begin(); it != dirList.end(); it++)
|
|
|
|
{
|
|
|
|
std::cerr << "ftServer::removeSharedDirectory()";
|
|
|
|
std::cerr << " existing: " << *it;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (dirList.end() == (it =
|
2008-08-09 13:03:24 -04:00
|
|
|
std::find(dirList.begin(), dirList.end(), dir)))
|
|
|
|
{
|
2008-11-15 15:00:29 -05:00
|
|
|
#ifdef SERVER_DEBUG
|
|
|
|
std::cerr << "ftServer::removeSharedDirectory()";
|
|
|
|
std::cerr << " Cannot Find Directory... Fail";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-15 15:00:29 -05:00
|
|
|
|
|
|
|
#ifdef SERVER_DEBUG
|
|
|
|
std::cerr << "ftServer::removeSharedDirectory()";
|
|
|
|
std::cerr << " Updating Directories";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-08-09 13:03:24 -04:00
|
|
|
dirList.erase(it);
|
|
|
|
mFiMon->setSharedDirectories(dirList);
|
|
|
|
|
|
|
|
return true;
|
2008-07-02 09:19:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************/
|
|
|
|
/****************** End of RsFiles Interface *******************/
|
|
|
|
/***************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************/
|
|
|
|
/**************** Config Interface *****************************/
|
|
|
|
/***************************************************************/
|
|
|
|
|
|
|
|
/* Key Functions to be overloaded for Full Configuration */
|
2008-08-03 08:45:53 -04:00
|
|
|
RsSerialiser *ftServer::setupSerialiser()
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
std::list<RsItem *> ftServer::saveList(bool &cleanup)
|
|
|
|
{
|
|
|
|
std::list<RsItem *> list;
|
|
|
|
return list;
|
|
|
|
}
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftServer::loadList(std::list<RsItem *> load)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
bool ftServer::loadConfigMap(std::map<std::string, std::string> &configMap)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************/
|
|
|
|
/********************** Data Flow **********************/
|
|
|
|
/***************************************************************/
|
|
|
|
|
|
|
|
/* Client Send */
|
|
|
|
bool ftServer::sendDataRequest(std::string peerId, std::string hash,
|
|
|
|
uint64_t size, uint64_t offset, uint32_t chunksize)
|
|
|
|
{
|
|
|
|
/* create a packet */
|
|
|
|
/* push to networking part */
|
|
|
|
RsFileRequest *rfi = new RsFileRequest();
|
|
|
|
|
|
|
|
/* id */
|
|
|
|
rfi->PeerId(peerId);
|
|
|
|
|
|
|
|
/* file info */
|
|
|
|
rfi->file.filesize = size;
|
|
|
|
rfi->file.hash = hash; /* ftr->hash; */
|
|
|
|
|
|
|
|
/* offsets */
|
|
|
|
rfi->fileoffset = offset; /* ftr->offset; */
|
|
|
|
rfi->chunksize = chunksize; /* ftr->chunk; */
|
|
|
|
|
|
|
|
mP3iface->SendFileRequest(rfi);
|
2008-08-09 13:03:24 -04:00
|
|
|
|
|
|
|
return true;
|
2008-08-03 08:45:53 -04:00
|
|
|
}
|
|
|
|
|
2008-11-02 06:38:11 -05:00
|
|
|
//const uint32_t MAX_FT_CHUNK = 32 * 1024; /* 32K */
|
|
|
|
//const uint32_t MAX_FT_CHUNK = 16 * 1024; /* 16K */
|
|
|
|
const uint32_t MAX_FT_CHUNK = 8 * 1024; /* 16K */
|
2008-08-03 08:45:53 -04:00
|
|
|
|
|
|
|
/* Server Send */
|
|
|
|
bool ftServer::sendData(std::string peerId, std::string hash, uint64_t size,
|
|
|
|
uint64_t baseoffset, uint32_t chunksize, void *data)
|
|
|
|
{
|
|
|
|
/* create a packet */
|
|
|
|
/* push to networking part */
|
|
|
|
uint32_t tosend = chunksize;
|
|
|
|
uint64_t offset = 0;
|
|
|
|
uint32_t chunk;
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
#ifdef SERVER_DEBUG
|
|
|
|
std::cerr << "ftServer::sendData() to " << peerId << std::endl;
|
|
|
|
std::cerr << "hash: " << hash;
|
2008-11-02 06:38:11 -05:00
|
|
|
std::cerr << " offset: " << baseoffset;
|
|
|
|
std::cerr << " chunk: " << chunksize;
|
2008-10-29 16:58:23 -04:00
|
|
|
std::cerr << " data: " << data;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-08-03 08:45:53 -04:00
|
|
|
|
|
|
|
while(tosend > 0)
|
|
|
|
{
|
|
|
|
/* workout size */
|
|
|
|
chunk = MAX_FT_CHUNK;
|
|
|
|
if (chunk > tosend)
|
|
|
|
{
|
|
|
|
chunk = tosend;
|
|
|
|
}
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
/******** New Serialiser Type *******/
|
2008-07-02 09:19:59 -04:00
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
RsFileData *rfd = new RsFileData();
|
|
|
|
|
|
|
|
/* set id */
|
|
|
|
rfd->PeerId(peerId);
|
|
|
|
|
|
|
|
/* file info */
|
|
|
|
rfd->fd.file.filesize = size;
|
|
|
|
rfd->fd.file.hash = hash;
|
|
|
|
rfd->fd.file.name = ""; /* blank other data */
|
|
|
|
rfd->fd.file.path = "";
|
|
|
|
rfd->fd.file.pop = 0;
|
|
|
|
rfd->fd.file.age = 0;
|
|
|
|
|
|
|
|
rfd->fd.file_offset = baseoffset + offset;
|
|
|
|
|
|
|
|
/* file data */
|
|
|
|
rfd->fd.binData.setBinData(
|
|
|
|
&(((uint8_t *) data)[offset]), chunk);
|
|
|
|
|
2008-10-29 16:58:23 -04:00
|
|
|
/* print the data pointer */
|
|
|
|
#ifdef SERVER_DEBUG
|
|
|
|
std::cerr << "ftServer::sendData() Packet: " << std::endl;
|
|
|
|
std::cerr << " offset: " << rfd->fd.file_offset;
|
|
|
|
std::cerr << " chunk: " << chunk;
|
|
|
|
std::cerr << " len: " << rfd->fd.binData.bin_len;
|
|
|
|
std::cerr << " data: " << rfd->fd.binData.bin_data;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
mP3iface->SendFileData(rfd);
|
|
|
|
|
|
|
|
offset += chunk;
|
|
|
|
tosend -= chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clean up data */
|
|
|
|
free(data);
|
2008-08-09 13:03:24 -04:00
|
|
|
|
|
|
|
return true;
|
2008-08-03 08:45:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-02 06:38:11 -05:00
|
|
|
/* NB: The rsCore lock must be activated before calling this.
|
|
|
|
* This Lock should be moved lower into the system...
|
|
|
|
* most likely destination is in ftServer.
|
|
|
|
*/
|
2008-08-03 08:45:53 -04:00
|
|
|
int ftServer::tick()
|
|
|
|
{
|
|
|
|
rslog(RSL_DEBUG_BASIC, ftserverzone,
|
|
|
|
"filedexserver::tick()");
|
|
|
|
|
|
|
|
if (mP3iface == NULL)
|
|
|
|
{
|
2008-08-17 11:23:11 -04:00
|
|
|
#ifdef SERVER_DEBUG
|
|
|
|
std::cerr << "ftServer::tick() ERROR: mP3iface == NULL";
|
|
|
|
#endif
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
std::ostringstream out;
|
|
|
|
rslog(RSL_DEBUG_BASIC, ftserverzone,
|
|
|
|
"filedexserver::tick() Invalid Interface()");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int moreToTick = 0;
|
|
|
|
|
|
|
|
if (0 < mP3iface -> tick())
|
|
|
|
{
|
|
|
|
moreToTick = 1;
|
|
|
|
#ifdef DEBUG_TICK
|
|
|
|
std::cerr << "filedexserver::tick() moreToTick from mP3iface" << std::endl;
|
2008-07-02 09:19:59 -04:00
|
|
|
#endif
|
2008-08-03 08:45:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (0 < handleInputQueues())
|
|
|
|
{
|
|
|
|
moreToTick = 1;
|
|
|
|
#ifdef DEBUG_TICK
|
|
|
|
std::cerr << "filedexserver::tick() moreToTick from InputQueues" << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return moreToTick;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This function needs to be divided up.
|
|
|
|
bool ftServer::handleInputQueues()
|
|
|
|
{
|
2008-12-07 09:53:57 -05:00
|
|
|
bool moreToTick = false;
|
|
|
|
|
|
|
|
if (handleCacheData())
|
|
|
|
moreToTick = true;
|
|
|
|
|
|
|
|
if (handleFileData())
|
|
|
|
moreToTick = true;
|
2008-08-09 13:03:24 -04:00
|
|
|
|
2008-12-07 09:53:57 -05:00
|
|
|
return moreToTick;
|
2008-08-03 08:45:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ftServer::handleCacheData()
|
|
|
|
{
|
|
|
|
// get all the incoming results.. and print to the screen.
|
|
|
|
RsCacheRequest *cr;
|
|
|
|
RsCacheItem *ci;
|
|
|
|
|
|
|
|
// Loop through Search Results.
|
|
|
|
int i = 0;
|
|
|
|
int i_init = 0;
|
|
|
|
|
2008-08-17 11:23:11 -04:00
|
|
|
#ifdef SERVER_DEBUG
|
2008-11-02 06:38:11 -05:00
|
|
|
//std::cerr << "ftServer::handleCacheData()" << std::endl;
|
2008-08-17 11:23:11 -04:00
|
|
|
#endif
|
2008-08-03 08:45:53 -04:00
|
|
|
while((ci = mP3iface -> GetSearchResult()) != NULL)
|
|
|
|
{
|
2008-08-17 11:23:11 -04:00
|
|
|
|
|
|
|
#ifdef SERVER_DEBUG
|
|
|
|
std::cerr << "ftServer::handleCacheData() Recvd SearchResult (CacheResponse!)" << std::endl;
|
2008-08-03 08:45:53 -04:00
|
|
|
std::ostringstream out;
|
|
|
|
if (i++ == i_init)
|
|
|
|
{
|
|
|
|
out << "Recieved Search Results:" << std::endl;
|
|
|
|
}
|
|
|
|
ci -> print(out);
|
|
|
|
rslog(RSL_DEBUG_BASIC, ftserverzone, out.str());
|
2008-08-17 11:23:11 -04:00
|
|
|
#endif
|
2008-08-03 08:45:53 -04:00
|
|
|
|
|
|
|
/* these go to the CacheStrapper! */
|
|
|
|
CacheData data;
|
|
|
|
data.cid = CacheId(ci->cacheType, ci->cacheSubId);
|
|
|
|
data.hash = ci->file.hash;
|
|
|
|
data.size = ci->file.filesize;
|
|
|
|
data.name = ci->file.name;
|
|
|
|
data.path = ci->file.path;
|
|
|
|
data.pid = ci->PeerId();
|
|
|
|
data.pname = mAuthMgr->getName(ci->PeerId());
|
|
|
|
mCacheStrapper->recvCacheResponse(data, time(NULL));
|
|
|
|
|
|
|
|
delete ci;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now requested Searches.
|
|
|
|
i_init = i;
|
|
|
|
while((cr = mP3iface -> RequestedSearch()) != NULL)
|
|
|
|
{
|
2008-08-17 11:23:11 -04:00
|
|
|
#ifdef SERVER_DEBUG
|
2008-08-03 08:45:53 -04:00
|
|
|
/* just delete these */
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "Requested Search:" << std::endl;
|
|
|
|
cr -> print(out);
|
|
|
|
rslog(RSL_DEBUG_BASIC, ftserverzone, out.str());
|
2008-08-17 11:23:11 -04:00
|
|
|
#endif
|
2008-08-03 08:45:53 -04:00
|
|
|
delete cr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now handle it replacement (pushed cache results)
|
|
|
|
{
|
|
|
|
std::list<std::pair<RsPeerId, CacheData> > cacheUpdates;
|
|
|
|
std::list<std::pair<RsPeerId, CacheData> >::iterator it;
|
|
|
|
|
|
|
|
mCacheStrapper->getCacheUpdates(cacheUpdates);
|
|
|
|
for(it = cacheUpdates.begin(); it != cacheUpdates.end(); it++)
|
|
|
|
{
|
|
|
|
/* construct reply */
|
|
|
|
RsCacheItem *ci = new RsCacheItem();
|
|
|
|
|
|
|
|
/* id from incoming */
|
|
|
|
ci -> PeerId(it->first);
|
|
|
|
|
|
|
|
ci -> file.hash = (it->second).hash;
|
|
|
|
ci -> file.name = (it->second).name;
|
|
|
|
ci -> file.path = ""; // (it->second).path;
|
|
|
|
ci -> file.filesize = (it->second).size;
|
|
|
|
ci -> cacheType = (it->second).cid.type;
|
|
|
|
ci -> cacheSubId = (it->second).cid.subid;
|
|
|
|
|
|
|
|
#ifdef SERVER_DEBUG
|
|
|
|
std::ostringstream out2;
|
|
|
|
out2 << "Outgoing CacheStrapper Update -> RsCacheItem:" << std::endl;
|
|
|
|
ci -> print(out2);
|
|
|
|
std::cerr << out2.str() << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//rslog(RSL_DEBUG_BASIC, ftserverzone, out2.str());
|
|
|
|
mP3iface -> SendSearchResult(ci);
|
2008-12-07 09:53:57 -05:00
|
|
|
|
|
|
|
i++;
|
2008-08-03 08:45:53 -04:00
|
|
|
}
|
|
|
|
}
|
2008-12-07 09:53:57 -05:00
|
|
|
return (i > 0);
|
2008-08-03 08:45:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ftServer::handleFileData()
|
|
|
|
{
|
|
|
|
// now File Input.
|
|
|
|
RsFileRequest *fr;
|
|
|
|
RsFileData *fd;
|
|
|
|
|
|
|
|
int i_init = 0;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
i_init = i;
|
|
|
|
while((fr = mP3iface -> GetFileRequest()) != NULL )
|
|
|
|
{
|
|
|
|
#ifdef SERVER_DEBUG
|
2008-08-17 11:23:11 -04:00
|
|
|
std::cerr << "ftServer::handleFileData() Recvd ftFiler Request" << std::endl;
|
2008-08-03 08:45:53 -04:00
|
|
|
std::ostringstream out;
|
|
|
|
if (i == i_init)
|
|
|
|
{
|
|
|
|
out << "Incoming(Net) File Item:" << std::endl;
|
|
|
|
}
|
|
|
|
fr -> print(out);
|
|
|
|
rslog(RSL_DEBUG_BASIC, ftserverzone, out.str());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
i++; /* count */
|
|
|
|
mFtDataplex->recvDataRequest(fr->PeerId(),
|
|
|
|
fr->file.hash, fr->file.filesize,
|
|
|
|
fr->fileoffset, fr->chunksize);
|
|
|
|
|
|
|
|
FileInfo(ffr);
|
|
|
|
delete fr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now File Data.
|
|
|
|
i_init = i;
|
|
|
|
while((fd = mP3iface -> GetFileData()) != NULL )
|
|
|
|
{
|
|
|
|
#ifdef SERVER_DEBUG
|
2008-08-17 11:23:11 -04:00
|
|
|
std::cerr << "ftServer::handleFileData() Recvd ftFiler Data" << std::endl;
|
2008-10-29 16:58:23 -04:00
|
|
|
std::cerr << "hash: " << fd->fd.file.hash;
|
|
|
|
std::cerr << " length: " << fd->fd.binData.bin_len;
|
|
|
|
std::cerr << " data: " << fd->fd.binData.bin_data;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2008-08-03 08:45:53 -04:00
|
|
|
std::ostringstream out;
|
|
|
|
if (i == i_init)
|
|
|
|
{
|
|
|
|
out << "Incoming(Net) File Data:" << std::endl;
|
|
|
|
}
|
|
|
|
fd -> print(out);
|
|
|
|
rslog(RSL_DEBUG_BASIC, ftserverzone, out.str());
|
|
|
|
#endif
|
|
|
|
i++; /* count */
|
|
|
|
|
|
|
|
/* incoming data */
|
|
|
|
mFtDataplex->recvData(fd->PeerId(),
|
|
|
|
fd->fd.file.hash, fd->fd.file.filesize,
|
|
|
|
fd->fd.file_offset,
|
|
|
|
fd->fd.binData.bin_len,
|
|
|
|
fd->fd.binData.bin_data);
|
|
|
|
|
|
|
|
/* we've stolen the data part -> so blank before delete
|
|
|
|
*/
|
2008-10-29 16:58:23 -04:00
|
|
|
fd->fd.binData.TlvShallowClear();
|
2008-08-03 08:45:53 -04:00
|
|
|
delete fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**********************************
|
|
|
|
**********************************
|
|
|
|
**********************************
|
|
|
|
*********************************/
|
|
|
|
|
2008-11-15 15:00:29 -05:00
|
|
|
/***************************** CONFIG ****************************/
|
|
|
|
|
|
|
|
bool ftServer::addConfiguration(p3ConfigMgr *cfgmgr)
|
|
|
|
{
|
|
|
|
/* add all the subbits to config mgr */
|
|
|
|
cfgmgr->addConfiguration("ft_shared.cfg", mFiMon);
|
|
|
|
cfgmgr->addConfiguration("ft_extra.cfg", mFtExtra);
|
|
|
|
cfgmgr->addConfiguration("ft_transfers.cfg", mFtController);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ftServer::ResumeTransfers()
|
|
|
|
{
|
2008-11-20 19:10:59 -05:00
|
|
|
mFtController->activate();
|
2008-11-15 15:00:29 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2008-08-03 08:45:53 -04:00
|
|
|
|