From ddb0d6bd4ff4f6a9e57f27230832f38dc2b9c343 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sat, 13 Nov 2004 16:21:48 +0000 Subject: [PATCH] Added prelimarny support for reading archives via the .net plugin --- examples/osgarchive/osgarchive.cpp | 2 ++ examples/osgviewer/osgviewer.cpp | 13 +++++++-- include/osgDB/Archive | 5 +++- include/osgDB/ReaderWriter | 4 +++ src/osgPlugins/net/ReaderWriterNET.cpp | 37 +++++++++++++++++++----- src/osgPlugins/net/sockinet.h | 8 ++--- src/osgPlugins/osga/OSGA_Archive.cpp | 6 ++++ src/osgPlugins/osga/OSGA_Archive.h | 3 ++ src/osgPlugins/osga/ReaderWriterOSGA.cpp | 12 ++++++++ src/osgProducer/ViewerEventHandler.cpp | 2 +- 10 files changed, 75 insertions(+), 17 deletions(-) diff --git a/examples/osgarchive/osgarchive.cpp b/examples/osgarchive/osgarchive.cpp index 7c694c2c9..94b9150be 100644 --- a/examples/osgarchive/osgarchive.cpp +++ b/examples/osgarchive/osgarchive.cpp @@ -129,9 +129,11 @@ int main( int argc, char **argv ) itr!=files.end(); ++itr) { + std::cout<<"reading "<<*itr< obj = osgDB::readObjectFile(*itr); if (obj.valid()) { + std::cout<<" write to archive "<<*itr<writeObject(*obj, *itr); } } diff --git a/examples/osgviewer/osgviewer.cpp b/examples/osgviewer/osgviewer.cpp index 39875b170..6b3cfc4a4 100644 --- a/examples/osgviewer/osgviewer.cpp +++ b/examples/osgviewer/osgviewer.cpp @@ -26,7 +26,10 @@ int main( int argc, char **argv ) arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); arguments.getApplicationUsage()->addCommandLineOption("--image ","Load an image and render it on a quad"); arguments.getApplicationUsage()->addCommandLineOption("--dem ","Load an image/DEM and render it on a HeightField"); - arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); + arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display command line paramters"); + arguments.getApplicationUsage()->addCommandLineOption("--help-env","Display environmental variables available"); + arguments.getApplicationUsage()->addCommandLineOption("--help-keys","Display keyboard & mouse bindings available"); + arguments.getApplicationUsage()->addCommandLineOption("--help-all","Display all command line, env vars and keyboard & mouse bindigs."); // construct the viewer. @@ -39,9 +42,13 @@ int main( int argc, char **argv ) viewer.getUsage(*arguments.getApplicationUsage()); // if user request help write it out to cout. - if (arguments.read("-h") || arguments.read("--help")) + bool helpAll = arguments.read("--help-all"); + unsigned int helpType = ((helpAll || arguments.read("-h") || arguments.read("--help"))? osg::ApplicationUsage::COMMAND_LINE_OPTION : 0 ) | + ((helpAll || arguments.read("--help-env"))? osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE : 0 ) | + ((helpAll || arguments.read("--help-keys"))? osg::ApplicationUsage::KEYBOARD_MOUSE_BINDING : 0 ); + if (helpType) { - arguments.getApplicationUsage()->write(std::cout); + arguments.getApplicationUsage()->write(std::cout, helpType); return 1; } diff --git a/include/osgDB/Archive b/include/osgDB/Archive index 4e61d71e5..fd85806da 100644 --- a/include/osgDB/Archive +++ b/include/osgDB/Archive @@ -36,9 +36,12 @@ class OSGDB_EXPORT Archive : public ReaderWriter virtual bool acceptsExtension(const std::string& /*extension*/) { return true; } - /** open the archive.*/ + /** open the archive for reading, writing or to create an empty archive for writing to.*/ virtual bool open(const std::string& filename, ArchiveStatus status, unsigned int indexBlockSizeHint=4096) = 0; + /** open the archive for reading.*/ + virtual bool open(std::istream& fin) = 0; + /** close the archive.*/ virtual void close() = 0; diff --git a/include/osgDB/ReaderWriter b/include/osgDB/ReaderWriter index 0fa81e846..3cd394453 100644 --- a/include/osgDB/ReaderWriter +++ b/include/osgDB/ReaderWriter @@ -161,8 +161,12 @@ class OSGDB_EXPORT ReaderWriter : public osg::Object CREATE }; + /** open an archive for reading, writing or or to create an empty archive for writing to.*/ virtual ReadResult openArchive(const std::string& /*fileName*/,ArchiveStatus, unsigned int =4096, const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } + /** open an archive for reading.*/ + virtual ReadResult openArchive(std::istream& /*fin*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } + virtual ReadResult readObject(const std::string& /*fileName*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } virtual ReadResult readImage(const std::string& /*fileName*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } virtual ReadResult readHeightField(const std::string& /*fileName*/,const Options* =NULL) { return ReadResult(ReadResult::FILE_NOT_HANDLED); } diff --git a/src/osgPlugins/net/ReaderWriterNET.cpp b/src/osgPlugins/net/ReaderWriterNET.cpp index 396113583..973919f36 100644 --- a/src/osgPlugins/net/ReaderWriterNET.cpp +++ b/src/osgPlugins/net/ReaderWriterNET.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -85,11 +86,18 @@ class NetReader : public osgDB::ReaderWriter enum ObjectType { OBJECT, + ARCHIVE, IMAGE, HEIGHTFIELD, NODE }; + virtual ReadResult openArchive(const std::string& fileName,ArchiveStatus status, unsigned int , const Options* options) + { + if (status!=READ) return ReadResult(ReadResult::FILE_NOT_HANDLED); + else return readFile(ARCHIVE,fileName,options); + } + virtual ReadResult readObject(const std::string& fileName, const Options* options) { return readFile(OBJECT,fileName,options); @@ -115,19 +123,20 @@ class NetReader : public osgDB::ReaderWriter switch(objectType) { case(OBJECT): return rw->readObject(fin,options); + case(ARCHIVE): return rw->openArchive(fin,options); case(IMAGE): return rw->readImage(fin,options); case(HEIGHTFIELD): return rw->readHeightField(fin,options); case(NODE): return rw->readNode(fin,options); default: break; } - return ReadResult::FILE_NOT_HANDLED; + return ReadResult::FILE_NOT_HANDLED; } virtual ReadResult readFile(ObjectType objectType, const std::string& inFileName, const Options *options) { osg::Timer_t start = osg::Timer::instance()->tick(); - osg::notify(osg::DEBUG_INFO) << "osgPlugin .net: start load" << inFileName << std::endl; + osg::notify(osg::NOTICE) << "osgPlugin .net: start load" << inFileName << std::endl; std::string hostname; std::string serverPrefix; @@ -196,6 +205,7 @@ class NetReader : public osgDB::ReaderWriter return ReadResult::FILE_NOT_HANDLED; */ + std::string fileName; int index = inFileName.find(":"); // If we haven't been given a hostname as an option @@ -224,6 +234,10 @@ class NetReader : public osgDB::ReaderWriter if( !serverPrefix.empty() ) fileName = serverPrefix + '/' + fileName; + osg::notify(osg::WARN) << "hostname " << hostname << std::endl; + osg::notify(osg::WARN) << "filename " << fileName << std::endl; + + // Invoke the reader corresponding to the extension osgDB::ReaderWriter *reader = osgDB::Registry::instance()->getReaderWriterForExtension( osgDB::getFileExtension(fileName)); @@ -248,9 +262,9 @@ class NetReader : public osgDB::ReaderWriter } // Fetch from the network - iosockinet sio (sockbuf::sock_stream); + osg::ref_ptr sio = new iosockinet(sockbuf::sock_stream); try { - sio->connect( hostname.c_str(), port ); + sio->rdbuf()->connect( hostname.c_str(), port ); } catch( sockerr e ) { @@ -258,13 +272,13 @@ class NetReader : public osgDB::ReaderWriter return ReadResult::FILE_NOT_FOUND; } - sio << "GET /" << fileName << " HTTP/1.1\n" << "Host:\n\n"; - sio.flush(); + *sio << "GET /" << fileName << " HTTP/1.1\n" << "Host:\n\n"; + sio->flush(); char linebuff[256]; do { - sio.getline( linebuff, sizeof( linebuff )); + sio->getline( linebuff, sizeof( linebuff )); std::istringstream iss(linebuff); std::string directive; @@ -329,13 +343,20 @@ class NetReader : public osgDB::ReaderWriter if( reader != 0L ) - readResult = readFile(objectType, reader, sio, local_opt.get() ); + readResult = readFile(objectType, reader, *sio, local_opt.get() ); double ms = osg::Timer::instance()->delta_m(start,osg::Timer::instance()->tick()); osg::notify(osg::DEBUG_INFO) << "osgPlugin .net: " << fileName << " fetched from server. in" << ms <<" ms"<< std::endl; + if (objectType==ARCHIVE && readResult.validArchive()) + { + // attach the socket istream to the archive to keep it alive. + osgDB::Archive* archive = readResult.getArchive(); + archive->setUserData(sio.get()); + } + if( !localCacheDir.empty() && cacheMode & Write ) { std::string cacheFile = localCacheDir + '/' + fileName; diff --git a/src/osgPlugins/net/sockinet.h b/src/osgPlugins/net/sockinet.h index 35d3495cf..23f66624f 100644 --- a/src/osgPlugins/net/sockinet.h +++ b/src/osgPlugins/net/sockinet.h @@ -11,6 +11,8 @@ #ifndef _SOCKINET_H #define _SOCKINET_H +#include + #include "sockstream.h" #if defined(__CYGWIN__) || !defined(WIN32) @@ -112,7 +114,7 @@ class isockinet: public isockstream sockinetbuf* operator -> () { return rdbuf (); } }; -class osockinet: public osockstream +class osockinet: public osg::Referenced, public osockstream { public: osockinet (const sockbuf::sockdesc& sd); @@ -121,10 +123,9 @@ class osockinet: public osockstream ~osockinet (); sockinetbuf* rdbuf () { return (sockinetbuf*)ios::rdbuf (); } - sockinetbuf* operator -> () { return rdbuf (); } }; -class iosockinet: public iosockstream +class iosockinet: public osg::Referenced, public iosockstream { public: iosockinet (const sockbuf::sockdesc& sd); @@ -133,7 +134,6 @@ class iosockinet: public iosockstream ~iosockinet (); sockinetbuf* rdbuf () { return (sockinetbuf*)ios::rdbuf (); } - sockinetbuf* operator -> () { return rdbuf (); } }; #endif // _SOCKINET_H diff --git a/src/osgPlugins/osga/OSGA_Archive.cpp b/src/osgPlugins/osga/OSGA_Archive.cpp index 5f2ae39b2..81a9b7db1 100644 --- a/src/osgPlugins/osga/OSGA_Archive.cpp +++ b/src/osgPlugins/osga/OSGA_Archive.cpp @@ -353,6 +353,12 @@ bool OSGA_Archive::open(const std::string& filename, ArchiveStatus status, unsig } } +bool OSGA_Archive::open(std::istream& fin) +{ + osg::notify(osg::NOTICE)<<"OSGA_Archive::open"< archive = new OSGA_Archive; + if (!archive->open(fin)) + { + return ReadResult(ReadResult::FILE_NOT_HANDLED); + } + + return archive.get(); + } + virtual ReadResult readImage(const std::string& file,const Options* options) { ReadResult result = openArchive(file,osgDB::Archive::READ); diff --git a/src/osgProducer/ViewerEventHandler.cpp b/src/osgProducer/ViewerEventHandler.cpp index d553a5759..3f2c4709a 100644 --- a/src/osgProducer/ViewerEventHandler.cpp +++ b/src/osgProducer/ViewerEventHandler.cpp @@ -981,7 +981,7 @@ void ViewerEventHandler::getUsage(osg::ApplicationUsage& usage) const usage.addKeyboardMouseBinding("f","Toggle fullscreen"); usage.addKeyboardMouseBinding("h","Display help"); usage.addKeyboardMouseBinding("o","Write scene graph to \"saved_model.osg\""); - usage.addKeyboardMouseBinding("O PrtSrn","Write camera images to \"saved_image*.rgb\""); + usage.addKeyboardMouseBinding("O PrtSrn","Write camera images to \"saved_image*.jpg\""); usage.addKeyboardMouseBinding("s","Toggle instrumention"); usage.addKeyboardMouseBinding("v","Toggle block and vsync"); usage.addKeyboardMouseBinding("z","Start recording camera path.");