new files

This commit is contained in:
David Rose 2009-06-12 02:11:26 +00:00
parent 1b6814e7ac
commit 83938771a1
6 changed files with 438 additions and 0 deletions

85
direct/src/plugin/p3dDownload.I Executable file
View File

@ -0,0 +1,85 @@
// Filename: p3dDownload.I
// Created by: drose (11Jun09)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) Carnegie Mellon University. All rights reserved.
//
// All use of this software is subject to the terms of the revised BSD
// license. You should have received a copy of this license along
// with this source code in a file named "LICENSE."
//
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// Function: P3DDownload::get_url
// Access: Public
// Description: Returns the URL that we are querying.
////////////////////////////////////////////////////////////////////
const string &P3DDownload::
get_url() const {
return _url;
}
////////////////////////////////////////////////////////////////////
// Function: P3DDownload::get_download_progress
// Access: Public
// Description: Returns an indication of the progress through the
// download file, 0.0 to 1.0. Returns -1 if the size of
// the file is not known.
////////////////////////////////////////////////////////////////////
inline double P3DDownload::
get_download_progress() const {
if (_total_expected_data == 0) {
return -1.0;
}
return (double)_total_data / (double)_total_expected_data;
}
////////////////////////////////////////////////////////////////////
// Function: P3DDownload::get_download_finished
// Access: Public
// Description: Returns true if the download has finished, either
// successfully or otherwise, or false if it is still in
// progress.
////////////////////////////////////////////////////////////////////
inline bool P3DDownload::
get_download_finished() const {
return _status != P3D_RC_in_progress;
}
////////////////////////////////////////////////////////////////////
// Function: P3DDownload::get_download_success
// Access: Public
// Description: Returns true if the download has finished
// successfully, or false if it is still in progress or
// if it has failed.
////////////////////////////////////////////////////////////////////
inline bool P3DDownload::
get_download_success() const {
return _status == P3D_RC_done;
}
////////////////////////////////////////////////////////////////////
// Function: P3DDownload::set_download_id
// Access: Public
// Description: Called only by P3DInstance to set a unique ID for
// this particular download object.
////////////////////////////////////////////////////////////////////
inline void P3DDownload::
set_download_id(int download_id) {
_download_id = download_id;
}
////////////////////////////////////////////////////////////////////
// Function: P3DDownload::get_download_id
// Access: Public
// Description: Returns the unique ID set by the P3DInstance.
////////////////////////////////////////////////////////////////////
inline int P3DDownload::
get_download_id() const {
return _download_id;
}

123
direct/src/plugin/p3dDownload.cxx Executable file
View File

@ -0,0 +1,123 @@
// Filename: p3dDownload.cxx
// Created by: drose (11Jun09)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) Carnegie Mellon University. All rights reserved.
//
// All use of this software is subject to the terms of the revised BSD
// license. You should have received a copy of this license along
// with this source code in a file named "LICENSE."
//
////////////////////////////////////////////////////////////////////
#include "p3dDownload.h"
////////////////////////////////////////////////////////////////////
// Function: P3DDownload::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
P3DDownload::
P3DDownload() {
_status = P3D_RC_in_progress;
_http_status_code = 0;
_total_data = 0;
_total_expected_data = 0;
_download_id = 0;
}
////////////////////////////////////////////////////////////////////
// Function: P3DDownload::Destructor
// Access: Public, Virtual
// Description:
////////////////////////////////////////////////////////////////////
P3DDownload::
~P3DDownload() {
}
////////////////////////////////////////////////////////////////////
// Function: P3DDownload::set_url
// Access: Public
// Description: Supplies the source URL for the download.
////////////////////////////////////////////////////////////////////
void P3DDownload::
set_url(const string &url) {
_url = url;
}
////////////////////////////////////////////////////////////////////
// Function: P3DDownload::feed_url_stream
// Access: Public
// Description: Called by P3DInstance as more data arrives from the
// host.
////////////////////////////////////////////////////////////////////
bool P3DDownload::
feed_url_stream(P3D_result_code result_code,
int http_status_code,
size_t total_expected_data,
const unsigned char *this_data,
size_t this_data_size) {
_status = result_code;
_http_status_code = http_status_code;
if (this_data_size != 0) {
if (!receive_data(this_data, this_data_size)) {
// Failure writing to disk.
_status = P3D_RC_generic_error;
download_finished(false);
return false;
}
_total_data += this_data_size;
}
_total_expected_data = max(total_expected_data, _total_data);
download_progress();
if (_status != P3D_RC_in_progress) {
// We're done.
download_finished(get_download_success());
}
return true;
}
////////////////////////////////////////////////////////////////////
// Function: P3DDownload::receive_data
// Access: Protected, Virtual
// Description: Called as new data is downloaded. Returns true on
// success, false on failure.
////////////////////////////////////////////////////////////////////
bool P3DDownload::
receive_data(const unsigned char *this_data, size_t this_data_size) {
return false;
}
////////////////////////////////////////////////////////////////////
// Function: P3DDownload::download_progress
// Access: Protected, Virtual
// Description: Intended to be overloaded to generate an occasional
// callback as new data comes in.
////////////////////////////////////////////////////////////////////
void P3DDownload::
download_progress() {
cerr << "Downloading " << get_url() << ": "
<< int(get_download_progress() * 1000.0) / 10.0 << "\n";
}
////////////////////////////////////////////////////////////////////
// Function: P3DDownload::download_finished
// Access: Protected, Virtual
// Description: Intended to be overloaded to generate a callback
// when the download finishes, either successfully or
// otherwise. The bool parameter is true if the
// download was successful.
////////////////////////////////////////////////////////////////////
void P3DDownload::
download_finished(bool success) {
}

