2016-09-12 18:01:26 -04:00
|
|
|
/*
|
|
|
|
* RetroShare C++ File lists service.
|
|
|
|
*
|
|
|
|
* file_sharing/p3filelists.h
|
|
|
|
*
|
|
|
|
* Copyright 2016 by Mr.Alice
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare.project@gmail.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2016-04-14 18:25:12 -04:00
|
|
|
// This class is responsible for
|
|
|
|
// - maintaining a list of shared file hierarchies for each known friends
|
|
|
|
// - talking to the GUI
|
|
|
|
// - providing handles for the directory tree listing GUI
|
|
|
|
// - providing search handles for FT
|
|
|
|
// - keeping these lists up to date
|
|
|
|
// - sending our own file list to friends depending on the defined access rights
|
|
|
|
// - serving file search requests from other services such as file transfer
|
|
|
|
//
|
|
|
|
// p3FileList does the following micro-tasks:
|
|
|
|
// - tick the watchers
|
|
|
|
// - get incoming info from the service layer, which can be:
|
|
|
|
// - directory content request => the directory content is shared to the friend
|
|
|
|
// - directory content => the directory watcher is notified
|
|
|
|
// - keep two queues of update requests:
|
|
|
|
// - fast queue that is handled in highest priority. This one is used for e.g. updating while browsing.
|
|
|
|
// - slow queue that is handled slowly. Used in background update of shared directories.
|
|
|
|
//
|
|
|
|
// The file lists are not directry updated. A FileListWatcher class is responsible for this
|
|
|
|
// in every case.
|
|
|
|
//
|
2016-07-20 15:28:55 -04:00
|
|
|
#pragma once
|
|
|
|
|
2016-07-30 15:52:42 -04:00
|
|
|
#include "ft/ftsearch.h"
|
2016-07-20 16:10:51 -04:00
|
|
|
#include "retroshare/rsfiles.h"
|
|
|
|
#include "services/p3service.h"
|
2016-07-18 21:33:54 -04:00
|
|
|
|
2016-07-30 15:52:42 -04:00
|
|
|
#include "file_sharing/hash_cache.h"
|
2016-07-31 09:59:58 -04:00
|
|
|
#include "file_sharing/directory_storage.h"
|
2016-07-30 15:52:42 -04:00
|
|
|
|
2016-07-20 16:10:51 -04:00
|
|
|
#include "pqi/p3cfgmgr.h"
|
|
|
|
#include "pqi/p3linkmgr.h"
|
|
|
|
|
2016-07-30 15:52:42 -04:00
|
|
|
class RemoteDirectoryUpdater ;
|
|
|
|
class LocalDirectoryUpdater ;
|
|
|
|
|
2016-07-20 16:10:51 -04:00
|
|
|
class RemoteDirectoryStorage ;
|
2016-07-30 15:52:42 -04:00
|
|
|
class LocalDirectoryStorage ;
|
2016-07-20 16:10:51 -04:00
|
|
|
|
2016-08-23 15:23:58 -04:00
|
|
|
class RsFileListsSyncRequestItem ;
|
|
|
|
class RsFileListsSyncResponseItem ;
|
2016-08-20 10:23:11 -04:00
|
|
|
|
2016-07-30 15:52:42 -04:00
|
|
|
class HashStorage ;
|
|
|
|
|
|
|
|
class p3FileDatabase: public p3Service, public p3Config, public ftSearch //, public RsSharedFileService
|
2016-04-14 18:25:12 -04:00
|
|
|
{
|
|
|
|
public:
|
2016-07-31 09:59:58 -04:00
|
|
|
typedef DirectoryStorage::EntryIndex EntryIndex; // this should probably be defined elsewhere
|
2016-07-18 21:33:54 -04:00
|
|
|
|
2016-07-30 15:52:42 -04:00
|
|
|
virtual RsServiceInfo getServiceInfo();
|
|
|
|
|
2016-07-20 16:10:51 -04:00
|
|
|
struct RsFileListSyncRequest
|
|
|
|
{
|
|
|
|
RsPeerId peerId ;
|
|
|
|
EntryIndex index ;
|
|
|
|
// [...] more to add here
|
|
|
|
};
|
|
|
|
|
2016-07-30 15:52:42 -04:00
|
|
|
p3FileDatabase(p3ServiceControl *mpeers) ;
|
|
|
|
~p3FileDatabase();
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief forceSyncWithPeers
|
|
|
|
*
|
2016-07-31 09:59:58 -04:00
|
|
|
* Forces the synchronisation of the database with connected peers. This is triggered when e.g. a new group of friend is created, or when
|
2016-07-30 15:52:42 -04:00
|
|
|
* a friend was added/removed from a group.
|
|
|
|
*/
|
2016-07-31 09:59:58 -04:00
|
|
|
void forceSyncWithPeers() { NOT_IMPLEMENTED() ; }
|
2016-07-18 21:33:54 -04:00
|
|
|
|
|
|
|
// derived from p3Service
|
|
|
|
//
|
|
|
|
virtual int tick() ;
|
|
|
|
|
2016-07-30 15:52:42 -04:00
|
|
|
// ftSearch
|
|
|
|
virtual bool search(const RsFileHash &hash, FileSearchFlags hintflags, FileInfo &info) const;
|
2016-09-18 15:16:25 -04:00
|
|
|
virtual int SearchKeywords(const std::list<std::string>& keywords, std::list<DirDetails>& results,FileSearchFlags flags,const RsPeerId& peer_id) ;
|
|
|
|
virtual int SearchBoolExp(RsRegularExpression::Expression *exp, std::list<DirDetails>& results,FileSearchFlags flags,const RsPeerId& peer_id) const ;
|
2016-07-30 15:52:42 -04:00
|
|
|
|
2016-07-18 21:33:54 -04:00
|
|
|
// Interface for browsing dir hierarchy
|
|
|
|
//
|
2016-07-30 15:52:42 -04:00
|
|
|
|
|
|
|
void stopThreads() ;
|
|
|
|
void startThreads() ;
|
|
|
|
|
2016-09-15 04:41:40 -04:00
|
|
|
bool findChildPointer(void *ref, int row, void *& result, FileSearchFlags flags) const;
|
2016-07-18 21:33:54 -04:00
|
|
|
|
2016-07-30 15:52:42 -04:00
|
|
|
// void * here is the type expected by the abstract model index from Qt. It gets turned into a DirectoryStorage::EntryIndex internally.
|
|
|
|
|
2016-09-11 11:52:12 -04:00
|
|
|
void requestDirUpdate(void *ref) ; // triggers an update. Used when browsing.
|
2016-07-30 15:52:42 -04:00
|
|
|
int RequestDirDetails(void *, DirDetails&, FileSearchFlags) const ;
|
|
|
|
uint32_t getType(void *) const ;
|
|
|
|
|
2016-09-18 15:16:25 -04:00
|
|
|
// proxy method used by the web UI.
|
|
|
|
int RequestDirDetails(const RsPeerId& uid, const std::string& path, DirDetails &details)const;
|
|
|
|
|
2016-07-30 15:52:42 -04:00
|
|
|
// set/update shared directories
|
2016-07-31 09:59:58 -04:00
|
|
|
|
2016-07-30 15:52:42 -04:00
|
|
|
void setSharedDirectories(const std::list<SharedDirInfo>& dirs);
|
|
|
|
void getSharedDirectories(std::list<SharedDirInfo>& dirs);
|
|
|
|
void updateShareFlags(const SharedDirInfo& info) ;
|
|
|
|
bool convertSharedFilePath(const std::string& path,std::string& fullpath);
|
|
|
|
|
|
|
|
// interface for hash caching
|
|
|
|
|
|
|
|
void setWatchPeriod(uint32_t seconds);
|
|
|
|
uint32_t watchPeriod() ;
|
2016-09-18 12:34:39 -04:00
|
|
|
void setWatchEnabled(bool b) ;
|
|
|
|
bool watchEnabled() ;
|
2016-07-30 15:52:42 -04:00
|
|
|
|
|
|
|
// interfact for directory parsing
|
2016-07-18 21:33:54 -04:00
|
|
|
|
|
|
|
void forceDirectoryCheck(); // Force re-sweep the directories and see what's changed
|
|
|
|
bool inDirectoryCheck();
|
|
|
|
|
2016-07-20 16:10:51 -04:00
|
|
|
protected:
|
2016-07-31 09:59:58 -04:00
|
|
|
|
|
|
|
int filterResults(const std::list<EntryIndex>& firesults,std::list<DirDetails>& results,FileSearchFlags flags,const RsPeerId& peer_id) const;
|
|
|
|
std::string makeRemoteFileName(const RsPeerId& pid) const;
|
|
|
|
|
2016-07-30 15:52:42 -04:00
|
|
|
// Derived from p3Config
|
|
|
|
//
|
2016-07-20 16:10:51 -04:00
|
|
|
virtual bool loadList(std::list<RsItem *>& items);
|
2016-07-30 15:52:42 -04:00
|
|
|
virtual bool saveList(bool &cleanup, std::list<RsItem *>&);
|
2016-08-27 10:38:15 -04:00
|
|
|
virtual RsSerialiser *setupSerialiser() ;
|
2016-07-30 15:52:42 -04:00
|
|
|
|
2016-07-20 16:10:51 -04:00
|
|
|
void cleanup();
|
|
|
|
void tickRecv();
|
|
|
|
void tickSend();
|
|
|
|
|
|
|
|
private:
|
2016-07-30 15:52:42 -04:00
|
|
|
p3ServiceControl *mServCtrl ;
|
2016-08-20 10:23:11 -04:00
|
|
|
RsPeerId mOwnId ;
|
|
|
|
|
|
|
|
typedef uint64_t DirSyncRequestId ;
|
|
|
|
|
2016-09-11 11:52:12 -04:00
|
|
|
static DirSyncRequestId makeDirSyncReqId(const RsPeerId& peer_id, const RsFileHash &hash) ;
|
|
|
|
|
2016-09-15 17:51:46 -04:00
|
|
|
// utility functions to send items with some maximum size.
|
|
|
|
|
|
|
|
void splitAndSendItem(RsFileListsSyncResponseItem *ritem);
|
|
|
|
RsFileListsSyncResponseItem *recvAndRebuildItem(RsFileListsSyncResponseItem *ritem);
|
|
|
|
|
2016-09-11 11:52:12 -04:00
|
|
|
/*!
|
|
|
|
* \brief generateAndSendSyncRequest
|
|
|
|
* \param rds Remote directory storage for the request
|
|
|
|
* \param e Entry index to update
|
|
|
|
* \return true if the request is correctly sent.
|
|
|
|
*/
|
2016-09-15 15:45:00 -04:00
|
|
|
bool generateAndSendSyncRequest(RemoteDirectoryStorage *rds,const DirectoryStorage::EntryIndex& e);
|
2016-07-18 21:33:54 -04:00
|
|
|
|
|
|
|
// File sync request queues. The fast one is used for online browsing when friends are connected.
|
|
|
|
// The slow one is used for background update of file lists.
|
|
|
|
//
|
2016-08-20 10:23:11 -04:00
|
|
|
std::map<DirSyncRequestId,RsFileListSyncRequest> mFastRequestQueue ;
|
|
|
|
std::map<DirSyncRequestId,RsFileListSyncRequest> mSlowRequestQueue ;
|
2016-07-18 21:33:54 -04:00
|
|
|
|
|
|
|
// Directory storage hierarchies
|
|
|
|
//
|
2016-08-05 16:00:25 -04:00
|
|
|
// The remote one is the reference for the PeerId index below:
|
|
|
|
// RemoteDirectories[ getFriendIndex(pid) - 1] = RemoteDirectoryStorage(pid)
|
|
|
|
|
2016-08-20 10:23:11 -04:00
|
|
|
std::vector<RemoteDirectoryStorage *> mRemoteDirectories ;
|
2016-07-30 15:52:42 -04:00
|
|
|
LocalDirectoryStorage *mLocalSharedDirs ;
|
2016-07-18 21:33:54 -04:00
|
|
|
|
2016-07-30 15:52:42 -04:00
|
|
|
LocalDirectoryUpdater *mLocalDirWatcher ;
|
2016-04-14 18:25:12 -04:00
|
|
|
|
2016-08-05 16:00:25 -04:00
|
|
|
// utility functions to make/get a pointer out of an (EntryIndex,PeerId) pair. This is further documented in the .cc
|
|
|
|
|
2016-08-11 08:07:45 -04:00
|
|
|
static bool convertEntryIndexToPointer(const EntryIndex &e, uint32_t friend_index, void *& p);
|
|
|
|
static bool convertPointerToEntryIndex(const void *p, EntryIndex& e, uint32_t& friend_index) ;
|
2016-08-17 08:48:54 -04:00
|
|
|
uint32_t locked_getFriendIndex(const RsPeerId& pid);
|
|
|
|
const RsPeerId& locked_getFriendFromIndex(uint32_t indx) const;
|
2016-08-05 16:00:25 -04:00
|
|
|
|
2016-08-23 15:23:58 -04:00
|
|
|
void handleDirSyncRequest (RsFileListsSyncRequestItem *) ;
|
|
|
|
void handleDirSyncResponse (RsFileListsSyncResponseItem *) ;
|
2016-08-20 10:23:11 -04:00
|
|
|
|
2016-08-05 16:00:25 -04:00
|
|
|
std::map<RsPeerId,uint32_t> mFriendIndexMap ;
|
|
|
|
std::vector<RsPeerId> mFriendIndexTab;
|
|
|
|
|
2016-08-22 01:49:45 -04:00
|
|
|
// Directory synchronization
|
|
|
|
//
|
|
|
|
struct DirSyncRequestData
|
|
|
|
{
|
2016-08-27 10:38:15 -04:00
|
|
|
RsPeerId peer_id ;
|
2016-08-22 01:49:45 -04:00
|
|
|
time_t request_TS ;
|
|
|
|
uint32_t flags ;
|
|
|
|
};
|
|
|
|
|
|
|
|
time_t mLastRemoteDirSweepTS ; // TS for friend list update
|
|
|
|
std::map<DirSyncRequestId,DirSyncRequestData> mPendingSyncRequests ; // pending requests, waiting for an answer
|
2016-09-15 17:51:46 -04:00
|
|
|
std::map<DirSyncRequestId,RsFileListsSyncResponseItem *> mPartialResponseItems;
|
2016-08-20 10:23:11 -04:00
|
|
|
|
2016-09-11 11:52:12 -04:00
|
|
|
void locked_recursSweepRemoteDirectory(RemoteDirectoryStorage *rds, DirectoryStorage::EntryIndex e, int depth);
|
2016-08-20 10:23:11 -04:00
|
|
|
|
2016-08-05 16:00:25 -04:00
|
|
|
// We use a shared file cache as well, to avoid re-hashing files with known modification TS and equal name.
|
2016-07-18 21:33:54 -04:00
|
|
|
//
|
2016-07-30 15:52:42 -04:00
|
|
|
HashStorage *mHashCache ;
|
2016-04-14 18:25:12 -04:00
|
|
|
|
2016-07-20 16:10:51 -04:00
|
|
|
// Local flags and mutexes
|
|
|
|
|
2016-08-17 08:48:54 -04:00
|
|
|
mutable RsMutex mFLSMtx ;
|
2016-07-20 16:10:51 -04:00
|
|
|
uint32_t mUpdateFlags ;
|
2016-09-02 15:49:43 -04:00
|
|
|
std::string mFileSharingDir ;
|
2016-09-03 07:29:23 -04:00
|
|
|
time_t mLastCleanupTime;
|
2016-04-14 18:25:12 -04:00
|
|
|
};
|
|
|
|
|