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 SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
54 if (!ambassador.valid()) {
55 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
60 return ambassador->getObjectInstanceName(_handle);
61 } catch (RTI::ObjectNotKnown& e) {
62 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name: " << e._name << " " << e._reason);
64 } catch (RTI::FederateNotExecutionMember& e) {
65 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name: " << e._name << " " << e._reason);
67 } catch (RTI::ConcurrentAccessAttempted& e) {
68 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name: " << e._name << " " << e._reason);
70 } catch (RTI::RTIinternalError& e) {
71 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name: " << e._name << " " << e._reason);
74 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name.");
80 RTI13ObjectInstance::addToRequestQueue()
82 SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
83 if (!ambassador.valid()) {
84 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
88 ambassador->addObjectInstanceForCallback(this);
92 RTI13ObjectInstance::deleteObjectInstance(const RTIData& tag)
94 SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
95 if (!ambassador.valid()) {
96 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
101 ambassador->deleteObjectInstance(_handle, tag);
102 } catch (RTI::ObjectNotKnown& e) {
103 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
104 } catch (RTI::DeletePrivilegeNotHeld& e) {
105 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
106 } catch (RTI::FederateNotExecutionMember& e) {
107 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
108 } catch (RTI::ConcurrentAccessAttempted& e) {
109 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
110 } catch (RTI::SaveInProgress& e) {
111 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
112 } catch (RTI::RestoreInProgress& e) {
113 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
114 } catch (RTI::RTIinternalError& e) {
115 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
120 RTI13ObjectInstance::deleteObjectInstance(const SGTimeStamp& timeStamp, const RTIData& tag)
122 SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
123 if (!ambassador.valid()) {
124 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
129 ambassador->deleteObjectInstance(_handle, timeStamp, tag);
130 } catch (RTI::ObjectNotKnown& e) {
131 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
132 } catch (RTI::DeletePrivilegeNotHeld& e) {
133 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
134 } catch (RTI::InvalidFederationTime& e) {
135 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
136 } catch (RTI::FederateNotExecutionMember& e) {
137 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
138 } catch (RTI::ConcurrentAccessAttempted& e) {
139 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
140 } catch (RTI::SaveInProgress& e) {
141 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
142 } catch (RTI::RestoreInProgress& e) {
143 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
144 } catch (RTI::RTIinternalError& e) {
145 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
150 RTI13ObjectInstance::localDeleteObjectInstance()
152 SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
153 if (!ambassador.valid()) {
154 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
159 ambassador->localDeleteObjectInstance(_handle);
160 } catch (RTI::ObjectNotKnown& e) {
161 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
162 } catch (RTI::FederateOwnsAttributes& e) {
163 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
164 } catch (RTI::FederateNotExecutionMember& e) {
165 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
166 } catch (RTI::ConcurrentAccessAttempted& e) {
167 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
168 } catch (RTI::SaveInProgress& e) {
169 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
170 } catch (RTI::RestoreInProgress& e) {
171 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
172 } catch (RTI::RTIinternalError& e) {
173 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
178 RTI13ObjectInstance::reflectAttributeValues(const RTI::AttributeHandleValuePairSet& attributeValuePairSet, const RTIData& tag)
180 // Retrieve an empty update struct from the memory pool
181 UpdateList updateList;
182 getUpdateFromPool(updateList);
184 RTI::ULong numAttribs = attributeValuePairSet.size();
185 for (RTI::ULong i = 0; i < numAttribs; ++i) {
186 unsigned index = getAttributeIndex(attributeValuePairSet.getHandle(i));
187 // Get a RTIData from the data pool
188 getDataFromPool(index, updateList.back()._indexDataPairList);
189 RTI::ULong length = attributeValuePairSet.getValueLength(i);
190 updateList.back()._indexDataPairList.back().second.resize(length);
191 attributeValuePairSet.getValue(i, updateList.back()._indexDataPairList.back().second.data(), length);
192 updateList.back()._tag = tag;
195 RTIObjectInstance::reflectAttributeValues(updateList.front()._indexDataPairList, tag);
196 // Return the update data back to the pool
197 putUpdateToPool(updateList);
201 RTI13ObjectInstance::reflectAttributeValues(const RTI::AttributeHandleValuePairSet& attributeValuePairSet, const SGTimeStamp& timeStamp, const RTIData& tag)
203 // Retrieve an empty update struct from the memory pool
204 UpdateList updateList;
205 getUpdateFromPool(updateList);
207 RTI::ULong numAttribs = attributeValuePairSet.size();
208 for (RTI::ULong i = 0; i < numAttribs; ++i) {
209 unsigned index = getAttributeIndex(attributeValuePairSet.getHandle(i));
210 // Get a RTIData from the data pool
211 getDataFromPool(index, updateList.back()._indexDataPairList);
212 RTI::ULong length = attributeValuePairSet.getValueLength(i);
213 updateList.back()._indexDataPairList.back().second.resize(length);
214 attributeValuePairSet.getValue(i, updateList.back()._indexDataPairList.back().second.data(), length);
215 updateList.back()._tag = tag;
218 scheduleUpdates(timeStamp, updateList);
222 RTI13ObjectInstance::requestObjectAttributeValueUpdate()
224 SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
225 if (!ambassador.valid()) {
226 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
231 unsigned numAttributes = getNumAttributes();
232 std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(numAttributes));
233 for (unsigned i = 0; i < numAttributes; ++i) {
234 if (!getRequestAttributeUpdate(i))
236 attributeHandleSet->add(getAttributeHandle(i));
238 if (!attributeHandleSet->size())
241 ambassador->requestObjectAttributeValueUpdate(_handle, *attributeHandleSet);
243 for (unsigned i = 0; i < numAttributes; ++i)
244 setRequestAttributeUpdate(i, false);
247 } catch (RTI::ObjectNotKnown& e) {
248 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
250 } catch (RTI::AttributeNotDefined& e) {
251 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
253 } catch (RTI::FederateNotExecutionMember& e) {
254 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
256 } catch (RTI::ConcurrentAccessAttempted& e) {
257 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
259 } catch (RTI::SaveInProgress& e) {
260 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
262 } catch (RTI::RestoreInProgress& e) {
263 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
265 } catch (RTI::RTIinternalError& e) {
266 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance: " << e._name << " " << e._reason);
269 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance.");
275 RTI13ObjectInstance::provideAttributeValueUpdate(const RTI::AttributeHandleSet& attributes)
277 // Called from the ambassador. Just marks some instance attributes dirty so that they are sent with the next update
278 RTI::ULong numAttribs = attributes.size();
279 for (RTI::ULong i = 0; i < numAttribs; ++i) {
280 unsigned index = getAttributeIndex(attributes.getHandle(i));
281 setAttributeForceUpdate(index);
286 RTI13ObjectInstance::updateAttributeValues(const RTIData& tag)
288 SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
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 delete object instance: " << e._name << " " << e._reason);
323 } catch (RTI::AttributeNotDefined& e) {
324 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
325 } catch (RTI::AttributeNotOwned& e) {
326 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
327 } catch (RTI::FederateNotExecutionMember& e) {
328 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
329 } catch (RTI::ConcurrentAccessAttempted& e) {
330 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
331 } catch (RTI::SaveInProgress& e) {
332 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
333 } catch (RTI::RestoreInProgress& e) {
334 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
335 } catch (RTI::RTIinternalError& e) {
336 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
339 // That means clear()
340 _attributeValuePairSet->empty();
344 RTI13ObjectInstance::updateAttributeValues(const SGTimeStamp& timeStamp, const RTIData& tag)
346 SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
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 (!getAttributeEffectiveUpdateEnabled(i))
360 const HLADataElement* dataElement = getDataElement(i);
363 // FIXME cache somewhere
365 HLAEncodeStream stream(data);
366 dataElement->encode(stream);
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 setAttributeUpdated(i);
379 } catch (RTI::ObjectNotKnown& e) {
380 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
381 } catch (RTI::AttributeNotDefined& e) {
382 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
383 } catch (RTI::AttributeNotOwned& e) {
384 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
385 } catch (RTI::InvalidFederationTime& e) {
386 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
387 } catch (RTI::FederateNotExecutionMember& e) {
388 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
389 } catch (RTI::ConcurrentAccessAttempted& e) {
390 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
391 } catch (RTI::SaveInProgress& e) {
392 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
393 } catch (RTI::RestoreInProgress& e) {
394 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
395 } catch (RTI::RTIinternalError& e) {
396 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not delete object instance: " << e._name << " " << e._reason);
399 // That means clear()
400 _attributeValuePairSet->empty();
404 RTI13ObjectInstance::attributesInScope(const RTI::AttributeHandleSet& attributeHandleSet)
406 RTI::ULong numAttribs = attributeHandleSet.size();
407 for (RTI::ULong i = 0; i < numAttribs; ++i) {
408 RTI::AttributeHandle attributeHandle = attributeHandleSet.getHandle(i);
409 setAttributeInScope(getAttributeIndex(attributeHandle), true);
414 RTI13ObjectInstance::attributesOutOfScope(const RTI::AttributeHandleSet& attributeHandleSet)
416 RTI::ULong numAttribs = attributeHandleSet.size();
417 for (RTI::ULong i = 0; i < numAttribs; ++i) {
418 RTI::AttributeHandle attributeHandle = attributeHandleSet.getHandle(i);
419 setAttributeInScope(getAttributeIndex(attributeHandle), false);
424 RTI13ObjectInstance::turnUpdatesOnForObjectInstance(const RTI::AttributeHandleSet& attributeHandleSet)
426 RTI::ULong numAttribs = attributeHandleSet.size();
427 for (RTI::ULong i = 0; i < numAttribs; ++i) {
428 RTI::AttributeHandle attributeHandle = attributeHandleSet.getHandle(i);
429 setAttributeUpdateEnabled(getAttributeIndex(attributeHandle), true);
434 RTI13ObjectInstance::turnUpdatesOffForObjectInstance(const RTI::AttributeHandleSet& attributeHandleSet)
436 RTI::ULong numAttribs = attributeHandleSet.size();
437 for (RTI::ULong i = 0; i < numAttribs; ++i) {
438 RTI::AttributeHandle attributeHandle = attributeHandleSet.getHandle(i);
439 setAttributeUpdateEnabled(getAttributeIndex(attributeHandle), false);
444 RTI13ObjectInstance::requestAttributeOwnershipAssumption(const RTI::AttributeHandleSet& attributes, const RTIData& tag)
449 RTI13ObjectInstance::attributeOwnershipDivestitureNotification(const RTI::AttributeHandleSet& attributes)
454 RTI13ObjectInstance::attributeOwnershipAcquisitionNotification(const RTI::AttributeHandleSet& attributes)
459 RTI13ObjectInstance::attributeOwnershipUnavailable(const RTI::AttributeHandleSet& attributes)
464 RTI13ObjectInstance::requestAttributeOwnershipRelease(const RTI::AttributeHandleSet& attributes, const RTIData& tag)
469 RTI13ObjectInstance::confirmAttributeOwnershipAcquisitionCancellation(const RTI::AttributeHandleSet& attributes)
474 RTI13ObjectInstance::informAttributeOwnership(RTI::AttributeHandle attributeHandle, RTI::FederateHandle federateHandle)
479 RTI13ObjectInstance::attributeIsNotOwned(RTI::AttributeHandle attributeHandle)
484 RTI13ObjectInstance::attributeOwnedByRTI(RTI::AttributeHandle attributeHandle)