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

Last change on this file since 160 was 160, checked in by Torben Dannhauer, 13 years ago

change dataIO configuration from command line to xml File.

Status: in Progress

File size: 12.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        clusterMode = osgVisual::dataIO_cluster::STANDALONE;
27        // Create Transport-Container:
28        slotContainer = new osgVisual::dataIO_transportContainer();
29}
30
31visual_dataIO::~visual_dataIO()
32{
33        // Delete all slots:
34        for(unsigned int i=0;i<dataSlots.size();i++)
35        {
36                delete dataSlots[i];
37        }
38        dataSlots.clear();
39       
40        OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO destructed" << std::endl;
41}
42
43visual_dataIO* visual_dataIO::getInstance()
44{
45        static visual_dataIO instance; 
46        return &instance; 
47};
48
49void visual_dataIO::init(osgViewer::Viewer* viewer_, osg::ArgumentParser& arguments_, std::string configFileName)
50{
51        OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO initialize.." << std::endl;
52
53        // Init variables
54        viewer = viewer_;
55
56        // Process XML configuration
57        this->configFileName = configFileName;
58        if(!processXMLConfiguration())
59                OSG_FATAL << "ERROR: visual_dataIO::init() - Failed to initialize dataIO via XML configuration!";
60
61
62        // Create Cluster.
63        #ifdef USE_CLUSTER_ASIO_TCP_IOSTREAM
64                cluster = new dataIO_clusterAsioTcpIostream();
65        #endif
66        #ifdef USE_CLUSTER_ENET
67                cluster = new dataIO_clusterENet();
68                cluster->enableHardSync( false );       /** \todo : rebuild this structure in cluster.h and move it this way to a general implementation. */
69        #endif
70        #ifdef USE_CLUSTER_DUMMY
71                cluster = new dataIO_clusterDummy();
72        #endif
73        if(cluster.valid())
74                //cluster->init(arguments_, clusterMode, slotContainer, true, false);
75                cluster->init(arguments_, viewer_, clusterMode, slotContainer, false, false);
76
77        // Create extLink.
78        #ifdef USE_EXTLINK_DUMMY
79                extLink = new dataIO_extLinkDummy( dataSlots );
80        #endif
81        #ifdef USE_EXTLINK_VCL
82                extLink = new dataIO_extLinkVCL( dataSlots );
83        #endif
84        extLink->init();
85
86       
87
88        // Install callbacks to perform DataIO activities every frame:
89        //// EventCallback at the absolute beginning of the frame
90        eventCallback = new dataIO_eventCallback(this);
91        viewer->getCamera()->setEventCallback( eventCallback );
92        //// FinalDrawCallback at the end of event and update handling, but BEFORE rendering the frame
93        finalDrawCallback = new dataIO_finalDrawCallback(this);
94        viewer->getCamera()->setFinalDrawCallback( finalDrawCallback );
95
96        initialized = true;
97}
98
99bool visual_dataIO::processXMLConfiguration()
100{
101        // Init XML
102        xmlDoc* tmpDoc;
103        bool disabled;
104        xmlNode* config = util::getModuleXMLConfig( configFileName, "dataio", tmpDoc, disabled );
105
106        if( disabled)
107                OSG_NOTIFY( osg::ALWAYS ) << "..disabled by XML configuration file. dataIO can't be disabled. Ignoring." << std::endl;
108
109        // extract configuration values
110        if(config)
111        {
112                xmlNode* a_node = config->children;
113
114                for (xmlNode *cur_node = a_node; cur_node; cur_node = cur_node->next)
115                {
116                        std::string node_name=reinterpret_cast<const char*>(cur_node->name);
117                        //OSG_ALWAYS << "----visual_distortion::processXMLConfiguration() - node type="<< cur_node->type <<", name=" << cur_node->name << std::endl;
118
119                        // Check for dataio node
120                        if(cur_node->type == XML_ELEMENT_NODE && node_name == "dataio")
121                        {
122                                // Extract cluster role
123                                xmlAttr  *attr = cur_node->properties;
124                                while ( attr ) 
125                                { 
126                                        std::string attr_name=reinterpret_cast<const char*>(attr->name);
127                                        std::string attr_value=reinterpret_cast<const char*>(attr->children->content);
128                                        if( attr_name == "clusterrole" )
129                                        {
130                                                if(attr_value == "master")
131                                                {
132                                                        OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as MASTER" << std::endl;
133                                                        clusterMode = osgVisual::dataIO_cluster::MASTER;
134                                                }
135                                                else if(attr_value == "slave")
136                                                {
137                                                        OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as SLAVE" << std::endl;
138                                                        clusterMode = osgVisual::dataIO_cluster::SLAVE;
139                                                        slotContainer = NULL;   // Slave only recieves container, therefor set this Pointer NULL (created instance will be deleted because it is an auto pointer).
140                                                }
141                                                else if(attr_value == "standalone")
142                                                {
143                                                        OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as STANDALONE" << std::endl;
144                                                        clusterMode = osgVisual::dataIO_cluster::STANDALONE;
145                                                }
146                                        }
147                                        attr = attr->next; 
148                                }       // WHILE attrib END
149                        }
150
151                        // Check for cluster node
152                        if(cur_node->type == XML_ELEMENT_NODE && node_name == "cluster")
153                        {
154                                // Check Attributes to determine if the dummy implementation or any other implementation must be instantiated
155
156                                // Pass XML attributes to cluster to analyse and configure it by cluster itself
157                        }
158
159                        // Check for extLink node
160                        if(cur_node->type == XML_ELEMENT_NODE && node_name == "extlink")
161                        {
162                                // Check Attributes to determine if the dummy implementation or any other implementation must be instantiated
163
164                                // Pass XML attributes to extlink to analyse and configure it by extLink itself
165                        }
166
167                }       // FOR all nodes END
168
169                // clean up
170                xmlFreeDoc(tmpDoc); xmlCleanupParser();
171                return true;
172        }       // IF Config valid END
173        else
174        {
175                OSG_WARN << "ERROR: visual_data::processXMLConfiguration() - Module configuration not found" << std::endl;
176                return false;
177        }
178
179        return true;
180}
181
182void visual_dataIO::shutdown()
183{
184        if(initialized)
185        {
186                OSG_NOTIFY( osg::ALWAYS ) << "Shutdown visual_dataIO..." << std::endl;
187
188                viewer->getCamera()->removeEventCallback( eventCallback );
189                eventCallback = NULL;
190                viewer->getCamera()->setFinalDrawCallback( NULL );
191                finalDrawCallback = NULL;
192               
193                viewer = NULL;
194               
195
196                if(cluster.valid())
197                        cluster->shutdown();
198                if(extLink.valid())
199                extLink->shutdown();
200        }
201}
202
203void visual_dataIO::dataIO_eventCallback::operator()(osg::Node* node, osg::NodeVisitor* nv)
204{
205        // perform all actions for the eventDrawCallback.
206        OSG_NOTIFY( osg::INFO ) << "---- Executing EventCallback.." <<  std::endl;
207
208        switch( dataIO->clusterMode )
209        {
210                case osgVisual::dataIO_cluster::MASTER : 
211                        {
212                                dataIO->extLink->readTO_OBJvalues();
213                                dataIO->cluster->sendTO_OBJvaluesToSlaves(dataIO->calcViewMatrix());
214                        }
215                        break;
216                case osgVisual::dataIO_cluster::SLAVE : 
217                        {
218                                dataIO->cluster->readTO_OBJvaluesFromMaster();
219                        }
220                        break;
221                case osgVisual::dataIO_cluster::STANDALONE : 
222                        {
223                                dataIO->extLink->readTO_OBJvalues();
224                        }
225                        break;
226                default:
227                        OSG_NOTIFY( osg::FATAL ) << "ERROR: Unkown clustermode!" <<  std::endl;
228                        break;
229        };
230        traverse(node, nv);
231}
232
233void visual_dataIO::dataIO_finalDrawCallback::operator() (const osg::Camera& camera) const
234{
235        // perform all actions for the initialDrawCallback.
236        OSG_NOTIFY( osg::INFO ) << "---- Executing InitialDrawCallback.." << std::endl;
237
238        switch( dataIO->clusterMode )
239        {
240                case osgVisual::dataIO_cluster::MASTER : 
241                        {
242                                dataIO->extLink->writebackFROM_OBJvalues();
243                                dataIO->cluster->waitForAllReadyToSwap();
244                                dataIO->cluster->sendSwapCommand();
245                        }
246                        break;
247                case osgVisual::dataIO_cluster::SLAVE : 
248                        {
249                                dataIO->cluster->reportAsReadyToSwap();
250                                dataIO->cluster->waitForSwap();
251                        }
252                        break;
253                case osgVisual::dataIO_cluster::STANDALONE : 
254                        {
255                                dataIO->extLink->writebackFROM_OBJvalues();
256                        }
257                        break;
258                default:
259                        OSG_NOTIFY( osg::FATAL ) << "ERROR: visual_dataIO::dataIO_finalDrawCallback::operator() - Unkown clustermode!" <<  std::endl;
260                        break;
261        };
262}
263
264void* visual_dataIO::getSlotPointer(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, osgVisual::dataIO_slot::varType variableTyp_ )
265{
266        // iterate through slotlist. If found, return pointer, else add slot to list and return pointer
267        for (unsigned int i=0; i<dataSlots.size(); i++)
268        {
269                // Check if this variable name&-type already exists
270                if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_  && dataSlots[i]->variableType ==  variableTyp_)
271                {
272                        //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotPointer() - Slot found at position " << i << std::endl;
273                        // Return pointer to the value
274                        return dataSlots[i];
275                }
276        }
277
278        // Slot does not exist -> add it to slot list
279        //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotPointer() - Slot not found, will add as new slot " << std::endl;
280        dataIO_slot* newSlot = new dataIO_slot();
281        newSlot->variableName = variableName_;
282        newSlot->variableType = variableTyp_;
283        newSlot->value = 0;
284        newSlot->sValue = "";
285        dataSlots.push_back( newSlot );
286        return dataSlots.back();
287}
288
289double visual_dataIO::getSlotDataAsDouble(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_ )
290{
291        // iterate through slotlist. If found, return value
292        for (unsigned int i=0; i<dataSlots.size(); i++)
293        {
294                // Check if this variable name&-type already exists
295                if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_  && dataSlots[i]->variableType == osgVisual::dataIO_slot::DOUBLE )
296                {
297                        //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotDataAsDouble() - Slot found at position " << i << std::endl;
298                        return dataSlots[i]->value;
299                }
300        }
301        return 0;
302}
303
304std::string visual_dataIO::getSlotDataAsString(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_ )
305{
306        // iterate through slotlist. If found, return value
307        for (unsigned int i=0; i<dataSlots.size(); i++)
308        {
309                // Check if this variable name&-type already exists
310                if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_  && dataSlots[i]->variableType == osgVisual::dataIO_slot::STRING )
311                {
312                        //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotDataAsDouble() - Slot found at position " << i << std::endl;
313                        return dataSlots[i]->sValue;
314                }
315        }
316        return "";
317}
318
319osgVisual::dataIO_slot* visual_dataIO::setSlotData(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, std::string sValue_ )
320{
321        bool slotFound = false;
322        // iterate through slotlist. If found, return pointer, else add slot to list
323        for (unsigned int i=0; i<dataSlots.size(); i++)
324        {
325                // Check if this variable name&-type already exists
326                if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType ==  osgVisual::dataIO_slot::STRING)
327                {
328                        // Update value
329                        dataSlots[i]->sValue = sValue_;
330                        slotFound = true;
331                        return dataSlots[i];
332                }
333               
334        }
335
336        if (!slotFound)
337        {
338                // Slot does not exist -> add it to slot list
339                dataIO_slot* newSlot = new dataIO_slot();
340                newSlot->variableName = variableName_;
341                newSlot->direction = direction_;
342                newSlot->variableType = osgVisual::dataIO_slot::STRING;
343                newSlot->value = 0;
344                newSlot->sValue = sValue_;
345                dataSlots.push_back( newSlot );
346                return dataSlots.back();
347        }
348
349        return NULL;
350}
351
352osgVisual::dataIO_slot* visual_dataIO::setSlotData(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, double value_ )
353{
354        // iterate through slotlist. If found, return pointer, else add slot to list
355        bool slotFound = false;
356        for (unsigned int i=0; i<dataSlots.size(); i++)
357        {
358                // Check if this variableName & -type already exists
359                if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType ==  osgVisual::dataIO_slot::DOUBLE)
360                {
361                        // Update value
362                        //OSG_NOTIFY( osg::ALWAYS ) << "setSlotData: " << variableName_ << " - value: " << value_ << std::endl;
363                        dataSlots[i]->value = value_;
364                        slotFound = true;
365                        return dataSlots[i];
366                }       
367        }
368
369        if (!slotFound)
370        {
371                // Slot does not exist -> add it to slot list
372                dataIO_slot* newSlot = new dataIO_slot();
373                newSlot->variableName = variableName_;
374                newSlot->direction = direction_;
375                newSlot->variableType = osgVisual::dataIO_slot::DOUBLE;
376                newSlot->value = value_;
377                newSlot->sValue = "";
378                dataSlots.push_back( newSlot );
379                return dataSlots.back();
380        }
381
382        return NULL;
383}
384
385osg::Matrixd visual_dataIO::calcViewMatrix()
386{
387        return viewer->getCameraManipulator()->getInverseMatrix();
388}
Note: See TracBrowser for help on using the repository browser.