ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

storage.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "backends/cloud/storage.h"
00024 #include "backends/cloud/downloadrequest.h"
00025 #include "backends/cloud/folderdownloadrequest.h"
00026 #include "backends/cloud/savessyncrequest.h"
00027 #include "backends/networking/curl/connectionmanager.h"
00028 #include "common/debug.h"
00029 #include "common/file.h"
00030 #include <common/translation.h>
00031 #include "common/osd_message_queue.h"
00032 
00033 namespace Cloud {
00034 
00035 Storage::Storage():
00036     _runningRequestsCount(0), _savesSyncRequest(nullptr), _syncRestartRequestsed(false),
00037     _downloadFolderRequest(nullptr) {}
00038 
00039 Storage::~Storage() {}
00040 
00041 Networking::ErrorCallback Storage::getErrorPrintingCallback() {
00042     return new Common::Callback<Storage, Networking::ErrorResponse>(this, &Storage::printErrorResponse);
00043 }
00044 
00045 void Storage::printErrorResponse(Networking::ErrorResponse error) {
00046     debug(9, "Storage: error response (%s, %ld):", (error.failed ? "failed" : "interrupted"), error.httpResponseCode);
00047     debug(9, "%s", error.response.c_str());
00048 }
00049 
00050 Networking::Request *Storage::addRequest(Networking::Request *request) {
00051     _runningRequestsMutex.lock();
00052     ++_runningRequestsCount;
00053     if (_runningRequestsCount == 1)
00054         debug(9, "Storage is working now");
00055     _runningRequestsMutex.unlock();
00056     return ConnMan.addRequest(request, new Common::Callback<Storage, Networking::Request *>(this, &Storage::requestFinishedCallback));
00057 }
00058 
00059 void Storage::requestFinishedCallback(Networking::Request *invalidRequestPointer) {
00060     bool restartSync = false;
00061 
00062     _runningRequestsMutex.lock();
00063     if (invalidRequestPointer == _savesSyncRequest)
00064         _savesSyncRequest = nullptr;
00065     --_runningRequestsCount;
00066     if (_syncRestartRequestsed)
00067         restartSync = true;
00068     if (_runningRequestsCount == 0 && !restartSync)
00069         debug(9, "Storage is not working now");
00070     _runningRequestsMutex.unlock();
00071 
00072     if (restartSync)
00073         syncSaves(nullptr, nullptr);
00074 }
00075 
00076 Networking::Request *Storage::upload(Common::String remotePath, Common::String localPath, UploadCallback callback, Networking::ErrorCallback errorCallback) {
00077     if (!errorCallback) errorCallback = getErrorPrintingCallback();
00078 
00079     Common::File *f = new Common::File();
00080     if (!f->open(localPath)) {
00081         warning("Storage: unable to open file to upload from");
00082         if (errorCallback)
00083             (*errorCallback)(Networking::ErrorResponse(nullptr, false, true, "", -1));
00084         delete errorCallback;
00085         delete callback;
00086         delete f;
00087         return nullptr;
00088     }
00089 
00090     return upload(remotePath, f, callback, errorCallback);
00091 }
00092 
00093 bool Storage::uploadStreamSupported() {
00094     return true;
00095 }
00096 
00097 Networking::Request *Storage::streamFile(Common::String path, Networking::NetworkReadStreamCallback callback, Networking::ErrorCallback errorCallback) {
00098     //most Storages use paths instead of ids, so this should work
00099     return streamFileById(path, callback, errorCallback);
00100 }
00101 
00102 Networking::Request *Storage::download(Common::String remotePath, Common::String localPath, BoolCallback callback, Networking::ErrorCallback errorCallback) {
00103     //most Storages use paths instead of ids, so this should work
00104     return downloadById(remotePath, localPath, callback, errorCallback);
00105 }
00106 
00107 Networking::Request *Storage::downloadById(Common::String remoteId, Common::String localPath, BoolCallback callback, Networking::ErrorCallback errorCallback) {
00108     if (!errorCallback) errorCallback = getErrorPrintingCallback();
00109 
00110     Common::DumpFile *f = new Common::DumpFile();
00111     if (!f->open(localPath, true)) {
00112         warning("Storage: unable to open file to download into");
00113         if (errorCallback) (*errorCallback)(Networking::ErrorResponse(nullptr, false, true, "", -1));
00114         delete errorCallback;
00115         delete callback;
00116         delete f;
00117         return nullptr;
00118     }
00119 
00120     return addRequest(new DownloadRequest(this, callback, errorCallback, remoteId, f));
00121 }
00122 
00123 Networking::Request *Storage::downloadFolder(Common::String remotePath, Common::String localPath, FileArrayCallback callback, Networking::ErrorCallback errorCallback, bool recursive) {
00124     if (!errorCallback)
00125         errorCallback = getErrorPrintingCallback();
00126     return addRequest(new FolderDownloadRequest(this, callback, errorCallback, remotePath, localPath, recursive));
00127 }
00128 
00129 SavesSyncRequest *Storage::syncSaves(BoolCallback callback, Networking::ErrorCallback errorCallback) {
00130     _runningRequestsMutex.lock();
00131     if (_savesSyncRequest) {
00132         warning("Storage::syncSaves: there is a sync in progress already");
00133         _syncRestartRequestsed = true;
00134         _runningRequestsMutex.unlock();
00135         return _savesSyncRequest;
00136     }
00137     if (!callback)
00138         callback = new Common::Callback<Storage, BoolResponse>(this, &Storage::savesSyncDefaultCallback);
00139     if (!errorCallback)
00140         errorCallback = new Common::Callback<Storage, Networking::ErrorResponse>(this, &Storage::savesSyncDefaultErrorCallback);
00141     _savesSyncRequest = new SavesSyncRequest(this, callback, errorCallback);
00142     _syncRestartRequestsed = false;
00143     _runningRequestsMutex.unlock();
00144     return (SavesSyncRequest *)addRequest(_savesSyncRequest); //who knows what that ConnMan could return in the future
00145 }
00146 
00147 bool Storage::isWorking() {
00148     _runningRequestsMutex.lock();
00149     bool working = _runningRequestsCount > 0;
00150     _runningRequestsMutex.unlock();
00151     return working;
00152 }
00153 
00155 
00156 bool Storage::isSyncing() {
00157     _runningRequestsMutex.lock();
00158     bool syncing = _savesSyncRequest != nullptr;
00159     _runningRequestsMutex.unlock();
00160     return syncing;
00161 }
00162 
00163 double Storage::getSyncDownloadingProgress() {
00164     double result = 1;
00165     _runningRequestsMutex.lock();
00166     if (_savesSyncRequest)
00167         result = _savesSyncRequest->getDownloadingProgress();
00168     _runningRequestsMutex.unlock();
00169     return result;
00170 }
00171 
00172 double Storage::getSyncProgress() {
00173     double result = 1;
00174     _runningRequestsMutex.lock();
00175     if (_savesSyncRequest)
00176         result = _savesSyncRequest->getProgress();
00177     _runningRequestsMutex.unlock();
00178     return result;
00179 }
00180 
00181 Common::Array<Common::String> Storage::getSyncingFiles() {
00182     Common::Array<Common::String> result;
00183     _runningRequestsMutex.lock();
00184     if (_savesSyncRequest)
00185         result = _savesSyncRequest->getFilesToDownload();
00186     _runningRequestsMutex.unlock();
00187     return result;
00188 }
00189 
00190 void Storage::cancelSync() {
00191     _runningRequestsMutex.lock();
00192     if (_savesSyncRequest)
00193         _savesSyncRequest->finish();
00194     _runningRequestsMutex.unlock();
00195 }
00196 
00197 void Storage::setSyncTarget(GUI::CommandReceiver *target) {
00198     _runningRequestsMutex.lock();
00199     if (_savesSyncRequest)
00200         _savesSyncRequest->setTarget(target);
00201     _runningRequestsMutex.unlock();
00202 }
00203 
00204 void Storage::savesSyncDefaultCallback(BoolResponse response) {
00205     _runningRequestsMutex.lock();
00206     _savesSyncRequest = nullptr;
00207     _runningRequestsMutex.unlock();
00208 
00209     if (!response.value)
00210         warning("SavesSyncRequest called success callback with `false` argument");
00211     Common::OSDMessageQueue::instance().addMessage(_("Saved games sync complete."));
00212 }
00213 
00214 void Storage::savesSyncDefaultErrorCallback(Networking::ErrorResponse error) {
00215     _runningRequestsMutex.lock();
00216     _savesSyncRequest = nullptr;
00217     _runningRequestsMutex.unlock();
00218 
00219     printErrorResponse(error);
00220 
00221     if (error.interrupted)
00222         Common::OSDMessageQueue::instance().addMessage(_("Saved games sync was cancelled."));
00223     else
00224         Common::OSDMessageQueue::instance().addMessage(_("Saved games sync failed.\nCheck your Internet connection."));
00225 }
00226 
00228 
00229 bool Storage::startDownload(Common::String remotePath, Common::String localPath) {
00230     _runningRequestsMutex.lock();
00231     if (_downloadFolderRequest) {
00232         warning("Storage::startDownload: there is a download in progress already");
00233         _runningRequestsMutex.unlock();
00234         return false;
00235     }
00236     _downloadFolderRequest = (FolderDownloadRequest *)downloadFolder(
00237         remotePath, localPath,
00238         new Common::Callback<Storage, FileArrayResponse>(this, &Storage::directoryDownloadedCallback),
00239         new Common::Callback<Storage, Networking::ErrorResponse>(this, &Storage::directoryDownloadedErrorCallback),
00240         true
00241     );
00242     _runningRequestsMutex.unlock();
00243     return true;
00244 }
00245 
00246 void Storage::cancelDownload() {
00247     _runningRequestsMutex.lock();
00248     if (_downloadFolderRequest)
00249         _downloadFolderRequest->finish();
00250     _runningRequestsMutex.unlock();
00251 }
00252 
00253 void Storage::setDownloadTarget(GUI::CommandReceiver *target) {
00254     _runningRequestsMutex.lock();
00255     if (_downloadFolderRequest)
00256         _downloadFolderRequest->setTarget(target);
00257     _runningRequestsMutex.unlock();
00258 }
00259 
00260 bool Storage::isDownloading() {
00261     _runningRequestsMutex.lock();
00262     bool syncing = _downloadFolderRequest != nullptr;
00263     _runningRequestsMutex.unlock();
00264     return syncing;
00265 }
00266 
00267 double Storage::getDownloadingProgress() {
00268     double result = 1;
00269     _runningRequestsMutex.lock();
00270     if (_downloadFolderRequest)
00271         result = _downloadFolderRequest->getProgress();
00272     _runningRequestsMutex.unlock();
00273     return result;
00274 }
00275 
00276 uint64 Storage::getDownloadBytesNumber() {
00277     uint64 result = 0;
00278     _runningRequestsMutex.lock();
00279     if (_downloadFolderRequest)
00280         result = _downloadFolderRequest->getDownloadedBytes();
00281     _runningRequestsMutex.unlock();
00282     return result;
00283 }
00284 
00285 uint64 Storage::getDownloadTotalBytesNumber() {
00286     uint64 result = 0;
00287     _runningRequestsMutex.lock();
00288     if (_downloadFolderRequest)
00289         result = _downloadFolderRequest->getTotalBytesToDownload();
00290     _runningRequestsMutex.unlock();
00291     return result;
00292 }
00293 
00294 uint64 Storage::getDownloadSpeed() {
00295     uint64 result = 0;
00296     _runningRequestsMutex.lock();
00297     if (_downloadFolderRequest)
00298         result = _downloadFolderRequest->getDownloadSpeed();
00299     _runningRequestsMutex.unlock();
00300     return result;
00301 }
00302 
00303 Common::String Storage::getDownloadRemoteDirectory() {
00304     Common::String result = "";
00305     _runningRequestsMutex.lock();
00306     if (_downloadFolderRequest)
00307         result = _downloadFolderRequest->getRemotePath();
00308     _runningRequestsMutex.unlock();
00309     return result;
00310 }
00311 
00312 Common::String Storage::getDownloadLocalDirectory() {
00313     Common::String result = "";
00314     _runningRequestsMutex.lock();
00315     if (_downloadFolderRequest)
00316         result = _downloadFolderRequest->getLocalPath();
00317     _runningRequestsMutex.unlock();
00318     return result;
00319 }
00320 
00321 void Storage::directoryDownloadedCallback(FileArrayResponse response) {
00322     _runningRequestsMutex.lock();
00323     _downloadFolderRequest = nullptr;
00324     _runningRequestsMutex.unlock();
00325 
00326     Common::String message;
00327     if (response.value.size()) {
00328         message = Common::String::format(_("Download complete.\nFailed to download %u files."), response.value.size());
00329     } else {
00330         message = _("Download complete.");
00331     }
00332     Common::OSDMessageQueue::instance().addMessage(message.c_str());
00333 }
00334 
00335 void Storage::directoryDownloadedErrorCallback(Networking::ErrorResponse error) {
00336     _runningRequestsMutex.lock();
00337     _downloadFolderRequest = nullptr;
00338     _runningRequestsMutex.unlock();
00339 
00340     Common::OSDMessageQueue::instance().addMessage(_("Download failed."));
00341 }
00342 
00343 } // End of namespace Cloud


Generated on Sat Jul 13 2019 05:00:53 for ResidualVM by doxygen 1.7.1
curved edge   curved edge