Added initial cut of libcurl based plugin

This commit is contained in:
Robert Osfield 2008-03-21 13:08:02 +00:00
parent 73ce6837fa
commit ac63d37f7b
6 changed files with 229 additions and 24 deletions

View File

@ -210,6 +210,7 @@ FIND_PACKAGE(OpenVRML)
FIND_PACKAGE(Performer)
FIND_PACKAGE(ZLIB)
FIND_PACKAGE(GDAL)
FIND_PACKAGE(CURL)
SET(wxWidgets_USE_LIBS base core gl net)
FIND_PACKAGE(wxWidgets)

View File

@ -1405,6 +1405,16 @@ ReaderWriter::ReadResult Registry::read(const ReadFunctor& readFunctor)
// use that archive to read the file.
if (containsServerAddress(readFunctor._filename))
{
ReaderWriter* rw = getReaderWriterForExtension("curl");
if (rw)
{
return readFunctor.doRead(*rw);
}
else
{
ReaderWriter* rw = getReaderWriterForExtension("net");
if (rw)
{
std::string serverName = getServerAddress(readFunctor._filename);
std::string serverFile = getServerFileName(readFunctor._filename);
@ -1421,9 +1431,6 @@ ReaderWriter::ReadResult Registry::read(const ReadFunctor& readFunctor)
return ReaderWriter::ReadResult("Warning: Server file name invalid.");
}
ReaderWriter* rw = getReaderWriterForExtension("net");
if (rw)
{
std::string& filename = const_cast<std::string&>(readFunctor._filename);
filename = serverName+':'+serverFile;
return readFunctor.doRead(*rw);
@ -1432,6 +1439,8 @@ ReaderWriter::ReadResult Registry::read(const ReadFunctor& readFunctor)
{
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.");
}
}
// record the errors reported by readerwriters.

View File

@ -96,6 +96,10 @@ IF(GDAL_FOUND)
ADD_SUBDIRECTORY(ogr)
ENDIF(GDAL_FOUND)
IF(CURL_FOUND)
ADD_SUBDIRECTORY(curl)
ENDIF(CURL_FOUND)
############################################################
#
# 3rd party 3d plugins

View 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)

View 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)

View File

@ -148,7 +148,7 @@ class NetReader : public osgDB::ReaderWriter
{
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;
@ -302,10 +302,16 @@ class NetReader : public osgDB::ReaderWriter
readResult = readFile(objectType, reader, in, options );
in.close();
osg::notify(osg::DEBUG_INFO) << "osgPlugin .net: " << fileName <<
osg::notify(osg::NOTICE) << "osgPlugin .net: " << fileName <<
" fetched from local cache." << std::endl;
return readResult;
}
else
{
osg::notify(osg::NOTICE) << "osgPlugin .net: " << fileName <<
" could not be found in local cache." << std::endl;
}
}
// Fetch from the network
@ -431,7 +437,7 @@ class NetReader : public osgDB::ReaderWriter
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;
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;
}
}