1 // Copyright (C) 2009 - 2010 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.
18 #include "RTIObjectInstance.hxx"
19 #include "RTI13Ambassador.hxx"
23 RTI13ObjectInstance::RTI13ObjectInstance(const RTI::ObjectHandle& handle, HLAObjectInstance* objectInstance,
24 const RTI13ObjectClass* objectClass, RTI13Ambassador* ambassador) :
25 RTIObjectInstance(objectInstance),
27 _objectClass(objectClass),
28 _ambassador(ambassador),
29 _attributeValuePairSet(RTI::AttributeSetFactory::create(objectClass->getNumAttributes()))
31 _setNumAttributes(getNumAttributes());
34 RTI13ObjectInstance::~RTI13ObjectInstance()
39 RTI13ObjectInstance::getObjectClass() const
41 return _objectClass.get();
44 const RTI13ObjectClass*
45 RTI13ObjectInstance::get13ObjectClass() const
47 return _objectClass.get();
51 RTI13ObjectInstance::getName() const
53 if (!_ambassador.valid()) {
54 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
59 return _ambassador->getObjectInstanceName(_handle);
60 } catch (RTI::ObjectNotKnown& e) {
61 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name: " << e._name << " " << e._reason);
63 } catch (RTI::FederateNotExecutionMember& e) {
64 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name: " << e._name << " " << e._reason);
66 } catch (RTI::ConcurrentAccessAttempted& e) {
67 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name: " << e._name << " " << e._reason);
69 } catch (RTI::RTIinternalError& e) {
70 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name: " << e._name << " " << e._reason);
73 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name.");
79 RTI13ObjectInstance::deleteObjectInstance(const RTIData& tag)
81 if (!_ambassador.valid()) {
82 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
86 SGSharedPtr<RTI13Federate> federate = _ambassador->_federate.lock();
87 if (!federate.valid()) {
88 SG_LOG(SG_NETWORK, SG_WARN, "Error: Federate is zero.");
93 _ambassador->deleteObjectInstance(_handle, tag);
94 } catch (RTI::ObjectNotKnown& e) {
95 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
96 } catch (RTI::DeletePrivilegeNotHeld& e) {
97 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
98 } catch (RTI::FederateNotExecutionMember& e) {
99 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
100 } catch (RTI::ConcurrentAccessAttempted& e) {
101 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
102 } catch (RTI::SaveInProgress& e) {
103 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
104 } catch (RTI::RestoreInProgress& e) {
105 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
106 } catch (RTI::RTIinternalError& e) {
107 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
112 RTI13ObjectInstance::deleteObjectInstance(const SGTimeStamp& timeStamp, const RTIData& tag)
114 if (!_ambassador.valid()) {
115 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
119 SGSharedPtr<RTI13Federate> federate = _ambassador->_federate.lock();
120 if (!federate.valid()) {
121 SG_LOG(SG_NETWORK, SG_WARN, "Error: Federate is zero.");
126 _ambassador->deleteObjectInstance(_handle, timeStamp, tag);
127 } catch (RTI::ObjectNotKnown& e) {
128 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
129 } catch (RTI::DeletePrivilegeNotHeld& e) {
130 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
131 } catch (RTI::InvalidFederationTime& e) {
132 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
133 } catch (RTI::FederateNotExecutionMember& e) {
134 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
135 } catch (RTI::ConcurrentAccessAttempted& e) {
136 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
137 } catch (RTI::SaveInProgress& e) {
138 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
139 } catch (RTI::RestoreInProgress& e) {
140 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
141 } catch (RTI::RTIinternalError& e) {
142 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
147 RTI13ObjectInstance::localDeleteObjectInstance()
149 if (!_ambassador.valid()) {
150 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
154 SGSharedPtr<RTI13Federate> federate = _ambassador->_federate.lock();
155 if (!federate.valid()) {
156 SG_LOG(SG_NETWORK, SG_WARN, "Error: Federate is zero.");
161 _ambassador->localDeleteObjectInstance(_handle);
162 } catch (RTI::ObjectNotKnown& e) {
163 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
164 } catch (RTI::FederateOwnsAttributes& e) {
165 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
166 } catch (RTI::FederateNotExecutionMember& e) {
167 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
168 } catch (RTI::ConcurrentAccessAttempted& e) {
169 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
170 } catch (RTI::SaveInProgress& e) {
171 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
172 } catch (RTI::RestoreInProgress& e) {
173 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
174 } catch (RTI::RTIinternalError& e) {
175 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
180 RTI13ObjectInstance::reflectAttributeValues(RTI13AttributeHandleDataPairList& attributeHandleDataPairList,
181 const RTIData& tag, HLAIndexList& indexPool)
183 HLAIndexList reflectedIndices;
184 for (RTI13AttributeHandleDataPairList::iterator i = attributeHandleDataPairList.begin();
185 i != attributeHandleDataPairList.end(); ++i) {
186 unsigned index = getAttributeIndex(i->first);
187 _attributeData[index]._data.swap(i->second);
189 if (indexPool.empty())
190 reflectedIndices.push_back(index);
192 reflectedIndices.splice(reflectedIndices.end(), indexPool, indexPool.begin());
193 reflectedIndices.back() = index;
197 RTIObjectInstance::reflectAttributeValues(reflectedIndices, tag);
199 // Return the index list to the pool
200 indexPool.splice(indexPool.end(), reflectedIndices);
204 RTI13ObjectInstance::reflectAttributeValues(RTI13AttributeHandleDataPairList& attributeHandleDataPairList,
205 const SGTimeStamp& timeStamp, const RTIData& tag, HLAIndexList& indexPool)
207 HLAIndexList reflectedIndices;
208 for (RTI13AttributeHandleDataPairList::iterator i = attributeHandleDataPairList.begin();
209 i != attributeHandleDataPairList.end(); ++i) {
210 unsigned index = getAttributeIndex(i->first);
211 _attributeData[index]._data.swap(i->second);
213 if (indexPool.empty())
214 reflectedIndices.push_back(index);
216 reflectedIndices.splice(reflectedIndices.end(), indexPool, indexPool.begin());
217 reflectedIndices.back() = index;
221 RTIObjectInstance::reflectAttributeValues(reflectedIndices, timeStamp, tag);
223 // Return the index list to the pool
224 indexPool.splice(indexPool.end(), reflectedIndices);
228 RTI13ObjectInstance::requestObjectAttributeValueUpdate(const HLAIndexList& indexList)
230 if (!_ambassador.valid()) {
231 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
235 if (indexList.empty())
239 unsigned numAttributes = getNumAttributes();
240 std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(numAttributes));
241 for (HLAIndexList::const_iterator i = indexList.begin(); i != indexList.end(); ++i) {
242 if (getAttributeOwned(*i)) {
243 SG_LOG(SG_NETWORK, SG_WARN, "RTI13ObjectInstance::requestObjectAttributeValueUpdate(): "
244 "Invalid attribute index!");
247 attributeHandleSet->add(getAttributeHandle(*i));
249 if (!attributeHandleSet->size())
252 _ambassador->requestObjectAttributeValueUpdate(_handle, *attributeHandleSet);
255 } catch (RTI::ObjectNotKnown& e) {
256 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
258 } catch (RTI::AttributeNotDefined& e) {
259 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
261 } catch (RTI::FederateNotExecutionMember& e) {
262 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
264 } catch (RTI::ConcurrentAccessAttempted& e) {
265 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
267 } catch (RTI::SaveInProgress& e) {
268 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
270 } catch (RTI::RestoreInProgress& e) {
271 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
273 } catch (RTI::RTIinternalError& e) {
274 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
277 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance.");
283 RTI13ObjectInstance::provideAttributeValueUpdate(const std::vector<RTI::AttributeHandle>& attributeHandleSet)
285 // Called from the ambassador. Just marks some instance attributes dirty so that they are sent with the next update
286 size_t numAttribs = attributeHandleSet.size();
287 for (RTI::ULong i = 0; i < numAttribs; ++i) {
288 unsigned index = getAttributeIndex(attributeHandleSet[i]);
289 _attributeData[index]._dirty = true;
294 RTI13ObjectInstance::updateAttributeValues(const RTIData& tag)
296 if (!_ambassador.valid()) {
297 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
302 // That means clear()
303 _attributeValuePairSet->empty();
305 unsigned numAttributes = getNumAttributes();
306 for (unsigned i = 0; i < numAttributes; ++i) {
307 if (!_attributeData[i]._dirty)
309 const RTIData& data = _attributeData[i]._data;
310 _attributeValuePairSet->add(getAttributeHandle(i), data.data(), data.size());
313 if (!_attributeValuePairSet->size())
316 _ambassador->updateAttributeValues(_handle, *_attributeValuePairSet, tag);
318 for (unsigned i = 0; i < numAttributes; ++i) {
319 _attributeData[i]._dirty = false;
322 } catch (RTI::ObjectNotKnown& e) {
323 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
324 } catch (RTI::AttributeNotDefined& e) {
325 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
326 } catch (RTI::AttributeNotOwned& e) {
327 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
328 } catch (RTI::FederateNotExecutionMember& e) {
329 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
330 } catch (RTI::ConcurrentAccessAttempted& e) {
331 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
332 } catch (RTI::SaveInProgress& e) {
333 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
334 } catch (RTI::RestoreInProgress& e) {
335 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
336 } catch (RTI::RTIinternalError& e) {
337 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
340 // That means clear()
341 _attributeValuePairSet->empty();
345 RTI13ObjectInstance::updateAttributeValues(const SGTimeStamp& timeStamp, const RTIData& tag)
347 if (!_ambassador.valid()) {
348 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
353 // That means clear()
354 _attributeValuePairSet->empty();
356 unsigned numAttributes = getNumAttributes();
357 for (unsigned i = 0; i < numAttributes; ++i) {
358 if (!_attributeData[i]._dirty)
360 const RTIData& data = _attributeData[i]._data;
361 _attributeValuePairSet->add(getAttributeHandle(i), data.data(), data.size());
364 if (!_attributeValuePairSet->size())
367 _ambassador->updateAttributeValues(_handle, *_attributeValuePairSet, timeStamp, tag);
369 for (unsigned i = 0; i < numAttributes; ++i) {
370 _attributeData[i]._dirty = false;
373 } catch (RTI::ObjectNotKnown& e) {
374 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
375 } catch (RTI::AttributeNotDefined& e) {
376 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
377 } catch (RTI::AttributeNotOwned& e) {
378 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
379 } catch (RTI::InvalidFederationTime& e) {
380 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
381 } catch (RTI::FederateNotExecutionMember& e) {
382 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
383 } catch (RTI::ConcurrentAccessAttempted& e) {
384 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
385 } catch (RTI::SaveInProgress& e) {
386 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
387 } catch (RTI::RestoreInProgress& e) {
388 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
389 } catch (RTI::RTIinternalError& e) {
390 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
393 // That means clear()
394 _attributeValuePairSet->empty();
398 RTI13ObjectInstance::attributesInScope(const std::vector<RTI::AttributeHandle>& attributeHandleSet)
400 size_t numAttribs = attributeHandleSet.size();
401 for (RTI::ULong i = 0; i < numAttribs; ++i) {
402 RTI::AttributeHandle attributeHandle = attributeHandleSet[i];
403 setAttributeInScope(getAttributeIndex(attributeHandle), true);
408 RTI13ObjectInstance::attributesOutOfScope(const std::vector<RTI::AttributeHandle>& attributeHandleSet)
410 size_t numAttribs = attributeHandleSet.size();
411 for (RTI::ULong i = 0; i < numAttribs; ++i) {
412 RTI::AttributeHandle attributeHandle = attributeHandleSet[i];
413 setAttributeInScope(getAttributeIndex(attributeHandle), false);
418 RTI13ObjectInstance::turnUpdatesOnForObjectInstance(const std::vector<RTI::AttributeHandle>& attributeHandleSet)
420 size_t numAttribs = attributeHandleSet.size();
421 for (RTI::ULong i = 0; i < numAttribs; ++i) {
422 RTI::AttributeHandle attributeHandle = attributeHandleSet[i];
423 setAttributeUpdateEnabled(getAttributeIndex(attributeHandle), true);
428 RTI13ObjectInstance::turnUpdatesOffForObjectInstance(const std::vector<RTI::AttributeHandle>& attributeHandleSet)
430 size_t numAttribs = attributeHandleSet.size();
431 for (RTI::ULong i = 0; i < numAttribs; ++i) {
432 RTI::AttributeHandle attributeHandle = attributeHandleSet[i];
433 setAttributeUpdateEnabled(getAttributeIndex(attributeHandle), false);
438 RTI13ObjectInstance::isAttributeOwnedByFederate(unsigned index) const
440 if (!_ambassador.valid()) {
441 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
445 RTI::AttributeHandle attributeHandle = getAttributeHandle(index);
446 return _ambassador->isAttributeOwnedByFederate(_handle, attributeHandle);
447 } catch (RTI::ObjectNotKnown& e) {
448 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not query attribute ownership for index " << index << ": "
449 << e._name << " " << e._reason);
451 } catch (RTI::AttributeNotDefined& e) {
452 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not query attribute ownership for index " << index << ": "
453 << e._name << " " << e._reason);
455 } catch (RTI::FederateNotExecutionMember& e) {
456 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not query attribute ownership for index " << index << ": "
457 << e._name << " " << e._reason);
459 } catch (RTI::ConcurrentAccessAttempted& e) {
460 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not query attribute ownership for index " << index << ": "
461 << e._name << " " << e._reason);
463 } catch (RTI::SaveInProgress& e) {
464 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not query attribute ownership for index " << index << ": "
465 << e._name << " " << e._reason);
467 } catch (RTI::RestoreInProgress& e) {
468 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not query attribute ownership for index " << index << ": "
469 << e._name << " " << e._reason);
471 } catch (RTI::RTIinternalError& e) {
472 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not query attribute ownership for index " << index << ": "
473 << e._name << " " << e._reason);
480 RTI13ObjectInstance::requestAttributeOwnershipAssumption(const std::vector<RTI::AttributeHandle>& attributes, const RTIData& tag)
485 RTI13ObjectInstance::attributeOwnershipDivestitureNotification(const std::vector<RTI::AttributeHandle>& attributes)
490 RTI13ObjectInstance::attributeOwnershipAcquisitionNotification(const std::vector<RTI::AttributeHandle>& attributes)
495 RTI13ObjectInstance::attributeOwnershipUnavailable(const std::vector<RTI::AttributeHandle>& attributes)
500 RTI13ObjectInstance::requestAttributeOwnershipRelease(const std::vector<RTI::AttributeHandle>& attributes, const RTIData& tag)
505 RTI13ObjectInstance::confirmAttributeOwnershipAcquisitionCancellation(const std::vector<RTI::AttributeHandle>& attributes)
510 RTI13ObjectInstance::informAttributeOwnership(RTI::AttributeHandle attributeHandle, RTI::FederateHandle federateHandle)
515 RTI13ObjectInstance::attributeIsNotOwned(RTI::AttributeHandle attributeHandle)
520 RTI13ObjectInstance::attributeOwnedByRTI(RTI::AttributeHandle attributeHandle)