]> git.mxchange.org Git - simgear.git/blob - simgear/hla/RTI13ObjectInstance.cxx
hla: add missing file fir the last commit.
[simgear.git] / simgear / hla / RTI13ObjectInstance.cxx
1 // Copyright (C) 2009 - 2010  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
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.
7 //
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.
12 //
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.
16 //
17
18 #include "RTIObjectInstance.hxx"
19 #include "RTI13Ambassador.hxx"
20
21 namespace simgear {
22
23 RTI13ObjectInstance::RTI13ObjectInstance(const RTI::ObjectHandle& handle, HLAObjectInstance* hlaObjectInstance,
24                                          const RTI13ObjectClass* objectClass, RTI13Ambassador* ambassador, bool owned) :
25     RTIObjectInstance(hlaObjectInstance),
26     _handle(handle),
27     _objectClass(objectClass),
28     _ambassador(ambassador),
29     _attributeValuePairSet(RTI::AttributeSetFactory::create(objectClass->getNumAttributes()))
30 {
31     updateAttributesFromClass(owned);
32 }
33
34 RTI13ObjectInstance::~RTI13ObjectInstance()
35 {
36 }
37
38 const RTIObjectClass*
39 RTI13ObjectInstance::getObjectClass() const
40 {
41     return _objectClass.get();
42 }
43
44 const RTI13ObjectClass*
45 RTI13ObjectInstance::get13ObjectClass() const
46 {
47     return _objectClass.get();
48 }
49
50 std::string
51 RTI13ObjectInstance::getName() const
52 {
53     if (!_ambassador.valid()) {
54         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
55         return std::string();
56     }
57
58     try {
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);
62         return std::string();
63     } catch (RTI::FederateNotExecutionMember& e) {
64         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name: " << e._name << " " << e._reason);
65         return std::string();
66     } catch (RTI::ConcurrentAccessAttempted& e) {
67         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name: " << e._name << " " << e._reason);
68         return std::string();
69     } catch (RTI::RTIinternalError& e) {
70         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name: " << e._name << " " << e._reason);
71         return std::string();
72     } catch (...) {
73         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object name.");
74         return std::string();
75     }
76 }
77
78 void
79 RTI13ObjectInstance::deleteObjectInstance(const RTIData& tag)
80 {
81     if (!_ambassador.valid()) {
82         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
83         return;
84     }
85
86     SGSharedPtr<RTI13Federate> federate = _ambassador->_federate.lock();
87     if (!federate.valid()) {
88         SG_LOG(SG_NETWORK, SG_WARN, "Error: Federate is zero.");
89         return;
90     }
91
92     try {
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);
108     }
109 }
110
111 void
112 RTI13ObjectInstance::deleteObjectInstance(const SGTimeStamp& timeStamp, const RTIData& tag)
113 {
114     if (!_ambassador.valid()) {
115         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
116         return;
117     }
118
119     SGSharedPtr<RTI13Federate> federate = _ambassador->_federate.lock();
120     if (!federate.valid()) {
121         SG_LOG(SG_NETWORK, SG_WARN, "Error: Federate is zero.");
122         return;
123     }
124
125     try {
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);
143     }
144 }
145
146 void
147 RTI13ObjectInstance::localDeleteObjectInstance()
148 {
149     if (!_ambassador.valid()) {
150         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
151         return;
152     }
153
154     SGSharedPtr<RTI13Federate> federate = _ambassador->_federate.lock();
155     if (!federate.valid()) {
156         SG_LOG(SG_NETWORK, SG_WARN, "Error: Federate is zero.");
157         return;
158     }
159
160     try {
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);
176     }
177 }
178
179 void
180 RTI13ObjectInstance::reflectAttributeValues(const RTI::AttributeHandleValuePairSet& attributeValuePairSet, const RTIData& tag)
181 {
182     // Retrieve an empty update struct from the memory pool
183     UpdateList updateList;
184     getUpdateFromPool(updateList);
185
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;
195     }
196
197     RTIObjectInstance::reflectAttributeValues(updateList.front()._indexDataPairList, tag);
198     // Return the update data back to the pool
199     putUpdateToPool(updateList);
200 }
201
202 void
203 RTI13ObjectInstance::reflectAttributeValues(const RTI::AttributeHandleValuePairSet& attributeValuePairSet, const SGTimeStamp& timeStamp, const RTIData& tag)
204 {
205     // Retrieve an empty update struct from the memory pool
206     UpdateList updateList;
207     getUpdateFromPool(updateList);
208
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;
218     }
219
220     scheduleUpdates(timeStamp, updateList);
221 }
222
223 void
224 RTI13ObjectInstance::requestObjectAttributeValueUpdate()
225 {
226     if (!_ambassador.valid()) {
227         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
228         return;
229     }
230
231     try {
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))
236                 continue;
237             attributeHandleSet->add(getAttributeHandle(i));
238         }
239         if (!attributeHandleSet->size())
240             return;
241
242         _ambassador->requestObjectAttributeValueUpdate(_handle, *attributeHandleSet);
243
244         for (unsigned i = 0; i < numAttributes; ++i)
245             setRequestAttributeUpdate(i, false);
246
247         return;
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);
250         return;
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);
253         return;
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);
256         return;
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);
259         return;
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);
262         return;
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);
265         return;
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);
268         return;
269     } catch (...) {
270         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not request attribute update for object instance.");
271         return;
272     }
273 }
274
275 void
276 RTI13ObjectInstance::provideAttributeValueUpdate(const RTI::AttributeHandleSet& attributes)
277 {
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);
283     }
284 }
285
286 void
287 RTI13ObjectInstance::updateAttributeValues(const RTIData& tag)
288 {
289     if (!_ambassador.valid()) {
290         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
291         return;
292     }
293
294     try {
295         // That means clear()
296         _attributeValuePairSet->empty();
297
298         unsigned numAttributes = getNumAttributes();
299         for (unsigned i = 0; i < numAttributes; ++i) {
300             if (!getAttributeEffectiveUpdateEnabled(i))
301                 continue;
302             const HLADataElement* dataElement = getDataElement(i);
303             if (!dataElement)
304                 continue;
305             // FIXME cache somewhere
306             RTIData data;
307             HLAEncodeStream stream(data);
308             dataElement->encode(stream);
309             _attributeValuePairSet->add(getAttributeHandle(i), data.data(), data.size());
310         }
311
312         if (!_attributeValuePairSet->size())
313             return;
314
315         _ambassador->updateAttributeValues(_handle, *_attributeValuePairSet, tag);
316
317         for (unsigned i = 0; i < numAttributes; ++i) {
318             setAttributeUpdated(i);
319         }
320
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);
337     }
338
339     // That means clear()
340     _attributeValuePairSet->empty();
341 }
342
343 void
344 RTI13ObjectInstance::updateAttributeValues(const SGTimeStamp& timeStamp, const RTIData& tag)
345 {
346     if (!_ambassador.valid()) {
347         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
348         return;
349     }
350
351     try {
352         // That means clear()
353         _attributeValuePairSet->empty();
354
355         unsigned numAttributes = getNumAttributes();
356         for (unsigned i = 0; i < numAttributes; ++i) {
357             if (!getAttributeEffectiveUpdateEnabled(i))
358                 continue;
359             const HLADataElement* dataElement = getDataElement(i);
360             if (!dataElement)
361                 continue;
362             // FIXME cache somewhere
363             RTIData data;
364             HLAEncodeStream stream(data);
365             dataElement->encode(stream);
366             _attributeValuePairSet->add(getAttributeHandle(i), data.data(), data.size());
367         }
368
369         if (!_attributeValuePairSet->size())
370             return;
371
372         _ambassador->updateAttributeValues(_handle, *_attributeValuePairSet, timeStamp, tag);
373
374         for (unsigned i = 0; i < numAttributes; ++i) {
375             setAttributeUpdated(i);
376         }
377
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);
396     }
397
398     // That means clear()
399     _attributeValuePairSet->empty();
400 }
401
402 void
403 RTI13ObjectInstance::attributesInScope(const RTI::AttributeHandleSet& attributeHandleSet)
404 {
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);
409     }
410 }
411
412 void
413 RTI13ObjectInstance::attributesOutOfScope(const RTI::AttributeHandleSet& attributeHandleSet)
414 {
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);
419     }
420 }
421
422 void
423 RTI13ObjectInstance::turnUpdatesOnForObjectInstance(const RTI::AttributeHandleSet& attributeHandleSet)
424 {
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);
429     }
430 }
431
432 void
433 RTI13ObjectInstance::turnUpdatesOffForObjectInstance(const RTI::AttributeHandleSet& attributeHandleSet)
434 {
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);
439     }
440 }
441
442 void
443 RTI13ObjectInstance::requestAttributeOwnershipAssumption(const RTI::AttributeHandleSet& attributes, const RTIData& tag)
444 {
445 }
446
447 void
448 RTI13ObjectInstance::attributeOwnershipDivestitureNotification(const RTI::AttributeHandleSet& attributes)
449 {
450 }
451
452 void
453 RTI13ObjectInstance::attributeOwnershipAcquisitionNotification(const RTI::AttributeHandleSet& attributes)
454 {
455 }
456
457 void
458 RTI13ObjectInstance::attributeOwnershipUnavailable(const RTI::AttributeHandleSet& attributes)
459 {
460 }
461
462 void
463 RTI13ObjectInstance::requestAttributeOwnershipRelease(const RTI::AttributeHandleSet& attributes, const RTIData& tag)
464 {
465 }
466
467 void
468 RTI13ObjectInstance::confirmAttributeOwnershipAcquisitionCancellation(const RTI::AttributeHandleSet& attributes)
469 {
470 }
471
472 void
473 RTI13ObjectInstance::informAttributeOwnership(RTI::AttributeHandle attributeHandle, RTI::FederateHandle federateHandle)
474 {
475 }
476
477 void
478 RTI13ObjectInstance::attributeIsNotOwned(RTI::AttributeHandle attributeHandle)
479 {
480 }
481
482 void
483 RTI13ObjectInstance::attributeOwnedByRTI(RTI::AttributeHandle attributeHandle)
484 {
485 }
486
487 }