From 2293b600f9fd44dc92980ebb5c37ed33b5160675 Mon Sep 17 00:00:00 2001 From: Konrad Date: Fri, 20 Oct 2017 20:49:59 +0200 Subject: [PATCH] Added in Libresapi: Handler to get search result --- libresapi/src/api/FileSearchHandler.cpp | 73 +++++++++++++++++++++++-- libresapi/src/api/FileSearchHandler.h | 5 +- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/libresapi/src/api/FileSearchHandler.cpp b/libresapi/src/api/FileSearchHandler.cpp index c159ad913..5d3d2a86d 100644 --- a/libresapi/src/api/FileSearchHandler.cpp +++ b/libresapi/src/api/FileSearchHandler.cpp @@ -2,6 +2,7 @@ #include "FileSearchHandler.h" +#include #include #include @@ -10,13 +11,14 @@ namespace resource_api { -FileSearchHandler::FileSearchHandler(StateTokenServer *sts, RsNotify *notify, RsTurtle *turtle, RsFiles */*files*/): - mStateTokenServer(sts), mNotify(notify), mTurtle(turtle),// mFiles(files), +FileSearchHandler::FileSearchHandler(StateTokenServer *sts, RsNotify *notify, RsTurtle *turtle, RsFiles *files): + mStateTokenServer(sts), mNotify(notify), mTurtle(turtle), mRsFiles(files), mMtx("FileSearchHandler") { mNotify->registerNotifyClient(this); addResourceHandler("*", this, &FileSearchHandler::handleWildcard); - addResourceHandler("create_search", this, &FileSearchHandler::handleCreateSearch); + addResourceHandler("create_search", this, &FileSearchHandler::handleCreateSearch); + addResourceHandler("get_search_result", this, &FileSearchHandler::handleGetSearchResult); mSearchesStateToken = mStateTokenServer->getNewToken(); } @@ -102,8 +104,11 @@ void FileSearchHandler::handleWildcard(Request &req, Response &resp) << makeKeyValueReference("id", fd.hash) << makeKeyValueReference("name", fd.name) << makeKeyValueReference("hash", fd.hash) + << makeKeyValueReference("path", fd.path) + << makeKeyValue("peer_id", fd.id.toStdString()) << makeKeyValueReference("size", size) - << makeKeyValueReference("rank", fd.rank); + << makeKeyValueReference("rank", fd.rank) + << makeKeyValueReference("age", fd.age); } } } @@ -194,7 +199,7 @@ void FileSearchHandler::handleCreateSearch(Request &req, Response &resp) if(local) { std::list local_results; - rsFiles->SearchBoolExp(&exprs, local_results, RS_FILE_HINTS_LOCAL); + mRsFiles->SearchBoolExp(&exprs, local_results, RS_FILE_HINTS_LOCAL); for(std::list::iterator lit = local_results.begin(); lit != local_results.end(); ++lit) { @@ -206,7 +211,7 @@ void FileSearchHandler::handleCreateSearch(Request &req, Response &resp) if(remote) { std::list remote_results; - rsFiles->SearchBoolExp(&exprs, remote_results, RS_FILE_HINTS_REMOTE); + mRsFiles->SearchBoolExp(&exprs, remote_results, RS_FILE_HINTS_REMOTE); for(std::list::iterator lit = remote_results.begin(); lit != remote_results.end(); ++lit) { FileDetail fd; @@ -239,4 +244,60 @@ void FileSearchHandler::handleCreateSearch(Request &req, Response &resp) resp.setOk(); } +void FileSearchHandler::handleGetSearchResult(Request& req, Response& resp) +{ + std::string search_id; + req.mStream << makeKeyValueReference("search_id", search_id); + + if(search_id.size() != 8) + { + resp.setFail("Error: id has wrong size, should be 8 characters"); + return; + } + + uint32_t id = 0; + for(uint8_t i = 0; i < 8; i++) + { + id += (uint32_t(search_id[i]-'A')) << (i*4); + } + + { + RsStackMutex stackMtx(mMtx); // ********** STACK LOCKED MTX ********** + std::map::iterator mit = mSearches.find(id); + if(mit == mSearches.end()) + { + resp.setFail("Error: search id invalid"); + return; + } + + Search& search = mit->second; + resp.mStateToken = search.mStateToken; + resp.mDataStream.getStreamToMember(); + + RsPgpId ownId = rsPeers->getGPGOwnId(); + for(std::list::iterator lit = search.mResults.begin(); lit != search.mResults.end(); ++lit) + { + FileDetail& fd = *lit; + bool isFriend = rsPeers->isFriend(fd.id); + bool isOwn = false; + if(ownId == rsPeers->getGPGId(fd.id)) + isOwn = true; + + double size = fd.size; + resp.mDataStream.getStreamToMember() + << makeKeyValueReference("id", fd.hash) + << makeKeyValueReference("name", fd.name) + << makeKeyValueReference("hash", fd.hash) + << makeKeyValueReference("path", fd.path) + << makeKeyValue("peer_id", fd.id.toStdString()) + << makeKeyValueReference("is_friends", isFriend) + << makeKeyValueReference("is_own", isOwn) + << makeKeyValueReference("size", size) + << makeKeyValueReference("rank", fd.rank) + << makeKeyValueReference("age", fd.age); + } + } + resp.setOk(); +} + } // namespace resource_api diff --git a/libresapi/src/api/FileSearchHandler.h b/libresapi/src/api/FileSearchHandler.h index a049c2207..5832577b5 100644 --- a/libresapi/src/api/FileSearchHandler.h +++ b/libresapi/src/api/FileSearchHandler.h @@ -19,12 +19,13 @@ public: virtual void notifyTurtleSearchResult(uint32_t search_id, const std::list& files); private: void handleWildcard(Request& req, Response& resp); - void handleCreateSearch(Request& req, Response& resp); + void handleCreateSearch(Request& req, Response& resp); + void handleGetSearchResult(Request& req, Response& resp); StateTokenServer* mStateTokenServer; RsNotify* mNotify; RsTurtle* mTurtle; - //RsFiles* mFiles; + RsFiles* mRsFiles; class Search{ public: