diff options
author | Alexander Tkachev | 2016-06-05 00:06:36 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | e98b0b12adc9ac6440ec4940d595095115f00d23 (patch) | |
tree | 6489025be7746eca2d0d1d9acfb5ad24afed00a0 | |
parent | 1bcbab7ad23b7dd47392e0d7a1fd2e56b385b367 (diff) | |
download | scummvm-rg350-e98b0b12adc9ac6440ec4940d595095115f00d23.tar.gz scummvm-rg350-e98b0b12adc9ac6440ec4940d595095115f00d23.tar.bz2 scummvm-rg350-e98b0b12adc9ac6440ec4940d595095115f00d23.zip |
CLOUD: Extend Storage & SavesSyncRequest
Now one can learn whether SavesSyncRequest is running, its progress and
which files are being synced.
-rw-r--r-- | backends/cloud/savessyncrequest.cpp | 22 | ||||
-rw-r--r-- | backends/cloud/savessyncrequest.h | 9 | ||||
-rw-r--r-- | backends/cloud/storage.cpp | 39 | ||||
-rw-r--r-- | backends/cloud/storage.h | 12 |
4 files changed, 77 insertions, 5 deletions
diff --git a/backends/cloud/savessyncrequest.cpp b/backends/cloud/savessyncrequest.cpp index 86dc83abf8..c7572c78f9 100644 --- a/backends/cloud/savessyncrequest.cpp +++ b/backends/cloud/savessyncrequest.cpp @@ -53,6 +53,7 @@ void SavesSyncRequest::start() { _filesToDownload.clear(); _filesToUpload.clear(); _localFilesTimestamps.clear(); + _totalFilesToHandle = 0; _ignoreCallback = false; //load timestamps @@ -120,6 +121,7 @@ void SavesSyncRequest::directoryListedCallback(Storage::ListDirectoryResponse re for (uint32 i = 0; i < _filesToUpload.size(); ++i) { debug("%s", _filesToUpload[i].c_str()); } + _totalFilesToHandle = _filesToDownload.size() + _filesToUpload.size(); /////// //start downloading files @@ -209,7 +211,7 @@ void SavesSyncRequest::downloadNextFile() { _filesToDownload.pop_back(); /////// - debug("downloading %s", _currentDownloadingFile.name().c_str()); + debug("downloading %s (%d %%)", _currentDownloadingFile.name().c_str(), (int)(getProgress() * 100)); /////// _workingRequest = _storage->download(_currentDownloadingFile.path(), concatWithSavesPath(_currentDownloadingFile.name()), new Common::Callback<SavesSyncRequest, Storage::BoolResponse>(this, &SavesSyncRequest::fileDownloadedCallback), @@ -253,7 +255,7 @@ void SavesSyncRequest::uploadNextFile() { _filesToUpload.pop_back(); /////// - debug("uploading %s", _currentUploadingFile.c_str()); + debug("uploading %s (%d %%)", _currentUploadingFile.c_str(), (int)(getProgress()*100)); /////// _workingRequest = _storage->upload(_storage->savesDirectoryPath() + _currentUploadingFile, g_system->getSavefileManager()->openRawFile(_currentUploadingFile), new Common::Callback<SavesSyncRequest, Storage::UploadResponse>(this, &SavesSyncRequest::fileUploadedCallback), @@ -285,6 +287,22 @@ void SavesSyncRequest::handle() {} void SavesSyncRequest::restart() { start(); } +double SavesSyncRequest::getProgress() { + if (_totalFilesToHandle == 0) { + if (_state == Networking::FINISHED) return 1; //nothing to upload and download => Request ends soon + return 0; //directory not listed yet + } + + return (double)(_totalFilesToHandle - _filesToDownload.size() - _filesToUpload.size()) / (double)(_totalFilesToHandle); +} + +Common::Array<Common::String> SavesSyncRequest::getFilesToUpload() { + Common::Array<Common::String> result = _filesToUpload; + if (_currentUploadingFile != "") + result.push_back(_currentUploadingFile); + return result; +} + void SavesSyncRequest::finishError(Networking::ErrorResponse error) { debug("SavesSync::finishError"); diff --git a/backends/cloud/savessyncrequest.h b/backends/cloud/savessyncrequest.h index 0e20159845..ad656107c9 100644 --- a/backends/cloud/savessyncrequest.h +++ b/backends/cloud/savessyncrequest.h @@ -43,6 +43,7 @@ class SavesSyncRequest: public Networking::Request { Common::String _currentUploadingFile; Request *_workingRequest; bool _ignoreCallback; + uint32 _totalFilesToHandle; void start(); void directoryListedCallback(Storage::ListDirectoryResponse response); @@ -65,7 +66,13 @@ public: virtual ~SavesSyncRequest(); virtual void handle(); - virtual void restart(); + virtual void restart(); + + /** Returns a number in range [0, 1], where 1 is "complete". */ + double getProgress(); + + /** Returns an array of saves names which are not uploaded yet. */ + Common::Array<Common::String> getFilesToUpload(); }; } // End of namespace Cloud diff --git a/backends/cloud/storage.cpp b/backends/cloud/storage.cpp index f035c93368..95786c2cba 100644 --- a/backends/cloud/storage.cpp +++ b/backends/cloud/storage.cpp @@ -30,7 +30,7 @@ namespace Cloud { -Storage::Storage(): _runningRequestsCount(0) {} +Storage::Storage(): _runningRequestsCount(0), _savesSyncRequest(nullptr) {} Storage::~Storage() {} @@ -53,6 +53,8 @@ Networking::Request *Storage::addRequest(Networking::Request *request) { void Storage::requestFinishedCallback(Networking::Request *invalidRequestPointer) { _runningRequestsMutex.lock(); + if (invalidRequestPointer == _savesSyncRequest) + _savesSyncRequest = nullptr; --_runningRequestsCount; if (_runningRequestsCount == 0) debug("Storage is not working now"); _runningRequestsMutex.unlock(); @@ -96,8 +98,16 @@ Networking::Request *Storage::downloadFolder(Common::String remotePath, Common:: } Networking::Request *Storage::syncSaves(BoolCallback callback, Networking::ErrorCallback errorCallback) { + _runningRequestsMutex.lock(); + if (_savesSyncRequest) { + warning("Storage::syncSaves: there is a sync in progress already"); + _runningRequestsMutex.unlock(); + return _savesSyncRequest; + } if (!errorCallback) errorCallback = getErrorPrintingCallback(); - return addRequest(new SavesSyncRequest(this, callback, errorCallback)); + _savesSyncRequest = new SavesSyncRequest(this, callback, errorCallback); + _runningRequestsMutex.unlock(); + return addRequest(_savesSyncRequest); } bool Storage::isWorking() { @@ -107,5 +117,30 @@ bool Storage::isWorking() { return working; } +bool Storage::isSyncing() { + _runningRequestsMutex.lock(); + bool syncing = _savesSyncRequest != nullptr; + _runningRequestsMutex.unlock(); + return syncing; +} + +double Storage::getSyncProgress() { + double result = 1; + _runningRequestsMutex.lock(); + if (_savesSyncRequest) + result = _savesSyncRequest->getProgress(); + _runningRequestsMutex.unlock(); + return result; +} + +Common::Array<Common::String> Storage::getSyncingFiles() { + Common::Array<Common::String> result; + _runningRequestsMutex.lock(); + if (_savesSyncRequest) + result = _savesSyncRequest->getFilesToUpload(); + _runningRequestsMutex.unlock(); + return result; +} + } // End of namespace Cloud diff --git a/backends/cloud/storage.h b/backends/cloud/storage.h index 1276b81827..0f518de5cd 100644 --- a/backends/cloud/storage.h +++ b/backends/cloud/storage.h @@ -35,6 +35,8 @@ namespace Cloud { +class SavesSyncRequest; + class Storage { public: typedef Networking::Response<Common::Array<StorageFile>&> FileArrayResponse; @@ -53,6 +55,7 @@ protected: /** Keeps track of running requests. */ uint32 _runningRequestsCount; Common::Mutex _runningRequestsMutex; + SavesSyncRequest *_savesSyncRequest; /** Returns default error callback (printErrorResponse). */ virtual Networking::ErrorCallback getErrorPrintingCallback(); @@ -134,6 +137,15 @@ public: /** Returns whether there are any requests running. */ virtual bool isWorking(); + + /** Returns whether there is a SavesSyncRequest running. */ + virtual bool isSyncing(); + + /** Returns a number in [0, 1] range which represents current sync progress (1 = complete). */ + virtual double getSyncProgress(); + + /** Returns an array of saves names which are not yet synced (thus cannot be used). */ + virtual Common::Array<Common::String> getSyncingFiles(); }; } // End of namespace Cloud |