diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index 183751356..27c8c260a 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -3429,3 +3429,9 @@ void RsGenExchange::turtleSearchRequest(const std::string& match_string) { mNetService->turtleSearchRequest(match_string) ; } + +bool RsGenExchange::localSearch( const std::string& matchString, + std::list& results ) +{ + return mNetService->search(matchString, results); +} diff --git a/libretroshare/src/gxs/rsgenexchange.h b/libretroshare/src/gxs/rsgenexchange.h index a180c7741..ea3a43d7c 100644 --- a/libretroshare/src/gxs/rsgenexchange.h +++ b/libretroshare/src/gxs/rsgenexchange.h @@ -313,6 +313,15 @@ public: void turtleGroupRequest(const RsGxsGroupId& group_id); void turtleSearchRequest(const std::string& match_string); + /** + * @brief Search local groups. Blocking API. + * @param matchString string to look for in the search + * @param results storage for results + * @return false on error, true otherwise + */ + bool localSearch( const std::string& matchString, + std::list& results ); + protected: bool messagePublicationTest(const RsGxsMsgMetaData&) ; diff --git a/libretroshare/src/retroshare/rsgxschannels.h b/libretroshare/src/retroshare/rsgxschannels.h index aa89b9341..f1aef6e7f 100644 --- a/libretroshare/src/retroshare/rsgxschannels.h +++ b/libretroshare/src/retroshare/rsgxschannels.h @@ -287,6 +287,19 @@ public: const std::function& multiCallback, rstime_t maxWait = 300 ) = 0; + /** + * @brief Search local channels + * @jsonapi{development} + * @param[in] matchString string to look for in the search + * @param multiCallback function that will be called for each result + * @param[in] maxWait maximum wait time in seconds for search results + * @return false on error, true otherwise + */ + virtual bool localSearchRequest( + const std::string& matchString, + const std::function& multiCallback, + rstime_t maxWait = 30 ) = 0; + /* Following functions are deprecated as they expose internal functioning * semantic, instead of a safe to use API */ diff --git a/libretroshare/src/services/p3gxschannels.cc b/libretroshare/src/services/p3gxschannels.cc index 84610b107..847395686 100644 --- a/libretroshare/src/services/p3gxschannels.cc +++ b/libretroshare/src/services/p3gxschannels.cc @@ -36,6 +36,7 @@ #include "retroshare/rsnotify.h" #include +#include // For Dummy Msgs. #include "util/rsrandom.h" @@ -50,7 +51,7 @@ * #define GXSCHANNEL_DEBUG 1 ****/ -/*extern*/ RsGxsChannels *rsGxsChannels = nullptr; +/*extern*/ RsGxsChannels* rsGxsChannels = nullptr; #define GXSCHANNEL_STOREPERIOD (3600 * 24 * 30) @@ -1969,6 +1970,31 @@ bool p3GxsChannels::turtleChannelRequest( return true; } +/// @see RsGxsChannels::localSearchRequest +bool p3GxsChannels::localSearchRequest( + const std::string& matchString, + const std::function& multiCallback, + rstime_t maxWait ) +{ + if(matchString.empty()) + { + std::cerr << __PRETTY_FUNCTION__ << " match string can't be empty!" + << std::endl; + return false; + } + + auto timeout = std::chrono::steady_clock::now() + std::chrono::seconds(maxWait); + RsThread::async([=]() + { + std::list results; + RsGenExchange::localSearch(matchString, results); + if(std::chrono::steady_clock::now() < timeout) + for(const RsGxsGroupSummary& result : results) multiCallback(result); + }); + + return true; +} + void p3GxsChannels::receiveDistantSearchResults( TurtleRequestId id, const RsGxsGroupId& grpId ) { diff --git a/libretroshare/src/services/p3gxschannels.h b/libretroshare/src/services/p3gxschannels.h index e5d49772b..20be70aec 100644 --- a/libretroshare/src/services/p3gxschannels.h +++ b/libretroshare/src/services/p3gxschannels.h @@ -118,6 +118,11 @@ virtual bool getChannelDownloadDirectory(const RsGxsGroupId &groupId, std::strin const std::function& multiCallback, rstime_t maxWait = 300 ); + /// @see RsGxsChannels::localSearchRequest + virtual bool localSearchRequest(const std::string& matchString, + const std::function& multiCallback, + rstime_t maxWait = 30 ) override; + /** * Receive results from turtle search @see RsGenExchange @see RsNxsObserver * @see RsGxsNetService::receiveTurtleSearchResults