From 5884c290fbb7e2d4be9c2b56f99046c58709e9b8 Mon Sep 17 00:00:00 2001 From: Alex Epifanov Date: Thu, 5 Apr 2018 20:07:39 +0300 Subject: [PATCH] Network: receive download progress callbacks with 'wantReceiveProgressCallbacks' flag. --- lib/commonAPI/coreapi/ext/Network.xml | 9 ++++ .../coreapi/ext/shared/NetworkImpl.cpp | 46 ++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/lib/commonAPI/coreapi/ext/Network.xml b/lib/commonAPI/coreapi/ext/Network.xml index 65b20ea0828..55559394a96 100644 --- a/lib/commonAPI/coreapi/ext/Network.xml +++ b/lib/commonAPI/coreapi/ext/Network.xml @@ -89,12 +89,18 @@ Be sure to review the [Ruby API Usage](/guide/api_ruby) guide for important info CreateFolders can automatically create the directory path. + + Set to true to receive progress callbacks with status 'progress_headers' and 'progress_data'. + + + Status of the callback can be 'ok', 'error', 'progress_headers' and 'progress_data'. + The body of the HTTP response. @@ -110,6 +116,9 @@ Be sure to review the [Ruby API Usage](/guide/api_ruby) guide for important info When overwriteFile is false and file exists, when error return and this flag set to true. + + Length of received content for 'progress_data' callback. + diff --git a/lib/commonAPI/coreapi/ext/shared/NetworkImpl.cpp b/lib/commonAPI/coreapi/ext/shared/NetworkImpl.cpp index 0aab827b8e2..b302143c7f4 100644 --- a/lib/commonAPI/coreapi/ext/shared/NetworkImpl.cpp +++ b/lib/commonAPI/coreapi/ext/shared/NetworkImpl.cpp @@ -261,7 +261,51 @@ void CNetworkImpl::downloadFile( const rho::Hashtable& bool overwriteFile = propertyMap.containsKey("overwriteFile") && (propertyMap.get("overwriteFile")=="true"); bool createFolders = propertyMap.containsKey("createFolders") && (propertyMap.get("createFolders")=="true"); - bool fileExists = false; + bool wantMoreCallbacks = propertyMap.containsKey( "wantReceiveProgressCallbacks" ) && (propertyMap.get( "wantReceiveProgressCallbacks" )=="true"); + bool fileExists = false; + + //inplace callback for download progress notifications + struct NetCallback : public net::INetRequestCallback + { + rho::apiGenerator::CMethodResult& m_result; + + NetCallback(); + NetCallback( rho::apiGenerator::CMethodResult& r ) : m_result(r) {} + + virtual void didReceiveResponse(NetResponse& resp, const Hashtable* headers) + { + Hashtable& mapRes = m_result.getStringHash(); + mapRes["status"] = "progress_headers"; + mapRes["http_error"] = convertToStringA(resp.getRespCode()); + if ( headers != 0 ) { + m_result.getStringHashL2()["headers"] = *headers; + } + + m_result.set(mapRes); + } + + virtual void didReceiveData(const char* data, int len) + { + (m_result.getStringHashL2()["headers"]).clear(); + + Hashtable& mapRes = m_result.getStringHash(); + mapRes["status"] = "progress_data"; + mapRes["length"] = convertToStringA( len ); + //mapRes["data"] = String( data, len ); + + m_result.set(mapRes); + } + + virtual void didFinishLoading() {} + virtual void didFail(NetResponse&) {} + }; + + NetCallback cb(oResult); + + if ( wantMoreCallbacks ) + { + reqWrapper.setCallback( &cb ); + } NetResponse resp = reqWrapper.pullFile( propertyMap.get("url"), propertyMap.get("filename"), NULL, &mapHeaders,overwriteFile,createFolders,&fileExists);