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

downloadrequest.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/downloadrequest.h"
00024 #include "backends/networking/curl/connectionmanager.h"
00025 #include "common/textconsole.h"
00026 
00027 namespace Cloud {
00028 
00029 DownloadRequest::DownloadRequest(Storage *storage, Storage::BoolCallback callback, Networking::ErrorCallback ecb, Common::String remoteFileId, Common::DumpFile *dumpFile):
00030     Request(nullptr, ecb), _boolCallback(callback), _localFile(dumpFile), _remoteFileId(remoteFileId), _storage(storage),
00031     _remoteFileStream(nullptr), _workingRequest(nullptr), _ignoreCallback(false), _buffer(new byte[DOWNLOAD_REQUEST_BUFFER_SIZE]) {
00032     start();
00033 }
00034 
00035 DownloadRequest::~DownloadRequest() {
00036     _ignoreCallback = true;
00037     if (_workingRequest)
00038         _workingRequest->finish();
00039     delete _boolCallback;
00040     delete _localFile;
00041     delete[] _buffer;
00042 }
00043 
00044 void DownloadRequest::start() {
00045     _ignoreCallback = true;
00046     if (_workingRequest)
00047         _workingRequest->finish();
00048     _remoteFileStream = nullptr;
00049     //TODO: add some way to reopen DumpFile, so DownloadRequest could be restarted
00050     _ignoreCallback = false;
00051 
00052     _workingRequest = _storage->streamFileById(
00053         _remoteFileId,
00054         new Common::Callback<DownloadRequest, Networking::NetworkReadStreamResponse>(this, &DownloadRequest::streamCallback),
00055         new Common::Callback<DownloadRequest, Networking::ErrorResponse>(this, &DownloadRequest::streamErrorCallback)
00056     );
00057 }
00058 
00059 void DownloadRequest::streamCallback(Networking::NetworkReadStreamResponse response) {
00060     _workingRequest = nullptr;
00061     if (_ignoreCallback)
00062         return;
00063     _remoteFileStream = (Networking::NetworkReadStream *)response.value;
00064 }
00065 
00066 void DownloadRequest::streamErrorCallback(Networking::ErrorResponse error) {
00067     _workingRequest = nullptr;
00068     if (_ignoreCallback)
00069         return;
00070     finishError(error);
00071 }
00072 
00073 void DownloadRequest::handle() {
00074     if (!_localFile) {
00075         warning("DownloadRequest: no file to write");
00076         finishError(Networking::ErrorResponse(this, false, true, "", -1));
00077         return;
00078     }
00079 
00080     if (!_localFile->isOpen()) {
00081         warning("DownloadRequest: failed to open file to write");
00082         finishError(Networking::ErrorResponse(this, false, true, "", -1));
00083         return;
00084     }
00085 
00086     if (!_remoteFileStream) {
00087         //waiting for callback
00088         return;
00089     }
00090 
00091     uint32 readBytes = _remoteFileStream->read(_buffer, DOWNLOAD_REQUEST_BUFFER_SIZE);
00092 
00093     if (readBytes != 0)
00094         if (_localFile->write(_buffer, readBytes) != readBytes) {
00095             warning("DownloadRequest: unable to write all received bytes into output file");
00096             finishError(Networking::ErrorResponse(this, false, true, "", -1));
00097             return;
00098         }
00099 
00100     if (_remoteFileStream->eos()) {
00101         if (_remoteFileStream->httpResponseCode() != 200) {
00102             warning("DownloadRequest: HTTP response code is not 200 OK (it's %ld)", _remoteFileStream->httpResponseCode());
00103             //TODO: do something about it actually
00104             // the problem is file's already downloaded, stream is over
00105             // so we can't return error message anymore
00106         }
00107 
00108         finishDownload(_remoteFileStream->httpResponseCode() == 200);
00109 
00110         _localFile->close(); //yes, I know it's closed automatically in ~DumpFile()
00111     }
00112 }
00113 
00114 void DownloadRequest::restart() {
00115     warning("DownloadRequest: can't restart as there are no means to reopen DumpFile");
00116     finishError(Networking::ErrorResponse(this, false, true, "", -1));
00117     //start();
00118 }
00119 
00120 void DownloadRequest::finishDownload(bool success) {
00121     Request::finishSuccess();
00122     if (_boolCallback)
00123         (*_boolCallback)(Storage::BoolResponse(this, success));
00124 }
00125 
00126 void DownloadRequest::finishError(Networking::ErrorResponse error) {
00127     if (_localFile)
00128         _localFile->close();
00129     Request::finishError(error);
00130 }
00131 
00132 double DownloadRequest::getProgress() const {
00133     if (_remoteFileStream)
00134         return _remoteFileStream->getProgress();
00135     return 0;
00136 }
00137 
00138 } // End of namespace Cloud


Generated on Sat Mar 16 2019 05:01:25 for ResidualVM by doxygen 1.7.1
curved edge   curved edge