From 42392f0d0c341b3ccd4b7827250169c2e6b6b3ac Mon Sep 17 00:00:00 2001 From: Mike Goslin Date: Wed, 13 Dec 2000 22:13:13 +0000 Subject: [PATCH] *** empty log message *** --- panda/src/downloader/extractor.cxx | 219 ++++++----------------------- panda/src/downloader/extractor.h | 34 +++-- 2 files changed, 64 insertions(+), 189 deletions(-) diff --git a/panda/src/downloader/extractor.cxx b/panda/src/downloader/extractor.cxx index 552d7d23b2..e9a45a34b7 100644 --- a/panda/src/downloader/extractor.cxx +++ b/panda/src/downloader/extractor.cxx @@ -9,43 +9,19 @@ #include "extractor.h" #include "config_downloader.h" -#include -#include -#include -#include #include //////////////////////////////////////////////////////////////////// // Defines //////////////////////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////// -// Class : ExtractorToken -// Description : Holds a request for the extractor. -//////////////////////////////////////////////////////////////////// -class ExtractorToken : public ReferenceCount { -public: - INLINE ExtractorToken(uint id, const Filename &source_file, - const string &event_name, - const Filename &rel_path) { - _id = id; - _source_file = source_file; - _event_name = event_name; - _rel_path = rel_path; - } - int _id; - Filename _source_file; - string _event_name; - Filename _rel_path; -}; - //////////////////////////////////////////////////////////////////// // Function: Extractor::Constructor // Access: Public // Description: //////////////////////////////////////////////////////////////////// Extractor:: -Extractor(void) : AsyncUtility() { +Extractor(void) { PT(Buffer) buffer = new Buffer(extractor_buffer_size); init(buffer); } @@ -56,7 +32,7 @@ Extractor(void) : AsyncUtility() { // Description: //////////////////////////////////////////////////////////////////// Extractor:: -Extractor(PT(Buffer) buffer) : AsyncUtility() { +Extractor(PT(Buffer) buffer) { init(buffer); } @@ -68,8 +44,6 @@ Extractor(PT(Buffer) buffer) : AsyncUtility() { void Extractor:: init(PT(Buffer) buffer) { nassertv(!buffer.is_null()); - _frequency = extractor_frequency; - _token_board = new ExtractorTokenBoard; _buffer = buffer; } @@ -80,172 +54,65 @@ init(PT(Buffer) buffer) { //////////////////////////////////////////////////////////////////// Extractor:: ~Extractor(void) { - destroy_thread(); - - delete _token_board; } //////////////////////////////////////////////////////////////////// -// Function: Extractor::request_extract +// Function: Extractor::initiate // Access: Public // Description: //////////////////////////////////////////////////////////////////// int Extractor:: -request_extract(const Filename &source_file, const string &event_name, - const Filename &rel_path) { - - PT(ExtractorToken) tok; - if (_threads_enabled) { - - // Make sure we actually are threaded - if (!_threaded) { - downloader_cat.info() - << "Extractor::request_extract() - create_thread() was " - << "never called! Calling it now..." << endl; - create_thread(); - } - - // We need to grab the lock in order to signal the condition variable -#ifdef HAVE_IPC - _lock.lock(); -#endif - - if (_token_board->_waiting.is_full()) { - downloader_cat.error() - << "Extractor::request_extract() - Too many pending requests\n"; - return 0; - } - - if (downloader_cat.is_debug()) { - downloader_cat.debug() - << "Extract requested for file: " << source_file << endl; - } - - tok = new ExtractorToken(_next_token++, source_file, event_name, - rel_path); - _token_board->_waiting.insert(tok); - -#ifdef HAVE_IPC - _request_cond->signal(); - _lock.unlock(); -#endif - - } else { - // If we're not running asynchronously, process the load request - // directly now. - if (_token_board->_waiting.is_full()) { - downloader_cat.error() - << "Extractor::request_extract() - Too many pending requests\n"; - return 0; - } - if (downloader_cat.is_debug()) { - downloader_cat.debug() - << "Extract requested for file: " << source_file << endl; - } - - tok = new ExtractorToken(_next_token++, source_file, event_name, - rel_path); - _token_board->_waiting.insert(tok); - process_request(); - } - - return tok->_id; -} - -//////////////////////////////////////////////////////////////////// -// Function: Extractor::process_request -// Access: Private -// Description: Serves any requests on the token board, moving them -// to the done queue. -//////////////////////////////////////////////////////////////////// -bool Extractor:: -process_request() { - if (_shutdown) { - if (downloader_cat.is_debug()) - downloader_cat.debug() - << "Extractor shutting down...\n"; - return false; - } - - // If there is actually a request token - process it - while (!_token_board->_waiting.is_empty()) { - PT(ExtractorToken) tok = _token_board->_waiting.extract(); - if (extract(tok->_source_file, tok->_rel_path)) { - _token_board->_done.insert(tok); - - // Throw a "done" event now. - if (!tok->_event_name.empty()) { - PT_Event done = new Event(tok->_event_name); - done->add_parameter(EventParameter((int)tok->_id)); - throw_event(done); - } - - if (downloader_cat.is_debug()) { - downloader_cat.debug() - << "Extractor::process_request() - extract complete for " - << tok->_source_file << "\n"; - } - } - } - - return true; -} - -//////////////////////////////////////////////////////////////////// -// Function: Extractor::extract -// Access: Public -// Description: -//////////////////////////////////////////////////////////////////// -bool Extractor:: -extract(Filename &source_file, const Filename &rel_path) { +initiate(Filename &source_file, const Filename &rel_path) { // Open source file - ifstream read_stream; - source_file.set_binary(); - if (!source_file.open_read(read_stream)) { + _source_file = source_file; + _source_file.set_binary(); + if (!_source_file.open_read(_read_stream)) { downloader_cat.error() << "Extractor::extract() - Error opening source file: " - << source_file << endl; - return false; + << _source_file << endl; + return ES_error_write; } - // Determine source file length - read_stream.seekg(0, ios::end); - int source_file_length = read_stream.tellg(); - read_stream.seekg(0, ios::beg); + _rel_path = rel_path; - // Read the multifile header - Multifile mfile; + // Determine source file length + _read_stream.seekg(0, ios::end); + _source_file_length = _read_stream.tellg(); + _read_stream.seekg(0, ios::beg); // Read from the source file and write to the appropriate extracted file - int total_bytes_read = 0; - bool read_all_input = false; - bool handled_all_input = false; - int source_buffer_length; - while (handled_all_input == false) { + _total_bytes_read = 0; + _read_all_input = false; + _handled_all_input = false; + return ES_success; +} - // See if there is anything left in the source file - if (read_all_input == false) { - read_stream.read(_buffer->_buffer, _buffer->get_length()); - source_buffer_length = read_stream.gcount(); - total_bytes_read += source_buffer_length; - if (read_stream.eof()) { - nassertr(total_bytes_read == source_file_length, false); - read_all_input = true; - } +//////////////////////////////////////////////////////////////////// +// Function: Extractor::run +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +int Extractor:: +run(void) { + // See if there is anything left in the source file + if (_read_all_input == false) { + _read_stream.read(_buffer->_buffer, _buffer->get_length()); + _source_buffer_length = _read_stream.gcount(); + _total_bytes_read += _source_buffer_length; + if (_read_stream.eof()) { + nassertr(_total_bytes_read == _source_file_length, false); + _read_all_input = true; } - - // Write to the out file - char *start = _buffer->_buffer; - int size = source_buffer_length; - if (mfile.write_extract(start, size, rel_path) == true) - handled_all_input = true; - - nap(); } - read_stream.close(); - source_file.unlink(); - - return true; + // Write to the out file + char *start = _buffer->_buffer; + int size = _source_buffer_length; + if (_mfile.write_extract(start, size, _rel_path) == true) { + _read_stream.close(); + _source_file.unlink(); + return ES_success; + } + return ES_ok; } diff --git a/panda/src/downloader/extractor.h b/panda/src/downloader/extractor.h index 35a4f5e5f4..6e6b3cd00e 100644 --- a/panda/src/downloader/extractor.h +++ b/panda/src/downloader/extractor.h @@ -11,36 +11,44 @@ //////////////////////////////////////////////////////////////////// #include #include -#include #include #include -#include "asyncUtility.h" - -class ExtractorToken; +#include //////////////////////////////////////////////////////////////////// // Class : Extractor // Description : //////////////////////////////////////////////////////////////////// -class EXPCL_PANDAEXPRESS Extractor : public AsyncUtility { +class EXPCL_PANDAEXPRESS Extractor { PUBLISHED: + enum ExtractorStatus { + ES_ok = 2, + ES_success = 1, + ES_error = -1, + ES_error_write = -2, + }; + Extractor(void); Extractor(PT(Buffer) buffer); virtual ~Extractor(void); - int request_extract(const Filename &source_file, - const string &event_name, const Filename &rel_path = ""); - - bool extract(Filename &source_file, const Filename &rel_path); + int initiate(Filename &source_file, const Filename &rel_path = ""); + int run(void); private: void init(PT(Buffer) buffer); - virtual bool process_request(void); - - typedef TokenBoard ExtractorTokenBoard; - ExtractorTokenBoard *_token_board; PT(Buffer) _buffer; + + ifstream _read_stream; + int _source_file_length; + Multifile _mfile; + int _total_bytes_read; + bool _read_all_input; + bool _handled_all_input; + int _source_buffer_length; + Filename _source_file; + Filename _rel_path; }; #endif