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 " .
*
*/
2012-04-12 19:29:39 -04:00
# include <unistd.h> /* for (u)sleep() */
# include <iostream>
2008-08-03 08:45:53 -04:00
# include "util/rsdebug.h"
2009-08-11 16:00:08 -04:00
# include "util/rsdir.h"
2010-08-06 05:40:23 -04:00
# include "retroshare/rstypes.h"
2008-08-03 08:45:53 -04:00
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"
2010-03-06 18:29:47 -05:00
//#include "ft/ftdwlqueue.h"
2009-05-26 17:42:45 -04:00
# include "turtle/p3turtle.h"
2008-08-03 08:45:53 -04:00
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"
2011-07-09 14:39:34 -04:00
# include "pqi/p3linkmgr.h"
2008-08-03 08:45:53 -04:00
2008-08-09 13:03:24 -04:00
# include "serialiser/rsserviceids.h"
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 */
2011-07-09 14:39:34 -04:00
ftServer : : ftServer ( p3PeerMgr * pm , p3LinkMgr * lm )
2011-08-12 09:42:30 -04:00
: mP3iface ( NULL ) , mPeerMgr ( pm ) ,
mLinkMgr ( lm ) ,
2008-12-07 12:51:11 -05:00
mCacheStrapper ( NULL ) ,
mFiStore ( NULL ) , mFiMon ( NULL ) ,
mFtController ( NULL ) , mFtExtra ( NULL ) ,
2011-07-04 18:59:39 -04:00
mFtDataplex ( NULL ) , mFtSearch ( NULL ) , srvMutex ( " ftServer " )
2008-07-02 09:19:59 -04:00
{
2011-07-09 14:39:34 -04:00
mCacheStrapper = new ftCacheStrapper ( lm ) ;
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 .
*/
2010-02-10 15:32:21 -05:00
std : : string basecachedir = mConfigPath + " /cache " ;
2008-08-17 11:23:11 -04:00
std : : string localcachedir = mConfigPath + " /cache/local " ;
std : : string remotecachedir = mConfigPath + " /cache/remote " ;
2010-02-10 15:32:21 -05:00
RsDirUtil : : checkCreateDirectory ( basecachedir ) ;
2009-08-11 16:00:08 -04:00
RsDirUtil : : checkCreateDirectory ( localcachedir ) ;
RsDirUtil : : checkCreateDirectory ( remotecachedir ) ;
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) */
2011-08-12 09:42:30 -04:00
void ftServer : : addConfigComponents ( p3ConfigMgr */ * mgr */ )
2008-08-03 08:45:53 -04:00
{
/* NOT SURE ABOUT THIS ONE */
}
2008-10-29 16:58:23 -04:00
std : : string ftServer : : OwnId ( )
{
std : : string ownId ;
2011-07-09 14:39:34 -04:00
if ( mLinkMgr )
ownId = mLinkMgr - > getOwnId ( ) ;
2008-10-29 16:58:23 -04:00
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 " ;
2011-07-09 14:39:34 -04:00
std : : string ownId = mLinkMgr - > getOwnId ( ) ;
2008-07-02 09:19:59 -04:00
2008-08-17 11:23:11 -04:00
/* search/extras List */
mFtExtra = new ftExtraList ( ) ;
mFtSearch = new ftFileSearch ( ) ;
/* Transport */
2009-05-26 17:42:45 -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 */
2011-07-09 14:39:34 -04:00
mFiStore = new ftFiStore ( mCacheStrapper , mFtController , cb , mPeerMgr , ownId , remotecachedir ) ;
2011-06-15 16:03:26 -04:00
mFiMon = new ftFiMonitor ( mCacheStrapper , cb , localcachedir , ownId , mConfigPath ) ;
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 ) ;
2011-07-09 14:39:34 -04:00
mLinkMgr - > addMonitor ( mFtController ) ;
mLinkMgr - > addMonitor ( mCacheStrapper ) ;
2008-08-17 11:23:11 -04:00
2008-07-02 09:19:59 -04:00
return ;
}
2009-05-26 17:42:45 -04:00
void ftServer : : connectToTurtleRouter ( p3turtle * fts )
{
mTurtleRouter = fts ;
mFtController - > setTurtleRouter ( fts ) ;
}
2008-07-02 09:19:59 -04:00
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) */
/* start it up */
2011-03-28 17:52:21 -04:00
2008-08-03 08:45:53 -04:00
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
}
2010-05-29 11:14:25 -04:00
void ftServer : : StopThreads ( )
{
/* stop own thread */
join ( ) ;
/* stop Dataplex */
mFtDataplex - > join ( ) ;
/* stop Controller thread */
mFtController - > join ( ) ;
/* stop Monitor Thread */
mFiMon - > join ( ) ;
/* self contained threads */
/* stop ExtraList Thread */
mFtExtra - > join ( ) ;
delete ( mFtDataplex ) ;
mFtDataplex = NULL ;
delete ( mFtController ) ;
mFtController = NULL ;
delete ( mFiMon ) ;
mFiMon = NULL ;
delete ( mFtExtra ) ;
mFtExtra = NULL ;
}
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 ( )
{
2011-03-03 18:30:08 -05:00
while ( isRunning ( ) )
2008-08-17 11:23:11 -04:00
{
2010-02-15 15:44:37 -05:00
mFtDataplex - > deleteUnusedServers ( ) ;
2012-03-15 15:55:43 -04:00
mFtDataplex - > handlePendingCrcRequests ( ) ;
mFtDataplex - > dispatchReceivedChunkCheckSum ( ) ;
2008-08-25 16:03:39 -04:00
# ifdef WIN32
2010-02-15 15:44:37 -05:00
Sleep ( 5000 ) ;
2008-08-25 16:03:39 -04:00
# else
2010-02-15 15:44:37 -05:00
sleep ( 5 ) ;
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 **********************/
/***************************************************************/
2010-02-07 13:10:12 -05:00
bool ftServer : : checkHash ( const std : : string & hash , std : : string & error_string )
{
static const uint32_t HASH_LENGTH = 40 ;
if ( hash . length ( ) ! = HASH_LENGTH )
{
2012-04-17 20:00:59 -04:00
rs_sprintf ( error_string , " Line too long : %u chars, %ld expected. " , hash . length ( ) , HASH_LENGTH ) ;
2010-02-07 13:10:12 -05:00
return false ;
}
2010-02-08 12:38:57 -05:00
for ( uint32_t i = 0 ; i < hash . length ( ) ; + + i )
2010-02-07 13:10:12 -05:00
if ( ! ( ( hash [ i ] > 47 & & hash [ i ] < 58 ) | | ( hash [ i ] > 96 & & hash [ i ] < 103 ) ) )
{
2012-04-17 20:00:59 -04:00
rs_sprintf ( error_string , " unexpected char code=%d '%c' " , ( int ) hash [ i ] , hash [ i ] ) ;
2010-02-07 13:10:12 -05:00
return false ;
}
return true ;
}
2010-10-05 16:39:14 -04:00
bool ftServer : : alreadyHaveFile ( const std : : string & hash , FileInfo & info )
{
return mFtController - > alreadyHaveFile ( hash , info ) ;
}
2012-11-02 09:52:29 -04:00
bool ftServer : : FileRequest ( const std : : string & fname , const std : : string & hash , uint64_t size , const std : : string & dest , TransferRequestFlags flags , const std : : list < std : : string > & srcIds )
2008-07-02 09:19:59 -04:00
{
2010-02-07 13:10:12 -05:00
std : : string error_string ;
if ( ! checkHash ( hash , error_string ) )
{
rsicontrol - > getNotify ( ) . notifyErrorMsg ( 0 , 0 , " Error handling hash \" " + hash + " \" . This hash appears to be invalid(Error string= \" " + error_string + " \" ). This is probably due an bad handling of strings. " ) ;
return false ;
}
2009-02-06 15:42:01 -05:00
std : : cerr < < " Requesting " < < fname < < std : : endl ;
2010-01-16 10:42:26 -05:00
2010-03-06 18:29:47 -05:00
if ( ! mFtController - > FileRequest ( fname , hash , size , dest , flags , srcIds ) )
2010-01-16 10:42:26 -05:00
return false ;
2009-08-03 15:43:52 -04:00
return true ;
2008-07-02 09:19:59 -04:00
}
2013-02-27 11:59:16 -05:00
bool ftServer : : setDestinationName ( const std : : string & hash , const std : : string & name )
{
return mFtController - > setDestinationName ( hash , name ) ;
}
bool ftServer : : setDestinationDirectory ( const std : : string & hash , const std : : string & directory )
{
return mFtController - > setDestinationDirectory ( hash , directory ) ;
}
2009-12-08 17:29:52 -05:00
bool ftServer : : setChunkStrategy ( const std : : string & hash , FileChunksInfo : : ChunkStrategy s )
{
return mFtController - > setChunkStrategy ( hash , s ) ;
}
2010-05-21 16:49:48 -04:00
uint32_t ftServer : : freeDiskSpaceLimit ( ) const
{
return mFtController - > freeDiskSpaceLimit ( ) ;
}
void ftServer : : setFreeDiskSpaceLimit ( uint32_t s )
{
mFtController - > setFreeDiskSpaceLimit ( s ) ;
}
2010-04-30 17:07:21 -04:00
void ftServer : : setDefaultChunkStrategy ( FileChunksInfo : : ChunkStrategy s )
2010-03-21 17:07:12 -04:00
{
2010-04-30 17:07:21 -04:00
mFtController - > setDefaultChunkStrategy ( s ) ;
2010-03-21 17:07:12 -04:00
}
FileChunksInfo : : ChunkStrategy ftServer : : defaultChunkStrategy ( )
{
return mFtController - > defaultChunkStrategy ( ) ;
}
2010-07-21 19:14:10 -04:00
bool ftServer : : FileCancel ( const std : : string & hash )
2008-07-02 09:19:59 -04:00
{
2010-01-26 18:25:00 -05:00
// Remove from both queue and ftController, by default.
//
mFtController - > FileCancel ( hash ) ;
return true ;
2008-07-02 09:19:59 -04:00
}
2010-07-21 19:14:10 -04:00
bool ftServer : : FileControl ( const 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 ( ) ;
}
2012-01-10 18:07:53 -05:00
void ftServer : : setMinPrioritizedTransfers ( uint32_t s )
{
mFtController - > setMinPrioritizedTransfers ( s ) ;
}
uint32_t ftServer : : getMinPrioritizedTransfers ( )
{
return mFtController - > getMinPrioritizedTransfers ( ) ;
}
2010-03-06 18:29:47 -05:00
void ftServer : : setQueueSize ( uint32_t s )
{
mFtController - > setQueueSize ( s ) ;
}
uint32_t ftServer : : getQueueSize ( )
{
return mFtController - > getQueueSize ( ) ;
}
2009-07-26 11:00:29 -04:00
/* Control of Downloads Priority. */
2010-03-06 18:29:47 -05:00
bool ftServer : : changeQueuePosition ( const std : : string hash , QueueMove mv )
2009-07-26 11:00:29 -04:00
{
2010-03-06 18:29:47 -05:00
mFtController - > moveInQueue ( hash , mv ) ;
return true ;
2010-01-26 18:25:00 -05:00
}
bool ftServer : : changeDownloadSpeed ( const std : : string hash , int speed )
{
mFtController - > setPriority ( hash , ( DwlSpeed ) speed ) ;
2010-01-26 15:40:21 -05:00
return true ;
2009-07-26 11:00:29 -04:00
}
2010-01-26 18:25:00 -05:00
bool ftServer : : getDownloadSpeed ( const std : : string hash , int & speed )
{
DwlSpeed _speed ;
int ret = mFtController - > getPriority ( hash , _speed ) ;
if ( ret )
speed = _speed ;
2009-07-26 11:00:29 -04:00
2010-01-26 18:25:00 -05:00
return ret ;
}
2011-08-12 09:42:30 -04:00
bool ftServer : : clearDownload ( const std : : string /*hash*/ )
2009-07-26 11:00:29 -04:00
{
2010-01-27 03:24:39 -05:00
return true ;
2009-07-26 11:00:29 -04:00
}
2010-01-11 11:00:42 -05:00
bool ftServer : : FileDownloadChunksDetails ( const std : : string & hash , FileChunksInfo & info )
{
return mFtController - > getFileDownloadChunksDetails ( hash , info ) ;
2009-11-17 07:45:06 -05:00
}
2008-07-02 09:19:59 -04:00
/* 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
}
2010-01-11 11:00:42 -05:00
bool ftServer : : FileUploadChunksDetails ( const std : : string & hash , const std : : string & peer_id , CompressedChunkMap & cmap )
{
return mFtDataplex - > getClientChunkMap ( hash , peer_id , cmap ) ;
}
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
}
2012-11-02 09:52:29 -04:00
bool ftServer : : FileDetails ( const std : : string & hash , FileSearchFlags hintflags , FileInfo & info )
2008-07-02 09:19:59 -04:00
{
2008-11-30 12:17:28 -05:00
if ( hintflags & RS_FILE_HINTS_DOWNLOAD )
2010-01-02 16:30:19 -05:00
if ( mFtController - > FileDetails ( hash , info ) )
return true ;
2008-07-02 09:19:59 -04:00
2010-01-02 16:30:19 -05:00
if ( hintflags & RS_FILE_HINTS_UPLOAD )
if ( mFtDataplex - > FileDetails ( hash , hintflags , info ) )
2011-10-01 17:10:03 -04:00
{
// We also check if the file is a DL as well. In such a case we use
// the DL as the file name, to replace the hash. If the file is a cache
// file, we skip the call to fileDetails() for efficiency reasons.
//
FileInfo info2 ;
2012-11-02 09:52:29 -04:00
if ( ( ! ( info . transfer_info_flags & RS_FILE_REQ_CACHE ) ) & & mFtController - > FileDetails ( hash , info2 ) )
2011-10-01 17:10:03 -04:00
info . fname = info2 . fname ;
2010-01-02 16:30:19 -05:00
return true ;
2011-10-01 17:10:03 -04:00
}
2010-01-02 16:30:19 -05:00
if ( hintflags & ~ ( RS_FILE_HINTS_UPLOAD | RS_FILE_HINTS_DOWNLOAD ) )
2010-01-11 11:00:42 -05:00
if ( mFtSearch - > search ( hash , hintflags , info ) )
2010-01-02 16:30:19 -05:00
return true ;
return false ;
2008-07-02 09:19:59 -04:00
}
/***************************************************************/
/******************* ExtraFileList Access **********************/
/***************************************************************/
2009-07-12 09:22:31 -04:00
bool ftServer : : ExtraFileAdd ( std : : string fname , std : : string hash , uint64_t size ,
2012-11-02 09:52:29 -04:00
uint32_t period , TransferRequestFlags flags )
2008-07-02 09:19:59 -04:00
{
return mFtExtra - > addExtraFile ( fname , hash , size , period , flags ) ;
}
2012-11-02 09:52:29 -04:00
bool ftServer : : ExtraFileRemove ( std : : string hash , TransferRequestFlags flags )
2008-07-02 09:19:59 -04:00
{
return mFtExtra - > removeExtraFile ( hash , flags ) ;
}
2012-11-02 09:52:29 -04:00
bool ftServer : : ExtraFileHash ( std : : string localpath , uint32_t period , TransferRequestFlags 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 ************************/
/***************************************************************/
2012-01-17 15:38:25 -05:00
int ftServer : : RequestDirDetails ( const std : : string & uid , const std : : string & path , DirDetails & details )
2008-07-02 09:19:59 -04:00
{
2009-07-12 09:22:31 -04:00
# ifdef SERVER_DEBUG
2008-12-07 12:51:11 -05:00
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
2012-01-17 15:38:25 -05:00
if ( uid = = mLinkMgr - > getOwnId ( ) )
return mFiMon - > RequestDirDetails ( path , details ) ;
else
return mFiStore - > RequestDirDetails ( uid , path , details ) ;
2008-07-02 09:19:59 -04:00
}
2009-07-12 09:22:31 -04:00
2012-11-02 09:52:29 -04:00
int ftServer : : RequestDirDetails ( void * ref , DirDetails & details , FileSearchFlags flags )
2008-07-02 09:19:59 -04:00
{
2009-07-12 09:22:31 -04:00
# ifdef SERVER_DEBUG
2008-12-07 12:51:11 -05:00
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
2012-11-01 06:06:12 -04:00
if ( flags & RS_FILE_HINTS_LOCAL )
2009-08-09 16:00:25 -04:00
return mFiMon - > RequestDirDetails ( ref , details , flags ) ;
else
return mFiStore - > RequestDirDetails ( ref , details , flags ) ;
2008-07-02 09:19:59 -04:00
}
2012-11-02 09:52:29 -04:00
uint32_t ftServer : : getType ( void * ref , FileSearchFlags flags )
2011-04-03 15:59:12 -04:00
{
# ifdef SERVER_DEBUG
std : : cerr < < " ftServer::RequestDirDetails(ref: " < < ref ;
std : : cerr < < " , flags: " < < flags < < " , ...) -> mFiStore " ;
std : : cerr < < std : : endl ;
2009-07-12 09:22:31 -04:00
2011-04-03 15:59:12 -04:00
if ( ! mFiStore )
{
std : : cerr < < " mFiStore not SET yet = FAIL " ;
std : : cerr < < std : : endl ;
}
# endif
2012-11-01 06:06:12 -04:00
if ( flags & RS_FILE_HINTS_LOCAL )
2011-04-03 15:59:12 -04:00
return mFiMon - > getType ( ref ) ;
else
return mFiStore - > getType ( ref ) ;
}
2008-07-02 09:19:59 -04:00
/***************************************************************/
/******************** Search Interface *************************/
/***************************************************************/
2012-11-02 09:52:29 -04:00
int ftServer : : SearchKeywords ( std : : list < std : : string > keywords , std : : list < DirDetails > & results , FileSearchFlags flags )
2012-11-01 06:06:12 -04:00
{
2012-11-02 10:25:35 -04:00
if ( flags & RS_FILE_HINTS_LOCAL )
return mFiMon - > SearchKeywords ( keywords , results , flags , " " ) ;
else
return mFiStore - > SearchKeywords ( keywords , results , flags ) ;
2012-11-01 06:06:12 -04:00
return 0 ;
}
2012-11-02 09:52:29 -04:00
int ftServer : : SearchKeywords ( std : : list < std : : string > keywords , std : : list < DirDetails > & results , FileSearchFlags flags , const std : : string & peer_id )
2008-07-02 09:19:59 -04:00
{
2009-07-12 09:22:31 -04:00
# ifdef SERVER_DEBUG
2008-12-07 12:51:11 -05:00
std : : cerr < < " ftServer::SearchKeywords() " ;
std : : cerr < < std : : endl ;
if ( ! mFiStore )
{
std : : cerr < < " mFiStore not SET yet = FAIL " ;
std : : cerr < < std : : endl ;
}
# endif
2012-11-01 06:06:12 -04:00
if ( flags & RS_FILE_HINTS_LOCAL )
return mFiMon - > SearchKeywords ( keywords , results , flags , peer_id ) ;
2009-08-10 15:44:45 -04:00
else
return mFiStore - > SearchKeywords ( keywords , results , flags ) ;
2008-07-02 09:19:59 -04:00
}
2009-07-12 09:22:31 -04:00
2012-11-02 09:52:29 -04:00
int ftServer : : SearchBoolExp ( Expression * exp , std : : list < DirDetails > & results , FileSearchFlags flags )
2008-07-02 09:19:59 -04:00
{
2012-11-02 10:25:35 -04:00
if ( flags & RS_FILE_HINTS_LOCAL )
return mFiMon - > SearchBoolExp ( exp , results , flags , " " ) ;
else
return mFiStore - > searchBoolExp ( exp , results ) ;
2012-11-01 06:06:12 -04:00
return 0 ;
}
2012-11-02 09:52:29 -04:00
int ftServer : : SearchBoolExp ( Expression * exp , std : : list < DirDetails > & results , FileSearchFlags flags , const std : : string & peer_id )
2012-11-01 06:06:12 -04:00
{
if ( flags & RS_FILE_HINTS_LOCAL )
return mFiMon - > SearchBoolExp ( exp , results , flags , peer_id ) ;
2009-08-25 08:04:43 -04:00
else
return mFiStore - > searchBoolExp ( exp , results ) ;
2008-07-02 09:19:59 -04:00
}
2009-07-12 09:22:31 -04:00
2008-07-02 09:19:59 -04:00
/***************************************************************/
/*************** Local Shared Dir Interface ********************/
/***************************************************************/
2009-07-12 09:22:31 -04:00
2008-07-02 09:19:59 -04:00
bool ftServer : : ConvertSharedFilePath ( std : : string path , std : : string & fullpath )
{
return mFiMon - > convertSharedFilePath ( path , fullpath ) ;
}
2009-07-12 09:22:31 -04:00
2012-11-12 17:51:27 -05:00
void ftServer : : updateSinceGroupPermissionsChanged ( )
{
mFiMon - > forceDirListsRebuildAndSend ( ) ;
}
2008-07-02 09:19:59 -04:00
void ftServer : : ForceDirectoryCheck ( )
{
mFiMon - > forceDirectoryCheck ( ) ;
return ;
}
2009-07-12 09:22:31 -04:00
2008-07-02 09:19:59 -04:00
bool ftServer : : InDirectoryCheck ( )
{
2008-08-03 08:45:53 -04:00
return mFiMon - > inDirectoryCheck ( ) ;
2008-07-02 09:19:59 -04:00
}
2009-07-12 09:22:31 -04:00
2010-10-31 15:29:26 -04:00
bool ftServer : : CopyFile ( const std : : string & source , const std : : string & dest )
{
2010-11-16 15:58:26 -05:00
return mFtController - > copyFile ( source , dest ) ;
2010-10-31 15:29:26 -04:00
}
2009-08-03 15:43:52 -04:00
bool ftServer : : getSharedDirectories ( std : : list < SharedDirInfo > & dirs )
2008-07-02 09:19:59 -04:00
{
2008-08-09 13:03:24 -04:00
mFiMon - > getSharedDirectories ( dirs ) ;
return true ;
}
2009-08-03 15:43:52 -04:00
bool ftServer : : setSharedDirectories ( std : : list < SharedDirInfo > & dirs )
2008-08-09 13:03:24 -04:00
{
mFiMon - > setSharedDirectories ( dirs ) ;
return true ;
2008-07-02 09:19:59 -04:00
}
2010-09-17 15:21:14 -04:00
bool ftServer : : addSharedDirectory ( const SharedDirInfo & dir )
2008-07-02 09:19:59 -04:00
{
2011-05-15 08:42:55 -04:00
SharedDirInfo _dir = dir ;
_dir . filename = RsDirUtil : : convertPathToUnix ( _dir . filename ) ;
2009-08-03 15:43:52 -04:00
std : : list < SharedDirInfo > dirList ;
2008-08-09 13:03:24 -04:00
mFiMon - > getSharedDirectories ( dirList ) ;
2009-08-03 15:43:52 -04:00
// check that the directory is not already in the list.
for ( std : : list < SharedDirInfo > : : const_iterator it ( dirList . begin ( ) ) ; it ! = dirList . end ( ) ; + + it )
2011-05-15 08:42:55 -04:00
if ( ( * it ) . filename = = _dir . filename )
2009-08-03 15:43:52 -04:00
return false ;
2009-08-10 15:01:27 -04:00
2009-08-03 15:43:52 -04:00
// ok then, add the shared directory.
2011-05-15 08:42:55 -04:00
dirList . push_back ( _dir ) ;
2008-08-09 13:03:24 -04:00
mFiMon - > setSharedDirectories ( dirList ) ;
return true ;
2008-07-02 09:19:59 -04:00
}
2009-08-03 15:43:52 -04:00
bool ftServer : : updateShareFlags ( const SharedDirInfo & info )
{
mFiMon - > updateShareFlags ( info ) ;
return true ;
}
2008-07-02 09:19:59 -04:00
bool ftServer : : removeSharedDirectory ( std : : string dir )
{
2011-05-15 08:42:55 -04:00
dir = RsDirUtil : : convertPathToUnix ( dir ) ;
2009-08-03 15:43:52 -04:00
std : : list < SharedDirInfo > dirList ;
std : : list < SharedDirInfo > : : iterator it ;
2008-08-09 13:03:24 -04:00
2009-07-12 09:22:31 -04:00
# ifdef SERVER_DEBUG
2008-11-15 15:00:29 -05:00
std : : cerr < < " ftServer::removeSharedDirectory( " < < dir < < " ) " ;
std : : cerr < < std : : endl ;
# endif
2008-08-09 13:03:24 -04:00
mFiMon - > getSharedDirectories ( dirList ) ;
2009-07-12 09:22:31 -04:00
# ifdef SERVER_DEBUG
2008-11-15 15:00:29 -05:00
for ( it = dirList . begin ( ) ; it ! = dirList . end ( ) ; it + + )
{
std : : cerr < < " ftServer::removeSharedDirectory() " ;
2009-08-03 15:43:52 -04:00
std : : cerr < < " existing: " < < ( * it ) . filename ;
2008-11-15 15:00:29 -05:00
std : : cerr < < std : : endl ;
}
# endif
2009-08-03 15:43:52 -04:00
for ( it = dirList . begin ( ) ; it ! = dirList . end ( ) & & ( * it ) . filename ! = dir ; + + it ) ;
if ( it = = dirList . end ( ) )
2008-08-09 13:03:24 -04:00
{
2009-07-12 09:22:31 -04:00
# ifdef SERVER_DEBUG
2008-11-15 15:00:29 -05:00
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
2009-07-12 09:22:31 -04:00
# ifdef SERVER_DEBUG
2008-11-15 15:00:29 -05:00
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
}
2011-03-28 17:52:21 -04:00
void ftServer : : setWatchPeriod ( int minutes )
{
mFiMon - > setWatchPeriod ( minutes * 60 ) ;
}
int ftServer : : watchPeriod ( ) const
{
return mFiMon - > watchPeriod ( ) / 60 ;
}
2010-10-31 16:35:31 -04:00
void ftServer : : setRememberHashFiles ( bool b )
{
mFiMon - > setRememberHashCache ( b ) ;
}
bool ftServer : : rememberHashFiles ( ) const
{
return mFiMon - > rememberHashCache ( ) ;
}
void ftServer : : setRememberHashFilesDuration ( uint32_t days )
{
mFiMon - > setRememberHashCacheDuration ( days ) ;
}
uint32_t ftServer : : rememberHashFilesDuration ( ) const
{
return mFiMon - > rememberHashCacheDuration ( ) ;
}
void ftServer : : clearHashCache ( )
{
mFiMon - > clearHashCache ( ) ;
}
2008-07-02 09:19:59 -04:00
2009-07-12 09:22:31 -04:00
bool ftServer : : getShareDownloadDirectory ( )
{
2011-03-08 15:05:36 -05:00
std : : list < SharedDirInfo > dirList ;
mFiMon - > getSharedDirectories ( dirList ) ;
2009-07-12 09:22:31 -04:00
2011-03-08 15:05:36 -05:00
std : : string dir = mFtController - > getDownloadDirectory ( ) ;
// check if the download directory is in the list.
for ( std : : list < SharedDirInfo > : : const_iterator it ( dirList . begin ( ) ) ; it ! = dirList . end ( ) ; + + it )
if ( ( * it ) . filename = = dir )
return true ;
2009-08-03 15:43:52 -04:00
2011-03-08 15:05:36 -05:00
return false ;
2009-07-12 09:22:31 -04:00
}
2011-03-08 15:05:36 -05:00
bool ftServer : : shareDownloadDirectory ( bool share )
2009-07-12 09:22:31 -04:00
{
2011-03-08 15:05:36 -05:00
if ( share ) {
/* Share */
SharedDirInfo inf ;
inf . filename = mFtController - > getDownloadDirectory ( ) ;
2012-11-01 06:06:12 -04:00
inf . shareflags = DIR_FLAGS_NETWORK_WIDE_OTHERS ;
2011-03-08 15:05:36 -05:00
return addSharedDirectory ( inf ) ;
}
/* Unshare */
2009-07-12 09:22:31 -04:00
std : : string dir = mFtController - > getDownloadDirectory ( ) ;
return removeSharedDirectory ( dir ) ;
}
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
2011-08-12 09:42:30 -04:00
bool ftServer : : saveList ( bool & /*cleanup*/ , std : : list < RsItem * > & /*list*/ )
2008-08-03 08:45:53 -04:00
{
2010-12-18 14:35:07 -05:00
return true ;
2008-08-03 08:45:53 -04:00
}
2008-07-02 09:19:59 -04:00
2011-08-12 09:42:30 -04:00
bool ftServer : : loadList ( std : : list < RsItem * > & /*load*/ )
2008-08-03 08:45:53 -04:00
{
return true ;
}
2008-07-02 09:19:59 -04:00
2011-08-12 09:42:30 -04:00
bool ftServer : : loadConfigMap ( std : : map < std : : string , std : : string > & /*configMap*/ )
2008-08-03 08:45:53 -04:00
{
return true ;
}
/***************************************************************/
/********************** Data Flow **********************/
/***************************************************************/
/* Client Send */
2010-01-11 11:00:42 -05:00
bool ftServer : : sendDataRequest ( const std : : string & peerId , const std : : string & hash , uint64_t size , uint64_t offset , uint32_t chunksize )
2008-08-03 08:45:53 -04:00
{
2009-05-26 17:42:45 -04:00
if ( mTurtleRouter - > isTurtlePeer ( peerId ) )
mTurtleRouter - > sendDataRequest ( peerId , hash , size , offset , chunksize ) ;
else
{
/* create a packet */
/* push to networking part */
RsFileRequest * rfi = new RsFileRequest ( ) ;
2008-08-03 08:45:53 -04:00
2009-05-26 17:42:45 -04:00
/* id */
rfi - > PeerId ( peerId ) ;
2008-08-03 08:45:53 -04:00
2009-05-26 17:42:45 -04:00
/* file info */
rfi - > file . filesize = size ;
rfi - > file . hash = hash ; /* ftr->hash; */
2008-08-03 08:45:53 -04:00
2009-05-26 17:42:45 -04:00
/* offsets */
rfi - > fileoffset = offset ; /* ftr->offset; */
rfi - > chunksize = chunksize ; /* ftr->chunk; */
2008-08-03 08:45:53 -04:00
2009-05-26 17:42:45 -04:00
mP3iface - > SendFileRequest ( rfi ) ;
}
2008-08-09 13:03:24 -04:00
return true ;
2008-08-03 08:45:53 -04:00
}
2010-07-25 15:04:31 -04:00
bool ftServer : : sendChunkMapRequest ( const std : : string & peerId , const std : : string & hash , bool is_client )
2010-01-11 11:00:42 -05:00
{
if ( mTurtleRouter - > isTurtlePeer ( peerId ) )
2010-07-25 15:04:31 -04:00
mTurtleRouter - > sendChunkMapRequest ( peerId , hash , is_client ) ;
2010-07-21 19:14:10 -04:00
else
2010-07-25 15:04:31 -04:00
{
/* create a packet */
/* push to networking part */
RsFileChunkMapRequest * rfi = new RsFileChunkMapRequest ( ) ;
/* id */
rfi - > PeerId ( peerId ) ;
/* file info */
rfi - > hash = hash ; /* ftr->hash; */
rfi - > is_client = is_client ;
mP3iface - > SendFileChunkMapRequest ( rfi ) ;
}
2010-01-11 11:00:42 -05:00
return true ;
}
2010-07-25 15:04:31 -04:00
bool ftServer : : sendChunkMap ( const std : : string & peerId , const std : : string & hash , const CompressedChunkMap & map , bool is_client )
2010-01-11 11:00:42 -05:00
{
if ( mTurtleRouter - > isTurtlePeer ( peerId ) )
2010-07-25 15:04:31 -04:00
mTurtleRouter - > sendChunkMap ( peerId , hash , map , is_client ) ;
2010-07-21 19:14:10 -04:00
else
2010-07-25 15:04:31 -04:00
{
/* create a packet */
/* push to networking part */
RsFileChunkMap * rfi = new RsFileChunkMap ( ) ;
/* id */
rfi - > PeerId ( peerId ) ;
/* file info */
rfi - > hash = hash ; /* ftr->hash; */
rfi - > is_client = is_client ; /* ftr->hash; */
rfi - > compressed_map = map ; /* ftr->hash; */
mP3iface - > SendFileChunkMap ( rfi ) ;
}
2010-01-11 11:00:42 -05:00
return true ;
}
2010-07-21 19:14:10 -04:00
bool ftServer : : sendCRC32MapRequest ( const std : : string & peerId , const std : : string & hash )
{
if ( mTurtleRouter - > isTurtlePeer ( peerId ) )
mTurtleRouter - > sendCRC32MapRequest ( peerId , hash ) ;
else
2010-07-29 17:07:07 -04:00
{
/* create a packet */
/* push to networking part */
RsFileCRC32MapRequest * rfi = new RsFileCRC32MapRequest ( ) ;
/* id */
rfi - > PeerId ( peerId ) ;
/* file info */
rfi - > hash = hash ; /* ftr->hash; */
mP3iface - > SendFileCRC32MapRequest ( rfi ) ;
}
2010-01-11 11:00:42 -05:00
2012-03-15 15:55:43 -04:00
return true ;
}
bool ftServer : : sendSingleChunkCRCRequest ( const std : : string & peerId , const std : : string & hash , uint32_t chunk_number )
{
if ( mTurtleRouter - > isTurtlePeer ( peerId ) )
mTurtleRouter - > sendSingleChunkCRCRequest ( peerId , hash , chunk_number ) ;
else
{
/* create a packet */
/* push to networking part */
RsFileSingleChunkCrcRequest * rfi = new RsFileSingleChunkCrcRequest ( ) ;
/* id */
rfi - > PeerId ( peerId ) ;
/* file info */
rfi - > hash = hash ; /* ftr->hash; */
rfi - > chunk_number = chunk_number ;
mP3iface - > SendFileSingleChunkCrcRequest ( rfi ) ;
}
2010-07-21 19:14:10 -04:00
return true ;
}
2010-07-29 17:07:07 -04:00
bool ftServer : : sendCRC32Map ( const std : : string & peerId , const std : : string & hash , const CRC32Map & crcmap )
2010-07-21 19:14:10 -04:00
{
if ( mTurtleRouter - > isTurtlePeer ( peerId ) )
2010-07-29 17:07:07 -04:00
mTurtleRouter - > sendCRC32Map ( peerId , hash , crcmap ) ;
2010-07-21 19:14:10 -04:00
else
2010-07-29 17:07:07 -04:00
{
/* create a packet */
/* push to networking part */
RsFileCRC32Map * rfi = new RsFileCRC32Map ( ) ;
/* id */
rfi - > PeerId ( peerId ) ;
/* file info */
rfi - > hash = hash ; /* ftr->hash; */
rfi - > crc_map = crcmap ; /* ftr->hash; */
mP3iface - > SendFileCRC32Map ( rfi ) ;
}
2010-07-21 19:14:10 -04:00
2012-03-15 15:55:43 -04:00
return true ;
}
bool ftServer : : sendSingleChunkCRC ( const std : : string & peerId , const std : : string & hash , uint32_t chunk_number , const Sha1CheckSum & crc )
{
if ( mTurtleRouter - > isTurtlePeer ( peerId ) )
mTurtleRouter - > sendSingleChunkCRC ( peerId , hash , chunk_number , crc ) ;
else
{
/* create a packet */
/* push to networking part */
RsFileSingleChunkCrc * rfi = new RsFileSingleChunkCrc ( ) ;
/* id */
rfi - > PeerId ( peerId ) ;
/* file info */
rfi - > hash = hash ; /* ftr->hash; */
rfi - > check_sum = crc ;
rfi - > chunk_number = chunk_number ;
mP3iface - > SendFileSingleChunkCrc ( rfi ) ;
}
2010-07-21 19:14:10 -04:00
return true ;
}
2010-01-11 11:00:42 -05: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 */
2010-02-06 17:27:44 -05:00
const uint32_t MAX_FT_CHUNK = 8 * 1024 ; /* 16K */
2008-08-03 08:45:53 -04:00
/* Server Send */
2010-01-11 11:00:42 -05:00
bool ftServer : : sendData ( const std : : string & peerId , const std : : string & hash , uint64_t size , uint64_t baseoffset , uint32_t chunksize , void * data )
2008-08-03 08:45:53 -04:00
{
/* create a packet */
/* push to networking part */
uint32_t tosend = chunksize ;
uint64_t offset = 0 ;
uint32_t chunk ;
2009-07-12 09:22:31 -04:00
# ifdef SERVER_DEBUG
2008-10-29 16:58:23 -04:00
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
2009-05-26 17:42:45 -04:00
if ( mTurtleRouter - > isTurtlePeer ( peerId ) )
mTurtleRouter - > sendFileData ( peerId , hash , size , baseoffset + offset , chunk , & ( ( ( uint8_t * ) data ) [ offset ] ) ) ;
else
{
RsFileData * rfd = new RsFileData ( ) ;
2008-08-03 08:45:53 -04:00
2009-05-26 17:42:45 -04:00
/* set id */
rfd - > PeerId ( peerId ) ;
2008-08-03 08:45:53 -04:00
2009-05-26 17:42:45 -04:00
/* 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 ;
2008-08-03 08:45:53 -04:00
2009-05-26 17:42:45 -04:00
rfd - > fd . file_offset = baseoffset + offset ;
2008-08-03 08:45:53 -04:00
2009-05-26 17:42:45 -04:00
/* file data */
rfd - > fd . binData . setBinData ( & ( ( ( uint8_t * ) data ) [ offset ] ) , chunk ) ;
2008-08-03 08:45:53 -04:00
2009-05-26 17:42:45 -04:00
mP3iface - > SendFileData ( rfd ) ;
/* print the data pointer */
2009-07-12 09:22:31 -04:00
# ifdef SERVER_DEBUG
2009-05-26 17:42:45 -04:00
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 ;
2008-10-29 16:58:23 -04:00
# endif
2009-05-26 17:42:45 -04:00
}
2008-08-03 08:45:53 -04:00
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 ( )
{
2009-07-12 09:22:31 -04:00
rslog ( RSL_DEBUG_BASIC , ftserverzone ,
2008-08-03 08:45:53 -04:00
" filedexserver::tick() " ) ;
if ( mP3iface = = NULL )
{
2008-08-17 11:23:11 -04:00
# ifdef SERVER_DEBUG
std : : cerr < < " ftServer::tick() ERROR: mP3iface == NULL " ;
# endif
2009-07-12 09:22:31 -04:00
rslog ( RSL_DEBUG_BASIC , ftserverzone ,
2008-08-03 08:45:53 -04:00
" 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 ;
2009-07-12 09:22:31 -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
2009-07-12 09:22:31 -04:00
# ifdef SERVER_DEBUG
2008-08-17 11:23:11 -04:00
std : : cerr < < " ftServer::handleCacheData() Recvd SearchResult (CacheResponse!) " < < std : : endl ;
2012-04-17 20:00:59 -04:00
std : : string out ;
2008-08-03 08:45:53 -04:00
if ( i + + = = i_init )
{
2012-04-17 20:00:59 -04:00
out + = " Recieved Search Results: \n " ;
2008-08-03 08:45:53 -04:00
}
2012-04-17 20:00:59 -04:00
ci - > print_string ( out ) ;
rslog ( RSL_DEBUG_BASIC , ftserverzone , out ) ;
2008-08-17 11:23:11 -04:00
# endif
2008-08-03 08:45:53 -04:00
/* these go to the CacheStrapper! */
CacheData data ;
2010-11-12 10:21:49 -05:00
data . pid = ci - > PeerId ( ) ;
2008-08-03 08:45:53 -04:00
data . cid = CacheId ( ci - > cacheType , ci - > cacheSubId ) ;
2010-11-12 10:21:49 -05:00
data . path = ci - > file . path ;
data . name = ci - > file . name ;
2008-08-03 08:45:53 -04:00
data . hash = ci - > file . hash ;
data . size = ci - > file . filesize ;
2010-11-12 10:21:49 -05:00
data . recvd = time ( NULL ) ;
2008-08-03 08:45:53 -04:00
mCacheStrapper - > recvCacheResponse ( data , time ( NULL ) ) ;
delete ci ;
}
// now requested Searches.
i_init = i ;
while ( ( cr = mP3iface - > RequestedSearch ( ) ) ! = NULL )
{
2009-07-12 09:22:31 -04:00
# ifdef SERVER_DEBUG
2008-08-03 08:45:53 -04:00
/* just delete these */
2012-04-17 20:00:59 -04:00
std : : string out = " Requested Search: \n " ;
cr - > print_string ( out ) ;
rslog ( RSL_DEBUG_BASIC , ftserverzone , out ) ;
2008-08-17 11:23:11 -04:00
# endif
2008-08-03 08:45:53 -04:00
delete cr ;
}
2009-07-12 09:22:31 -04:00
// Now handle it replacement (pushed cache results)
2008-08-03 08:45:53 -04:00
{
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 ( ) ;
2009-07-12 09:22:31 -04:00
2008-08-03 08:45:53 -04:00
/* 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 ;
2009-07-12 09:22:31 -04:00
# ifdef SERVER_DEBUG
2012-04-17 20:00:59 -04:00
std : : string out2 = " Outgoing CacheStrapper Update -> RsCacheItem: \n " ;
ci - > print_string ( out2 ) ;
std : : cerr < < out2 < < std : : endl ;
2008-08-03 08:45:53 -04:00
# endif
//rslog(RSL_DEBUG_BASIC, ftserverzone, out2.str());
mP3iface - > SendSearchResult ( ci ) ;
2009-07-12 09:22:31 -04:00
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 ;
2010-07-25 15:04:31 -04:00
RsFileChunkMapRequest * fcmr ;
RsFileChunkMap * fcm ;
2010-07-29 17:07:07 -04:00
RsFileCRC32MapRequest * fccrcmr ;
RsFileCRC32Map * fccrcm ;
2012-03-15 15:55:43 -04:00
RsFileSingleChunkCrcRequest * fscrcr ;
RsFileSingleChunkCrc * fscrc ;
2008-08-03 08:45:53 -04:00
int i_init = 0 ;
int i = 0 ;
i_init = i ;
while ( ( fr = mP3iface - > GetFileRequest ( ) ) ! = NULL )
{
2009-07-12 09:22:31 -04:00
# ifdef SERVER_DEBUG
2008-08-17 11:23:11 -04:00
std : : cerr < < " ftServer::handleFileData() Recvd ftFiler Request " < < std : : endl ;
2012-04-17 20:00:59 -04:00
std : : string out ;
2008-08-03 08:45:53 -04:00
if ( i = = i_init )
{
2012-04-17 20:00:59 -04:00
out + = " Incoming(Net) File Item: \n " ;
2008-08-03 08:45:53 -04:00
}
2012-04-17 20:00:59 -04:00
fr - > print_string ( out ) ;
rslog ( RSL_DEBUG_BASIC , ftserverzone , out ) ;
2008-08-03 08:45:53 -04:00
# endif
i + + ; /* count */
2009-07-12 09:22:31 -04:00
mFtDataplex - > recvDataRequest ( fr - > PeerId ( ) ,
fr - > file . hash , fr - > file . filesize ,
2008-08-03 08:45:53 -04:00
fr - > fileoffset , fr - > chunksize ) ;
FileInfo ( ffr ) ;
delete fr ;
}
// now File Data.
i_init = i ;
while ( ( fd = mP3iface - > GetFileData ( ) ) ! = NULL )
{
2009-07-12 09:22:31 -04:00
# 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 ;
2012-04-17 20:00:59 -04:00
std : : string out ;
2008-08-03 08:45:53 -04:00
if ( i = = i_init )
{
2012-04-17 20:00:59 -04:00
out + = " Incoming(Net) File Data: \n " ;
2008-08-03 08:45:53 -04:00
}
2012-04-17 20:00:59 -04:00
fd - > print_string ( out ) ;
rslog ( RSL_DEBUG_BASIC , ftserverzone , out ) ;
2008-08-03 08:45:53 -04:00
# endif
i + + ; /* count */
/* incoming data */
mFtDataplex - > recvData ( fd - > PeerId ( ) ,
2009-07-12 09:22:31 -04:00
fd - > fd . file . hash , fd - > fd . file . filesize ,
fd - > fd . file_offset ,
fd - > fd . binData . bin_len ,
2008-08-03 08:45:53 -04:00
fd - > fd . binData . bin_data ) ;
2009-07-12 09:22:31 -04:00
/* we've stolen the data part -> so blank before delete
2008-08-03 08:45:53 -04:00
*/
2008-10-29 16:58:23 -04:00
fd - > fd . binData . TlvShallowClear ( ) ;
2008-08-03 08:45:53 -04:00
delete fd ;
}
2010-07-25 15:04:31 -04:00
// now file chunkmap requests
i_init = i ;
while ( ( fcmr = mP3iface - > GetFileChunkMapRequest ( ) ) ! = NULL )
{
# ifdef SERVER_DEBUG
std : : cerr < < " ftServer::handleFileData() Recvd ChunkMap request " < < std : : endl ;
2012-04-17 20:00:59 -04:00
std : : string out ;
2010-07-25 15:04:31 -04:00
if ( i = = i_init )
{
2012-04-17 20:00:59 -04:00
out + = " Incoming(Net) File Data: \n " ;
2010-07-25 15:04:31 -04:00
}
2012-04-17 20:00:59 -04:00
fcmr - > print_string ( out ) ;
rslog ( RSL_DEBUG_BASIC , ftserverzone , out ) ;
2010-07-25 15:04:31 -04:00
# endif
i + + ; /* count */
/* incoming data */
mFtDataplex - > recvChunkMapRequest ( fcmr - > PeerId ( ) , fcmr - > hash , fcmr - > is_client ) ;
delete fcmr ;
}
// now file chunkmaps
i_init = i ;
while ( ( fcm = mP3iface - > GetFileChunkMap ( ) ) ! = NULL )
{
# ifdef SERVER_DEBUG
std : : cerr < < " ftServer::handleFileData() Recvd ChunkMap request " < < std : : endl ;
2012-04-17 20:00:59 -04:00
std : : string out ;
2010-07-25 15:04:31 -04:00
if ( i = = i_init )
{
2012-04-17 20:00:59 -04:00
out + = " Incoming(Net) File Data: \n " ;
2010-07-25 15:04:31 -04:00
}
2012-04-17 20:00:59 -04:00
fcm - > print_string ( out ) ;
rslog ( RSL_DEBUG_BASIC , ftserverzone , out ) ;
2010-07-25 15:04:31 -04:00
# endif
i + + ; /* count */
2008-08-03 08:45:53 -04:00
2010-07-25 15:04:31 -04:00
/* incoming data */
mFtDataplex - > recvChunkMap ( fcm - > PeerId ( ) , fcm - > hash , fcm - > compressed_map , fcm - > is_client ) ;
delete fcm ;
}
2010-07-29 17:07:07 -04:00
// now file chunkmap requests
i_init = i ;
while ( ( fccrcmr = mP3iface - > GetFileCRC32MapRequest ( ) ) ! = NULL )
{
# ifdef SERVER_DEBUG
std : : cerr < < " ftServer::handleFileData() Recvd ChunkMap request " < < std : : endl ;
2012-04-17 20:00:59 -04:00
std : : string out ;
2010-07-29 17:07:07 -04:00
if ( i = = i_init )
{
2012-04-17 20:00:59 -04:00
out + = " Incoming(Net) File Data: \n " ;
2010-07-29 17:07:07 -04:00
}
2012-04-17 20:00:59 -04:00
fccrcmr - > print_string ( out ) ;
rslog ( RSL_DEBUG_BASIC , ftserverzone , out ) ;
2010-07-29 17:07:07 -04:00
# endif
i + + ; /* count */
/* incoming data */
mFtDataplex - > recvCRC32MapRequest ( fccrcmr - > PeerId ( ) , fccrcmr - > hash ) ;
delete fccrcmr ;
}
// now file chunkmaps
i_init = i ;
while ( ( fccrcm = mP3iface - > GetFileCRC32Map ( ) ) ! = NULL )
{
# ifdef SERVER_DEBUG
std : : cerr < < " ftServer::handleFileData() Recvd ChunkMap request " < < std : : endl ;
2012-04-17 20:00:59 -04:00
std : : string out ;
2010-07-29 17:07:07 -04:00
if ( i = = i_init )
{
2012-04-17 20:00:59 -04:00
out + = " Incoming(Net) File Data: \n " ;
2010-07-29 17:07:07 -04:00
}
2012-04-17 20:00:59 -04:00
fccrcm - > print_string ( out ) ;
rslog ( RSL_DEBUG_BASIC , ftserverzone , out ) ;
2010-07-29 17:07:07 -04:00
# endif
i + + ; /* count */
/* incoming data */
mFtDataplex - > recvCRC32Map ( fccrcm - > PeerId ( ) , fccrcm - > hash , fccrcm - > crc_map ) ;
delete fccrcm ;
}
2012-03-15 15:55:43 -04:00
// now file chunk crc requests
i_init = i ;
while ( ( fscrcr = mP3iface - > GetFileSingleChunkCrcRequest ( ) ) ! = NULL )
{
# ifdef SERVER_DEBUG
std : : cerr < < " ftServer::handleFileData() Recvd ChunkMap request " < < std : : endl ;
2012-04-17 20:00:59 -04:00
std : : string out ;
2012-03-15 15:55:43 -04:00
if ( i = = i_init )
{
2012-04-17 20:00:59 -04:00
out + = " Incoming(Net) File CRC Request: \n " ;
2012-03-15 15:55:43 -04:00
}
2012-04-17 20:00:59 -04:00
fscrcr - > print_string ( out ) ;
rslog ( RSL_DEBUG_BASIC , ftserverzone , out ) ;
2012-03-15 15:55:43 -04:00
# endif
i + + ; /* count */
/* incoming data */
2012-12-26 13:12:19 -05:00
mFtDataplex - > recvSingleChunkCRCRequest ( fscrcr - > PeerId ( ) , fscrcr - > hash , fscrcr - > chunk_number ) ;
2012-03-15 15:55:43 -04:00
delete fscrcr ;
}
// now file chunkmaps
i_init = i ;
while ( ( fscrc = mP3iface - > GetFileSingleChunkCrc ( ) ) ! = NULL )
{
# ifdef SERVER_DEBUG
std : : cerr < < " ftServer::handleFileData() Recvd ChunkMap request " < < std : : endl ;
2012-04-17 20:00:59 -04:00
std : : string out ;
2012-03-15 15:55:43 -04:00
if ( i = = i_init )
{
2012-04-17 20:00:59 -04:00
out + = " Incoming(Net) File Data: \n " ;
2012-03-15 15:55:43 -04:00
}
2012-04-17 20:00:59 -04:00
fscrc - > print_string ( out ) ;
rslog ( RSL_DEBUG_BASIC , ftserverzone , out ) ;
2012-03-15 15:55:43 -04:00
# endif
i + + ; /* count */
/* incoming data */
2012-12-26 13:12:19 -05:00
mFtDataplex - > recvSingleChunkCRC ( fscrc - > PeerId ( ) , fscrc - > hash , fscrc - > chunk_number , fscrc - > check_sum ) ;
2012-03-15 15:55:43 -04:00
delete fscrcr ;
}
2008-08-03 08:45:53 -04:00
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 ) ;
2009-07-12 09:22:31 -04:00
2008-11-15 15:00:29 -05:00
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