From 92df847ecee80f1feb5722aed0e282095522684e Mon Sep 17 00:00:00 2001 From: Konrad Date: Wed, 27 Dec 2017 22:33:23 +0100 Subject: [PATCH] Added in Libresapi: StateTokens to FileSharingHandler --- libresapi/src/api/FileSharingHandler.cpp | 48 ++++++++++++++++++++++-- libresapi/src/api/FileSharingHandler.h | 16 ++++++-- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/libresapi/src/api/FileSharingHandler.cpp b/libresapi/src/api/FileSharingHandler.cpp index d948a9511..19098a188 100644 --- a/libresapi/src/api/FileSharingHandler.cpp +++ b/libresapi/src/api/FileSharingHandler.cpp @@ -21,8 +21,10 @@ namespace resource_api { -FileSharingHandler::FileSharingHandler(StateTokenServer *sts, RsFiles *files): - mStateTokenServer(sts), mRsFiles(files) +FileSharingHandler::FileSharingHandler(StateTokenServer *sts, RsFiles *files, + RsNotify* notify): + mStateTokenServer(sts), mRsFiles(files), mNotify(notify), + mMtx("FileSharingHandler Mtx") { addResourceHandler("*", this, &FileSharingHandler::handleWildcard); addResourceHandler("force_check", this, &FileSharingHandler::handleForceCheck); @@ -40,7 +42,30 @@ FileSharingHandler::FileSharingHandler(StateTokenServer *sts, RsFiles *files): addResourceHandler("download", this, &FileSharingHandler::handleDownload); - mStateToken = mStateTokenServer->getNewToken(); + mLocalDirStateToken = mStateTokenServer->getNewToken(); + mRemoteDirStateToken = mStateTokenServer->getNewToken(); + mNotify->registerNotifyClient(this); +} + +FileSharingHandler::~FileSharingHandler() +{ + mNotify->unregisterNotifyClient(this); +} + +void FileSharingHandler::notifyListChange(int list, int /* type */) +{ + if(list == NOTIFY_LIST_DIRLIST_LOCAL) + { + RsStackMutex stack(mMtx); /********** STACK LOCKED MTX ******/ + mStateTokenServer->discardToken(mLocalDirStateToken); + mLocalDirStateToken = mStateTokenServer->getNewToken(); + } + else if(list == NOTIFY_LIST_DIRLIST_FRIENDS) + { + RsStackMutex stack(mMtx); /********** STACK LOCKED MTX ******/ + mStateTokenServer->discardToken(mRemoteDirStateToken); + mRemoteDirStateToken = mStateTokenServer->getNewToken(); + } } void FileSharingHandler::handleWildcard(Request & /*req*/, Response & /*resp*/) @@ -104,6 +129,7 @@ void FileSharingHandler::handleGetSharedDir(Request& req, Response& resp) << makeKeyValue("name", dirDetails.name) << makeKeyValue("path", dirDetails.path) << makeKeyValue("hash", dirDetails.hash.toStdString()) + << makeKeyValue("peer_id", dirDetails.id.toStdString()) << makeKeyValue("parent_reference", *reinterpret_cast(&dirDetails.parent)) << makeKeyValue("reference", *reinterpret_cast(&dirDetails.ref)) << makeKeyValue("count", static_cast(dirDetails.count)) @@ -140,6 +166,8 @@ void FileSharingHandler::handleGetSharedDir(Request& req, Response& resp) << makeKeyValueReference("contain_folders", contain_folders); } } + + resp.mStateToken = mLocalDirStateToken; } void FileSharingHandler::handleSetSharedDir(Request& req, Response& resp) @@ -231,10 +259,16 @@ void FileSharingHandler::handleGetDirectoryParent(Request& req, Response& resp) FileSearchFlags flags; if(remote) + { flags |= RS_FILE_HINTS_REMOTE; + resp.mStateToken = mRemoteDirStateToken; + } if(local) + { flags |= RS_FILE_HINTS_LOCAL; + resp.mStateToken = mLocalDirStateToken; + } DirDetails dirDetails; mRsFiles->RequestDirDetails(ref, dirDetails, flags); @@ -282,6 +316,7 @@ void FileSharingHandler::handleGetDirectoryParent(Request& req, Response& resp) << makeKeyValue("name", dirDetails.name) << makeKeyValue("path", dirDetails.path) << makeKeyValue("hash", dirDetails.hash.toStdString()) + << makeKeyValue("peer_id", dirDetails.id.toStdString()) << makeKeyValue("parent_reference", *reinterpret_cast(&dirDetails.parent)) << makeKeyValue("reference", *reinterpret_cast(&dirDetails.ref)) << makeKeyValue("count", static_cast(dirDetails.count)) @@ -334,10 +369,16 @@ void FileSharingHandler::handleGetDirectoryChilds(Request& req, Response& resp) FileSearchFlags flags; if(remote) + { flags |= RS_FILE_HINTS_REMOTE; + resp.mStateToken = mRemoteDirStateToken; + } if(local) + { flags |= RS_FILE_HINTS_LOCAL; + resp.mStateToken = mLocalDirStateToken; + } DirDetails dirDetails; mRsFiles->RequestDirDetails(ref, dirDetails, flags); @@ -385,6 +426,7 @@ void FileSharingHandler::handleGetDirectoryChilds(Request& req, Response& resp) << makeKeyValue("name", dirDetails.name) << makeKeyValue("path", dirDetails.path) << makeKeyValue("hash", dirDetails.hash.toStdString()) + << makeKeyValue("peer_id", dirDetails.id.toStdString()) << makeKeyValue("parent_reference", *reinterpret_cast(&dirDetails.parent)) << makeKeyValue("reference", *reinterpret_cast(&dirDetails.ref)) << makeKeyValue("count", static_cast(dirDetails.count)) diff --git a/libresapi/src/api/FileSharingHandler.h b/libresapi/src/api/FileSharingHandler.h index 8b741e80d..221803060 100644 --- a/libresapi/src/api/FileSharingHandler.h +++ b/libresapi/src/api/FileSharingHandler.h @@ -22,14 +22,21 @@ #include "StateTokenServer.h" #include +#include +#include namespace resource_api { -class FileSharingHandler: public ResourceRouter +class FileSharingHandler: public ResourceRouter, NotifyClient { public: - FileSharingHandler(StateTokenServer* sts, RsFiles* files); + FileSharingHandler(StateTokenServer* sts, RsFiles* files, RsNotify* notify); + ~FileSharingHandler(); + + // from NotifyClient + // note: this may get called from foreign threads + virtual void notifyListChange(int list, int type); private: void handleWildcard(Request& req, Response& resp); @@ -48,10 +55,13 @@ private: void handleDownload(Request& req, Response& resp); - StateToken mStateToken; + StateToken mLocalDirStateToken; + StateToken mRemoteDirStateToken; StateTokenServer* mStateTokenServer; + RsMutex mMtx; RsFiles* mRsFiles; + RsNotify* mNotify; }; } // namespace resource_api