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.
This commit is contained in:
parent
6a82bb28fc
commit
0724243959
@ -5,6 +5,7 @@ CXXFILES =\
|
|||||||
ReaderWriterNET.cpp\
|
ReaderWriterNET.cpp\
|
||||||
sockinet.cpp\
|
sockinet.cpp\
|
||||||
sockstream.cpp\
|
sockstream.cpp\
|
||||||
|
makeDir.cpp\
|
||||||
|
|
||||||
LIBS += $(OSG_LIBS) $(OTHER_LIBS)
|
LIBS += $(OSG_LIBS) $(OTHER_LIBS)
|
||||||
|
|
||||||
|
@ -1,18 +1,19 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include <osg/Notify>
|
#include <osg/Notify>
|
||||||
|
|
||||||
#include <osgDB/Input>
|
#include <osgDB/Input>
|
||||||
|
|
||||||
#include <osgDB/Registry>
|
#include <osgDB/Registry>
|
||||||
#include <osgDB/FileNameUtils>
|
#include <osgDB/FileNameUtils>
|
||||||
#include <osgDB/ReadFile>
|
#include <osgDB/ReadFile>
|
||||||
|
#include <osgDB/WriteFile>
|
||||||
#include <osgDB/FileUtils>
|
#include <osgDB/FileUtils>
|
||||||
#include <osg/MatrixTransform>
|
#include <osg/MatrixTransform>
|
||||||
#include <osg/Group>
|
#include <osg/Group>
|
||||||
|
|
||||||
#include "sockinet.h"
|
#include "sockinet.h"
|
||||||
|
#include "makeDir.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Semantics:
|
* Semantics:
|
||||||
@ -47,7 +48,8 @@ class NetReader : public osgDB::ReaderWriter
|
|||||||
virtual ReadResult readNode(const std::string& inFileName, const Options *options )
|
virtual ReadResult readNode(const std::string& inFileName, const Options *options )
|
||||||
{
|
{
|
||||||
std::string hostname;
|
std::string hostname;
|
||||||
std::string server_prefix;
|
std::string serverPrefix;
|
||||||
|
std::string localCacheDir;
|
||||||
int port = 80;
|
int port = 80;
|
||||||
|
|
||||||
if (options)
|
if (options)
|
||||||
@ -68,13 +70,22 @@ class NetReader : public osgDB::ReaderWriter
|
|||||||
port = atoi( opt.substr(index+1).c_str() );
|
port = atoi( opt.substr(index+1).c_str() );
|
||||||
}
|
}
|
||||||
else if( opt.substr( 0, index ) == "server_prefix" ||
|
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
|
/* * we accept all extensions
|
||||||
std::string ext = osgDB::getFileExtension(inFileName);
|
std::string ext = osgDB::getFileExtension(inFileName);
|
||||||
if (!acceptsExtension(ext))
|
if (!acceptsExtension(ext))
|
||||||
@ -106,6 +117,30 @@ class NetReader : public osgDB::ReaderWriter
|
|||||||
fileName = fileName.substr( 0, rindex );
|
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);
|
iosockinet sio (sockbuf::sock_stream);
|
||||||
try {
|
try {
|
||||||
sio->connect( hostname.c_str(), port );
|
sio->connect( hostname.c_str(), port );
|
||||||
@ -116,9 +151,6 @@ class NetReader : public osgDB::ReaderWriter
|
|||||||
return ReadResult::FILE_NOT_FOUND;
|
return ReadResult::FILE_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( !server_prefix.empty() )
|
|
||||||
fileName = server_prefix + '/' + fileName;
|
|
||||||
|
|
||||||
sio << "GET /" << fileName << " HTTP/1.1\n" << "Host:\n\n";
|
sio << "GET /" << fileName << " HTTP/1.1\n" << "Host:\n\n";
|
||||||
sio.flush();
|
sio.flush();
|
||||||
|
|
||||||
@ -176,14 +208,22 @@ class NetReader : public osgDB::ReaderWriter
|
|||||||
|
|
||||||
} while( linebuff[0] != '\r' );
|
} while( linebuff[0] != '\r' );
|
||||||
|
|
||||||
|
/*
|
||||||
// Invoke the reader corresponding to the extension
|
// Invoke the reader corresponding to the extension
|
||||||
osgDB::ReaderWriter *reader =
|
osgDB::ReaderWriter *reader =
|
||||||
osgDB::Registry::instance()->getReaderWriterForExtension( osgDB::getFileExtension(fileName));
|
osgDB::Registry::instance()->getReaderWriterForExtension( osgDB::getFileExtension(fileName));
|
||||||
|
*/
|
||||||
|
|
||||||
ReadResult readResult = ReadResult::FILE_NOT_HANDLED;
|
|
||||||
if( reader != 0L )
|
if( reader != 0L )
|
||||||
readResult = reader->readNode( sio );
|
readResult = reader->readNode( sio );
|
||||||
|
|
||||||
|
if( !localCacheDir.empty() )
|
||||||
|
{
|
||||||
|
std::string cacheFile = localCacheDir + '/' + fileName;
|
||||||
|
if( TemporaryFileUtils::makeDirectory( cacheFile ) )
|
||||||
|
osgDB::writeNodeFile( *(readResult.getNode()), cacheFile );
|
||||||
|
}
|
||||||
|
|
||||||
return readResult;
|
return readResult;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user