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 "RTIObjectInstance.hxx"
25 #include "RTI13Ambassador.hxx"
29 RTI13ObjectInstance::RTI13ObjectInstance(const RTI::ObjectHandle& handle, HLAObjectInstance* objectInstance,
30 const RTI13ObjectClass* objectClass, RTI13Ambassador* ambassador) :
31 RTIObjectInstance(objectInstance),
33 _objectClass(objectClass),
34 _ambassador(ambassador),
35 _attributeValuePairSet(RTI::AttributeSetFactory::create(objectClass->getNumAttributes()))
37 _setNumAttributes(getNumAttributes());
40 RTI13ObjectInstance::~RTI13ObjectInstance()
45 RTI13ObjectInstance::getObjectClass() const
47 return _objectClass.get();
50 const RTI13ObjectClass*
51 RTI13ObjectInstance::get13ObjectClass() const
53 return _objectClass.get();
57 RTI13ObjectInstance::getName() const
59 if (!_ambassador.valid()) {
60 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
65 return _ambassador->getObjectInstanceName(_handle);
66 } catch (RTI::ObjectNotKnown& e) {
67 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name: " << e._name << " " << e._reason);
69 } catch (RTI::FederateNotExecutionMember& e) {
70 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name: " << e._name << " " << e._reason);
72 } catch (RTI::ConcurrentAccessAttempted& e) {
73 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name: " << e._name << " " << e._reason);
75 } catch (RTI::RTIinternalError& e) {
76 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name: " << e._name << " " << e._reason);
79 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name.");
85 RTI13ObjectInstance::deleteObjectInstance(const RTIData& tag)
87 if (!_ambassador.valid()) {
88 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
92 SGSharedPtr<RTI13Federate> federate = _ambassador->_federate.lock();
93 if (!federate.valid()) {
94 SG_LOG(SG_NETWORK, SG_WARN, "Error: Federate is zero.");
99 _ambassador->deleteObjectInstance(_handle, tag);
100 } catch (RTI::ObjectNotKnown& e) {
101 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
102 } catch (RTI::DeletePrivilegeNotHeld& e) {
103 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
104 } catch (RTI::FederateNotExecutionMember& e) {
105 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
106 } catch (RTI::ConcurrentAccessAttempted& e) {
107 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
108 } catch (RTI::SaveInProgress& e) {
109 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
110 } catch (RTI::RestoreInProgress& e) {
111 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
112 } catch (RTI::RTIinternalError& e) {
113 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
118 RTI13ObjectInstance::deleteObjectInstance(const SGTimeStamp& timeStamp, const RTIData& tag)
120 if (!_ambassador.valid()) {
121 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
125 SGSharedPtr<RTI13Federate> federate = _ambassador->_federate.lock();
126 if (!federate.valid()) {
127 SG_LOG(SG_NETWORK, SG_WARN, "Error: Federate is zero.");
132 _ambassador->deleteObjectInstance(_handle, timeStamp, tag);
133 } catch (RTI::ObjectNotKnown& e) {
134 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
135 } catch (RTI::DeletePrivilegeNotHeld& e) {
136 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
137 } catch (RTI::InvalidFederationTime& e) {
138 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
139 } catch (RTI::FederateNotExecutionMember& e) {
140 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
141 } catch (RTI::ConcurrentAccessAttempted& e) {
142 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
143 } catch (RTI::SaveInProgress& e) {
144 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
145 } catch (RTI::RestoreInProgress& e) {
146 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
147 } catch (RTI::RTIinternalError& e) {
148 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
153 RTI13ObjectInstance::localDeleteObjectInstance()
155 if (!_ambassador.valid()) {
156 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
160 SGSharedPtr<RTI13Federate> federate = _ambassador->_federate.lock();
161 if (!federate.valid()) {
162 SG_LOG(SG_NETWORK, SG_WARN, "Error: Federate is zero.");
167 _ambassador->localDeleteObjectInstance(_handle);
168 } catch (RTI::ObjectNotKnown& e) {
169 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
170 } catch (RTI::FederateOwnsAttributes& e) {
171 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
172 } catch (RTI::FederateNotExecutionMember& e) {
173 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
174 } catch (RTI::ConcurrentAccessAttempted& e) {
175 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
176 } catch (RTI::SaveInProgress& e) {
177 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
178 } catch (RTI::RestoreInProgress& e) {
179 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
180 } catch (RTI::RTIinternalError& e) {
181 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
186 RTI13ObjectInstance::reflectAttributeValues(RTI13AttributeHandleDataPairList& attributeHandleDataPairList,
187 const RTIData& tag, HLAIndexList& indexPool)
189 HLAIndexList reflectedIndices;
190 for (RTI13AttributeHandleDataPairList::iterator i = attributeHandleDataPairList.begin();
191 i != attributeHandleDataPairList.end(); ++i) {
192 unsigned index = getAttributeIndex(i->first);
193 _attributeData[index]._data.swap(i->second);
195 if (indexPool.empty())
196 reflectedIndices.push_back(index);
198 reflectedIndices.splice(reflectedIndices.end(), indexPool, indexPool.begin());
199 reflectedIndices.back() = index;
203 RTIObjectInstance::reflectAttributeValues(reflectedIndices, tag);
205 // Return the index list to the pool
206 indexPool.splice(indexPool.end(), reflectedIndices);
210 RTI13ObjectInstance::reflectAttributeValues(RTI13AttributeHandleDataPairList& attributeHandleDataPairList,
211 const SGTimeStamp& timeStamp, const RTIData& tag, HLAIndexList& indexPool)
213 HLAIndexList reflectedIndices;
214 for (RTI13AttributeHandleDataPairList::iterator i = attributeHandleDataPairList.begin();
215 i != attributeHandleDataPairList.end(); ++i) {
216 unsigned index = getAttributeIndex(i->first);
217 _attributeData[index]._data.swap(i->second);
219 if (indexPool.empty())
220 reflectedIndices.push_back(index);
222 reflectedIndices.splice(reflectedIndices.end(), indexPool, indexPool.begin());
223 reflectedIndices.back() = index;
227 RTIObjectInstance::reflectAttributeValues(reflectedIndices, timeStamp, tag);
229 // Return the index list to the pool
230 indexPool.splice(indexPool.end(), reflectedIndices);
234 RTI13ObjectInstance::requestObjectAttributeValueUpdate(const HLAIndexList& indexList)
236 if (!_ambassador.valid()) {
237 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
241 if (indexList.empty())
245 unsigned numAttributes = getNumAttributes();
246 std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(numAttributes));
247 for (HLAIndexList::const_iterator i = indexList.begin(); i != indexList.end(); ++i) {
248 if (getAttributeOwned(*i)) {
249 SG_LOG(SG_NETWORK, SG_WARN, "RTI13ObjectInstance::requestObjectAttributeValueUpdate(): "
250 "Invalid attribute index!");
253 attributeHandleSet->add(getAttributeHandle(*i));
255 if (!attributeHandleSet->size())
258 _ambassador->requestObjectAttributeValueUpdate(_handle, *attributeHandleSet);
261 } catch (RTI::ObjectNotKnown& e) {
262 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
264 } catch (RTI::AttributeNotDefined& e) {
265 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
267 } catch (RTI::FederateNotExecutionMember& e) {
268 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
270 } catch (RTI::ConcurrentAccessAttempted& e) {
271 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
273 } catch (RTI::SaveInProgress& e) {
274 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
276 } catch (RTI::RestoreInProgress& e) {
277 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
279 } catch (RTI::RTIinternalError& e) {
280 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
283 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance.");
289 RTI13ObjectInstance::provideAttributeValueUpdate(const std::vector<RTI::AttributeHandle>& attributeHandleSet)
291 // Called from the ambassador. Just marks some instance attributes dirty so that they are sent with the next update
292 size_t numAttribs = attributeHandleSet.size();
293 for (RTI::ULong i = 0; i < numAttribs; ++i) {
294 unsigned index = getAttributeIndex(attributeHandleSet[i]);
295 _attributeData[index]._dirty = true;
300 RTI13ObjectInstance::updateAttributeValues(const RTIData& tag)
302 if (!_ambassador.valid()) {
303 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
308 // That means clear()
309 _attributeValuePairSet->empty();
311 unsigned numAttributes = getNumAttributes();
312 for (unsigned i = 0; i < numAttributes; ++i) {
313 if (!_attributeData[i]._dirty)
315 const RTIData& data = _attributeData[i]._data;
316 _attributeValuePairSet->add(getAttributeHandle(i), data.data(), data.size());
319 if (!_attributeValuePairSet->size())
322 _ambassador->updateAttributeValues(_handle, *_attributeValuePairSet, tag);
324 for (unsigned i = 0; i < numAttributes; ++i) {
325 _attributeData[i]._dirty = false;
328 } catch (RTI::ObjectNotKnown& e) {
329 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
330 } catch (RTI::AttributeNotDefined& e) {
331 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
332 } catch (RTI::AttributeNotOwned& e) {
333 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
334 } catch (RTI::FederateNotExecutionMember& e) {
335 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
336 } catch (RTI::ConcurrentAccessAttempted& e) {
337 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
338 } catch (RTI::SaveInProgress& e) {
339 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
340 } catch (RTI::RestoreInProgress& e) {
341 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
342 } catch (RTI::RTIinternalError& e) {
343 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
346 // That means clear()
347 _attributeValuePairSet->empty();
351 RTI13ObjectInstance::updateAttributeValues(const SGTimeStamp& timeStamp, const RTIData& tag)
353 if (!_ambassador.valid()) {
354 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
359 // That means clear()
360 _attributeValuePairSet->empty();
362 unsigned numAttributes = getNumAttributes();
363 for (unsigned i = 0; i < numAttributes; ++i) {
364 if (!_attributeData[i]._dirty)
366 const RTIData& data = _attributeData[i]._data;
367 _attributeValuePairSet->add(getAttributeHandle(i), data.data(), data.size());
370 if (!_attributeValuePairSet->size())
373 _ambassador->updateAttributeValues(_handle, *_attributeValuePairSet, timeStamp, tag);
375 for (unsigned i = 0; i < numAttributes; ++i) {
376 _attributeData[i]._dirty = false;
379 } catch (RTI::ObjectNotKnown& e) {
380 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
381 } catch (RTI::AttributeNotDefined& e) {
382 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
383 } catch (RTI::AttributeNotOwned& e) {
384 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
385 } catch (RTI::InvalidFederationTime& e) {
386 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
387 } catch (RTI::FederateNotExecutionMember& e) {
388 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
389 } catch (RTI::ConcurrentAccessAttempted& e) {
390 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
391 } catch (RTI::SaveInProgress& e) {
392 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
393 } catch (RTI::RestoreInProgress& e) {
394 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
395 } catch (RTI::RTIinternalError& e) {
396 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
399 // That means clear()
400 _attributeValuePairSet->empty();
404 RTI13ObjectInstance::attributesInScope(const std::vector<RTI::AttributeHandle>& attributeHandleSet)
406 size_t numAttribs = attributeHandleSet.size();
407 for (RTI::ULong i = 0; i < numAttribs; ++i) {
408 RTI::AttributeHandle attributeHandle = attributeHandleSet[i];
409 setAttributeInScope(getAttributeIndex(attributeHandle), true);
414 RTI13ObjectInstance::attributesOutOfScope(const std::vector<RTI::AttributeHandle>& attributeHandleSet)
416 size_t numAttribs = attributeHandleSet.size();
417 for (RTI::ULong i = 0; i < numAttribs; ++i) {
418 RTI::AttributeHandle attributeHandle = attributeHandleSet[i];
419 setAttributeInScope(getAttributeIndex(attributeHandle), false);
424 RTI13ObjectInstance::turnUpdatesOnForObjectInstance(const std::vector<RTI::AttributeHandle>& attributeHandleSet)
426 size_t numAttribs = attributeHandleSet.size();
427 for (RTI::ULong i = 0; i < numAttribs; ++i) {
428 RTI::AttributeHandle attributeHandle = attributeHandleSet[i];
429 setAttributeUpdateEnabled(getAttributeIndex(attributeHandle), true);
434 RTI13ObjectInstance::turnUpdatesOffForObjectInstance(const std::vector<RTI::AttributeHandle>& attributeHandleSet)
436 size_t numAttribs = attributeHandleSet.size();
437 for (RTI::ULong i = 0; i < numAttribs; ++i) {
438 RTI::AttributeHandle attributeHandle = attributeHandleSet[i];
439 setAttributeUpdateEnabled(getAttributeIndex(attributeHandle), false);
444 RTI13ObjectInstance::isAttributeOwnedByFederate(unsigned index) const
446 if (!_ambassador.valid()) {
447 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
451 RTI::AttributeHandle attributeHandle = getAttributeHandle(index);
452 return _ambassador->isAttributeOwnedByFederate(_handle, attributeHandle);
453 } catch (RTI::ObjectNotKnown& e) {
454 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not query attribute ownership for index " << index << ": "
455 << e._name << " " << e._reason);
457 } catch (RTI::AttributeNotDefined& e) {
458 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not query attribute ownership for index " << index << ": "
459 << e._name << " " << e._reason);
461 } catch (RTI::FederateNotExecutionMember& e) {
462 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not query attribute ownership for index " << index << ": "
463 << e._name << " " << e._reason);
465 } catch (RTI::ConcurrentAccessAttempted& e) {
466 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not query attribute ownership for index " << index << ": "
467 << e._name << " " << e._reason);
469 } catch (RTI::SaveInProgress& e) {
470 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not query attribute ownership for index " << index << ": "
471 << e._name << " " << e._reason);
473 } catch (RTI::RestoreInProgress& e) {
474 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not query attribute ownership for index " << index << ": "
475 << e._name << " " << e._reason);
477 } catch (RTI::RTIinternalError& e) {
478 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not query attribute ownership for index " << index << ": "
479 << e._name << " " << e._reason);
486 RTI13ObjectInstance::requestAttributeOwnershipAssumption(const std::vector<RTI::AttributeHandle>& attributes, const RTIData& tag)
491 RTI13ObjectInstance::attributeOwnershipDivestitureNotification(const std::vector<RTI::AttributeHandle>& attributes)
496 RTI13ObjectInstance::attributeOwnershipAcquisitionNotification(const std::vector<RTI::AttributeHandle>& attributes)
501 RTI13ObjectInstance::attributeOwnershipUnavailable(const std::vector<RTI::AttributeHandle>& attributes)
506 RTI13ObjectInstance::requestAttributeOwnershipRelease(const std::vector<RTI::AttributeHandle>& attributes, const RTIData& tag)
511 RTI13ObjectInstance::confirmAttributeOwnershipAcquisitionCancellation(const std::vector<RTI::AttributeHandle>& attributes)
516 RTI13ObjectInstance::informAttributeOwnership(RTI::AttributeHandle attributeHandle, RTI::FederateHandle federateHandle)
521 RTI13ObjectInstance::attributeIsNotOwned(RTI::AttributeHandle attributeHandle)
526 RTI13ObjectInstance::attributeOwnedByRTI(RTI::AttributeHandle attributeHandle)