mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-08 14:42:51 -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),
|
mPeerMgr(pm), mServiceCtrl(sc),
|
||||||
mFileDatabase(NULL),
|
mFileDatabase(NULL),
|
||||||
mFtController(NULL), mFtExtra(NULL),
|
mFtController(NULL), mFtExtra(NULL),
|
||||||
mFtDataplex(NULL), mFtSearch(NULL), srvMutex("ftServer")
|
mFtDataplex(NULL), mFtSearch(NULL), srvMutex("ftServer"),
|
||||||
|
mSearchCallbacksMapMutex("ftServer callbacks map")
|
||||||
{
|
{
|
||||||
addSerialType(new RsFileTransferSerialiser()) ;
|
addSerialType(new RsFileTransferSerialiser()) ;
|
||||||
}
|
}
|
||||||
|
@ -1630,6 +1631,7 @@ int ftServer::tick()
|
||||||
mFtDataplex->deleteUnusedServers() ;
|
mFtDataplex->deleteUnusedServers() ;
|
||||||
mFtDataplex->handlePendingCrcRequests() ;
|
mFtDataplex->handlePendingCrcRequests() ;
|
||||||
mFtDataplex->dispatchReceivedChunkCheckSum() ;
|
mFtDataplex->dispatchReceivedChunkCheckSum() ;
|
||||||
|
cleanTimedOutSearches();
|
||||||
}
|
}
|
||||||
|
|
||||||
return moreToTick;
|
return moreToTick;
|
||||||
|
@ -1822,9 +1824,21 @@ int ftServer::handleIncoming()
|
||||||
|
|
||||||
void ftServer::receiveSearchResult(RsTurtleFTSearchResultItem *item)
|
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 ****************************/
|
/***************************** CONFIG ****************************/
|
||||||
|
@ -1839,3 +1853,38 @@ bool ftServer::addConfiguration(p3ConfigMgr *cfgmgr)
|
||||||
return true;
|
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 <map>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <functional>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
#include "ft/ftdata.h"
|
#include "ft/ftdata.h"
|
||||||
#include "turtle/turtleclientservice.h"
|
#include "turtle/turtleclientservice.h"
|
||||||
|
@ -143,6 +145,12 @@ public:
|
||||||
virtual void setFilePermDirectDL(uint32_t perm) ;
|
virtual void setFilePermDirectDL(uint32_t perm) ;
|
||||||
virtual uint32_t filePermDirectDL() ;
|
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 std::string& string_to_match) ;
|
||||||
virtual TurtleSearchRequestId turtleSearch(const RsRegularExpression::LinearizedExpression& expr) ;
|
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<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,RsFileHash> mEncryptedPeerIds ; // This map holds the hash to be used with each peer id
|
||||||
std::map<RsPeerId,std::map<RsFileHash,time_t> > mUploadLimitMap ;
|
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();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,8 @@
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <functional>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
#include "rstypes.h"
|
#include "rstypes.h"
|
||||||
#include "serialiser/rsserializable.h"
|
#include "serialiser/rsserializable.h"
|
||||||
|
@ -315,8 +317,24 @@ public:
|
||||||
virtual uint32_t getMaxUploadSlotsPerFriend()=0;
|
virtual uint32_t getMaxUploadSlotsPerFriend()=0;
|
||||||
virtual void setFilePermDirectDL(uint32_t perm)=0;
|
virtual void setFilePermDirectDL(uint32_t perm)=0;
|
||||||
virtual uint32_t filePermDirectDL()=0;
|
virtual uint32_t filePermDirectDL()=0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Request remote files search
|
||||||
|
* @jsonapi{development}
|
||||||
|
* @param[in] matchString string to look for in the search
|
||||||
|
* @param multiCallback function that will be called each time a search
|
||||||
|
* result is received
|
||||||
|
* @param[in] maxWait maximum wait time in seconds for search results
|
||||||
|
* @return false on error, true otherwise
|
||||||
|
*/
|
||||||
|
virtual bool turtleSearchRequest(
|
||||||
|
const std::string& matchString,
|
||||||
|
const std::function<void (const std::list<TurtleFileInfo>& results)>& multiCallback,
|
||||||
|
std::time_t maxWait = 300 ) = 0;
|
||||||
|
|
||||||
virtual TurtleRequestId turtleSearch(const std::string& string_to_match) = 0;
|
virtual TurtleRequestId turtleSearch(const std::string& string_to_match) = 0;
|
||||||
virtual TurtleRequestId turtleSearch(const RsRegularExpression::LinearizedExpression& expr) =0;
|
virtual TurtleRequestId turtleSearch(
|
||||||
|
const RsRegularExpression::LinearizedExpression& expr) = 0;
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* Control of Downloads Priority.
|
* Control of Downloads Priority.
|
||||||
|
|
|
@ -38,30 +38,33 @@ class RsTurtle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pointer to global instance of RsTurtle service implementation
|
* Pointer to global instance of RsTurtle service implementation
|
||||||
* @jsonapi{development}
|
|
||||||
*/
|
*/
|
||||||
extern RsTurtle* rsTurtle;
|
extern RsTurtle* rsTurtle;
|
||||||
|
|
||||||
typedef uint32_t TurtleRequestId ;
|
typedef uint32_t TurtleRequestId ;
|
||||||
typedef RsPeerId TurtleVirtualPeerId;
|
typedef RsPeerId TurtleVirtualPeerId;
|
||||||
|
|
||||||
// This is the structure used to send back results of the turtle search
|
/**
|
||||||
// to the notifyBase class, or send info to the GUI.
|
* This is the structure used to send back results of the turtle search,
|
||||||
|
* to other peers, to the notifyBase class, to the search caller or to the GUI.
|
||||||
struct TurtleFileInfo //: RsSerializable
|
*/
|
||||||
|
struct TurtleFileInfo : RsSerializable
|
||||||
{
|
{
|
||||||
RsFileHash hash;
|
uint64_t size; /// File size
|
||||||
std::string name;
|
RsFileHash hash; /// File hash
|
||||||
uint64_t size;
|
std::string name; /// File name
|
||||||
/*
|
|
||||||
/// @see RsSerializable::serial_process
|
/// @see RsSerializable::serial_process
|
||||||
void serial_process( RsGenericSerializer::SerializeJob j,
|
void serial_process( RsGenericSerializer::SerializeJob j,
|
||||||
RsGenericSerializer::SerializeContext& ctx )
|
RsGenericSerializer::SerializeContext& ctx )
|
||||||
{
|
{
|
||||||
RS_SERIAL_PROCESS(hash);
|
|
||||||
RS_SERIAL_PROCESS(name);
|
|
||||||
RS_SERIAL_PROCESS(size);
|
RS_SERIAL_PROCESS(size);
|
||||||
}*/
|
RS_SERIAL_PROCESS(hash);
|
||||||
|
|
||||||
|
// Use String TLV serial process for retrocompatibility
|
||||||
|
RsTypeSerializer::serial_process(
|
||||||
|
j, ctx, TLV_TYPE_STR_NAME, name, "name" );
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TurtleTunnelRequestDisplayInfo
|
struct TurtleTunnelRequestDisplayInfo
|
||||||
|
|
|
@ -216,55 +216,6 @@ RsTurtleSearchResultItem *RsTurtleGenericSearchResultItem::duplicate() const
|
||||||
return sr ;
|
return sr ;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> uint32_t RsTypeSerializer::serial_size(const TurtleFileInfo& i)
|
|
||||||
{
|
|
||||||
uint32_t s = 0 ;
|
|
||||||
|
|
||||||
s += 8 ; // size
|
|
||||||
s += i.hash.SIZE_IN_BYTES ;
|
|
||||||
s += GetTlvStringSize(i.name) ;
|
|
||||||
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<> bool RsTypeSerializer::deserialize(const uint8_t data[],uint32_t size,uint32_t& offset,TurtleFileInfo& i)
|
|
||||||
{
|
|
||||||
uint32_t saved_offset = offset ;
|
|
||||||
bool ok = true ;
|
|
||||||
|
|
||||||
ok &= getRawUInt64(data, size, &offset, &i.size); // file size
|
|
||||||
ok &= i.hash.deserialise(data, size, offset); // file hash
|
|
||||||
ok &= GetTlvString(data, size, &offset, TLV_TYPE_STR_NAME, i.name); // file name
|
|
||||||
|
|
||||||
if(!ok)
|
|
||||||
offset = saved_offset ;
|
|
||||||
|
|
||||||
return ok;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<> bool RsTypeSerializer::serialize(uint8_t data[],uint32_t size,uint32_t& offset,const TurtleFileInfo& i)
|
|
||||||
{
|
|
||||||
uint32_t saved_offset = offset ;
|
|
||||||
bool ok = true ;
|
|
||||||
|
|
||||||
ok &= setRawUInt64(data, size, &offset, i.size); // file size
|
|
||||||
ok &= i.hash.serialise(data, size, offset); // file hash
|
|
||||||
ok &= SetTlvString(data, size, &offset, TLV_TYPE_STR_NAME, i.name); // file name
|
|
||||||
|
|
||||||
if(!ok)
|
|
||||||
offset = saved_offset ;
|
|
||||||
|
|
||||||
return ok;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<> void RsTypeSerializer::print_data(const std::string& n, const TurtleFileInfo& i)
|
|
||||||
{
|
|
||||||
std::cerr << " [FileInfo ] " << n << " size=" << i.size << " hash=" << i.hash << ", name=" << i.name << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
RS_TYPE_SERIALIZER_TO_JSON_NOT_IMPLEMENTED_DEF(TurtleFileInfo)
|
|
||||||
RS_TYPE_SERIALIZER_FROM_JSON_NOT_IMPLEMENTED_DEF(TurtleFileInfo)
|
|
||||||
|
|
||||||
void RsTurtleOpenTunnelItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx)
|
void RsTurtleOpenTunnelItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx)
|
||||||
{
|
{
|
||||||
RsTypeSerializer::serial_process (j,ctx,file_hash ,"file_hash") ;
|
RsTypeSerializer::serial_process (j,ctx,file_hash ,"file_hash") ;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue