/* -*-c++-*- osgVisual - Copyright (C) 2009-2010 Torben Dannhauer * * This library is based on OpenSceneGraph, 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. * * osgVisual requires for some proprietary modules a license from the correspondig manufacturer. * You have to aquire licenses for all used proprietary modules. * * 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 "visual_dataIO.h" using namespace osgVisual; visual_dataIO::visual_dataIO() { OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO constructed" << std::endl; initialized = false; } visual_dataIO::~visual_dataIO() { OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO destructed" << std::endl; } visual_dataIO* visual_dataIO::getInstance() { static visual_dataIO instance; return &instance; }; void visual_dataIO::init(osgViewer::Viewer* viewer_, osg::ArgumentParser& arguments_) { OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO init()" << std::endl; // Init variables viewer = viewer_; // Parse operating Mode if (arguments_.read("-m")) { OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as MASTER" << std::endl; clusterMode = osgVisual::dataIO_cluster::MASTER; // Create Container: slotContainer = new osgVisual::dataIO_transportContainer(); } else if (arguments_.read("-s")) { OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as SLAVE" << std::endl; clusterMode = osgVisual::dataIO_cluster::SLAVE; // Slave only recieves container, therefor set this Pointer null. slotContainer = NULL; } else { OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as STANDALONE" << std::endl; clusterMode = osgVisual::dataIO_cluster::STANDALONE; // Create Container: slotContainer = new osgVisual::dataIO_transportContainer(); } // Create Cluster. #ifdef USE_CLUSTER_DUMMY cluster = new dataIO_clusterDummy(); #endif #ifdef USE_CLUSTER_UDP cluster = new dataIO_clusterUDP(); #endif cluster->init(arguments_, slotContainer, true, false); // Create extLink. #ifdef USE_EXTLINK_DUMMY extLink = new dataIO_extLinkDummy( dataSlots ); #endif #ifdef USE_EXTLINK_VCL extLink = new dataIO_extLinkVCL( dataSlots ); #endif extLink->init(); // Install callbacks to perform DataIO activities every frame: //// EventCallback at the absolute beginning of the frame eventCallback = new dataIO_eventCallback(this); viewer->getCamera()->setEventCallback( eventCallback ); //// FinalDrawCallback at the end of event and update handling, but BEFORE rendering the frame finalDrawCallback = new dataIO_finalDrawCallback(this); viewer->getCamera()->setFinalDrawCallback( finalDrawCallback ); initialized = true; } void visual_dataIO::shutdown() { if(initialized) { OSG_NOTIFY( osg::ALWAYS ) << "Shutdown visual_dataIO..." << std::endl; viewer->getCamera()->removeEventCallback( NULL ); eventCallback = NULL; viewer->getCamera()->setFinalDrawCallback( NULL ); finalDrawCallback = NULL; viewer = NULL; cluster->shutdown(); extLink->shutdown(); } } void visual_dataIO::dataIO_eventCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) { // perform all actions for the eventDrawCallback. OSG_NOTIFY( osg::INFO ) << "---- Executing EventCallback.." << std::endl; switch( dataIO->clusterMode ) { case osgVisual::dataIO_cluster::MASTER : { dataIO->extLink->readTO_OBJvalues(); dataIO->cluster->sendTO_OBJvaluesToSlaves(); } break; case osgVisual::dataIO_cluster::SLAVE : { dataIO->cluster->readTO_OBJvaluesFromMaster(); } break; case osgVisual::dataIO_cluster::STANDALONE : { dataIO->extLink->readTO_OBJvalues(); } break; default: OSG_NOTIFY( osg::FATAL ) << "ERROR: Unkown clustermode!" << std::endl; break; }; } void visual_dataIO::dataIO_finalDrawCallback::operator() (const osg::Camera& camera) const { // perform all actions for the initialDrawCallback. OSG_NOTIFY( osg::INFO ) << "---- Executing InitialDrawCallback.." << std::endl; switch( dataIO->clusterMode ) { case osgVisual::dataIO_cluster::MASTER : { dataIO->extLink->writebackFROM_OBJvalues(); dataIO->cluster->waitForAllReadyToSwap(); dataIO->cluster->sendSwapCommand(); } break; case osgVisual::dataIO_cluster::SLAVE : { dataIO->cluster->reportAsReadyToSwap(); dataIO->cluster->waitForSwap(); } break; case osgVisual::dataIO_cluster::STANDALONE : { dataIO->extLink->writebackFROM_OBJvalues(); } break; default: OSG_NOTIFY( osg::FATAL ) << "ERROR: visual_dataIO::dataIO_finalDrawCallback::operator() - Unkown clustermode!" << std::endl; break; }; } void* visual_dataIO::getSlotPointer(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, osgVisual::dataIO_slot::varType variableTyp_ ) { // iterate through slotlist. If found, return pointer, else add slot to list and return pointer for (unsigned int i=0; i add it to slot list //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotPointer() - Slot not found, will add as new slot " << std::endl; dataIO_slot newSlot; newSlot.variableName = variableName_; newSlot.variableType = variableTyp_; newSlot.value = 0; newSlot.sValue = ""; dataSlots.push_back( newSlot ); if (variableTyp_ == osgVisual::dataIO_slot::STRING ) return &(dataSlots.back().sValue); else { return &(dataSlots.back().value); } } double visual_dataIO::getSlotDataAsDouble(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_ ) { // iterate through slotlist. If found, return value for (unsigned int i=0; i add it to slot list dataIO_slot newSlot; newSlot.variableName = variableName_; newSlot.direction = direction_; newSlot.variableType = osgVisual::dataIO_slot::STRING; newSlot.value = 0; newSlot.sValue = sValue_; dataSlots.push_back( newSlot ); } } void visual_dataIO::setSlotData(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, double value_ ) { // iterate through slotlist. If found, return pointer, else add slot to list bool slotFound = false; for (unsigned int i=0; i add it to slot list dataIO_slot newSlot; newSlot.variableName = variableName_; newSlot.direction = direction_; newSlot.variableType = osgVisual::dataIO_slot::DOUBLE; newSlot.value = value_; newSlot.sValue = ""; dataSlots.push_back( newSlot ); } }