source: osgVisual/src/dataIO/visual_dataIO.cpp @ 67

Last change on this file since 67 was 67, checked in by Torben Dannhauer, 14 years ago
File size: 9.7 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 "visual_dataIO.h"
18
19using namespace osgVisual;
20
21visual_dataIO::visual_dataIO()
22{
23        OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO constructed" << std::endl;
24
25        initialized = false;
26}
27
28visual_dataIO::~visual_dataIO()
29{
30        OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO destructed" << std::endl;
31}
32
33visual_dataIO* visual_dataIO::getInstance()
34{
35        static visual_dataIO instance; 
36        return &instance; 
37};
38
39void visual_dataIO::init(osgViewer::Viewer* viewer_, osg::ArgumentParser& arguments_)
40{
41        OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO init()" << std::endl;
42
43        // Init variables
44        viewer = viewer_;
45
46        // Parse operating Mode
47    if (arguments_.read("-m"))
48        {
49                OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as MASTER" << std::endl;
50                clusterMode = osgVisual::dataIO_cluster::MASTER;
51                // Create Container:
52                slotContainer = new osgVisual::dataIO_transportContainer();
53        }
54        else if (arguments_.read("-s"))
55        {
56                OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as SLAVE" << std::endl;
57                clusterMode = osgVisual::dataIO_cluster::SLAVE;
58                slotContainer = NULL;   // Slave only recieves container, therefor set this Pointer null.
59        }
60        else
61        {
62                OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as STANDALONE" << std::endl;
63                clusterMode = osgVisual::dataIO_cluster::STANDALONE;
64                // Create Container:
65                slotContainer = new osgVisual::dataIO_transportContainer();
66        }
67
68        // Create Cluster.
69        #ifdef USE_CLUSTER_DUMMY
70                cluster = new dataIO_clusterDummy();
71        #endif
72        #ifdef USE_CLUSTER_ASIO_TCP_IOSTREAM
73                cluster = new dataIO_clusterAsioTcpIostream();
74        #endif
75        #ifdef USE_CLUSTER_UDP
76                cluster = new dataIO_clusterUDP();
77        #endif
78        #ifdef USE_CLUSTER_ENET
79                cluster = new dataIO_clusterENet();
80        #endif
81        if(cluster.valid())
82                cluster->init(arguments_, clusterMode, slotContainer, true, false);
83
84        // Create extLink.
85        #ifdef USE_EXTLINK_DUMMY
86                extLink = new dataIO_extLinkDummy( dataSlots );
87        #endif
88        #ifdef USE_EXTLINK_VCL
89                extLink = new dataIO_extLinkVCL( dataSlots );
90        #endif
91        extLink->init();
92
93       
94
95        // Install callbacks to perform DataIO activities every frame:
96        //// EventCallback at the absolute beginning of the frame
97        eventCallback = new dataIO_eventCallback(this);
98        viewer->getCamera()->setEventCallback( eventCallback );
99        //// FinalDrawCallback at the end of event and update handling, but BEFORE rendering the frame
100        finalDrawCallback = new dataIO_finalDrawCallback(this);
101        viewer->getCamera()->setFinalDrawCallback( finalDrawCallback );
102
103
104        initialized = true;
105}
106
107void visual_dataIO::shutdown()
108{
109        if(initialized)
110        {
111                OSG_NOTIFY( osg::ALWAYS ) << "Shutdown visual_dataIO..." << std::endl;
112
113                viewer->getCamera()->removeEventCallback( NULL );
114                eventCallback = NULL;
115                viewer->getCamera()->setFinalDrawCallback( NULL );
116                finalDrawCallback = NULL;
117                viewer = NULL;
118
119                if(cluster.valid())
120                        cluster->shutdown();
121                if(extLink.valid())
122                extLink->shutdown();
123        }
124}
125
126void visual_dataIO::dataIO_eventCallback::operator()(osg::Node* node, osg::NodeVisitor* nv)
127{
128        // perform all actions for the eventDrawCallback.
129        OSG_NOTIFY( osg::INFO ) << "---- Executing EventCallback.." <<  std::endl;
130
131        switch( dataIO->clusterMode )
132        {
133                case osgVisual::dataIO_cluster::MASTER : 
134                        {
135                                dataIO->extLink->readTO_OBJvalues();
136                                dataIO->cluster->sendTO_OBJvaluesToSlaves();
137                        }
138                        break;
139                case osgVisual::dataIO_cluster::SLAVE : 
140                        {
141                                dataIO->cluster->readTO_OBJvaluesFromMaster();
142                        }
143                        break;
144                case osgVisual::dataIO_cluster::STANDALONE : 
145                        {
146                                dataIO->extLink->readTO_OBJvalues();
147                        }
148                        break;
149                default:
150                        OSG_NOTIFY( osg::FATAL ) << "ERROR: Unkown clustermode!" <<  std::endl;
151                        break;
152        };
153}
154
155void visual_dataIO::dataIO_finalDrawCallback::operator() (const osg::Camera& camera) const
156{
157        // perform all actions for the initialDrawCallback.
158        OSG_NOTIFY( osg::INFO ) << "---- Executing InitialDrawCallback.." << std::endl;
159
160        switch( dataIO->clusterMode )
161        {
162                case osgVisual::dataIO_cluster::MASTER : 
163                        {
164                                dataIO->extLink->writebackFROM_OBJvalues();
165                                dataIO->cluster->waitForAllReadyToSwap();
166                                dataIO->cluster->sendSwapCommand();
167                        }
168                        break;
169                case osgVisual::dataIO_cluster::SLAVE : 
170                        {
171                                dataIO->cluster->reportAsReadyToSwap();
172                                dataIO->cluster->waitForSwap();
173                        }
174                        break;
175                case osgVisual::dataIO_cluster::STANDALONE : 
176                        {
177                                dataIO->extLink->writebackFROM_OBJvalues();
178                        }
179                        break;
180                default:
181                        OSG_NOTIFY( osg::FATAL ) << "ERROR: visual_dataIO::dataIO_finalDrawCallback::operator() - Unkown clustermode!" <<  std::endl;
182                        break;
183        };
184}
185
186void* visual_dataIO::getSlotPointer(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, osgVisual::dataIO_slot::varType variableTyp_ )
187{
188        // iterate through slotlist. If found, return pointer, else add slot to list and return pointer
189        for (unsigned int i=0; i<dataSlots.size(); i++)
190        {
191                // Check if this variable name&-type already exists
192                if( dataSlots[i].variableName == variableName_ && dataSlots[i].direction == direction_  && dataSlots[i].variableType ==  variableTyp_)
193                {
194                        //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotPointer() - Slot found at position " << i << std::endl;
195                        // Return pointer to the value
196                        if (variableTyp_ == osgVisual::dataIO_slot::STRING )
197                                return &(dataSlots[i].sValue);
198                        else
199                                return &(dataSlots[i].value);
200                }
201        }
202
203        // Slot does not exist -> add it to slot list
204        //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotPointer() - Slot not found, will add as new slot " << std::endl;
205        dataIO_slot newSlot;
206        newSlot.variableName = variableName_;
207        newSlot.variableType = variableTyp_;
208        newSlot.value = 0;
209        newSlot.sValue = "";
210        dataSlots.push_back( newSlot );
211        if (variableTyp_ == osgVisual::dataIO_slot::STRING )
212                return &(dataSlots.back().sValue);
213        else
214        {
215                return &(dataSlots.back().value);
216        }
217}
218
219double visual_dataIO::getSlotDataAsDouble(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_ )
220{
221        // iterate through slotlist. If found, return value
222        for (unsigned int i=0; i<dataSlots.size(); i++)
223        {
224                // Check if this variable name&-type already exists
225                if( dataSlots[i].variableName == variableName_ && dataSlots[i].direction == direction_  && dataSlots[i].variableType == osgVisual::dataIO_slot::DOUBLE )
226                {
227                        //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotDataAsDouble() - Slot found at position " << i << std::endl;
228                        return dataSlots[i].value;
229                }
230        }
231        return 0;
232}
233
234std::string visual_dataIO::getSlotDataAsString(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_ )
235{
236        // iterate through slotlist. If found, return value
237        for (unsigned int i=0; i<dataSlots.size(); i++)
238        {
239                // Check if this variable name&-type already exists
240                if( dataSlots[i].variableName == variableName_ && dataSlots[i].direction == direction_  && dataSlots[i].variableType == osgVisual::dataIO_slot::STRING )
241                {
242                        //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotDataAsDouble() - Slot found at position " << i << std::endl;
243                        return dataSlots[i].sValue;
244                }
245        }
246        return "";
247}
248
249void visual_dataIO::setSlotData(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, std::string sValue_ )
250{
251        bool slotFound = false;
252        // iterate through slotlist. If found, return pointer, else add slot to list
253        for (unsigned int i=0; i<dataSlots.size(); i++)
254        {
255                // Check if this variable name&-type already exists
256                if( dataSlots[i].variableName == variableName_ && dataSlots[i].direction == direction_ && dataSlots[i].variableType ==  osgVisual::dataIO_slot::STRING)
257                {
258                        // Update value
259                        dataSlots[i].sValue = sValue_;
260                        slotFound = true;
261                }
262               
263        }
264
265        if (!slotFound)
266        {
267                // Slot does not exist -> add it to slot list
268                dataIO_slot newSlot;
269                newSlot.variableName = variableName_;
270                newSlot.direction = direction_;
271                newSlot.variableType = osgVisual::dataIO_slot::STRING;
272                newSlot.value = 0;
273                newSlot.sValue = sValue_;
274                dataSlots.push_back( newSlot );
275        }
276}
277
278void visual_dataIO::setSlotData(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, double value_ )
279{
280        // iterate through slotlist. If found, return pointer, else add slot to list
281        bool slotFound = false;
282        for (unsigned int i=0; i<dataSlots.size(); i++)
283        {
284                // Check if this variableName & -type already exists
285                if( dataSlots[i].variableName == variableName_ && dataSlots[i].direction == direction_ && dataSlots[i].variableType ==  osgVisual::dataIO_slot::DOUBLE)
286                {
287                        // Update value
288                        //OSG_NOTIFY( osg::ALWAYS ) << "setSlotData: " << variableName_ << " - value: " << value_ << std::endl;
289                        dataSlots[i].value = value_;
290                        slotFound = true;
291                }       
292        }
293
294        if (!slotFound)
295        {
296                // Slot does not exist -> add it to slot list
297                dataIO_slot newSlot;
298                newSlot.variableName = variableName_;
299                newSlot.direction = direction_;
300                newSlot.variableType = osgVisual::dataIO_slot::DOUBLE;
301                newSlot.value = value_;
302                newSlot.sValue = "";
303                dataSlots.push_back( newSlot );
304        }
305}
Note: See TracBrowser for help on using the repository browser.