[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 <dataIO_extLinkVCL.h> |
---|
| 18 | |
---|
[57] | 19 | #include <visual_dataIO.h> // include in.cpp to avoid circular inclusion (visual_dataIO <-> extLinkVCL) |
---|
[31] | 20 | |
---|
| 21 | using namespace osgVisual; |
---|
| 22 | |
---|
[118] | 23 | dataIO_extLinkVCL::dataIO_extLinkVCL(std::vector<dataIO_slot *>& dataSlots_) : dataIO_extLink(dataSlots_) |
---|
[31] | 24 | { |
---|
| 25 | OSG_NOTIFY( osg::ALWAYS ) << "extLinkVCL constructed" << std::endl; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | dataIO_extLinkVCL::~dataIO_extLinkVCL(void) |
---|
| 29 | { |
---|
| 30 | OSG_NOTIFY( osg::ALWAYS ) << "extLinkVCL destroyed" << std::endl; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | void dataIO_extLinkVCL::init() |
---|
| 34 | { |
---|
| 35 | OSG_NOTIFY( osg::ALWAYS ) << "extLinkVCL init()" << std::endl; |
---|
| 36 | |
---|
[116] | 37 | VCLConfigFilename = "osgVisual.xml"; |
---|
| 38 | |
---|
[31] | 39 | if ( osgDB::fileExists( VCLConfigFilename ) ) |
---|
| 40 | { |
---|
| 41 | CVCLIO::GetInstance().LoadProject(VCLConfigFilename.c_str()); |
---|
[116] | 42 | |
---|
| 43 | parseVCLConfig(); |
---|
[118] | 44 | |
---|
[31] | 45 | initialized = true; |
---|
| 46 | } |
---|
| 47 | else |
---|
| 48 | { |
---|
| 49 | OSG_NOTIFY( osg::FATAL ) << "ERROR: Could not find VCL Configuration file " << VCLConfigFilename << std::endl; |
---|
| 50 | exit(-1); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | void dataIO_extLinkVCL::shutdown() |
---|
| 56 | { |
---|
| 57 | OSG_NOTIFY( osg::ALWAYS ) << "extLinkVCL shutdown()" << std::endl; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | bool dataIO_extLinkVCL::readTO_OBJvalues() |
---|
| 61 | { |
---|
| 62 | OSG_NOTIFY( osg::INFO ) << "extLinkVCL readTO_OBJvalues()" << std::endl; |
---|
| 63 | |
---|
| 64 | // perform external data exchange |
---|
| 65 | CVCLIO::GetInstance().DoDataExchange(); |
---|
| 66 | |
---|
[118] | 67 | // read TO_OBJ values from VCL |
---|
| 68 | for(unsigned int i=0;i<extLinkChannels.size();i++) |
---|
| 69 | { |
---|
| 70 | if(extLinkSlots[i]->getdataDirection() == osgVisual::dataIO_slot::TO_OBJ) |
---|
| 71 | { |
---|
| 72 | //Copy data from VCL to slot. IMPORTANT: Due to VCL's string incapability only double slots are filled. |
---|
| 73 | osgVisual::visual_dataIO::getInstance()->setSlotData( extLinkSlots[i]->getVariableName(), osgVisual::dataIO_slot::TO_OBJ, extLinkChannels[i]->GetValue() ); |
---|
| 74 | } // IF (TO_OBJ) END |
---|
| 75 | } |
---|
[31] | 76 | |
---|
| 77 | //OSG_NOTIFY( osg::ALWAYS ) << "LAT: " << osgVisual::visual_dataIO::getInstance()->getSlotDataAsDouble("SAENGER1_POS_LAT", osgVisual::dataIO_slot::TO_OBJ ) << std::endl; |
---|
| 78 | return true; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | bool dataIO_extLinkVCL::writebackFROM_OBJvalues() |
---|
| 82 | { |
---|
| 83 | OSG_NOTIFY( osg::INFO ) << "extLinkVCL writebackFROM_OBJvalues()" << std::endl; |
---|
| 84 | |
---|
| 85 | // Copy slot data to variables |
---|
| 86 | // Nothing to do |
---|
| 87 | // Example: MY_CHANNEL.setValue( myValue ); |
---|
| 88 | |
---|
[118] | 89 | // write FROM_OBJ values into VCL |
---|
| 90 | for(unsigned int i=0;i<extLinkChannels.size();i++) |
---|
| 91 | { |
---|
| 92 | if(extLinkSlots[i]->getdataDirection() == osgVisual::dataIO_slot::FROM_OBJ && extLinkSlots[i]->getvarType() == osgVisual::dataIO_slot::DOUBLE) |
---|
| 93 | { |
---|
| 94 | //Copy data from slot to VCL. IMPORTANT: Due to VCL's string incapability only double slots are filled. |
---|
| 95 | extLinkChannels[i]->SetValue( osgVisual::visual_dataIO::getInstance()->getSlotDataAsDouble( extLinkSlots[i]->getVariableName(), osgVisual::dataIO_slot::FROM_OBJ ) ); |
---|
| 96 | } // IF (FROM_OBJ) END |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /* In VCL no VCL dataexchange is performed, |
---|
[31] | 100 | it is postponed until the next frame beginning, |
---|
| 101 | where TO_OBJ exchange calls CVCLIO::GetInstance().DoDataExchange();. |
---|
| 102 | */ |
---|
| 103 | return true; |
---|
| 104 | } |
---|
[116] | 105 | |
---|
| 106 | bool dataIO_extLinkVCL::parseVCLConfig() |
---|
| 107 | { |
---|
| 108 | configFileValid = false; |
---|
| 109 | xmlDoc *doc = NULL; |
---|
| 110 | xmlNode *root_element = NULL; |
---|
| 111 | |
---|
| 112 | doc = xmlReadFile(VCLConfigFilename.c_str(), NULL, 0); |
---|
| 113 | if (doc == NULL) |
---|
| 114 | { |
---|
| 115 | configFileValid = false; |
---|
| 116 | std::cout << "error: could not parse file" << VCLConfigFilename; |
---|
| 117 | } |
---|
| 118 | else |
---|
| 119 | { |
---|
| 120 | // Get the root element node |
---|
| 121 | root_element = xmlDocGetRootElement(doc); |
---|
| 122 | |
---|
| 123 | checkXMLNode(root_element); |
---|
| 124 | |
---|
| 125 | // free the document |
---|
| 126 | xmlFreeDoc(doc);; |
---|
| 127 | } |
---|
| 128 | // Free the global variables that may have been allocated by the parser. |
---|
| 129 | xmlCleanupParser(); |
---|
| 130 | |
---|
| 131 | if(!configFileValid) |
---|
| 132 | OSG_ALWAYS << "ERROR: XML file seems not to be a valid VCL configuration file!" << std::endl; |
---|
| 133 | |
---|
| 134 | return true; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | void dataIO_extLinkVCL::checkXMLNode(xmlNode * a_node) |
---|
| 138 | { |
---|
| 139 | for (xmlNode *cur_node = a_node; cur_node; cur_node = cur_node->next) |
---|
| 140 | { |
---|
| 141 | std::string node_name=reinterpret_cast<const char*>(cur_node->name); |
---|
| 142 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "CONFIGURATION") |
---|
| 143 | { |
---|
[117] | 144 | //OSG_DEBUG << "XML node CONFIGURATION found" << std::endl; |
---|
[116] | 145 | configFileValid = true; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | if (cur_node->type == XML_ELEMENT_NODE && node_name == "CHANNEL") |
---|
| 149 | { |
---|
[117] | 150 | //OSG_DEBUG << "XML node CHANNEL found" << std::endl; |
---|
[116] | 151 | |
---|
| 152 | // Extract channel infos like name, direction and extract channels |
---|
| 153 | std::string name; |
---|
| 154 | dataIO_slot::dataDirection direction; |
---|
| 155 | xmlAttr *attr = cur_node->properties; |
---|
| 156 | while ( attr ) |
---|
| 157 | { |
---|
| 158 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 159 | if( attr_name == "name" ) |
---|
| 160 | name = reinterpret_cast<const char*>(attr->children->content); |
---|
| 161 | if( attr_name == "multicast_in_group" ) |
---|
| 162 | direction = dataIO_slot::TO_OBJ; |
---|
| 163 | if( attr_name == "multicast_out_group" ) |
---|
| 164 | direction = dataIO_slot::FROM_OBJ; |
---|
| 165 | //std::cout << "Attribute name: " << attr->name << " value: " << attr->children->content << std::endl; |
---|
| 166 | attr = attr->next; |
---|
| 167 | } |
---|
| 168 | addChannels(cur_node->children, name, direction ); |
---|
| 169 | |
---|
[117] | 170 | //OSG_DEBUG << "node type=Element, name:" << cur_node->name << std::endl; |
---|
| 171 | //OSG_DEBUG << "Processing children at " << cur_node->children << std::endl; |
---|
[116] | 172 | } // IF(CHANNEL) END |
---|
| 173 | |
---|
| 174 | // Iterate to the next nodes to find channels. |
---|
| 175 | checkXMLNode(cur_node->children); |
---|
| 176 | } // FOR END |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | void dataIO_extLinkVCL::addChannels(xmlNode * a_node, std::string channelName_, dataIO_slot::dataDirection direction_ ) |
---|
| 180 | { |
---|
| 181 | if(!a_node) |
---|
| 182 | return; |
---|
| 183 | |
---|
| 184 | OSG_ALWAYS << "Processing channels for channel " << channelName_ << " with the direction " << direction_ << std::endl; |
---|
| 185 | |
---|
| 186 | for (xmlNode *cur_node = a_node; cur_node; cur_node = cur_node->next) |
---|
| 187 | { |
---|
[117] | 188 | if (cur_node->type != XML_ELEMENT_NODE) // only emelent nodes are relevant entries. otherwise skip this iteration. VCL files only contain text nodes in comments or as untrimmed element nodes. |
---|
| 189 | continue; |
---|
[116] | 190 | |
---|
[117] | 191 | // Extract ENTRY - name (from the Channel <ENTRY> in the XML file |
---|
[116] | 192 | std::string entryName; |
---|
| 193 | xmlAttr *attr = cur_node->properties; |
---|
| 194 | while ( attr ) |
---|
| 195 | { |
---|
| 196 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 197 | if( attr_name == "name" ) |
---|
| 198 | entryName = reinterpret_cast<const char*>(attr->children->content); |
---|
[117] | 199 | //OSG_DEBUG << "Attribute name: " << attr->name << " value: " << attr->children->content << std::endl; |
---|
[116] | 200 | attr = attr->next; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | // Store VCL variable |
---|
| 204 | CVCLVariable<double>* tmp = new CVCLVariable<double>; |
---|
| 205 | extLinkChannels.push_back(tmp); |
---|
| 206 | |
---|
| 207 | // Attach VCL variable to channel: |
---|
[117] | 208 | //OSG_DEBUG << "attaching.... name: " << channelName_ << ", entryName: " << entryName << std::endl; |
---|
[116] | 209 | if( !tmp->Attach(channelName_.c_str(), entryName.c_str() ) ) |
---|
| 210 | OSG_ALWAYS << "ERROR - dataIO_extLinkVCL::addChannels(): unable to attach VCL variable entryName: " << entryName << " to channel: " << channelName_ << std::endl; |
---|
| 211 | |
---|
| 212 | // Set SLOT data and store SLOT pointer |
---|
[118] | 213 | osgVisual::dataIO_slot* tmpSlot = osgVisual::visual_dataIO::getInstance()->setSlotData( entryName, direction_, 0 ); |
---|
| 214 | extLinkSlots.push_back( tmpSlot ); |
---|
[116] | 215 | } // FOR each ENTRY END |
---|
| 216 | } |
---|