From dfd68306de6f655a7bd2c68cea0b9299956ce8fc Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Thu, 14 Jul 2016 10:17:26 +0600 Subject: CLOUD: Upgrade FolderDownloadRequest::getProgress() Now NetworkReadStream, which is used in DownloadRequest, which is used in FolderDownloadRequest, returns progress information provided by libcurl. --- backends/cloud/downloadrequest.cpp | 6 ++++++ backends/cloud/downloadrequest.h | 3 +++ backends/cloud/folderdownloadrequest.cpp | 17 ++++++++++++++--- backends/cloud/folderdownloadrequest.h | 2 +- backends/cloud/id/iddownloadrequest.cpp | 7 +++++++ backends/cloud/id/iddownloadrequest.h | 3 +++ backends/networking/curl/networkreadstream.cpp | 26 +++++++++++++++++++++++++- backends/networking/curl/networkreadstream.h | 7 +++++++ 8 files changed, 66 insertions(+), 5 deletions(-) (limited to 'backends') diff --git a/backends/cloud/downloadrequest.cpp b/backends/cloud/downloadrequest.cpp index 43bb02a756..5efb87e0c8 100644 --- a/backends/cloud/downloadrequest.cpp +++ b/backends/cloud/downloadrequest.cpp @@ -122,4 +122,10 @@ void DownloadRequest::finishError(Networking::ErrorResponse error) { Request::finishError(error); } +double DownloadRequest::getProgress() const { + if (_remoteFileStream) + return _remoteFileStream->getProgress(); + return 0; +} + } // End of namespace Cloud diff --git a/backends/cloud/downloadrequest.h b/backends/cloud/downloadrequest.h index 99b7de37f0..138616a04b 100644 --- a/backends/cloud/downloadrequest.h +++ b/backends/cloud/downloadrequest.h @@ -54,6 +54,9 @@ public: virtual void handle(); virtual void restart(); + + /** Returns a number in range [0, 1], where 1 is "complete". */ + double getProgress() const; }; } // End of namespace Cloud diff --git a/backends/cloud/folderdownloadrequest.cpp b/backends/cloud/folderdownloadrequest.cpp index 6cf55b28cf..bbb3c646ac 100644 --- a/backends/cloud/folderdownloadrequest.cpp +++ b/backends/cloud/folderdownloadrequest.cpp @@ -21,6 +21,8 @@ */ #include "backends/cloud/folderdownloadrequest.h" +#include "backends/cloud/downloadrequest.h" +#include "backends/cloud/id/iddownloadrequest.h" #include "common/debug.h" #include "gui/downloaddialog.h" @@ -133,9 +135,18 @@ void FolderDownloadRequest::finishDownload(Common::Array &files) { if (_fileArrayCallback) (*_fileArrayCallback)(Storage::FileArrayResponse(this, files)); } -double FolderDownloadRequest::getProgress() { - if (_totalFiles == 0) return 0; - return (double)(_totalFiles - _files.size()) / (double)(_totalFiles); +double FolderDownloadRequest::getProgress() const { + if (_totalFiles == 0) return 0; + + double currentFileProgress = 0; + DownloadRequest *downloadRequest = dynamic_cast(_workingRequest); + if (downloadRequest != nullptr) currentFileProgress = downloadRequest->getProgress(); + else { + Id::IdDownloadRequest *idDownloadRequest = dynamic_cast(_workingRequest); + if (idDownloadRequest != nullptr) currentFileProgress = idDownloadRequest->getProgress(); + } + + return (double)(_totalFiles - _files.size() + currentFileProgress) / (double)(_totalFiles); } } // End of namespace Cloud diff --git a/backends/cloud/folderdownloadrequest.h b/backends/cloud/folderdownloadrequest.h index 41eacc2afe..a5f13b740b 100644 --- a/backends/cloud/folderdownloadrequest.h +++ b/backends/cloud/folderdownloadrequest.h @@ -56,7 +56,7 @@ public: virtual void restart(); /** Returns a number in range [0, 1], where 1 is "complete". */ - double getProgress(); + double getProgress() const; /** Returns remote directory path. */ Common::String getRemotePath() { return _remoteDirectoryPath; } diff --git a/backends/cloud/id/iddownloadrequest.cpp b/backends/cloud/id/iddownloadrequest.cpp index 154bd16edd..ac62284d46 100644 --- a/backends/cloud/id/iddownloadrequest.cpp +++ b/backends/cloud/id/iddownloadrequest.cpp @@ -22,6 +22,7 @@ #include "backends/cloud/id/iddownloadrequest.h" #include "backends/cloud/id/idstorage.h" +#include "backends/cloud/downloadrequest.h" namespace Cloud { namespace Id { @@ -87,5 +88,11 @@ void IdDownloadRequest::finishDownload(bool success) { if (_boolCallback) (*_boolCallback)(Storage::BoolResponse(this, success)); } +double IdDownloadRequest::getProgress() const { + DownloadRequest *downloadRequest = dynamic_cast(_workingRequest); + if (downloadRequest == nullptr) return 0.02; // resolving id still + return 0.1 + 0.9 * downloadRequest->getProgress(); // downloading +} + } // End of namespace Id } // End of namespace Cloud diff --git a/backends/cloud/id/iddownloadrequest.h b/backends/cloud/id/iddownloadrequest.h index 70397696a8..65e05c00b3 100644 --- a/backends/cloud/id/iddownloadrequest.h +++ b/backends/cloud/id/iddownloadrequest.h @@ -51,6 +51,9 @@ public: virtual void handle(); virtual void restart(); + + /** Returns a number in range [0, 1], where 1 is "complete". */ + double getProgress() const; }; } // End of namespace Id diff --git a/backends/networking/curl/networkreadstream.cpp b/backends/networking/curl/networkreadstream.cpp index 41839f79c9..9b34aaa3d5 100644 --- a/backends/networking/curl/networkreadstream.cpp +++ b/backends/networking/curl/networkreadstream.cpp @@ -47,10 +47,17 @@ static size_t curlHeadersCallback(char *d, size_t n, size_t l, void *p) { return 0; } +static int curlProgressCallback(void *p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) { + NetworkReadStream *stream = (NetworkReadStream *)p; + if (stream) stream->setProgress(dlnow, dltotal); + return 0; +} + void NetworkReadStream::init(const char *url, curl_slist *headersList, const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post) { _eos = _requestComplete = false; _sendingContentsBuffer = nullptr; _sendingContentsSize = _sendingContentsPos = 0; + _progressDownloaded = _progressTotal = 0; _easy = curl_easy_init(); curl_easy_setopt(_easy, CURLOPT_WRITEFUNCTION, curlDataCallback); @@ -62,7 +69,10 @@ void NetworkReadStream::init(const char *url, curl_slist *headersList, const byt curl_easy_setopt(_easy, CURLOPT_URL, url); curl_easy_setopt(_easy, CURLOPT_VERBOSE, 0L); curl_easy_setopt(_easy, CURLOPT_FOLLOWLOCATION, 1L); //probably it's OK to have it always on - curl_easy_setopt(_easy, CURLOPT_HTTPHEADER, headersList); + curl_easy_setopt(_easy, CURLOPT_HTTPHEADER, headersList); + curl_easy_setopt(_easy, CURLOPT_NOPROGRESS, 0L); + curl_easy_setopt(_easy, CURLOPT_XFERINFOFUNCTION, curlProgressCallback); + curl_easy_setopt(_easy, CURLOPT_XFERINFODATA, this); if (uploading) { curl_easy_setopt(_easy, CURLOPT_UPLOAD, 1L); curl_easy_setopt(_easy, CURLOPT_READDATA, this); @@ -84,6 +94,7 @@ void NetworkReadStream::init(const char *url, curl_slist *headersList, Common::H _eos = _requestComplete = false; _sendingContentsBuffer = nullptr; _sendingContentsSize = _sendingContentsPos = 0; + _progressDownloaded = _progressTotal = 0; _easy = curl_easy_init(); curl_easy_setopt(_easy, CURLOPT_WRITEFUNCTION, curlDataCallback); @@ -96,6 +107,9 @@ void NetworkReadStream::init(const char *url, curl_slist *headersList, Common::H curl_easy_setopt(_easy, CURLOPT_VERBOSE, 0L); curl_easy_setopt(_easy, CURLOPT_FOLLOWLOCATION, 1L); //probably it's OK to have it always on curl_easy_setopt(_easy, CURLOPT_HTTPHEADER, headersList); + curl_easy_setopt(_easy, CURLOPT_NOPROGRESS, 0L); + curl_easy_setopt(_easy, CURLOPT_XFERINFOFUNCTION, curlProgressCallback); + curl_easy_setopt(_easy, CURLOPT_XFERINFODATA, this); // set POST multipart upload form fields/files struct curl_httppost *formpost = NULL; @@ -201,4 +215,14 @@ uint32 NetworkReadStream::addResponseHeaders(char *buffer, uint32 size) { return size; } +double NetworkReadStream::getProgress() const { + if (_progressTotal < 1) return 0; + return (double)_progressDownloaded / (double)_progressTotal; +} + +void NetworkReadStream::setProgress(uint64 downloaded, uint64 total) { + _progressDownloaded = downloaded; + _progressTotal = total; +} + } // End of namespace Cloud diff --git a/backends/networking/curl/networkreadstream.h b/backends/networking/curl/networkreadstream.h index acd8eee1c9..a3221011ec 100644 --- a/backends/networking/curl/networkreadstream.h +++ b/backends/networking/curl/networkreadstream.h @@ -41,6 +41,7 @@ class NetworkReadStream: public Common::MemoryReadWriteStream { uint32 _sendingContentsSize; uint32 _sendingContentsPos; Common::String _responseHeaders; + uint64 _progressDownloaded, _progressTotal; void init(const char *url, curl_slist *headersList, const byte *buffer, uint32 bufferSize, bool uploading, bool usingPatch, bool post); void init(const char *url, curl_slist *headersList, Common::HashMap formFields, Common::HashMap formFiles); @@ -128,6 +129,12 @@ public: * @returns how many bytes were actually read */ uint32 addResponseHeaders(char *buffer, uint32 size); + + /** Returns a number in range [0, 1], where 1 is "complete". */ + double getProgress() const; + + /** Used in curl progress callback to pass current downloaded/total values. */ + void setProgress(uint64 downloaded, uint64 total); }; } // End of namespace Networking -- cgit v1.2.3