1 // Copyright (C) 2009 - 2012 Mathias Froehlich - Mathias.Froehlich@web.de
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # include <simgear_config.h>
22 #include <simgear/compiler.h>
24 #include "HLAObjectInstance.hxx"
27 #include "simgear/debug/logstream.hxx"
28 #include "HLAArrayDataElement.hxx"
29 #include "HLABasicDataElement.hxx"
30 #include "HLADataElement.hxx"
31 #include "HLAEnumeratedDataElement.hxx"
32 #include "HLAFederate.hxx"
33 #include "HLAFixedRecordDataElement.hxx"
34 #include "HLAObjectClass.hxx"
35 #include "HLAVariantRecordDataElement.hxx"
36 #include "RTIObjectClass.hxx"
37 #include "RTIObjectInstance.hxx"
41 HLAObjectInstance::UpdateCallback::~UpdateCallback()
45 HLAObjectInstance::ReflectCallback::~ReflectCallback()
49 HLAObjectInstance::HLAObjectInstance(HLAObjectClass* objectClass) :
50 _objectClass(objectClass)
53 _federate = objectClass->_federate;
56 HLAObjectInstance::~HLAObjectInstance()
58 _clearRTIObjectInstance();
62 HLAObjectInstance::getName() const
67 const SGWeakPtr<HLAFederate>&
68 HLAObjectInstance::getFederate() const
73 const SGSharedPtr<HLAObjectClass>&
74 HLAObjectInstance::getObjectClass() const
80 HLAObjectInstance::getNumAttributes() const
82 if (!_objectClass.valid())
84 return _objectClass->getNumAttributes();
88 HLAObjectInstance::getAttributeIndex(const std::string& name) const
90 if (!_objectClass.valid())
92 return _objectClass->getAttributeIndex(name);
96 HLAObjectInstance::getAttributeName(unsigned index) const
98 if (!_objectClass.valid())
100 return _objectClass->getAttributeName(index);
104 HLAObjectInstance::getAttributeOwned(unsigned index) const
106 if (!_rtiObjectInstance.valid())
108 return _rtiObjectInstance->getAttributeOwned(index);
112 HLAObjectInstance::getAttributeDataType(unsigned index) const
114 if (!_objectClass.valid())
116 return _objectClass->getAttributeDataType(index);
120 HLAObjectInstance::getAttributeDataElement(unsigned index)
122 if (_attributeVector.size() <= index)
124 return _attributeVector[index]._dataElement.get();
127 const HLADataElement*
128 HLAObjectInstance::getAttributeDataElement(unsigned index) const
130 if (_attributeVector.size() <= index)
132 return _attributeVector[index]._dataElement.get();
136 HLAObjectInstance::getAttributeData(unsigned index, RTIData& data) const
138 if (!_rtiObjectInstance.valid()) {
139 SG_LOG(SG_IO, SG_ALERT, "Trying to get raw attribute data without rti object instance for \"" << getName() << "\"!");
142 return _rtiObjectInstance->getAttributeData(index, data);
146 HLAObjectInstance::setAttributeDataElement(unsigned index, const SGSharedPtr<HLADataElement>& dataElement)
148 unsigned numAttributes = getNumAttributes();
149 if (numAttributes <= index)
151 _attributeVector.resize(numAttributes);
152 if (_attributeVector[index]._dataElement.valid())
153 _attributeVector[index]._dataElement->clearStamp();
154 _attributeVector[index]._dataElement = dataElement;
155 if (_attributeVector[index]._dataElement.valid())
156 _attributeVector[index]._dataElement->createStamp();
159 class HLAObjectInstance::DataElementFactoryVisitor : public HLADataElementFactoryVisitor {
161 DataElementFactoryVisitor(const HLAPathElementMap& pathElementMap) :
162 _pathElementMap(pathElementMap)
164 DataElementFactoryVisitor(const HLADataElement::Path& path, const HLAPathElementMap& pathElementMap) :
165 _pathElementMap(pathElementMap),
168 virtual ~DataElementFactoryVisitor() {}
170 virtual void apply(const HLADataType& dataType)
172 _dataElement = createDataElement(_path, dataType);
173 if (_dataElement.valid())
176 SG_LOG(SG_NETWORK, SG_ALERT, "HLA: Can not find a suitable data element for data type \""
177 << dataType.getName() << "\"");
180 virtual void apply(const HLAInt8DataType& dataType)
182 _dataElement = createDataElement(_path, dataType);
183 if (_dataElement.valid())
186 HLADataElementFactoryVisitor::apply(dataType);
188 virtual void apply(const HLAUInt8DataType& dataType)
190 _dataElement = createDataElement(_path, dataType);
191 if (_dataElement.valid())
194 HLADataElementFactoryVisitor::apply(dataType);
196 virtual void apply(const HLAInt16DataType& dataType)
198 _dataElement = createDataElement(_path, dataType);
199 if (_dataElement.valid())
202 HLADataElementFactoryVisitor::apply(dataType);
204 virtual void apply(const HLAUInt16DataType& dataType)
206 _dataElement = createDataElement(_path, dataType);
207 if (_dataElement.valid())
210 HLADataElementFactoryVisitor::apply(dataType);
212 virtual void apply(const HLAInt32DataType& dataType)
214 _dataElement = createDataElement(_path, dataType);
215 if (_dataElement.valid())
218 HLADataElementFactoryVisitor::apply(dataType);
220 virtual void apply(const HLAUInt32DataType& dataType)
222 _dataElement = createDataElement(_path, dataType);
223 if (_dataElement.valid())
226 HLADataElementFactoryVisitor::apply(dataType);
228 virtual void apply(const HLAInt64DataType& dataType)
230 _dataElement = createDataElement(_path, dataType);
231 if (_dataElement.valid())
234 HLADataElementFactoryVisitor::apply(dataType);
236 virtual void apply(const HLAUInt64DataType& dataType)
238 _dataElement = createDataElement(_path, dataType);
239 if (_dataElement.valid())
242 HLADataElementFactoryVisitor::apply(dataType);
244 virtual void apply(const HLAFloat32DataType& dataType)
246 _dataElement = createDataElement(_path, dataType);
247 if (_dataElement.valid())
250 HLADataElementFactoryVisitor::apply(dataType);
252 virtual void apply(const HLAFloat64DataType& dataType)
254 _dataElement = createDataElement(_path, dataType);
255 if (_dataElement.valid())
258 HLADataElementFactoryVisitor::apply(dataType);
261 class ArrayDataElementFactory : public HLAArrayDataElement::DataElementFactory {
263 ArrayDataElementFactory(const HLADataElement::Path& path, const HLAPathElementMap& pathElementMap) :
266 for (HLAPathElementMap::const_iterator i = pathElementMap.lower_bound(path);
267 i != pathElementMap.end(); ++i) {
268 if (i->first.begin() != std::search(i->first.begin(), i->first.end(),
269 path.begin(), path.end()))
271 _pathElementMap.insert(*i);
274 virtual HLADataElement* createElement(const HLAArrayDataElement& element, unsigned index)
276 const HLADataType* dataType = element.getElementDataType();
279 HLADataElement::Path path = _path;
280 path.push_back(HLADataElement::PathElement(index));
281 DataElementFactoryVisitor visitor(path, _pathElementMap);
282 dataType->accept(visitor);
283 return visitor._dataElement.release();
286 HLADataElement::Path _path;
287 HLAPathElementMap _pathElementMap;
290 virtual void apply(const HLAFixedArrayDataType& dataType)
292 _dataElement = createDataElement(_path, dataType);
293 if (_dataElement.valid())
296 SGSharedPtr<HLAArrayDataElement> arrayDataElement;
297 arrayDataElement = new HLAArrayDataElement(&dataType);
298 arrayDataElement->setDataElementFactory(new ArrayDataElementFactory(_path, _pathElementMap));
299 arrayDataElement->setNumElements(dataType.getNumElements());
301 _dataElement = arrayDataElement;
304 virtual void apply(const HLAVariableArrayDataType& dataType)
306 _dataElement = createDataElement(_path, dataType);
307 if (_dataElement.valid())
310 SGSharedPtr<HLAArrayDataElement> arrayDataElement;
311 arrayDataElement = new HLAArrayDataElement(&dataType);
312 arrayDataElement->setDataElementFactory(new ArrayDataElementFactory(_path, _pathElementMap));
314 _dataElement = arrayDataElement;
317 virtual void apply(const HLAEnumeratedDataType& dataType)
319 _dataElement = createDataElement(_path, dataType);
320 if (_dataElement.valid())
323 HLADataElementFactoryVisitor::apply(dataType);
326 virtual void apply(const HLAFixedRecordDataType& dataType)
328 _dataElement = createDataElement(_path, dataType);
329 if (_dataElement.valid())
332 SGSharedPtr<HLAFixedRecordDataElement> recordDataElement;
333 recordDataElement = new HLAFixedRecordDataElement(&dataType);
335 unsigned numFields = dataType.getNumFields();
336 for (unsigned i = 0; i < numFields; ++i) {
338 _path.push_back(HLADataElement::PathElement(dataType.getFieldName(i)));
340 dataType.getFieldDataType(i)->accept(*this);
341 recordDataElement->setField(i, _dataElement.release());
345 _dataElement = recordDataElement;
348 class VariantRecordDataElementFactory : public HLAVariantRecordDataElement::DataElementFactory {
350 VariantRecordDataElementFactory(const HLADataElement::Path& path, const HLAPathElementMap& pathElementMap) :
353 for (HLAPathElementMap::const_iterator i = pathElementMap.lower_bound(path);
354 i != pathElementMap.end(); ++i) {
355 if (i->first.begin() != std::search(i->first.begin(), i->first.end(),
356 path.begin(), path.end()))
358 _pathElementMap.insert(*i);
361 virtual HLADataElement* createElement(const HLAVariantRecordDataElement& element, unsigned index)
363 const HLAVariantRecordDataType* dataType = element.getDataType();
366 const HLADataType* alternativeDataType = element.getAlternativeDataType();
367 if (!alternativeDataType)
369 HLADataElement::Path path = _path;
370 path.push_back(HLADataElement::PathElement(dataType->getAlternativeName(index)));
371 DataElementFactoryVisitor visitor(path, _pathElementMap);
372 alternativeDataType->accept(visitor);
373 return visitor._dataElement.release();
376 HLADataElement::Path _path;
377 HLAPathElementMap _pathElementMap;
380 virtual void apply(const HLAVariantRecordDataType& dataType)
382 _dataElement = createDataElement(_path, dataType);
383 if (_dataElement.valid())
386 SGSharedPtr<HLAVariantRecordDataElement> variantRecordDataElement;
387 variantRecordDataElement = new HLAVariantRecordDataElement(&dataType);
388 variantRecordDataElement->setDataElementFactory(new VariantRecordDataElementFactory(_path, _pathElementMap));
390 _dataElement = variantRecordDataElement;
394 SGSharedPtr<HLADataElement> createDataElement(const HLADataElement::Path& path, const HLADataType& dataType)
396 HLAPathElementMap::const_iterator i = _pathElementMap.find(path);
397 if (i == _pathElementMap.end()) {
398 SG_LOG(SG_IO, SG_WARN, "No dataElement provided for \""
399 << HLADataElement::toString(path) << "\".");
403 SGSharedPtr<HLADataElement> dataElement = i->second.getDataElement(path);
404 if (!dataElement->setDataType(&dataType)) {
405 SG_LOG(SG_IO, SG_ALERT, "Cannot set data type for data element at \""
406 << HLADataElement::toString(path) << "\"!");
409 SG_LOG(SG_IO, SG_DEBUG, "Using provided dataElement for \""
410 << HLADataElement::toString(path) << "\".");
414 const HLAPathElementMap& _pathElementMap;
415 HLADataElement::Path _path;
419 HLAObjectInstance::setAttribute(unsigned index, const HLAPathElementMap& pathElementMap)
421 const HLADataType* dataType = getAttributeDataType(index);
423 SG_LOG(SG_IO, SG_ALERT, "Cannot get attribute data type for setting attribute \""
424 << getAttributeName(index) << "\" at index " << index << "!");
428 SG_LOG(SG_IO, SG_DEBUG, "Setting DataElement for attribute \""
429 << getAttributeName(index) << "\".");
431 DataElementFactoryVisitor visitor(pathElementMap);
432 dataType->accept(visitor);
433 setAttributeDataElement(index, visitor.getDataElement());
437 HLAObjectInstance::setAttributes(const HLAAttributePathElementMap& attributePathElementMap)
439 for (HLAAttributePathElementMap::const_iterator i = attributePathElementMap.begin();
440 i != attributePathElementMap.end(); ++i) {
441 setAttribute(i->first, i->second);
446 HLAObjectInstance::registerInstance()
448 registerInstance(_objectClass.get());
452 HLAObjectInstance::registerInstance(HLAObjectClass* objectClass)
454 if (_rtiObjectInstance.valid()) {
455 SG_LOG(SG_IO, SG_ALERT, "Trying to register object " << getName() << " already known to the RTI!");
459 SG_LOG(SG_IO, SG_ALERT, "Could not register object with unknown object class!");
462 if (_objectClass.valid() && objectClass != _objectClass.get()) {
463 SG_LOG(SG_IO, SG_ALERT, "Could not change object class while registering!");
466 _objectClass = objectClass;
467 _federate = _objectClass->_federate;
468 // This error must have been flagged before
469 if (!_objectClass->_rtiObjectClass.valid())
471 _setRTIObjectInstance(_objectClass->_rtiObjectClass->registerObjectInstance(this));
472 if (!_rtiObjectInstance.valid()) {
473 SG_LOG(SG_IO, SG_ALERT, "Could not register object at the RTI!");
476 _objectClass->_registerInstance(this);
480 HLAObjectInstance::deleteInstance(const RTIData& tag)
482 if (!_rtiObjectInstance.valid()) {
483 SG_LOG(SG_IO, SG_ALERT, "Trying to delete inactive object!");
486 if (!_objectClass.valid())
488 _objectClass->_deleteInstance(*this);
489 _rtiObjectInstance->deleteObjectInstance(tag);
493 HLAObjectInstance::updateAttributeValues(const RTIData& tag)
495 if (_attributeCallback.valid())
496 _attributeCallback->updateAttributeValues(*this, tag);
497 if (_updateCallback.valid()) {
498 _updateCallback->updateAttributeValues(*this, tag);
500 encodeAttributeValues();
501 sendAttributeValues(tag);
506 HLAObjectInstance::updateAttributeValues(const SGTimeStamp& timeStamp, const RTIData& tag)
508 if (_attributeCallback.valid())
509 _attributeCallback->updateAttributeValues(*this, tag);
510 if (_updateCallback.valid()) {
511 _updateCallback->updateAttributeValues(*this, timeStamp, tag);
513 encodeAttributeValues();
514 sendAttributeValues(timeStamp, tag);
519 HLAObjectInstance::encodeAttributeValues()
521 unsigned numAttributes = _attributeVector.size();
522 for (unsigned i = 0; i < numAttributes;++i) {
523 if (_attributeVector[i]._unconditionalUpdate) {
524 encodeAttributeValue(i);
525 } else if (_attributeVector[i]._enabledUpdate) {
526 const HLADataElement* dataElement = getAttributeDataElement(i);
527 if (dataElement && dataElement->getDirty())
528 encodeAttributeValue(i);
534 HLAObjectInstance::encodeAttributeValue(unsigned index)
536 if (!_rtiObjectInstance.valid()) {
537 SG_LOG(SG_IO, SG_INFO, "Not updating inactive object!");
540 HLADataElement* dataElement = getAttributeDataElement(index);
543 _rtiObjectInstance->encodeAttributeData(index, *dataElement);
544 dataElement->setDirty(false);
548 HLAObjectInstance::sendAttributeValues(const RTIData& tag)
550 if (!_rtiObjectInstance.valid()) {
551 SG_LOG(SG_IO, SG_INFO, "Not updating inactive object!");
554 _rtiObjectInstance->updateAttributeValues(tag);
558 HLAObjectInstance::sendAttributeValues(const SGTimeStamp& timeStamp, const RTIData& tag)
560 if (!_rtiObjectInstance.valid()) {
561 SG_LOG(SG_IO, SG_INFO, "Not updating inactive object!");
564 _rtiObjectInstance->updateAttributeValues(timeStamp, tag);
568 HLAObjectInstance::reflectAttributeValues(const HLAIndexList& indexList, const RTIData& tag)
570 for (HLAIndexList::const_iterator i = indexList.begin(); i != indexList.end(); ++i)
571 reflectAttributeValue(*i, tag);
575 HLAObjectInstance::reflectAttributeValues(const HLAIndexList& indexList,
576 const SGTimeStamp& timeStamp, const RTIData& tag)
578 for (HLAIndexList::const_iterator i = indexList.begin(); i != indexList.end(); ++i)
579 reflectAttributeValue(*i, timeStamp, tag);
583 HLAObjectInstance::reflectAttributeValue(unsigned index, const RTIData& tag)
585 HLADataElement* dataElement = getAttributeDataElement(index);
588 dataElement->setTimeStampValid(false);
589 _rtiObjectInstance->decodeAttributeData(index, *dataElement);
593 HLAObjectInstance::reflectAttributeValue(unsigned index, const SGTimeStamp& timeStamp, const RTIData& tag)
595 HLADataElement* dataElement = getAttributeDataElement(index);
598 dataElement->setTimeStamp(timeStamp);
599 dataElement->setTimeStampValid(true);
600 _rtiObjectInstance->decodeAttributeData(index, *dataElement);
604 HLAObjectInstance::_setRTIObjectInstance(RTIObjectInstance* rtiObjectInstance)
606 if (!_objectClass.valid())
609 _rtiObjectInstance = rtiObjectInstance;
610 _rtiObjectInstance->setObjectInstance(this);
611 _name = _rtiObjectInstance->getName();
613 unsigned numAttributes = getNumAttributes();
614 _attributeVector.resize(numAttributes);
615 for (unsigned i = 0; i < numAttributes; ++i) {
616 HLAUpdateType updateType = _objectClass->getAttributeUpdateType(i);
617 if (getAttributeOwned(i) && updateType != HLAUndefinedUpdate) {
618 _attributeVector[i]._enabledUpdate = true;
619 _attributeVector[i]._unconditionalUpdate = (updateType == HLAPeriodicUpdate);
620 // In case of an owned attribute, now encode its value
621 encodeAttributeValue(i);
623 _attributeVector[i]._enabledUpdate = false;
624 _attributeVector[i]._unconditionalUpdate = false;
628 // This makes sense with any new object. Even if we registered one, there might be unpublished attributes.
629 HLAIndexList indexList;
630 for (unsigned i = 0; i < numAttributes; ++i) {
631 HLAUpdateType updateType = _objectClass->getAttributeUpdateType(i);
632 if (getAttributeOwned(i))
634 if (updateType == HLAUndefinedUpdate)
636 if (updateType == HLAPeriodicUpdate)
638 indexList.push_back(i);
640 _rtiObjectInstance->requestObjectAttributeValueUpdate(indexList);
644 HLAObjectInstance::_clearRTIObjectInstance()
646 if (!_rtiObjectInstance.valid())
649 for (unsigned i = 0; i < _attributeVector.size(); ++i) {
650 _attributeVector[i]._enabledUpdate = false;
651 _attributeVector[i]._unconditionalUpdate = false;
654 _rtiObjectInstance->setObjectInstance(0);
655 _rtiObjectInstance = 0;
659 HLAObjectInstance::_removeInstance(const RTIData& tag)
661 if (!_objectClass.valid())
663 _objectClass->_removeInstance(*this, tag);
667 HLAObjectInstance::_reflectAttributeValues(const HLAIndexList& indexList, const RTIData& tag)
669 if (_reflectCallback.valid()) {
670 _reflectCallback->reflectAttributeValues(*this, indexList, tag);
671 } else if (_attributeCallback.valid()) {
672 reflectAttributeValues(indexList, tag);
674 RTIIndexDataPairList dataPairList;
675 for (HLAIndexList::const_iterator i = indexList.begin(); i != indexList.end(); ++i) {
676 dataPairList.push_back(RTIIndexDataPair());
677 dataPairList.back().first = *i;
678 getAttributeData(*i, dataPairList.back().second);
680 _attributeCallback->reflectAttributeValues(*this, dataPairList, tag);
682 reflectAttributeValues(indexList, tag);
687 HLAObjectInstance::_reflectAttributeValues(const HLAIndexList& indexList, const SGTimeStamp& timeStamp, const RTIData& tag)
689 if (_reflectCallback.valid()) {
690 _reflectCallback->reflectAttributeValues(*this, indexList, timeStamp, tag);
691 } else if (_attributeCallback.valid()) {
692 reflectAttributeValues(indexList, timeStamp, tag);
694 RTIIndexDataPairList dataPairList;
695 for (HLAIndexList::const_iterator i = indexList.begin(); i != indexList.end(); ++i) {
696 dataPairList.push_back(RTIIndexDataPair());
697 dataPairList.back().first = *i;
698 getAttributeData(*i, dataPairList.back().second);
700 _attributeCallback->reflectAttributeValues(*this, dataPairList, timeStamp, tag);
702 reflectAttributeValues(indexList, timeStamp, tag);
706 } // namespace simgear