CLOUD: Extend Storage & SavesSyncRequest

Now one can learn whether SavesSyncRequest is running, its progress and
which files are being synced.
This commit is contained in:
Alexander Tkachev 2016-06-05 00:06:36 +06:00
parent 1bcbab7ad2
commit e98b0b12ad
4 changed files with 77 additions and 5 deletions

View file

@ -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");

View file

@ -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

View file

@ -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

View file

@ -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