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 "RTI13ObjectClass.hxx"
19 #include "RTI13Ambassador.hxx"
23 RTI13ObjectClass::RTI13ObjectClass(HLAObjectClass* hlaObjectClass, RTI::ObjectClassHandle& handle, RTI13Ambassador* ambassador) :
24 RTIObjectClass(hlaObjectClass),
26 _ambassador(ambassador)
28 if (0 != getOrCreateAttributeIndex("privilegeToDelete") &&
29 0 != getOrCreateAttributeIndex("HLAprivilegeToDeleteObject"))
30 SG_LOG(SG_NETWORK, SG_WARN, "RTI13ObjectClass: Cannot find object root attribute.");
33 RTI13ObjectClass::~RTI13ObjectClass()
38 RTI13ObjectClass::getName() const
40 SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
41 if (!ambassador.valid()) {
42 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
45 return ambassador->getObjectClassName(_handle);
49 RTI13ObjectClass::getNumAttributes() const
51 return _attributeHandleVector.size();
55 RTI13ObjectClass::getAttributeIndex(const std::string& name) const
57 SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
58 if (!ambassador.valid()) {
59 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
64 RTI::AttributeHandle attributeHandle = ambassador->getAttributeHandle(name, _handle);
66 AttributeHandleIndexMap::const_iterator i = _attributeHandleIndexMap.find(attributeHandle);
67 if (i != _attributeHandleIndexMap.end())
72 } catch (RTI::ObjectClassNotDefined& e) {
73 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute: " << e._name << " " << e._reason);
75 } catch (RTI::NameNotFound& e) {
76 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute: " << e._name << " " << e._reason);
78 } catch (RTI::FederateNotExecutionMember& e) {
79 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute: " << e._name << " " << e._reason);
81 } catch (RTI::ConcurrentAccessAttempted& e) {
82 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute: " << e._name << " " << e._reason);
84 } catch (RTI::RTIinternalError& e) {
85 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute: " << e._name << " " << e._reason);
88 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute.");
94 RTI13ObjectClass::getOrCreateAttributeIndex(const std::string& name)
96 SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
97 if (!ambassador.valid()) {
98 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
103 RTI::AttributeHandle attributeHandle = ambassador->getAttributeHandle(name, _handle);
105 AttributeHandleIndexMap::const_iterator i = _attributeHandleIndexMap.find(attributeHandle);
106 if (i != _attributeHandleIndexMap.end())
109 unsigned index = _attributeHandleVector.size();
110 _attributeHandleIndexMap[attributeHandle] = index;
111 _attributeHandleVector.push_back(attributeHandle);
112 _attributeDataVector.push_back(name);
116 } catch (RTI::ObjectClassNotDefined& e) {
117 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute: " << e._name << " " << e._reason);
119 } catch (RTI::NameNotFound& e) {
120 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute: " << e._name << " " << e._reason);
122 } catch (RTI::FederateNotExecutionMember& e) {
123 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute: " << e._name << " " << e._reason);
125 } catch (RTI::ConcurrentAccessAttempted& e) {
126 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute: " << e._name << " " << e._reason);
128 } catch (RTI::RTIinternalError& e) {
129 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute: " << e._name << " " << e._reason);
132 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute.");
138 // RTI13ObjectClass::getAttributeName(unsigned index) const
140 // SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
141 // if (!ambassador.valid()) {
142 // SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
143 // return std::string();
147 // return ambassador->getAttributeName(getAttributeHandle(index), _handle);
148 // } catch (RTI::ObjectClassNotDefined& e) {
149 // SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute name: " << e._name << " " << e._reason);
150 // return std::string();
151 // } catch (RTI::AttributeNotDefined& e) {
152 // SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute name: " << e._name << " " << e._reason);
153 // return std::string();
154 // } catch (RTI::FederateNotExecutionMember& e) {
155 // SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute name: " << e._name << " " << e._reason);
156 // return std::string();
157 // } catch (RTI::ConcurrentAccessAttempted& e) {
158 // SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute name: " << e._name << " " << e._reason);
159 // return std::string();
160 // } catch (RTI::RTIinternalError& e) {
161 // SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute name: " << e._name << " " << e._reason);
162 // return std::string();
164 // SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute name.");
165 // return std::string();
170 RTI13ObjectClass::publish(const std::set<unsigned>& indexSet)
172 SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
173 if (!ambassador.valid()) {
174 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
179 std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(indexSet.size()));
180 for (std::set<unsigned>::const_iterator i = indexSet.begin(); i != indexSet.end(); ++i) {
181 if (_attributeHandleVector.size() <= *i) {
182 SG_LOG(SG_NETWORK, SG_WARN, "RTI13ObjectClass::publish(): Invalid attribute index!");
185 attributeHandleSet->add(_attributeHandleVector[*i]);
188 ambassador->publishObjectClass(_handle, *attributeHandleSet);
190 for (unsigned i = 0; i < getNumAttributes(); ++i) {
191 _attributeDataVector[i]._published = true;
195 } catch (RTI::ObjectClassNotDefined& e) {
196 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class: " << e._name << " " << e._reason);
198 } catch (RTI::AttributeNotDefined& e) {
199 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class: " << e._name << " " << e._reason);
201 } catch (RTI::OwnershipAcquisitionPending& e) {
202 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class: " << e._name << " " << e._reason);
204 } catch (RTI::FederateNotExecutionMember& e) {
205 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class: " << e._name << " " << e._reason);
207 } catch (RTI::ConcurrentAccessAttempted& e) {
208 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class: " << e._name << " " << e._reason);
210 } catch (RTI::SaveInProgress& e) {
211 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class: " << e._name << " " << e._reason);
213 } catch (RTI::RestoreInProgress& e) {
214 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class: " << e._name << " " << e._reason);
216 } catch (RTI::RTIinternalError& e) {
217 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class: " << e._name << " " << e._reason);
220 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class.");
226 RTI13ObjectClass::unpublish()
228 SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
229 if (!ambassador.valid()) {
230 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
235 ambassador->unpublishObjectClass(_handle);
237 for (unsigned i = 0; i < getNumAttributes(); ++i) {
238 _attributeDataVector[i]._published = false;
242 } catch (RTI::ObjectClassNotDefined& e) {
243 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class: " << e._name << " " << e._reason);
245 } catch (RTI::ObjectClassNotPublished& e) {
246 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class: " << e._name << " " << e._reason);
248 } catch (RTI::OwnershipAcquisitionPending& e) {
249 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class: " << e._name << " " << e._reason);
251 } catch (RTI::FederateNotExecutionMember& e) {
252 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class: " << e._name << " " << e._reason);
254 } catch (RTI::ConcurrentAccessAttempted& e) {
255 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class: " << e._name << " " << e._reason);
257 } catch (RTI::SaveInProgress& e) {
258 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class: " << e._name << " " << e._reason);
260 } catch (RTI::RestoreInProgress& e) {
261 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class: " << e._name << " " << e._reason);
263 } catch (RTI::RTIinternalError& e) {
264 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class: " << e._name << " " << e._reason);
267 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class.");
273 RTI13ObjectClass::subscribe(const std::set<unsigned>& indexSet, bool active)
275 SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
276 if (!ambassador.valid()) {
277 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
282 std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(indexSet.size()));
283 for (std::set<unsigned>::const_iterator i = indexSet.begin();
284 i != indexSet.end(); ++i) {
285 if (_attributeHandleVector.size() <= *i) {
286 SG_LOG(SG_NETWORK, SG_WARN, "RTI13ObjectClass::subscribe(): Invalid attribute index!");
289 attributeHandleSet->add(_attributeHandleVector[*i]);
292 ambassador->subscribeObjectClassAttributes(_handle, *attributeHandleSet, active);
294 for (unsigned i = 0; i < getNumAttributes(); ++i) {
295 _attributeDataVector[i]._subscribed = true;
299 } catch (RTI::ObjectClassNotDefined& e) {
300 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe object class: " << e._name << " " << e._reason);
302 } catch (RTI::AttributeNotDefined& e) {
303 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe object class: " << e._name << " " << e._reason);
305 } catch (RTI::FederateNotExecutionMember& e) {
306 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe object class: " << e._name << " " << e._reason);
308 } catch (RTI::ConcurrentAccessAttempted& e) {
309 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe object class: " << e._name << " " << e._reason);
311 } catch (RTI::SaveInProgress& e) {
312 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe object class: " << e._name << " " << e._reason);
314 } catch (RTI::RestoreInProgress& e) {
315 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe object class: " << e._name << " " << e._reason);
317 } catch (RTI::RTIinternalError& e) {
318 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe object class: " << e._name << " " << e._reason);
321 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe object class.");
327 RTI13ObjectClass::unsubscribe()
329 SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
330 if (!ambassador.valid()) {
331 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
336 ambassador->unsubscribeObjectClass(_handle);
338 for (unsigned i = 0; i < getNumAttributes(); ++i) {
339 _attributeDataVector[i]._subscribed = false;
343 } catch (RTI::ObjectClassNotDefined& e) {
344 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe object class: " << e._name << " " << e._reason);
346 } catch (RTI::ObjectClassNotSubscribed& e) {
347 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe object class: " << e._name << " " << e._reason);
349 } catch (RTI::FederateNotExecutionMember& e) {
350 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe object class: " << e._name << " " << e._reason);
352 } catch (RTI::ConcurrentAccessAttempted& e) {
353 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe object class: " << e._name << " " << e._reason);
355 } catch (RTI::SaveInProgress& e) {
356 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe object class: " << e._name << " " << e._reason);
358 } catch (RTI::RestoreInProgress& e) {
359 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe object class: " << e._name << " " << e._reason);
361 } catch (RTI::RTIinternalError& e) {
362 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe object class: " << e._name << " " << e._reason);
365 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe object class.");
371 RTI13ObjectClass::registerObjectInstance(HLAObjectInstance* hlaObjectInstance)
373 SGSharedPtr<RTI13Ambassador> ambassador = _ambassador.lock();
374 if (!ambassador.valid()) {
375 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
380 return ambassador->registerObjectInstance(this, hlaObjectInstance);
381 } catch (RTI::ObjectClassNotDefined& e) {
382 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not register object instance: " << e._name << " " << e._reason);
384 } catch (RTI::ObjectClassNotPublished& e) {
385 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not register object instance: " << e._name << " " << e._reason);
387 } catch (RTI::FederateNotExecutionMember& e) {
388 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not register object instance: " << e._name << " " << e._reason);
390 } catch (RTI::ConcurrentAccessAttempted& e) {
391 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not register object instance: " << e._name << " " << e._reason);
393 } catch (RTI::SaveInProgress& e) {
394 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not register object instance: " << e._name << " " << e._reason);
396 } catch (RTI::RestoreInProgress& e) {
397 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not register object instance: " << e._name << " " << e._reason);
399 } catch (RTI::RTIinternalError& e) {
400 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not register object instance: " << e._name << " " << e._reason);