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