source: osgVisual/src/cluster/dataIO_clusterUDP.cpp @ 69

Last change on this file since 69 was 69, checked in by Torben Dannhauer, 14 years ago

Network sync:
now works:

  • serialization of transport container
  • transport via ENet UDP
  • de-serialization of the transport container.
  • transport of viewmatrix and framenumber to the slave.

ToDo?: apply viewmatrix on slave still do not work.

File size: 5.4 KB
Line 
1/* -*-c++-*- osgVisual - Copyright (C) 2009-2010 Torben Dannhauer
2 *
3 * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * osgVisual requires for some proprietary modules a license from the correspondig manufacturer.
9 * You have to aquire licenses for all used proprietary modules.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * OpenSceneGraph Public License for more details.
15*/
16
17#include "dataIO_clusterUDP.h"
18
19using namespace osgVisual;
20
21
22dataIO_clusterUDP::dataIO_clusterUDP()
23{
24        OSG_NOTIFY( osg::ALWAYS ) << "clusterUDP constructed" << std::endl;
25
26        compressionEnabled = true;
27        broadcaster = NULL;
28        receiver = NULL;
29}
30
31dataIO_clusterUDP::~dataIO_clusterUDP(void)
32{
33        OSG_NOTIFY( osg::ALWAYS ) << "clusterUDP destructed" << std::endl;
34}
35
36void dataIO_clusterUDP::init( osg::ArgumentParser& arguments_, osgVisual::dataIO_cluster::clustermode clusterMode_, osgVisual::dataIO_transportContainer* sendContainer_, bool compressionEnabled_, bool asAscii_ )
37{
38        // set Variables
39        sendContainer = sendContainer_;
40
41        // Configure Compression and instantiate read/write-options
42        std::string readOptionString = "";
43        std::string writeOptionString = "";
44        if(asAscii_)
45        {
46                readOptionString = "Ascii";
47                writeOptionString = "Ascii";
48        }
49        if (compressionEnabled_)
50                writeOptionString+=" Compressor=zlib";
51        readOptions = new osgDB::Options( readOptionString.c_str() );
52        writeOptions = new osgDB::Options( writeOptionString.c_str() );
53
54        // Get ReaderWriter
55        rw = osgDB::Registry::instance()->getReaderWriterForExtension("osgb"); 
56
57        // Instantiate broadcaster and reciever
58        broadcaster = new osgVisual::Broadcaster();
59        broadcaster->init(12345);
60        receiver = new osgVisual::Receiver();
61        receiver->init(12345);
62       
63        OSG_NOTIFY( osg::ALWAYS ) << "clusterUDP init()" << std::endl;
64}
65
66void dataIO_clusterUDP::shutdown()
67{
68        OSG_NOTIFY( osg::ALWAYS ) << "clusterUDP shutdown()" << std::endl;
69}
70
71bool dataIO_clusterUDP::sendTO_OBJvaluesToSlaves()
72{
73        OSG_NOTIFY( osg::INFO ) << "clusterUDP sendTO_OBJvaluesToSlaves()" << std::endl;
74
75        if(sendContainer.valid())
76        {
77                // Writing node to stream
78                std::stringstream myOstream;
79                if ( rw )
80                {
81                        osgDB::ReaderWriter::WriteResult wr = rw->writeObject( *sendContainer.get(), myOstream, writeOptions );
82                        if (wr.success() )                     
83                        {
84                                // Send Data via UDP:
85                                //OSG_NOTIFY( osg::ALWAYS ) << "dataIO_clusterUDP::sendTO_OBJvaluesToSlaves() - Bytes to send: " << myOstream.str().length() << std::endl;
86                                //OSG_NOTIFY( osg::ALWAYS ) << "Send: " << myOstream.str() << std::endl;
87                                broadcaster->transmitBuffer( myOstream.str().c_str() , myOstream.str().length() );
88                        }
89                        else OSG_NOTIFY( osg::WARN ) << "ERROR: clusterUDP sendTO_OBJvaluesToSlaves() :: Save failed: " << wr.message() << std::endl;
90                }
91                else OSG_NOTIFY( osg::WARN ) << "ERROR: clusterUDP sendTO_OBJvaluesToSlaves() :: Unable to get readerWriter for osgb" << std::endl;
92        }
93        else OSG_NOTIFY( osg::WARN ) << "ERROR: clusterUDP sendTO_OBJvaluesToSlaves() :: Invalid transportContainer" << std::endl;
94
95        return true;
96}
97
98bool dataIO_clusterUDP::readTO_OBJvaluesFromMaster()
99{
100        OSG_NOTIFY( osg::INFO ) << "clusterUDP readTO_OBJvaluesFromMaster()" << std::endl;
101
102        std::stringstream received;
103        receiver->waitBlockingforSocketData( received );
104
105        int bytes_received = received.str().length();
106        if (bytes_received > 0 )
107        {
108                OSG_NOTIFY( osg::ALWAYS ) << "dataIO_clusterUDP::readTO_OBJvaluesFromMaster() - Bytes received: " << bytes_received << std::endl;
109                OSG_NOTIFY( osg::ALWAYS ) << "Received: " << received.str() << std::endl;
110
111                // Unserialize data
112                if ( rw )
113                {
114                        osgDB::ReaderWriter::ReadResult rr = rw->readObject( received, readOptions );
115                        if (rr.success())
116                        {
117                                sendContainer = dynamic_cast<osgVisual::dataIO_transportContainer*>(rr.takeObject());
118                                if (sendContainer)
119                                {
120                                        OSG_NOTIFY( osg::ALWAYS ) << "Received::FrameID is: " << sendContainer->getFrameID() << std::endl;
121                                }
122                                else
123                                        OSG_NOTIFY( osg::WARN ) << "ERROR: dataIO_clusterUDP::readTO_OBJvaluesFromMaster() - Unable to cast converted node to transportContainer" << std::endl;
124                        }
125                        else
126                                OSG_NOTIFY( osg::WARN ) << "ERROR: dataIO_clusterUDP::readTO_OBJvaluesFromMaster() - Unable to convert stream to node" << std::endl;
127                }
128                else
129                        OSG_NOTIFY( osg::WARN ) << "ERROR: dataIO_clusterUDP::readTO_OBJvaluesFromMaster() - Unable to get readerWriter for osgb" << std::endl;
130        }       // IF bytes recv >0
131
132
133        return true;
134}
135
136void dataIO_clusterUDP::reportAsReadyToSwap()
137{
138        OSG_NOTIFY( osg::INFO ) << "clusterUDP reportAsReadyToSwap()" << std::endl;
139}
140
141bool dataIO_clusterUDP::waitForSwap()
142{
143        OSG_NOTIFY( osg::INFO ) << "clusterUDP waitForSwap()" << std::endl;
144
145        return true;
146}
147
148bool dataIO_clusterUDP::waitForAllReadyToSwap()
149{
150        OSG_NOTIFY( osg::INFO ) << "clusterUDP waitForAllReadyToSwap()" << std::endl;
151
152        return true;
153}
154
155bool dataIO_clusterUDP::sendSwapCommand()
156{
157        OSG_NOTIFY( osg::INFO ) << "clusterUDP sendSwapCommand()" << std::endl;
158
159        return true;
160}
Note: See TracBrowser for help on using the repository browser.