Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/utils/urlFileLoaderExample/bin/data/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kepp folder in git
16 changes: 16 additions & 0 deletions examples/utils/urlFileLoaderExample/src/main.cpp
Original file line number Diff line number Diff line change
@@ -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());

}
98 changes: 98 additions & 0 deletions examples/utils/urlFileLoaderExample/src/testApp.cpp
Original file line number Diff line number Diff line change
@@ -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){

}
29 changes: 29 additions & 0 deletions examples/utils/urlFileLoaderExample/src/testApp.h
Original file line number Diff line number Diff line change
@@ -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;
};
44 changes: 33 additions & 11 deletions libs/openFrameworks/utils/ofURLFileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ ofEvent<ofHttpResponse> & ofURLResponseEvent(){
return *event;
}

ofEvent<ofHttpResponse> & ofURLProgressEvent(){
static ofEvent<ofHttpResponse> * event = new ofEvent<ofHttpResponse>;
return *event;
}

ofURLFileLoader::ofURLFileLoader() {
if(!factoryLoaded){
try {
Expand Down Expand Up @@ -168,35 +173,45 @@ ofHttpResponse ofURLFileLoader::handleRequest(ofHttpRequest request) {
rs = &httpSession->receiveResponse(res);
session = ofPtr<HTTPSession>(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);

}

Expand All @@ -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(){
Expand Down
38 changes: 34 additions & 4 deletions libs/openFrameworks/utils/ofURLFileLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -63,6 +90,7 @@ void ofRemoveURLRequest(int id);
void ofRemoveAllURLRequests();

ofEvent<ofHttpResponse> & ofURLResponseEvent();
ofEvent<ofHttpResponse> & ofURLProgressEvent();

template<class T>
void ofRegisterURLNotification(T * obj){
Expand Down Expand Up @@ -102,6 +130,8 @@ class ofURLFileLoader : public ofThread {

deque<ofHttpRequest> requests;
queue<ofHttpResponse> responses;
ofHttpResponse downloading;
int prevProgress;

Poco::Condition condition;

Expand Down