mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-06 21:58:57 -04:00
Expose /rsFiles/turtleSearchRequest via JSON API
Expose new async C++ API RsFiles::turtleSearchRequest with callback Modernize TurtleFileInfo serialization code keeping retrocompatibility
This commit is contained in:
parent
a8b2532d3b
commit
c50405c070
5 changed files with 107 additions and 66 deletions
|
@ -71,7 +71,8 @@ ftServer::ftServer(p3PeerMgr *pm, p3ServiceControl *sc)
|
|||
mPeerMgr(pm), mServiceCtrl(sc),
|
||||
mFileDatabase(NULL),
|
||||
mFtController(NULL), mFtExtra(NULL),
|
||||
mFtDataplex(NULL), mFtSearch(NULL), srvMutex("ftServer")
|
||||
mFtDataplex(NULL), mFtSearch(NULL), srvMutex("ftServer"),
|
||||
mSearchCallbacksMapMutex("ftServer callbacks map")
|
||||
{
|
||||
addSerialType(new RsFileTransferSerialiser()) ;
|
||||
}
|
||||
|
@ -1630,6 +1631,7 @@ int ftServer::tick()
|
|||
mFtDataplex->deleteUnusedServers() ;
|
||||
mFtDataplex->handlePendingCrcRequests() ;
|
||||
mFtDataplex->dispatchReceivedChunkCheckSum() ;
|
||||
cleanTimedOutSearches();
|
||||
}
|
||||
|
||||
return moreToTick;
|
||||
|
@ -1822,9 +1824,21 @@ int ftServer::handleIncoming()
|
|||
|
||||
void ftServer::receiveSearchResult(RsTurtleFTSearchResultItem *item)
|
||||
{
|
||||
// @Gio: add your thing here
|
||||
bool hasCallback = false;
|
||||
|
||||
RsServer::notify()->notifyTurtleSearchResult(item->request_id,item->result) ;
|
||||
{
|
||||
RS_STACK_MUTEX(mSearchCallbacksMapMutex);
|
||||
auto cbpt = mSearchCallbacksMap.find(item->request_id);
|
||||
if(cbpt != mSearchCallbacksMap.end())
|
||||
{
|
||||
hasCallback = true;
|
||||
cbpt->second.first(item->result);
|
||||
}
|
||||
} // end RS_STACK_MUTEX(mSearchCallbacksMapMutex);
|
||||
|
||||
if(!hasCallback)
|
||||
RsServer::notify()->notifyTurtleSearchResult(
|
||||
item->request_id, item->result );
|
||||
}
|
||||
|
||||
/***************************** CONFIG ****************************/
|
||||
|
@ -1839,3 +1853,38 @@ bool ftServer::addConfiguration(p3ConfigMgr *cfgmgr)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool ftServer::turtleSearchRequest(
|
||||
const std::string& matchString,
|
||||
const std::function<void (const std::list<TurtleFileInfo>& results)>& multiCallback,
|
||||
std::time_t maxWait )
|
||||
{
|
||||
if(matchString.empty())
|
||||
{
|
||||
std::cerr << __PRETTY_FUNCTION__ << " match string can't be empty!"
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
TurtleRequestId sId = turtleSearch(matchString);
|
||||
|
||||
RS_STACK_MUTEX(mSearchCallbacksMapMutex);
|
||||
mSearchCallbacksMap.emplace(
|
||||
sId,
|
||||
std::make_pair(
|
||||
multiCallback,
|
||||
std::chrono::system_clock::now() +
|
||||
std::chrono::seconds(maxWait) ) );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ftServer::cleanTimedOutSearches()
|
||||
{
|
||||
RS_STACK_MUTEX(mSearchCallbacksMapMutex);
|
||||
auto now = std::chrono::system_clock::now();
|
||||
for( auto cbpt = mSearchCallbacksMap.begin();
|
||||
cbpt != mSearchCallbacksMap.end(); )
|
||||
if(cbpt->second.second <= now)
|
||||
cbpt = mSearchCallbacksMap.erase(cbpt);
|
||||
else ++cbpt;
|
||||
}
|
||||
|
|
|
@ -39,6 +39,8 @@
|
|||
#include <map>
|
||||
#include <list>
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include <chrono>
|
||||
|
||||
#include "ft/ftdata.h"
|
||||
#include "turtle/turtleclientservice.h"
|
||||
|
@ -143,6 +145,12 @@ public:
|
|||
virtual void setFilePermDirectDL(uint32_t perm) ;
|
||||
virtual uint32_t filePermDirectDL() ;
|
||||
|
||||
/// @see RsFiles
|
||||
virtual bool turtleSearchRequest(
|
||||
const std::string& matchString,
|
||||
const std::function<void (const std::list<TurtleFileInfo>& results)>& multiCallback,
|
||||
std::time_t maxWait = 300 );
|
||||
|
||||
virtual TurtleSearchRequestId turtleSearch(const std::string& string_to_match) ;
|
||||
virtual TurtleSearchRequestId turtleSearch(const RsRegularExpression::LinearizedExpression& expr) ;
|
||||
|
||||
|
@ -313,6 +321,18 @@ private:
|
|||
std::map<RsFileHash,RsFileHash> mEncryptedHashes ; // This map is such that sha1(it->second) = it->first
|
||||
std::map<RsPeerId,RsFileHash> mEncryptedPeerIds ; // This map holds the hash to be used with each peer id
|
||||
std::map<RsPeerId,std::map<RsFileHash,time_t> > mUploadLimitMap ;
|
||||
|
||||
/** Store search callbacks with timeout*/
|
||||
std::map<
|
||||
TurtleRequestId,
|
||||
std::pair<
|
||||
std::function<void (const std::list<TurtleFileInfo>& results)>,
|
||||
std::chrono::system_clock::time_point >
|
||||
> mSearchCallbacksMap;
|
||||
RsMutex mSearchCallbacksMapMutex;
|
||||
|
||||
/// Cleanup mSearchCallbacksMap
|
||||
void cleanTimedOutSearches();
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue