Added initial cut of libcurl based plugin
This commit is contained in:
@@ -210,6 +210,7 @@ FIND_PACKAGE(OpenVRML)
|
|||||||
FIND_PACKAGE(Performer)
|
FIND_PACKAGE(Performer)
|
||||||
FIND_PACKAGE(ZLIB)
|
FIND_PACKAGE(ZLIB)
|
||||||
FIND_PACKAGE(GDAL)
|
FIND_PACKAGE(GDAL)
|
||||||
|
FIND_PACKAGE(CURL)
|
||||||
|
|
||||||
SET(wxWidgets_USE_LIBS base core gl net)
|
SET(wxWidgets_USE_LIBS base core gl net)
|
||||||
FIND_PACKAGE(wxWidgets)
|
FIND_PACKAGE(wxWidgets)
|
||||||
|
|||||||
@@ -1406,31 +1406,40 @@ ReaderWriter::ReadResult Registry::read(const ReadFunctor& readFunctor)
|
|||||||
|
|
||||||
if (containsServerAddress(readFunctor._filename))
|
if (containsServerAddress(readFunctor._filename))
|
||||||
{
|
{
|
||||||
std::string serverName = getServerAddress(readFunctor._filename);
|
ReaderWriter* rw = getReaderWriterForExtension("curl");
|
||||||
std::string serverFile = getServerFileName(readFunctor._filename);
|
|
||||||
osg::notify(osg::INFO)<<"Contains sever address : "<<serverName<<std::endl;
|
|
||||||
osg::notify(osg::INFO)<<" file name on server : "<<serverFile<<std::endl;
|
|
||||||
|
|
||||||
if (serverName.empty())
|
|
||||||
{
|
|
||||||
return ReaderWriter::ReadResult("Warning: Server address invalid.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (serverFile.empty())
|
|
||||||
{
|
|
||||||
return ReaderWriter::ReadResult("Warning: Server file name invalid.");
|
|
||||||
}
|
|
||||||
|
|
||||||
ReaderWriter* rw = getReaderWriterForExtension("net");
|
|
||||||
if (rw)
|
if (rw)
|
||||||
{
|
{
|
||||||
std::string& filename = const_cast<std::string&>(readFunctor._filename);
|
|
||||||
filename = serverName+':'+serverFile;
|
|
||||||
return readFunctor.doRead(*rw);
|
return readFunctor.doRead(*rw);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return ReaderWriter::ReadResult("Warning: Could not find the .net plugin to read from server.");
|
ReaderWriter* rw = getReaderWriterForExtension("net");
|
||||||
|
if (rw)
|
||||||
|
{
|
||||||
|
std::string serverName = getServerAddress(readFunctor._filename);
|
||||||
|
std::string serverFile = getServerFileName(readFunctor._filename);
|
||||||
|
osg::notify(osg::INFO)<<"Contains sever address : "<<serverName<<std::endl;
|
||||||
|
osg::notify(osg::INFO)<<" file name on server : "<<serverFile<<std::endl;
|
||||||
|
|
||||||
|
if (serverName.empty())
|
||||||
|
{
|
||||||
|
return ReaderWriter::ReadResult("Warning: Server address invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (serverFile.empty())
|
||||||
|
{
|
||||||
|
return ReaderWriter::ReadResult("Warning: Server file name invalid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string& filename = const_cast<std::string&>(readFunctor._filename);
|
||||||
|
filename = serverName+':'+serverFile;
|
||||||
|
return readFunctor.doRead(*rw);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return ReaderWriter::ReadResult("Warning: Could not find the .net plugin to read from server.");
|
||||||
|
}
|
||||||
|
return ReaderWriter::ReadResult("Warning: Could not find the .curl plugin to read from server.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -96,6 +96,10 @@ IF(GDAL_FOUND)
|
|||||||
ADD_SUBDIRECTORY(ogr)
|
ADD_SUBDIRECTORY(ogr)
|
||||||
ENDIF(GDAL_FOUND)
|
ENDIF(GDAL_FOUND)
|
||||||
|
|
||||||
|
IF(CURL_FOUND)
|
||||||
|
ADD_SUBDIRECTORY(curl)
|
||||||
|
ENDIF(CURL_FOUND)
|
||||||
|
|
||||||
############################################################
|
############################################################
|
||||||
#
|
#
|
||||||
# 3rd party 3d plugins
|
# 3rd party 3d plugins
|
||||||
|
|||||||
8
src/osgPlugins/curl/CMakeLists.txt
Normal file
8
src/osgPlugins/curl/CMakeLists.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#this file is automatically generated
|
||||||
|
|
||||||
|
SET(TARGET_SRC ReaderWriterCURL.cpp )
|
||||||
|
|
||||||
|
SET(TARGET_EXTERNAL_LIBRARIES ${CURL_LIBRARY} )
|
||||||
|
|
||||||
|
#### end var setup ###
|
||||||
|
SETUP_PLUGIN(curl)
|
||||||
177
src/osgPlugins/curl/ReaderWriterCURL.cpp
Normal file
177
src/osgPlugins/curl/ReaderWriterCURL.cpp
Normal file
@@ -0,0 +1,177 @@
|
|||||||
|
/* -*-c++-*- VirtualPlanetBuilder - Copyright (C) 1998-2007 Robert Osfield
|
||||||
|
*
|
||||||
|
* This application is open source and may be redistributed and/or modified
|
||||||
|
* freely and without restriction, both in commericial and non commericial applications,
|
||||||
|
* as long as this copyright notice is maintained.
|
||||||
|
*
|
||||||
|
* This application is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <osgDB/ReadFile>
|
||||||
|
#include <osgDB/WriteFile>
|
||||||
|
#include <osgDB/Registry>
|
||||||
|
#include <osgDB/FileNameUtils>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#include <curl/curl.h>
|
||||||
|
#include <curl/types.h>
|
||||||
|
|
||||||
|
|
||||||
|
class ReaderWriterCURL : public osgDB::ReaderWriter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
static size_t StreamMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
|
||||||
|
{
|
||||||
|
size_t realsize = size * nmemb;
|
||||||
|
std::ostream* buffer = (std::ostream*)data;
|
||||||
|
|
||||||
|
buffer->write((const char*)ptr, realsize);
|
||||||
|
|
||||||
|
return realsize;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ReaderWriterCURL()
|
||||||
|
{
|
||||||
|
_curl = curl_easy_init();
|
||||||
|
|
||||||
|
curl_easy_setopt(_curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
|
||||||
|
curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, StreamMemoryCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
~ReaderWriterCURL()
|
||||||
|
{
|
||||||
|
if (_curl) curl_easy_cleanup(_curl);
|
||||||
|
|
||||||
|
_curl = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual const char* className() const { return "HTTP Protocol Model Reader"; }
|
||||||
|
|
||||||
|
virtual bool acceptsExtension(const std::string& extension) const
|
||||||
|
{
|
||||||
|
return osgDB::equalCaseInsensitive(extension,"curl");
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ObjectType
|
||||||
|
{
|
||||||
|
OBJECT,
|
||||||
|
ARCHIVE,
|
||||||
|
IMAGE,
|
||||||
|
HEIGHTFIELD,
|
||||||
|
NODE
|
||||||
|
};
|
||||||
|
|
||||||
|
virtual ReadResult openArchive(const std::string& fileName,ArchiveStatus status, unsigned int , const Options* options) const
|
||||||
|
{
|
||||||
|
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) const
|
||||||
|
{
|
||||||
|
return readFile(OBJECT,fileName,options);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ReadResult readImage(const std::string& fileName, const Options *options) const
|
||||||
|
{
|
||||||
|
return readFile(IMAGE,fileName,options);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ReadResult readHeightField(const std::string& fileName, const Options *options) const
|
||||||
|
{
|
||||||
|
return readFile(HEIGHTFIELD,fileName,options);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ReadResult readNode(const std::string& fileName, const Options *options) const
|
||||||
|
{
|
||||||
|
return readFile(NODE,fileName,options);
|
||||||
|
}
|
||||||
|
|
||||||
|
ReadResult readFile(ObjectType objectType, osgDB::ReaderWriter* rw, std::istream& fin, const Options *options) const
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ReadResult readFile(ObjectType objectType, const std::string& fullFileName, const Options *options) const
|
||||||
|
{
|
||||||
|
std::string fileName;
|
||||||
|
|
||||||
|
std::string ext = osgDB::getFileExtension(fullFileName);
|
||||||
|
if (ext=="curl")
|
||||||
|
{
|
||||||
|
fileName = osgDB::getNameLessExtension(fullFileName);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fileName = fullFileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
osgDB::ReaderWriter *reader =
|
||||||
|
osgDB::Registry::instance()->getReaderWriterForExtension( osgDB::getFileExtension(fileName));
|
||||||
|
|
||||||
|
if (!reader)
|
||||||
|
{
|
||||||
|
osg::notify(osg::NOTICE)<<"No ReaderWriter for file "<<fileName<<std::endl;
|
||||||
|
return ReadResult::FILE_NOT_HANDLED;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::stringstream buffer;
|
||||||
|
|
||||||
|
curl_easy_setopt(_curl, CURLOPT_URL, fileName.c_str());
|
||||||
|
curl_easy_setopt(_curl, CURLOPT_WRITEDATA, (void *)&buffer);
|
||||||
|
|
||||||
|
CURLcode res = curl_easy_perform(_curl);
|
||||||
|
|
||||||
|
|
||||||
|
curl_easy_setopt(_curl, CURLOPT_WRITEDATA, (void *)0);
|
||||||
|
|
||||||
|
if (res==0)
|
||||||
|
{
|
||||||
|
osg::ref_ptr<Options> local_opt = const_cast<Options*>(options);
|
||||||
|
if (!local_opt) local_opt = new Options;
|
||||||
|
|
||||||
|
if (local_opt.valid() && local_opt->getDatabasePathList().empty())
|
||||||
|
{
|
||||||
|
local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
ReadResult result = readFile(objectType, reader, buffer, local_opt.get() );
|
||||||
|
|
||||||
|
local_opt->getDatabasePathList().pop_front();
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
osg::notify(osg::NOTICE)<<"Read error res = "<<res<<std::endl;
|
||||||
|
return ReadResult::FILE_NOT_HANDLED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
CURL* _curl;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// now register with Registry to instantiate the above
|
||||||
|
// reader/writer.
|
||||||
|
REGISTER_OSGPLUGIN(rgb, ReaderWriterCURL)
|
||||||
@@ -148,7 +148,7 @@ class NetReader : public osgDB::ReaderWriter
|
|||||||
{
|
{
|
||||||
osg::Timer_t start = osg::Timer::instance()->tick();
|
osg::Timer_t start = osg::Timer::instance()->tick();
|
||||||
|
|
||||||
//osg::notify(osg::NOTICE) << "osgPlugin .net: start load" << inFileName << std::endl;
|
osg::notify(osg::NOTICE) << "osgPlugin .net: start load" << inFileName << std::endl;
|
||||||
|
|
||||||
|
|
||||||
std::string hostname;
|
std::string hostname;
|
||||||
@@ -302,10 +302,16 @@ class NetReader : public osgDB::ReaderWriter
|
|||||||
readResult = readFile(objectType, reader, in, options );
|
readResult = readFile(objectType, reader, in, options );
|
||||||
|
|
||||||
in.close();
|
in.close();
|
||||||
osg::notify(osg::DEBUG_INFO) << "osgPlugin .net: " << fileName <<
|
osg::notify(osg::NOTICE) << "osgPlugin .net: " << fileName <<
|
||||||
" fetched from local cache." << std::endl;
|
" fetched from local cache." << std::endl;
|
||||||
return readResult;
|
return readResult;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
osg::notify(osg::NOTICE) << "osgPlugin .net: " << fileName <<
|
||||||
|
" could not be found in local cache." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch from the network
|
// Fetch from the network
|
||||||
@@ -431,8 +437,8 @@ class NetReader : public osgDB::ReaderWriter
|
|||||||
|
|
||||||
double ms = osg::Timer::instance()->delta_m(start,osg::Timer::instance()->tick());
|
double ms = osg::Timer::instance()->delta_m(start,osg::Timer::instance()->tick());
|
||||||
|
|
||||||
osg::notify(osg::DEBUG_INFO) << "osgPlugin .net: " << fileName <<
|
osg::notify(osg::NOTICE) << "osgPlugin .net: " << fileName <<
|
||||||
" fetched from server. in" << ms <<" ms"<< std::endl;
|
" fetched from server. in " << ms <<" ms"<< std::endl;
|
||||||
|
|
||||||
if (objectType==ARCHIVE && readResult.validArchive())
|
if (objectType==ARCHIVE && readResult.validArchive())
|
||||||
{
|
{
|
||||||
@@ -457,7 +463,7 @@ class NetReader : public osgDB::ReaderWriter
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
osg::notify(osg::DEBUG_INFO) << "osgPlugin .net: " << fileName <<
|
osg::notify(osg::NOTICE) << "osgPlugin .net: " << fileName <<
|
||||||
" stored to local cache." << std::endl;
|
" stored to local cache." << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user