/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or * (at your option) any later version. The full license is in LICENSE file * included with this distribution, and on the openscenegraph.org website. * * This library 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. See the * OpenSceneGraph Public License for more details. */ #include #include #include #include #include #include #include #include #include #include "ReaderWriterCURL.h" using namespace osg_curl; // // StreamObject // EasyCurl::StreamObject::StreamObject(std::ostream* stream1, const std::string& cacheFileName): _stream1(stream1), _cacheFileName(cacheFileName) { _foutOpened = false; } void EasyCurl::StreamObject::write(const char* ptr, size_t realsize) { if (_stream1) _stream1->write(ptr, realsize); if (!_cacheFileName.empty()) { if (!_foutOpened) { osg::notify(osg::INFO)<<"Writing to cache: "<<_cacheFileName<write((const char*)ptr, realsize); return realsize; } /////////////////////////////////////////////////////////////////////////////////////////////////// // // EasyCurl // EasyCurl::EasyCurl() { osg::notify(osg::INFO)<<"EasyCurl::EasyCurl()"<=400) { osg::notify(osg::NOTICE)<<"Error: libcurl read error, file="<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; } osgDB::ReaderWriter::ReadResult ReaderWriterCURL::readFile(ObjectType objectType, const std::string& fullFileName, const osgDB::ReaderWriter::Options *options) const { if (!osgDB::containsServerAddress(fullFileName)) { if (options && !options->getDatabasePathList().empty()) { if (osgDB::containsServerAddress(options->getDatabasePathList().front())) { std::string newFileName = options->getDatabasePathList().front() + "/" + fullFileName; return readFile(objectType, newFileName,options); } } return ReadResult::FILE_NOT_HANDLED; } osg::notify(osg::INFO)<<"ReaderWriterCURL::readFile("<getOptionString()); std::string opt; while (iss >> opt) { int index = opt.find( "=" ); if( opt.substr( 0, index ) == "OSG_FILE_CACHE" ) cacheFilePath = opt.substr( index+1 ); //Setting Cache Directory by OSG Options else if( opt.substr( 0, index ) == "OSG_CURL_PROXY" ) optProxy = opt.substr( index+1 ); else if( opt.substr( 0, index ) == "OSG_CURL_PROXYPORT" ) optProxyPort = opt.substr( index+1 ); } //Setting Proxy by OSG Options if(!optProxy.empty()) if(!optProxyPort.empty()) proxyAddress = optProxy + ":" + optProxyPort; else proxyAddress = optProxy + ":8080"; //Port not found, using default } std::string fileName; std::string ext = osgDB::getFileExtension(fullFileName); if (ext=="curl") { fileName = osgDB::getNameLessExtension(fullFileName); } else { fileName = fullFileName; } //Getting CURL Environment Variables (If found rewrite OSG Options) const char* fileCachePath = getenv("OSG_FILE_CACHE"); if (fileCachePath) //Env Cache Directory cacheFilePath = std::string(fileCachePath); if (!cacheFilePath.empty()) { cacheFileName = cacheFilePath + "/" + osgDB::getServerAddress(fileName) + "/" + osgDB::getServerFileName(fileName); std::string path = osgDB::getFilePath(cacheFileName); if (!osgDB::fileExists(path) && !osgDB::makeDirectory(path)) { cacheFileName.clear(); } } #if 0 if (!cacheFilePath.empty() && osgDB::fileExists(cacheFileName)) { osg::notify(osg::NOTICE) << "Reading cache file " << cacheFileName <<", previous path "<readObject(cacheFileName,options); return result; } #endif osgDB::ReaderWriter *reader = osgDB::Registry::instance()->getReaderWriterForExtension( osgDB::getFileExtension(fileName)); if (!reader) { osg::notify(osg::NOTICE)<<"Error: No ReaderWriter for file "< local_opt = const_cast(options); if (!local_opt) local_opt = new Options; local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName)); ReadResult readResult = readFile(objectType, reader, buffer, local_opt.get() ); #if 0 if (!cacheFileName.empty() && readResult.success()) { switch(objectType) { case(NODE): osg::notify(osg::NOTICE)<<"Write to cache "<writeNode(*readResult.getNode(), cacheFileName, local_opt.get()); break; default: osg::notify(osg::NOTICE)<<"Curl plugin write to cache not implemented yet"<getDatabasePathList().pop_front(); return readResult; } else { return curlResult; } } // now register with Registry to instantiate the above // reader/writer. REGISTER_OSGPLUGIN(curl, ReaderWriterCURL)