diff --git a/examples/utils/urlFileLoaderExample/bin/data/.gitkeep b/examples/utils/urlFileLoaderExample/bin/data/.gitkeep new file mode 100644 index 00000000000..e28f0318d12 --- /dev/null +++ b/examples/utils/urlFileLoaderExample/bin/data/.gitkeep @@ -0,0 +1 @@ +kepp folder in git diff --git a/examples/utils/urlFileLoaderExample/src/main.cpp b/examples/utils/urlFileLoaderExample/src/main.cpp new file mode 100644 index 00000000000..6a32c6ae8bc --- /dev/null +++ b/examples/utils/urlFileLoaderExample/src/main.cpp @@ -0,0 +1,16 @@ +#include "ofMain.h" +#include "testApp.h" +#include "ofAppGlutWindow.h" + +//======================================================================== +int main( ){ + + ofAppGlutWindow window; + ofSetupOpenGL(&window, 1024,768, OF_WINDOW); // <-------- setup the GL context + + // this kicks off the running of my app + // can be OF_WINDOW or OF_FULLSCREEN + // pass in width and height too: + ofRunApp( new testApp()); + +} diff --git a/examples/utils/urlFileLoaderExample/src/testApp.cpp b/examples/utils/urlFileLoaderExample/src/testApp.cpp new file mode 100644 index 00000000000..9364c381f56 --- /dev/null +++ b/examples/utils/urlFileLoaderExample/src/testApp.cpp @@ -0,0 +1,98 @@ +#include "testApp.h" + +//-------------------------------------------------------------- +void testApp::setup(){ + progress = 0; + done = false; + + // synchronous loading + html = ofLoadURL("http://www.openframeworks.cc").data; + + // add listeners + asynchronous saving to disk + ofAddListener(ofURLProgressEvent(),this,&testApp::onProgress); + ofAddListener(ofURLResponseEvent(),this,&testApp::onFinished); + ofSaveURLAsync("http://www.openframeworks.cc/ofvideo.mov","ofvideo.mov"); +} + + +//-------------------------------------------------------------- +void testApp::onProgress(ofHttpResponse & response){ + progress = response.getProgressPct(); +} + +//-------------------------------------------------------------- +void testApp::onFinished(ofHttpResponse & response){ + done = true; + video.loadMovie(response.request.name); + video.play(); +} + +//-------------------------------------------------------------- +void testApp::update(){ + if(done) video.update(); +} + +//-------------------------------------------------------------- +void testApp::draw(){ + if(!done){ + if(progress>-1){ + ofNoFill(); + ofRect(20,20,100,20); + ofFill(); + ofRect(20,20,progress*100,20); + }else{ + ofDrawBitmapString("can't retrieve full size of download, downloading...", 20,20); + } + }else{ + ofDrawBitmapString("downloaded!", 20,20); + video.draw(20,40); + } + + + ofDrawBitmapString(html, 500,20); +} + +//-------------------------------------------------------------- +void testApp::keyPressed(int key){ + +} + +//-------------------------------------------------------------- +void testApp::keyReleased(int key){ + +} + +//-------------------------------------------------------------- +void testApp::mouseMoved(int x, int y ){ + +} + +//-------------------------------------------------------------- +void testApp::mouseDragged(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void testApp::mousePressed(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void testApp::mouseReleased(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void testApp::windowResized(int w, int h){ + +} + +//-------------------------------------------------------------- +void testApp::gotMessage(ofMessage msg){ + +} + +//-------------------------------------------------------------- +void testApp::dragEvent(ofDragInfo dragInfo){ + +} diff --git a/examples/utils/urlFileLoaderExample/src/testApp.h b/examples/utils/urlFileLoaderExample/src/testApp.h new file mode 100644 index 00000000000..b9d7eb6ed4f --- /dev/null +++ b/examples/utils/urlFileLoaderExample/src/testApp.h @@ -0,0 +1,29 @@ +#pragma once + +#include "ofMain.h" + +class testApp : public ofBaseApp{ + + public: + void setup(); + void update(); + void draw(); + + void keyPressed (int key); + void keyReleased(int key); + void mouseMoved(int x, int y ); + void mouseDragged(int x, int y, int button); + void mousePressed(int x, int y, int button); + void mouseReleased(int x, int y, int button); + void windowResized(int w, int h); + void dragEvent(ofDragInfo dragInfo); + void gotMessage(ofMessage msg); + + void onProgress(ofHttpResponse & response); + void onFinished(ofHttpResponse & response); + + float progress; + bool done; + string html; + ofVideoPlayer video; +}; diff --git a/libs/openFrameworks/utils/ofURLFileLoader.cpp b/libs/openFrameworks/utils/ofURLFileLoader.cpp index e1ae5cfd2f5..9b55a173774 100644 --- a/libs/openFrameworks/utils/ofURLFileLoader.cpp +++ b/libs/openFrameworks/utils/ofURLFileLoader.cpp @@ -31,6 +31,11 @@ ofEvent & ofURLResponseEvent(){ return *event; } +ofEvent & ofURLProgressEvent(){ + static ofEvent * event = new ofEvent; + return *event; +} + ofURLFileLoader::ofURLFileLoader() { if(!factoryLoaded){ try { @@ -168,35 +173,45 @@ ofHttpResponse ofURLFileLoader::handleRequest(ofHttpRequest request) { rs = &httpSession->receiveResponse(res); session = ofPtr(httpSession); } + + downloading = ofHttpResponse(request,res.getStatus(),res.getReason(),res.getContentLength()); + prevProgress = 0; + downloading.downloading = true; if(!request.saveTo){ - return ofHttpResponse(request,*rs,res.getStatus(),res.getReason()); + downloading.data.set(*rs); + downloading.downloading = false; + return downloading;//ofHttpResponse(request,*rs,res.getStatus(),res.getReason(),res.getContentLength()); }else{ ofFile saveTo(request.name,ofFile::WriteOnly,true); char aux_buffer[1024]; rs->read(aux_buffer, 1024); std::streamsize n = rs->gcount(); - while (n > 0){ + downloading.downloaded += n; + while (downloading.downloaded < downloading.size){ // we resize to size+1 initialized to 0 to have a 0 at the end for strings - saveTo.write(aux_buffer,n); + if(n>0) saveTo.write(aux_buffer,n); if (rs->good()){ rs->read(aux_buffer, 1024); n = rs->gcount(); - } - else n = 0; + downloading.downloaded += n; + }else{ + n = 0; + } } - return ofHttpResponse(request,res.getStatus(),res.getReason()); + downloading.downloading = false; + return downloading; } } catch (const Exception& exc) { ofLog(OF_LOG_ERROR, "ofURLFileLoader " + exc.displayText()); - return ofHttpResponse(request,-1,exc.displayText()); + return ofHttpResponse(request,-1,exc.displayText(),0); } catch (...) { - return ofHttpResponse(request,-1,"ofURLFileLoader fatal error, couldn't catch Exception"); + return ofHttpResponse(request,-1,"ofURLFileLoader fatal error, couldn't catch Exception",0); } - return ofHttpResponse(request,-1,"ofURLFileLoader fatal error, couldn't catch Exception"); + return ofHttpResponse(request,-1,"ofURLFileLoader fatal error, couldn't catch Exception",0); } @@ -207,11 +222,18 @@ void ofURLFileLoader::update(ofEventArgs & args){ ofLog(OF_LOG_VERBOSE,"ofURLLoader::update: new response " +response.request.name); responses.pop(); unlock(); - ofNotifyEvent(ofURLResponseEvent(),response); + ofNotifyEvent(ofURLProgressEvent(), response, this); + ofNotifyEvent(ofURLResponseEvent(), response , this); lock(); } unlock(); - + if(downloading.downloading){ + int currentProgress = downloading.getDownloaded(); + if(prevProgress!=currentProgress){ + ofNotifyEvent(ofURLProgressEvent(), downloading, this); + prevProgress = currentProgress; + } + } } static ofURLFileLoader & getFileLoader(){ diff --git a/libs/openFrameworks/utils/ofURLFileLoader.h b/libs/openFrameworks/utils/ofURLFileLoader.h index b9c8cfe60c7..cd94bd4d465 100644 --- a/libs/openFrameworks/utils/ofURLFileLoader.h +++ b/libs/openFrameworks/utils/ofURLFileLoader.h @@ -34,25 +34,52 @@ class ofHttpResponse{ public: ofHttpResponse(){} - ofHttpResponse(ofHttpRequest request,const ofBuffer & data,int status, string error) + ofHttpResponse(ofHttpRequest request,const ofBuffer & data,int status, string error, int size) :request(request) ,data(data) ,status(status) - ,error(error){} + ,error(error) + ,size(size) + ,downloaded(data.size()) + ,downloading(false){} - ofHttpResponse(ofHttpRequest request,int status,string error) + ofHttpResponse(ofHttpRequest request,int status,string error, int size) :request(request) ,status(status) - ,error(error){} + ,error(error) + ,size(size) + ,downloaded(0) + ,downloading(false){} operator ofBuffer&(){ return data; } + + int getDownloaded(){ + if(request.saveTo){ + return downloaded; + }else{ + return data.size(); + } + } + + float getProgressPct(){ + if(size!=0){ + return getDownloaded()/float(size); + }else{ + return -1; + } + } ofHttpRequest request; ofBuffer data; int status; string error; + int size; + bool downloading; +private: + friend class ofURLFileLoader; + int downloaded; }; ofHttpResponse ofLoadURL(string url); @@ -63,6 +90,7 @@ void ofRemoveURLRequest(int id); void ofRemoveAllURLRequests(); ofEvent & ofURLResponseEvent(); +ofEvent & ofURLProgressEvent(); template void ofRegisterURLNotification(T * obj){ @@ -102,6 +130,8 @@ class ofURLFileLoader : public ofThread { deque requests; queue responses; + ofHttpResponse downloading; + int prevProgress; Poco::Condition condition;