71
direct/src/plugin/p3dDownload.h Executable file
View File

@ -0,0 +1,71 @@
// Filename: p3dDownload.h
// Created by: drose (11Jun09)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) Carnegie Mellon University. All rights reserved.
//
// All use of this software is subject to the terms of the revised BSD
// license. You should have received a copy of this license along
// with this source code in a file named "LICENSE."
//
////////////////////////////////////////////////////////////////////
#ifndef P3DDOWNLOAD_H
#define P3DDOWNLOAD_H
#include "p3d_plugin_common.h"
////////////////////////////////////////////////////////////////////
// Class : P3DDownload
// Description : This represents a request to download a single file
// from a URL, with no particular destination. It is
// intended to be used as an abstract base class; to use
// it, subclass it and redefine the appropriate callback
// methods.
////////////////////////////////////////////////////////////////////
class P3DDownload {
public:
P3DDownload();
virtual ~P3DDownload();
void set_url(const string &url);
inline const string &get_url() const;
inline double get_download_progress() const;
inline bool get_download_finished() const;
inline bool get_download_success() const;
public:
// These are intended to be called only by P3DInstance.
inline void set_download_id(int download_id);
inline int get_download_id() const;
bool feed_url_stream(P3D_result_code result_code,
int http_status_code,
size_t total_expected_data,
const unsigned char *this_data,
size_t this_data_size);
protected:
virtual bool receive_data(const unsigned char *this_data,
size_t this_data_size);
virtual void download_progress();
virtual void download_finished(bool success);
protected:
P3D_result_code _status;
int _http_status_code;
size_t _total_data;
size_t _total_expected_data;
private:
int _download_id;
string _url;
};
#include "p3dDownload.I"
#endif

View File

@ -0,0 +1,24 @@
// Filename: p3dFileDownload.I
// Created by: drose (11Jun09)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) Carnegie Mellon University. All rights reserved.
//
// All use of this software is subject to the terms of the revised BSD
// license. You should have received a copy of this license along
// with this source code in a file named "LICENSE."
//
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
// Function: P3DFileDownload::get_filename
// Access: Public
// Description: Returns the filename that we are downloading into.
////////////////////////////////////////////////////////////////////
const string &P3DFileDownload::
get_filename() const {
return _filename;
}

View File

@ -0,0 +1,85 @@
// Filename: p3dFileDownload.cxx
// Created by: drose (11Jun09)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) Carnegie Mellon University. All rights reserved.
//
// All use of this software is subject to the terms of the revised BSD
// license. You should have received a copy of this license along
// with this source code in a file named "LICENSE."
//
////////////////////////////////////////////////////////////////////
#include "p3dFileDownload.h"
////////////////////////////////////////////////////////////////////
// Function: P3DFileDownload::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
P3DFileDownload::
P3DFileDownload() {
}
////////////////////////////////////////////////////////////////////
// Function: P3DFileDownload::set_filename
// Access: Public
// Description: Supplies the target local filename for the download.
// Returns true on success, false on failure.
////////////////////////////////////////////////////////////////////
bool P3DFileDownload::
set_filename(const string &filename) {
_filename = filename;
return open_file();
}
////////////////////////////////////////////////////////////////////
// Function: P3DFileDownload::open_file
// Access: Protected, Virtual
// Description: Opens the local file for receiving the download.
// Returns true on success, false on failure.
////////////////////////////////////////////////////////////////////
bool P3DFileDownload::
open_file() {
unlink(_filename.c_str());
_file.open(_filename.c_str(), ios::out | ios::trunc | ios::binary);
if (!_file) {
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////
// Function: P3DFileDownload::receive_data
// Access: Protected, Virtual
// Description: Called as new data is downloaded. Returns true on
// success, false on failure.
////////////////////////////////////////////////////////////////////
bool P3DFileDownload::
receive_data(const unsigned char *this_data, size_t this_data_size) {
_file.write((const char *)this_data, this_data_size);
if (!_file) {
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////
// Function: P3DDownload::download_finished
// Access: Protected, Virtual
// Description: Intended to be overloaded to generate a callback
// when the download finishes, either successfully or
// otherwise. The bool parameter is true if the
// download was successful.
////////////////////////////////////////////////////////////////////
void P3DFileDownload::
download_finished(bool success) {
P3DDownload::download_finished(success);
_file.close();
}

View File

@ -0,0 +1,50 @@
// Filename: p3dFileDownload.h
// Created by: drose (11Jun09)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) Carnegie Mellon University. All rights reserved.
//
// All use of this software is subject to the terms of the revised BSD
// license. You should have received a copy of this license along
// with this source code in a file named "LICENSE."
//
////////////////////////////////////////////////////////////////////
#ifndef P3DFILEDOWNLOAD_H
#define P3DFILEDOWNLOAD_H
#include "p3d_plugin_common.h"
#include "p3DDownload.h"
#include <fstream>
////////////////////////////////////////////////////////////////////
// Class : P3DFileDownload
// Description : This is a specialization on P3DDownload that
// specifically writes a disk file.
////////////////////////////////////////////////////////////////////
class P3DFileDownload : public P3DDownload {
public:
P3DFileDownload();
bool set_filename(const string &filename);
inline const string &get_filename() const;
protected:
virtual bool open_file();
virtual bool receive_data(const unsigned char *this_data,
size_t this_data_size);
virtual void download_finished(bool success);
protected:
ofstream _file;
private:
string _filename;
};
#include "p3dFileDownload.I"
#endif