/* -*-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 "dataIO_clusterUDP.h" using namespace osgVisual; dataIO_clusterUDP::dataIO_clusterUDP() { OSG_NOTIFY( osg::ALWAYS ) << "clusterUDP constructed" << std::endl; compressionEnabled = true; broadcaster = NULL; receiver = NULL; } dataIO_clusterUDP::~dataIO_clusterUDP(void) { OSG_NOTIFY( osg::ALWAYS ) << "clusterUDP destructed" << std::endl; } void dataIO_clusterUDP::init( osg::ArgumentParser& arguments_, osgVisual::dataIO_transportContainer* sendContainer_, bool compressionEnabled_, bool asAscii_ ) { // set Variables sendContainer = sendContainer_; // Configure Compression and instantiate read/write-options std::string readOptionString = ""; std::string writeOptionString = ""; if(asAscii_) { readOptionString = "Ascii"; writeOptionString = "Ascii"; } if (compressionEnabled_) writeOptionString+=" Compressor=zlib"; readOptions = new osgDB::Options( readOptionString.c_str() ); writeOptions = new osgDB::Options( writeOptionString.c_str() ); // Get ReaderWriter rw = osgDB::Registry::instance()->getReaderWriterForExtension("osgb"); // Instantiate broadcaster and reciever broadcaster = new osgVisual::Broadcaster(); broadcaster->init(12345); receiver = new osgVisual::Receiver(); receiver->init(12345); OSG_NOTIFY( osg::ALWAYS ) << "clusterUDP init()" << std::endl; } void dataIO_clusterUDP::shutdown() { OSG_NOTIFY( osg::ALWAYS ) << "clusterUDP shutdown()" << std::endl; } bool dataIO_clusterUDP::sendTO_OBJvaluesToSlaves() { OSG_NOTIFY( osg::INFO ) << "clusterUDP sendTO_OBJvaluesToSlaves()" << std::endl; if(sendContainer.valid()) { // Writing node to stream std::stringstream myOstream; if ( rw ) { osgDB::ReaderWriter::WriteResult wr = rw->writeObject( *sendContainer.get(), myOstream, writeOptions ); if (wr.success() ) { // Send Data via UDP: //OSG_NOTIFY( osg::ALWAYS ) << "dataIO_clusterUDP::sendTO_OBJvaluesToSlaves() - Bytes to send: " << myOstream.str().length() << std::endl; //OSG_NOTIFY( osg::ALWAYS ) << "Send: " << myOstream.str() << std::endl; broadcaster->transmitBuffer( myOstream.str().c_str() , myOstream.str().length() ); } else OSG_NOTIFY( osg::WARN ) << "ERROR: clusterUDP sendTO_OBJvaluesToSlaves() :: Save failed: " << wr.message() << std::endl; } else OSG_NOTIFY( osg::WARN ) << "ERROR: clusterUDP sendTO_OBJvaluesToSlaves() :: Unable to get readerWriter for osgb" << std::endl; } else OSG_NOTIFY( osg::WARN ) << "ERROR: clusterUDP sendTO_OBJvaluesToSlaves() :: Invalid transportContainer" << std::endl; return true; } bool dataIO_clusterUDP::readTO_OBJvaluesFromMaster() { OSG_NOTIFY( osg::INFO ) << "clusterUDP readTO_OBJvaluesFromMaster()" << std::endl; std::stringstream received; receiver->waitBlockingforSocketData( received ); int bytes_received = received.str().length(); if (bytes_received > 0 ) { OSG_NOTIFY( osg::ALWAYS ) << "dataIO_clusterUDP::readTO_OBJvaluesFromMaster() - Bytes received: " << bytes_received << std::endl; OSG_NOTIFY( osg::ALWAYS ) << "Received: " << received.str() << std::endl; // Unserialize data if ( rw ) { osgDB::ReaderWriter::ReadResult rr = rw->readObject( received, readOptions ); if (rr.success()) { sendContainer = dynamic_cast(rr.takeObject()); if (sendContainer) { OSG_NOTIFY( osg::ALWAYS ) << "Received::FrameID is: " << sendContainer->getFrameID() << std::endl; } else OSG_NOTIFY( osg::WARN ) << "ERROR: dataIO_clusterUDP::readTO_OBJvaluesFromMaster() - Unable to cast converted node to transportContainer" << std::endl; } else OSG_NOTIFY( osg::WARN ) << "ERROR: dataIO_clusterUDP::readTO_OBJvaluesFromMaster() - Unable to convert stream to node" << std::endl; } else OSG_NOTIFY( osg::WARN ) << "ERROR: dataIO_clusterUDP::readTO_OBJvaluesFromMaster() - Unable to get readerWriter for osgb" << std::endl; } return true; } void dataIO_clusterUDP::reportAsReadyToSwap() { OSG_NOTIFY( osg::INFO ) << "clusterUDP reportAsReadyToSwap()" << std::endl; } bool dataIO_clusterUDP::waitForSwap() { OSG_NOTIFY( osg::INFO ) << "clusterUDP waitForSwap()" << std::endl; return true; } bool dataIO_clusterUDP::waitForAllReadyToSwap() { OSG_NOTIFY( osg::INFO ) << "clusterUDP waitForAllReadyToSwap()" << std::endl; return true; } bool dataIO_clusterUDP::sendSwapCommand() { OSG_NOTIFY( osg::INFO ) << "clusterUDP sendSwapCommand()" << std::endl; return true; }