From 072424395903889b514b0b96ebf4a83118acd036 Mon Sep 17 00:00:00 2001 From: Don BURNS Date: Mon, 30 Aug 2004 01:15:49 +0000 Subject: [PATCH] Added the local_cache_dir option to the net plug-in allowing for the keeping and populating of a local cache. With this option, the cache is checked first before fetching from the network. --- src/osgPlugins/net/GNUmakefile | 1 + src/osgPlugins/net/ReaderWriterNET.cpp | 58 ++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/osgPlugins/net/GNUmakefile b/src/osgPlugins/net/GNUmakefile index 7ae7d60f8..994004629 100644 --- a/src/osgPlugins/net/GNUmakefile +++ b/src/osgPlugins/net/GNUmakefile @@ -5,6 +5,7 @@ CXXFILES =\ ReaderWriterNET.cpp\ sockinet.cpp\ sockstream.cpp\ + makeDir.cpp\ LIBS += $(OSG_LIBS) $(OTHER_LIBS) diff --git a/src/osgPlugins/net/ReaderWriterNET.cpp b/src/osgPlugins/net/ReaderWriterNET.cpp index 3dc98577f..0dc85891d 100644 --- a/src/osgPlugins/net/ReaderWriterNET.cpp +++ b/src/osgPlugins/net/ReaderWriterNET.cpp @@ -1,18 +1,19 @@ #include +#include #include #include #include - #include #include #include +#include #include #include #include - #include "sockinet.h" +#include "makeDir.h" /* * Semantics: @@ -47,7 +48,8 @@ class NetReader : public osgDB::ReaderWriter virtual ReadResult readNode(const std::string& inFileName, const Options *options ) { std::string hostname; - std::string server_prefix; + std::string serverPrefix; + std::string localCacheDir; int port = 80; if (options) @@ -68,13 +70,22 @@ class NetReader : public osgDB::ReaderWriter port = atoi( opt.substr(index+1).c_str() ); } else if( opt.substr( 0, index ) == "server_prefix" || - opt.substr( 0, index ) == "server_prefix" ) + opt.substr( 0, index ) == "SERVER_PREFIX" || + opt.substr( 0, index ) == "prefix" || + opt.substr( 0, index ) == "PREFIX" ) { - server_prefix = opt.substr(index+1); + serverPrefix = opt.substr(index+1); + } + else if( opt.substr( 0, index ) == "local_cache_dir" || + opt.substr( 0, index ) == "LOCAL_CACHE_DIR" ) + { + localCacheDir = opt.substr(index+1); } } } + ReadResult readResult = ReadResult::FILE_NOT_HANDLED; + /* * we accept all extensions std::string ext = osgDB::getFileExtension(inFileName); if (!acceptsExtension(ext)) @@ -106,6 +117,30 @@ class NetReader : public osgDB::ReaderWriter fileName = fileName.substr( 0, rindex ); } + if( !serverPrefix.empty() ) + fileName = serverPrefix + '/' + fileName; + + // Invoke the reader corresponding to the extension + osgDB::ReaderWriter *reader = + osgDB::Registry::instance()->getReaderWriterForExtension( osgDB::getFileExtension(fileName)); + if( reader == 0L ) + return ReadResult::FILE_NOT_HANDLED; + + // Before we go to the network, lets see if it is in local cache, if cache + // was specified + if( !localCacheDir.empty() ) + { + std::string cacheFile = localCacheDir + '/' + fileName; + if( osgDB::fileExists( cacheFile )) + { + std::ifstream in(cacheFile.c_str()); + readResult = reader->readNode( in ); + in.close(); + return readResult; + } + } + + // Fetch from the network iosockinet sio (sockbuf::sock_stream); try { sio->connect( hostname.c_str(), port ); @@ -116,9 +151,6 @@ class NetReader : public osgDB::ReaderWriter return ReadResult::FILE_NOT_FOUND; } - if( !server_prefix.empty() ) - fileName = server_prefix + '/' + fileName; - sio << "GET /" << fileName << " HTTP/1.1\n" << "Host:\n\n"; sio.flush(); @@ -176,14 +208,22 @@ class NetReader : public osgDB::ReaderWriter } while( linebuff[0] != '\r' ); + /* // Invoke the reader corresponding to the extension osgDB::ReaderWriter *reader = osgDB::Registry::instance()->getReaderWriterForExtension( osgDB::getFileExtension(fileName)); + */ - ReadResult readResult = ReadResult::FILE_NOT_HANDLED; if( reader != 0L ) readResult = reader->readNode( sio ); + if( !localCacheDir.empty() ) + { + std::string cacheFile = localCacheDir + '/' + fileName; + if( TemporaryFileUtils::makeDirectory( cacheFile ) ) + osgDB::writeNodeFile( *(readResult.getNode()), cacheFile ); + } + return readResult; } };