diff options
Diffstat (limited to 'backends/cloud/downloadrequest.cpp')
-rw-r--r-- | backends/cloud/downloadrequest.cpp | 68 |
1 files changed, 43 insertions, 25 deletions
diff --git a/backends/cloud/downloadrequest.cpp b/backends/cloud/downloadrequest.cpp index 8d5e244e45..7dde74f88d 100644 --- a/backends/cloud/downloadrequest.cpp +++ b/backends/cloud/downloadrequest.cpp @@ -27,31 +27,55 @@ namespace Cloud { -DownloadRequest::DownloadRequest(Storage *storage, Storage::BoolCallback callback, Common::String remoteFile, Common::DumpFile *dumpFile): - Request(0), _boolCallback(callback), _remoteFileStream(0), _localFile(dumpFile) { - storage->streamFile(remoteFile, new Common::Callback<DownloadRequest, Networking::NetworkReadStreamResponse>(this, &DownloadRequest::streamCallback)); +DownloadRequest::DownloadRequest(Storage *storage, Storage::BoolCallback callback, Networking::ErrorCallback ecb, Common::String remoteFile, Common::DumpFile *dumpFile): + Request(nullptr, ecb), _boolCallback(callback), _localFile(dumpFile), _remoteFileName(remoteFile), _storage(storage), + _remoteFileStream(nullptr), _workingRequest(nullptr), _ignoreCallback(false) { + start(); } -void DownloadRequest::streamCallback(Networking::NetworkReadStreamResponse pair) { - if (!pair.value) { - warning("DownloadRequest: no ReadStream passed"); - finish(); - return; - } +DownloadRequest::~DownloadRequest() { + _ignoreCallback = true; + if (_workingRequest) _workingRequest->finish(); + delete _boolCallback; + delete _localFile; +} + +void DownloadRequest::start() { + _ignoreCallback = true; + if (_workingRequest) _workingRequest->finish(); + _remoteFileStream = nullptr; + //TODO: reopen DumpFile + _ignoreCallback = false; + + _workingRequest = _storage->streamFile( + _remoteFileName, + new Common::Callback<DownloadRequest, Networking::NetworkReadStreamResponse>(this, &DownloadRequest::streamCallback), + new Common::Callback<DownloadRequest, Networking::ErrorResponse>(this, &DownloadRequest::streamErrorCallback) + ); +} - _remoteFileStream = (Networking::NetworkReadStream *)pair.value; +void DownloadRequest::streamCallback(Networking::NetworkReadStreamResponse response) { + _workingRequest = nullptr; + if (_ignoreCallback) return; + _remoteFileStream = (Networking::NetworkReadStream *)response.value; +} + +void DownloadRequest::streamErrorCallback(Networking::ErrorResponse error) { + _workingRequest = nullptr; + if (_ignoreCallback) return; + finishError(error); } void DownloadRequest::handle() { if (!_localFile) { warning("DownloadRequest: no file to write"); - finish(); + finishError(Networking::ErrorResponse(this, false, true, "", -1)); return; } if (!_localFile->isOpen()) { warning("DownloadRequest: failed to open file to write"); - finish(); + finishError(Networking::ErrorResponse(this, false, true, "", -1)); return; } @@ -67,7 +91,7 @@ void DownloadRequest::handle() { if (readBytes != 0) if (_localFile->write(buf, readBytes) != readBytes) { warning("DownloadRequest: unable to write all received bytes into output file"); - finish(); + finishError(Networking::ErrorResponse(this, false, true, "", -1)); return; } @@ -77,26 +101,20 @@ void DownloadRequest::handle() { //TODO: do something about it actually } - finishBool(_remoteFileStream->httpResponseCode() == 200); + finishSuccess(_remoteFileStream->httpResponseCode() == 200); _localFile->close(); //yes, I know it's closed automatically in ~DumpFile() } } void DownloadRequest::restart() { - //this request doesn't know anything about the _remoteFileStream it's reading - //thus, it can't restart it - warning("DownloadRequest: cannot be restarted"); - finish(); - //TODO: fix that -} - -void DownloadRequest::finish() { - finishBool(false); + warning("DownloadRequest: can't restart as there are no means to reopen DumpFile"); + finishError(Networking::ErrorResponse(this, false, true, "", -1)); + //start(); } -void DownloadRequest::finishBool(bool success) { - Request::finish(); +void DownloadRequest::finishSuccess(bool success) { + Request::finishSuccess(); if (_boolCallback) (*_boolCallback)(Storage::BoolResponse(this, success)); } |