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* hlaObjectInstance,
24 const RTI13ObjectClass* objectClass, RTI13Ambassador* ambassador, bool owned) :
25 RTIObjectInstance(hlaObjectInstance),
27 _objectClass(objectClass),
28 _ambassador(ambassador),
29 _attributeValuePairSet(RTI::AttributeSetFactory::create(objectClass->getNumAttributes()))
31 updateAttributesFromClass(owned);
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(const RTI::AttributeHandleValuePairSet& attributeValuePairSet, const RTIData& tag)
182 // Retrieve an empty update struct from the memory pool
183 UpdateList updateList;
184 getUpdateFromPool(updateList);
186 RTI::ULong numAttribs = attributeValuePairSet.size();
187 for (RTI::ULong i = 0; i < numAttribs; ++i) {
188 unsigned index = getAttributeIndex(attributeValuePairSet.getHandle(i));
189 // Get a RTIData from the data pool
190 getDataFromPool(index, updateList.back()._indexDataPairList);
191 RTI::ULong length = attributeValuePairSet.getValueLength(i);
192 updateList.back()._indexDataPairList.back().second.resize(length);
193 attributeValuePairSet.getValue(i, updateList.back()._indexDataPairList.back().second.data(), length);
194 updateList.back()._tag = tag;
197 RTIObjectInstance::reflectAttributeValues(updateList.front()._indexDataPairList, tag);
198 // Return the update data back to the pool
199 putUpdateToPool(updateList);
203 RTI13ObjectInstance::reflectAttributeValues(const RTI::AttributeHandleValuePairSet& attributeValuePairSet, const SGTimeStamp& timeStamp, const RTIData& tag)
205 // Retrieve an empty update struct from the memory pool
206 UpdateList updateList;
207 getUpdateFromPool(updateList);
209 RTI::ULong numAttribs = attributeValuePairSet.size();
210 for (RTI::ULong i = 0; i < numAttribs; ++i) {
211 unsigned index = getAttributeIndex(attributeValuePairSet.getHandle(i));
212 // Get a RTIData from the data pool
213 getDataFromPool(index, updateList.back()._indexDataPairList);
214 RTI::ULong length = attributeValuePairSet.getValueLength(i);
215 updateList.back()._indexDataPairList.back().second.resize(length);
216 attributeValuePairSet.getValue(i, updateList.back()._indexDataPairList.back().second.data(), length);
217 updateList.back()._tag = tag;
220 scheduleUpdates(timeStamp, updateList);
224 RTI13ObjectInstance::requestObjectAttributeValueUpdate()
226 if (!_ambassador.valid()) {
227 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
232 unsigned numAttributes = getNumAttributes();
233 std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(numAttributes));
234 for (unsigned i = 0; i < numAttributes; ++i) {
235 if (!getRequestAttributeUpdate(i))
237 attributeHandleSet->add(getAttributeHandle(i));
239 if (!attributeHandleSet->size())
242 _ambassador->requestObjectAttributeValueUpdate(_handle, *attributeHandleSet);
244 for (unsigned i = 0; i < numAttributes; ++i)
245 setRequestAttributeUpdate(i, false);
248 } catch (RTI::ObjectNotKnown& e) {
249 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
251 } catch (RTI::AttributeNotDefined& e) {
252 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
254 } catch (RTI::FederateNotExecutionMember& e) {
255 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
257 } catch (RTI::ConcurrentAccessAttempted& e) {
258 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
260 } catch (RTI::SaveInProgress& e) {
261 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
263 } catch (RTI::RestoreInProgress& e) {
264 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
266 } catch (RTI::RTIinternalError& e) {
267 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
270 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance.");
276 RTI13ObjectInstance::provideAttributeValueUpdate(const RTI::AttributeHandleSet& attributes)
278 // Called from the ambassador. Just marks some instance attributes dirty so that they are sent with the next update
279 RTI::ULong numAttribs = attributes.size();
280 for (RTI::ULong i = 0; i < numAttribs; ++i) {
281 unsigned index = getAttributeIndex(attributes.getHandle(i));
282 setAttributeForceUpdate(index);
287 RTI13ObjectInstance::updateAttributeValues(const RTIData& tag)
289 if (!_ambassador.valid()) {
290 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
295 // That means clear()
296 _attributeValuePairSet->empty();
298 unsigned numAttributes = getNumAttributes();
299 for (unsigned i = 0; i < numAttributes; ++i) {
300 if (!getAttributeEffectiveUpdateEnabled(i))
302 const HLADataElement* dataElement = getDataElement(i);
305 // FIXME cache somewhere
307 HLAEncodeStream stream(data);
308 dataElement->encode(stream);
309 _attributeValuePairSet->add(getAttributeHandle(i), data.data(), data.size());
312 if (!_attributeValuePairSet->size())
315 _ambassador->updateAttributeValues(_handle, *_attributeValuePairSet, tag);
317 for (unsigned i = 0; i < numAttributes; ++i) {
318 setAttributeUpdated(i);
321 } catch (RTI::ObjectNotKnown& e) {
322 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
323 } catch (RTI::AttributeNotDefined& e) {
324 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
325 } catch (RTI::AttributeNotOwned& e) {
326 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
327 } catch (RTI::FederateNotExecutionMember& e) {
328 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
329 } catch (RTI::ConcurrentAccessAttempted& e) {
330 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
331 } catch (RTI::SaveInProgress& e) {
332 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
333 } catch (RTI::RestoreInProgress& e) {
334 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
335 } catch (RTI::RTIinternalError& e) {
336 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
339 // That means clear()
340 _attributeValuePairSet->empty();
344 RTI13ObjectInstance::updateAttributeValues(const SGTimeStamp& timeStamp, const RTIData& tag)
346 if (!_ambassador.valid()) {
347 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
352 // That means clear()
353 _attributeValuePairSet->empty();
355 unsigned numAttributes = getNumAttributes();
356 for (unsigned i = 0; i < numAttributes; ++i) {
357 if (!getAttributeEffectiveUpdateEnabled(i))
359 const HLADataElement* dataElement = getDataElement(i);
362 // FIXME cache somewhere
364 HLAEncodeStream stream(data);
365 dataElement->encode(stream);
366 _attributeValuePairSet->add(getAttributeHandle(i), data.data(), data.size());
369 if (!_attributeValuePairSet->size())
372 _ambassador->updateAttributeValues(_handle, *_attributeValuePairSet, timeStamp, tag);
374 for (unsigned i = 0; i < numAttributes; ++i) {
375 setAttributeUpdated(i);
378 } catch (RTI::ObjectNotKnown& e) {
379 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
380 } catch (RTI::AttributeNotDefined& e) {
381 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
382 } catch (RTI::AttributeNotOwned& e) {
383 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
384 } catch (RTI::InvalidFederationTime& e) {
385 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
386 } catch (RTI::FederateNotExecutionMember& e) {
387 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
388 } catch (RTI::ConcurrentAccessAttempted& e) {
389 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
390 } catch (RTI::SaveInProgress& e) {
391 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
392 } catch (RTI::RestoreInProgress& e) {
393 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
394 } catch (RTI::RTIinternalError& e) {
395 SG_LOG(SG_NETWORK, SG_WARN, "RTI: could not update attribute values object instance: " << e._name << " " << e._reason);
398 // That means clear()
399 _attributeValuePairSet->empty();
403 RTI13ObjectInstance::attributesInScope(const RTI::AttributeHandleSet& attributeHandleSet)
405 RTI::ULong numAttribs = attributeHandleSet.size();
406 for (RTI::ULong i = 0; i < numAttribs; ++i) {
407 RTI::AttributeHandle attributeHandle = attributeHandleSet.getHandle(i);
408 setAttributeInScope(getAttributeIndex(attributeHandle), true);
413 RTI13ObjectInstance::attributesOutOfScope(const RTI::AttributeHandleSet& attributeHandleSet)
415 RTI::ULong numAttribs = attributeHandleSet.size();
416 for (RTI::ULong i = 0; i < numAttribs; ++i) {
417 RTI::AttributeHandle attributeHandle = attributeHandleSet.getHandle(i);
418 setAttributeInScope(getAttributeIndex(attributeHandle), false);
423 RTI13ObjectInstance::turnUpdatesOnForObjectInstance(const RTI::AttributeHandleSet& attributeHandleSet)
425 RTI::ULong numAttribs = attributeHandleSet.size();
426 for (RTI::ULong i = 0; i < numAttribs; ++i) {
427 RTI::AttributeHandle attributeHandle = attributeHandleSet.getHandle(i);
428 setAttributeUpdateEnabled(getAttributeIndex(attributeHandle), true);
433 RTI13ObjectInstance::turnUpdatesOffForObjectInstance(const RTI::AttributeHandleSet& attributeHandleSet)
435 RTI::ULong numAttribs = attributeHandleSet.size();
436 for (RTI::ULong i = 0; i < numAttribs; ++i) {
437 RTI::AttributeHandle attributeHandle = attributeHandleSet.getHandle(i);
438 setAttributeUpdateEnabled(getAttributeIndex(attributeHandle), false);
443 RTI13ObjectInstance::requestAttributeOwnershipAssumption(const RTI::AttributeHandleSet& attributes, const RTIData& tag)
448 RTI13ObjectInstance::attributeOwnershipDivestitureNotification(const RTI::AttributeHandleSet& attributes)
453 RTI13ObjectInstance::attributeOwnershipAcquisitionNotification(const RTI::AttributeHandleSet& attributes)
458 RTI13ObjectInstance::attributeOwnershipUnavailable(const RTI::AttributeHandleSet& attributes)
463 RTI13ObjectInstance::requestAttributeOwnershipRelease(const RTI::AttributeHandleSet& attributes, const RTIData& tag)
468 RTI13ObjectInstance::confirmAttributeOwnershipAcquisitionCancellation(const RTI::AttributeHandleSet& attributes)
473 RTI13ObjectInstance::informAttributeOwnership(RTI::AttributeHandle attributeHandle, RTI::FederateHandle federateHandle)
478 RTI13ObjectInstance::attributeIsNotOwned(RTI::AttributeHandle attributeHandle)
483 RTI13ObjectInstance::attributeOwnedByRTI(RTI::AttributeHandle attributeHandle)