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 "HLAObjectClass.hxx"
26 #include "simgear/debug/logstream.hxx"
27 #include "RTIFederate.hxx"
28 #include "RTIObjectClass.hxx"
29 #include "RTIObjectInstance.hxx"
30 #include "HLADataType.hxx"
31 #include "HLADataTypeVisitor.hxx"
32 #include "HLAFederate.hxx"
33 #include "HLAObjectInstance.hxx"
37 HLAObjectClass::InstanceCallback::~InstanceCallback()
42 HLAObjectClass::InstanceCallback::discoverInstance(const HLAObjectClass&, HLAObjectInstance& objectInstance, const RTIData& tag)
47 HLAObjectClass::InstanceCallback::removeInstance(const HLAObjectClass&, HLAObjectInstance& objectInstance, const RTIData& tag)
52 HLAObjectClass::InstanceCallback::registerInstance(const HLAObjectClass&, HLAObjectInstance& objectInstance)
57 HLAObjectClass::InstanceCallback::deleteInstance(const HLAObjectClass&, HLAObjectInstance& objectInstance)
61 HLAObjectClass::RegistrationCallback::~RegistrationCallback()
65 HLAObjectClass::HLAObjectClass(const std::string& name, HLAFederate* federate) :
70 SG_LOG(SG_NETWORK, SG_ALERT, "HLAObjectClass::HLAObjectClass(): "
71 "No parent federate given for object class \"" << getName() << "\"!");
74 federate->_insertObjectClass(this);
77 HLAObjectClass::~HLAObjectClass()
79 // HLAObjectClass objects only get deleted when the parent federate
80 // dies. So we do not need to deregister there.
82 _clearRTIObjectClass();
86 HLAObjectClass::getName() const
91 const SGWeakPtr<HLAFederate>&
92 HLAObjectClass::getFederate() const
98 HLAObjectClass::getNumAttributes() const
100 return _attributeVector.size();
104 HLAObjectClass::addAttribute(const std::string& name)
106 unsigned index = _attributeVector.size();
107 _nameIndexMap[name] = index;
108 _attributeVector.push_back(Attribute(name));
109 _resolveAttributeIndex(name, index);
114 HLAObjectClass::getAttributeIndex(const std::string& name) const
116 NameIndexMap::const_iterator i = _nameIndexMap.find(name);
117 if (i == _nameIndexMap.end())
123 HLAObjectClass::getAttributeName(unsigned index) const
125 if (_attributeVector.size() <= index)
126 return std::string();
127 return _attributeVector[index]._name;
131 HLAObjectClass::getAttributeDataType(unsigned index) const
133 if (_attributeVector.size() <= index)
135 return _attributeVector[index]._dataType.get();
139 HLAObjectClass::setAttributeDataType(unsigned index, const HLADataType* dataType)
141 if (_attributeVector.size() <= index)
143 _attributeVector[index]._dataType = dataType;
147 HLAObjectClass::getAttributeUpdateType(unsigned index) const
149 if (_attributeVector.size() <= index)
150 return HLAUndefinedUpdate;
151 return _attributeVector[index]._updateType;
155 HLAObjectClass::setAttributeUpdateType(unsigned index, HLAUpdateType updateType)
157 if (_attributeVector.size() <= index)
159 _attributeVector[index]._updateType = updateType;
163 HLAObjectClass::getAttributeSubscriptionType(unsigned index) const
165 if (_attributeVector.size() <= index)
166 return HLAUnsubscribed;
167 return _attributeVector[index]._subscriptionType;
171 HLAObjectClass::setAttributeSubscriptionType(unsigned index, HLASubscriptionType subscriptionType)
173 if (_attributeVector.size() <= index)
175 _attributeVector[index]._subscriptionType = subscriptionType;
179 HLAObjectClass::getAttributePublicationType(unsigned index) const
181 if (_attributeVector.size() <= index)
182 return HLAUnpublished;
183 return _attributeVector[index]._publicationType;
187 HLAObjectClass::setAttributePublicationType(unsigned index, HLAPublicationType publicationType)
189 if (_attributeVector.size() <= index)
191 _attributeVector[index]._publicationType = publicationType;
194 HLADataElement::IndexPathPair
195 HLAObjectClass::getIndexPathPair(const HLADataElement::StringPathPair& stringPathPair) const
197 unsigned index = getAttributeIndex(stringPathPair.first);
198 if (getNumAttributes() <= index) {
199 SG_LOG(SG_NETWORK, SG_ALERT, "HLAObjectClass::getIndexPathPair(\""
200 << HLADataElement::toString(stringPathPair)
201 << "\"): Could not resolve attribute \"" << stringPathPair.first
202 << "\" for object class \"" << getName() << "\"!");
204 return HLADataElement::IndexPathPair(index, stringPathPair.second);
207 HLADataElement::IndexPathPair
208 HLAObjectClass::getIndexPathPair(const std::string& path) const
210 return getIndexPathPair(HLADataElement::toStringPathPair(path));
214 HLAObjectClass::getAttributeIndex(HLADataElementIndex& dataElementIndex, const std::string& path) const
217 SG_LOG(SG_NETWORK, SG_ALERT, "HLAObjectClass: failed to parse empty element path!");
220 size_t len = path.find_first_of("[.");
222 while (index < getNumAttributes()) {
223 if (path.compare(0, len, getAttributeName(index)) == 0)
227 if (getNumAttributes() <= index) {
228 SG_LOG(SG_NETWORK, SG_ALERT, "HLAObjectClass: faild to parse data element index \"" << path << "\":\n"
229 << "Attribute \"" << path.substr(0, len) << "\" not found in object class \""
230 << getName() << "\"!");
233 if (!getAttributeDataType(index)) {
234 SG_LOG(SG_NETWORK, SG_ALERT, "HLAObjectClass: faild to parse data element index \"" << path << "\":\n"
235 << "Undefined attribute data type in variant record data type \""
236 << getAttributeName(index) << "\"!");
239 dataElementIndex.push_back(index);
240 return getAttributeDataType(index)->getDataElementIndex(dataElementIndex, path, len);
244 HLAObjectClass::subscribe()
246 if (!_rtiObjectClass.valid()) {
247 SG_LOG(SG_NETWORK, SG_WARN, "HLAObjectClass::subscribe(): "
248 "No RTIObject class for object class \"" << getName() << "\"!");
252 HLAIndexList indexList;
253 for (unsigned i = 1; i < getNumAttributes(); ++i) {
254 if (_attributeVector[i]._subscriptionType != HLASubscribedActive)
256 indexList.push_back(i);
258 if (!indexList.empty()) {
259 if (!_rtiObjectClass->subscribe(indexList, true))
264 for (unsigned i = 1; i < getNumAttributes(); ++i) {
265 if (_attributeVector[i]._subscriptionType != HLASubscribedPassive)
267 indexList.push_back(i);
269 if (!indexList.empty()) {
270 if (!_rtiObjectClass->subscribe(indexList, false))
277 HLAObjectClass::unsubscribe()
279 if (!_rtiObjectClass.valid()) {
280 SG_LOG(SG_NETWORK, SG_WARN, "HLAObjectClass::unsubscribe(): "
281 "No RTIObject class for object class \"" << getName() << "\"!");
284 return _rtiObjectClass->unsubscribe();
288 HLAObjectClass::publish()
290 if (!_rtiObjectClass.valid()) {
291 SG_LOG(SG_NETWORK, SG_WARN, "HLAObjectClass::publish(): "
292 "No RTIObject class for object class \"" << getName() << "\"!");
296 HLAIndexList indexList;
297 for (unsigned i = 1; i < getNumAttributes(); ++i) {
298 if (_attributeVector[i]._publicationType == HLAUnpublished)
300 indexList.push_back(i);
302 if (indexList.empty())
304 if (!_rtiObjectClass->publish(indexList))
310 HLAObjectClass::unpublish()
312 if (!_rtiObjectClass.valid()) {
313 SG_LOG(SG_NETWORK, SG_WARN, "HLAObjectClass::unpublish(): "
314 "No RTIObject class for object class \"" << getName() << "\"!");
317 return _rtiObjectClass->unpublish();
321 HLAObjectClass::discoverInstance(HLAObjectInstance& objectInstance, const RTIData& tag)
323 if (_instanceCallback.valid())
324 _instanceCallback->discoverInstance(*this, objectInstance, tag);
328 HLAObjectClass::removeInstance(HLAObjectInstance& objectInstance, const RTIData& tag)
330 if (_instanceCallback.valid())
331 _instanceCallback->removeInstance(*this, objectInstance, tag);
335 HLAObjectClass::registerInstance(HLAObjectInstance& objectInstance)
337 if (_instanceCallback.valid())
338 _instanceCallback->registerInstance(*this, objectInstance);
342 HLAObjectClass::deleteInstance(HLAObjectInstance& objectInstance)
344 if (_instanceCallback.valid())
345 _instanceCallback->deleteInstance(*this, objectInstance);
349 HLAObjectClass::startRegistration() const
354 HLAObjectClass::stopRegistration() const
359 HLAObjectClass::createObjectInstance(const std::string& name)
361 SGSharedPtr<HLAFederate> federate = _federate.lock();
362 if (!federate.valid())
364 return federate->createObjectInstance(this, name);
368 HLAObjectClass::createAttributeDataElements(HLAObjectInstance& objectInstance)
370 unsigned numAttributes = getNumAttributes();
371 for (unsigned i = 0; i < numAttributes; ++i)
372 objectInstance.createAndSetAttributeDataElement(i);
376 HLAObjectClass::createAttributeDataElement(HLAObjectInstance& objectInstance, unsigned index)
378 // FIXME here we want to have a vector of factories and if this fails do the following
379 const HLADataType* dataType = getAttributeDataType(index);
382 HLADataElementFactoryVisitor dataElementFactoryVisitor;
383 dataType->accept(dataElementFactoryVisitor);
384 return dataElementFactoryVisitor.getDataElement();
388 HLAObjectClass::_setRTIObjectClass(RTIObjectClass* objectClass)
390 if (_rtiObjectClass) {
391 SG_LOG(SG_NETWORK, SG_ALERT, "HLAObjectClass: Setting RTIObjectClass twice for object class \"" << getName() << "\"!");
394 _rtiObjectClass = objectClass;
395 if (_rtiObjectClass->_objectClass != this) {
396 SG_LOG(SG_NETWORK, SG_ALERT, "HLAObjectClass: backward reference does not match!");
399 for (unsigned i = 0; i < _attributeVector.size(); ++i)
400 _resolveAttributeIndex(_attributeVector[i]._name, i);
404 HLAObjectClass::_resolveAttributeIndex(const std::string& name, unsigned index)
406 if (!_rtiObjectClass)
408 if (!_rtiObjectClass->resolveAttributeIndex(name, index))
409 SG_LOG(SG_NETWORK, SG_ALERT, "HLAObjectClass: Could not resolve attribute \""
410 << name << "\" for object class \"" << getName() << "\"!");
414 HLAObjectClass::_clearRTIObjectClass()
416 if (!_rtiObjectClass.valid())
418 _rtiObjectClass->_objectClass = 0;
423 HLAObjectClass::_discoverInstance(RTIObjectInstance* rtiObjectInstance, const RTIData& tag)
425 SGSharedPtr<HLAFederate> federate = _federate.lock();
426 if (!federate.valid()) {
427 SG_LOG(SG_NETWORK, SG_ALERT, "RTI: could not find parent federate while discovering object instance");
431 SGSharedPtr<HLAObjectInstance> objectInstance = createObjectInstance(rtiObjectInstance->getName());
432 if (!objectInstance.valid()) {
433 SG_LOG(SG_NETWORK, SG_INFO, "RTI: could not create new object instance for discovered \""
434 << rtiObjectInstance->getName() << "\" object");
437 SG_LOG(SG_NETWORK, SG_INFO, "RTI: create new object instance for discovered \""
438 << rtiObjectInstance->getName() << "\" object");
439 objectInstance->_setRTIObjectInstance(rtiObjectInstance);
440 if (!federate->_insertObjectInstance(objectInstance)) {
441 SG_LOG(SG_NETWORK, SG_ALERT, "RTI: could not insert new object instance for discovered \""
442 << rtiObjectInstance->getName() << "\" object");
445 objectInstance->discoverInstance(tag);
446 objectInstance->createAttributeDataElements();
450 HLAObjectClass::_removeInstance(HLAObjectInstance& objectInstance, const RTIData& tag)
452 SGSharedPtr<HLAFederate> federate = _federate.lock();
453 if (!federate.valid()) {
454 SG_LOG(SG_NETWORK, SG_ALERT, "RTI: could not find parent federate while removing object instance");
457 SG_LOG(SG_NETWORK, SG_INFO, "RTI: remove object instance \"" << objectInstance.getName() << "\"");
458 objectInstance.removeInstance(tag);
459 federate->_eraseObjectInstance(objectInstance.getName());
463 HLAObjectClass::_registerInstance(HLAObjectInstance* objectInstance)
465 SGSharedPtr<HLAFederate> federate = _federate.lock();
466 if (!federate.valid()) {
467 SG_LOG(SG_NETWORK, SG_ALERT, "RTI: could not find parent federate while registering object instance");
472 // We can only register object instances with a valid name at the rti.
473 // So, we cannot do that at HLAObjectInstance creation time.
474 if (!federate->_insertObjectInstance(objectInstance)) {
475 SG_LOG(SG_NETWORK, SG_ALERT, "RTI: could not insert new object instance \""
476 << objectInstance->getName() << "\" object");
479 registerInstance(*objectInstance);
480 objectInstance->createAttributeDataElements();
484 HLAObjectClass::_deleteInstance(HLAObjectInstance& objectInstance)
486 SGSharedPtr<HLAFederate> federate = _federate.lock();
487 if (!federate.valid()) {
488 SG_LOG(SG_NETWORK, SG_ALERT, "RTI: could not find parent federate while deleting object instance");
491 deleteInstance(objectInstance);
492 federate->_eraseObjectInstance(objectInstance.getName());
496 HLAObjectClass::_startRegistration()
498 if (_registrationCallback.valid())
499 _registrationCallback->startRegistration(*this);
505 HLAObjectClass::_stopRegistration()
507 if (_registrationCallback.valid())
508 _registrationCallback->stopRegistration(*this);
513 } // namespace simgear