From 8d146038c2fd7065413868452008cb1b5ef5a0d3 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 3 Sep 2017 22:52:31 +0200 Subject: [PATCH 1/2] fixed bug preventing search by hash to be called on own files --- libretroshare/src/file_sharing/p3filelists.cc | 21 ++++++------ libretroshare/src/ft/ftcontroller.cc | 34 ++++++++++++++++--- libretroshare/src/ft/ftserver.cc | 3 +- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/libretroshare/src/file_sharing/p3filelists.cc b/libretroshare/src/file_sharing/p3filelists.cc index 492c37f67..7569668ff 100644 --- a/libretroshare/src/file_sharing/p3filelists.cc +++ b/libretroshare/src/file_sharing/p3filelists.cc @@ -1073,24 +1073,23 @@ bool p3FileDatabase::search(const RsFileHash &hash, FileSearchFlags hintflags, F RsFileHash real_hash ; EntryIndex indx; - if(!mLocalSharedDirs->searchHash(hash,real_hash,indx)) - return false; + if(mLocalSharedDirs->searchHash(hash,real_hash,indx)) + { + mLocalSharedDirs->getFileInfo(indx,info) ; - mLocalSharedDirs->getFileInfo(indx,info) ; + if(!real_hash.isNull()) + { + info.hash = real_hash ; + info.transfer_info_flags |= RS_FILE_REQ_ENCRYPTED ; + } - if(!real_hash.isNull()) - { - info.hash = real_hash ; - info.transfer_info_flags |= RS_FILE_REQ_ENCRYPTED ; - } - - return true; + return true; + } } if(hintflags & RS_FILE_HINTS_REMOTE) { NOT_IMPLEMENTED(); - return false; } return false; } diff --git a/libretroshare/src/ft/ftcontroller.cc b/libretroshare/src/ft/ftcontroller.cc index 37ec8ac0f..a8b1d5d26 100644 --- a/libretroshare/src/ft/ftcontroller.cc +++ b/libretroshare/src/ft/ftcontroller.cc @@ -69,8 +69,9 @@ * #define CONTROL_DEBUG 1 * #define DEBUG_DWLQUEUE 1 *****/ +#define CONTROL_DEBUG 1 -static const int32_t SAVE_TRANSFERS_DELAY = 301 ; // save transfer progress every 301 seconds. +static const int32_t SAVE_TRANSFERS_DELAY = 60;//301 ; // save transfer progress every 301 seconds. static const int32_t INACTIVE_CHUNKS_CHECK_DELAY = 240 ; // time after which an inactive chunk is released static const int32_t MAX_TIME_INACTIVE_REQUEUED = 120 ; // time after which an inactive ftFileControl is bt-queued @@ -222,8 +223,8 @@ void ftController::data_tick() usleep(1*1000*1000); // 1 sec #ifdef CONTROL_DEBUG - std::cerr << "ftController::run()"; - std::cerr << std::endl; + //std::cerr << "ftController::run()"; + //std::cerr << std::endl; #endif bool doPending = false; { @@ -280,12 +281,24 @@ void ftController::data_tick() void ftController::searchForDirectSources() { +#ifdef CONTROL_DEBUG + std::cerr << "ftController::searchForDirectSources()" << std::endl; +#endif RsStackMutex stack(ctrlMutex); /******* LOCKED ********/ - if (!mSearch) return; + if (!mSearch) + { +#ifdef CONTROL_DEBUG + std::cerr << " search module not available!" << std::endl; +#endif + return; + } for(std::map::iterator it(mDownloads.begin()); it != mDownloads.end(); ++it ) if(it->second->mState != ftFileControl::QUEUED && it->second->mState != ftFileControl::PAUSED ) { +#ifdef CONTROL_DEBUG + std::cerr << " file " << it->first << ":" << std::endl; +#endif FileInfo info ; // Info needs to be re-allocated each time, to start with a clear list of peers (it's not cleared down there) if( mSearch->search(it->first, RS_FILE_HINTS_REMOTE | RS_FILE_HINTS_SPEC_ONLY, info) ) @@ -300,8 +313,19 @@ void ftController::searchForDirectSources() if( bAllowDirectDL ) if( it->second->mTransfer->addFileSource(pit->peerId) ) /* if the sources don't exist already - add in */ setPeerState( it->second->mTransfer, pit->peerId, FT_CNTRL_STANDARD_RATE, mServiceCtrl->isPeerConnected(mFtServiceType, pit->peerId) ); +#ifdef CONTROL_DEBUG + std::cerr << " found source " << pit->peerId << ", allowDirectDL=" << bAllowDirectDL << ". " << (bAllowDirectDL?"adding":"not adding") << std::endl; +#endif } +#ifdef CONTROL_DEBUG + else + std::cerr << " search returned empty.: " << std::endl; +#endif } +#ifdef CONTROL_DEBUG + else + std::cerr << " file " << it->first << ": state is " << it->second->mState << ": ignored." << std::endl; +#endif } void ftController::tickTransfers() @@ -311,7 +335,7 @@ void ftController::tickTransfers() RsStackMutex stack(ctrlMutex); /******* LOCKED ********/ #ifdef CONTROL_DEBUG - std::cerr << "ticking transfers." << std::endl ; + // std::cerr << "ticking transfers." << std::endl ; #endif // Collect all non queued files. // diff --git a/libretroshare/src/ft/ftserver.cc b/libretroshare/src/ft/ftserver.cc index 2a0178864..3951f9de1 100644 --- a/libretroshare/src/ft/ftserver.cc +++ b/libretroshare/src/ft/ftserver.cc @@ -160,7 +160,8 @@ void ftServer::SetupFtServer() void ftServer::connectToFileDatabase(p3FileDatabase *fdb) { mFileDatabase = fdb ; - mFtSearch->addSearchMode(fdb, RS_FILE_HINTS_LOCAL | RS_FILE_HINTS_REMOTE); + mFtSearch->addSearchMode(fdb, RS_FILE_HINTS_LOCAL); // due to a bug in addSearchModule, modules can only be added one by one. Using | between flags wont work. + mFtSearch->addSearchMode(fdb, RS_FILE_HINTS_REMOTE); } void ftServer::connectToTurtleRouter(p3turtle *fts) { From 160ab7b4f36a16c9b2563687e60cb7fbb3ea559a Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 3 Sep 2017 23:32:48 +0200 Subject: [PATCH 2/2] added missing code for remote search by hash, fixing the bug of direct sources not added --- .../src/file_sharing/directory_storage.cc | 7 ++++++- .../src/file_sharing/directory_storage.h | 12 ++++++++++++ libretroshare/src/file_sharing/p3filelists.cc | 18 +++++++++++++++++- libretroshare/src/ft/ftcontroller.cc | 9 ++++----- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/libretroshare/src/file_sharing/directory_storage.cc b/libretroshare/src/file_sharing/directory_storage.cc index 5cb47650c..8a58ec0fc 100644 --- a/libretroshare/src/file_sharing/directory_storage.cc +++ b/libretroshare/src/file_sharing/directory_storage.cc @@ -923,7 +923,12 @@ bool RemoteDirectoryStorage::deserialiseUpdateDirEntry(const EntryIndex& indx,co return true ; } - +int RemoteDirectoryStorage::searchHash(const RsFileHash& hash, EntryIndex& result) const +{ + RS_STACK_MUTEX(mDirStorageMtx) ; + + return mFileHierarchy->searchHash(hash,result); +} diff --git a/libretroshare/src/file_sharing/directory_storage.h b/libretroshare/src/file_sharing/directory_storage.h index c0eee75e0..91e67fcff 100644 --- a/libretroshare/src/file_sharing/directory_storage.h +++ b/libretroshare/src/file_sharing/directory_storage.h @@ -203,6 +203,18 @@ public: * \return */ time_t& lastSweepTime() { return mLastSweepTime ; } + + /*! + * \brief searchHash + * Looks into local database of shared files for the given hash. + * \param hash hash to look for + * \param results Entry index of the file that is found + * \return + * true is a file is found + * false otherwise. + */ + virtual int searchHash(const RsFileHash& hash, EntryIndex& results) const ; + private: time_t mLastSweepTime ; }; diff --git a/libretroshare/src/file_sharing/p3filelists.cc b/libretroshare/src/file_sharing/p3filelists.cc index 7569668ff..1f138454a 100644 --- a/libretroshare/src/file_sharing/p3filelists.cc +++ b/libretroshare/src/file_sharing/p3filelists.cc @@ -1089,7 +1089,23 @@ bool p3FileDatabase::search(const RsFileHash &hash, FileSearchFlags hintflags, F if(hintflags & RS_FILE_HINTS_REMOTE) { - NOT_IMPLEMENTED(); + EntryIndex indx; + bool found = false ; + + for(uint32_t i=0;isearchHash(hash,indx)) + { + TransferInfo ti ; + ti.peerId = mRemoteDirectories[i]->peerId(); + + info.hash = hash ; + info.peers.push_back(ti) ; + + found = true ; + } + + if(found) + return true; } return false; } diff --git a/libretroshare/src/ft/ftcontroller.cc b/libretroshare/src/ft/ftcontroller.cc index a8b1d5d26..265c88817 100644 --- a/libretroshare/src/ft/ftcontroller.cc +++ b/libretroshare/src/ft/ftcontroller.cc @@ -69,10 +69,9 @@ * #define CONTROL_DEBUG 1 * #define DEBUG_DWLQUEUE 1 *****/ -#define CONTROL_DEBUG 1 -static const int32_t SAVE_TRANSFERS_DELAY = 60;//301 ; // save transfer progress every 301 seconds. -static const int32_t INACTIVE_CHUNKS_CHECK_DELAY = 240 ; // time after which an inactive chunk is released +static const int32_t SAVE_TRANSFERS_DELAY = 301 ; // save transfer progress every 301 seconds. +static const int32_t INACTIVE_CHUNKS_CHECK_DELAY = 240 ; // time after which an inactive chunk is released static const int32_t MAX_TIME_INACTIVE_REQUEUED = 120 ; // time after which an inactive ftFileControl is bt-queued static const int32_t FT_FILECONTROL_QUEUE_ADD_END = 0 ; @@ -235,14 +234,14 @@ void ftController::data_tick() time_t now = time(NULL) ; if(now > last_save_time + SAVE_TRANSFERS_DELAY) { - searchForDirectSources() ; - IndicateConfigChanged() ; last_save_time = now ; } if(now > last_clean_time + INACTIVE_CHUNKS_CHECK_DELAY) { + searchForDirectSources() ; + RsStackMutex stack(ctrlMutex); /******* LOCKED ********/ for(std::map::iterator it(mDownloads.begin());it!=mDownloads.end();++it)