1 // Copyright (C) 2009 - 2012 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.
19 # include <simgear_config.h>
22 #include <simgear/compiler.h>
24 #include "RTI13ObjectClass.hxx"
25 #include "RTI13Ambassador.hxx"
29 RTI13ObjectClass::RTI13ObjectClass(HLAObjectClass* hlaObjectClass, const RTI::ObjectClassHandle& handle, RTI13Ambassador* ambassador) :
30 RTIObjectClass(hlaObjectClass),
32 _ambassador(ambassador)
36 RTI13ObjectClass::~RTI13ObjectClass()
41 RTI13ObjectClass::resolveAttributeIndex(const std::string& name, unsigned index)
43 if (!_ambassador.valid()) {
44 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
48 if (index != _attributeHandleVector.size()) {
49 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Resolving needs to happen in growing index order!");
54 RTI::AttributeHandle attributeHandle = _ambassador->getAttributeHandle(name, _handle);
56 AttributeHandleIndexMap::const_iterator i = _attributeHandleIndexMap.find(attributeHandle);
57 if (i != _attributeHandleIndexMap.end()) {
58 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Resolving attributeIndex for attribute \"" << name << "\" twice!");
62 _attributeHandleIndexMap[attributeHandle] = index;
63 _attributeHandleVector.push_back(attributeHandle);
67 } catch (RTI::ObjectClassNotDefined& e) {
68 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute: " << e._name << " " << e._reason);
70 } catch (RTI::NameNotFound& e) {
71 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute: " << e._name << " " << e._reason);
73 } catch (RTI::FederateNotExecutionMember& e) {
74 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute: " << e._name << " " << e._reason);
76 } catch (RTI::ConcurrentAccessAttempted& e) {
77 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute: " << e._name << " " << e._reason);
79 } catch (RTI::RTIinternalError& e) {
80 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute: " << e._name << " " << e._reason);
83 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get object class attribute.");
89 RTI13ObjectClass::getNumAttributes() const
91 return _attributeHandleVector.size();
95 RTI13ObjectClass::publish(const HLAIndexList& indexList)
97 if (!_ambassador.valid()) {
98 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
103 unsigned numAttributes = getNumAttributes();
104 std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(numAttributes));
105 for (HLAIndexList::const_iterator i = indexList.begin(); i != indexList.end(); ++i) {
106 if (_attributeHandleVector.size() <= *i) {
107 SG_LOG(SG_NETWORK, SG_WARN, "RTI13ObjectClass::publish(): Invalid attribute index!");
110 attributeHandleSet->add(_attributeHandleVector[*i]);
113 _ambassador->publishObjectClass(_handle, *attributeHandleSet);
116 } catch (RTI::ObjectClassNotDefined& e) {
117 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class: " << e._name << " " << e._reason);
119 } catch (RTI::AttributeNotDefined& e) {
120 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class: " << e._name << " " << e._reason);
122 } catch (RTI::OwnershipAcquisitionPending& e) {
123 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class: " << e._name << " " << e._reason);
125 } catch (RTI::FederateNotExecutionMember& e) {
126 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class: " << e._name << " " << e._reason);
128 } catch (RTI::ConcurrentAccessAttempted& e) {
129 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class: " << e._name << " " << e._reason);
131 } catch (RTI::SaveInProgress& e) {
132 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class: " << e._name << " " << e._reason);
134 } catch (RTI::RestoreInProgress& e) {
135 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class: " << e._name << " " << e._reason);
137 } catch (RTI::RTIinternalError& e) {
138 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class: " << e._name << " " << e._reason);
141 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish object class.");
147 RTI13ObjectClass::unpublish()
149 if (!_ambassador.valid()) {
150 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
155 _ambassador->unpublishObjectClass(_handle);
158 } catch (RTI::ObjectClassNotDefined& e) {
159 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class: " << e._name << " " << e._reason);
161 } catch (RTI::ObjectClassNotPublished& e) {
162 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class: " << e._name << " " << e._reason);
164 } catch (RTI::OwnershipAcquisitionPending& e) {
165 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class: " << e._name << " " << e._reason);
167 } catch (RTI::FederateNotExecutionMember& e) {
168 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class: " << e._name << " " << e._reason);
170 } catch (RTI::ConcurrentAccessAttempted& e) {
171 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class: " << e._name << " " << e._reason);
173 } catch (RTI::SaveInProgress& e) {
174 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class: " << e._name << " " << e._reason);
176 } catch (RTI::RestoreInProgress& e) {
177 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class: " << e._name << " " << e._reason);
179 } catch (RTI::RTIinternalError& e) {
180 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class: " << e._name << " " << e._reason);
183 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish object class.");
189 RTI13ObjectClass::subscribe(const HLAIndexList& indexList, bool active)
191 if (!_ambassador.valid()) {
192 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
197 unsigned numAttributes = getNumAttributes();
198 std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(numAttributes));
199 for (HLAIndexList::const_iterator i = indexList.begin(); i != indexList.end(); ++i) {
200 if (_attributeHandleVector.size() <= *i) {
201 SG_LOG(SG_NETWORK, SG_WARN, "RTI13ObjectClass::subscribe(): Invalid attribute index!");
204 attributeHandleSet->add(_attributeHandleVector[*i]);
207 _ambassador->subscribeObjectClassAttributes(_handle, *attributeHandleSet, active);
210 } catch (RTI::ObjectClassNotDefined& e) {
211 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe object class: " << e._name << " " << e._reason);
213 } catch (RTI::AttributeNotDefined& e) {
214 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe object class: " << e._name << " " << e._reason);
216 } catch (RTI::FederateNotExecutionMember& e) {
217 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe object class: " << e._name << " " << e._reason);
219 } catch (RTI::ConcurrentAccessAttempted& e) {
220 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe object class: " << e._name << " " << e._reason);
222 } catch (RTI::SaveInProgress& e) {
223 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe object class: " << e._name << " " << e._reason);
225 } catch (RTI::RestoreInProgress& e) {
226 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe object class: " << e._name << " " << e._reason);
228 } catch (RTI::RTIinternalError& e) {
229 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe object class: " << e._name << " " << e._reason);
232 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe object class.");
238 RTI13ObjectClass::unsubscribe()
240 if (!_ambassador.valid()) {
241 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
246 _ambassador->unsubscribeObjectClass(_handle);
249 } catch (RTI::ObjectClassNotDefined& e) {
250 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe object class: " << e._name << " " << e._reason);
252 } catch (RTI::ObjectClassNotSubscribed& e) {
253 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe object class: " << e._name << " " << e._reason);
255 } catch (RTI::FederateNotExecutionMember& e) {
256 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe object class: " << e._name << " " << e._reason);
258 } catch (RTI::ConcurrentAccessAttempted& e) {
259 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe object class: " << e._name << " " << e._reason);
261 } catch (RTI::SaveInProgress& e) {
262 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe object class: " << e._name << " " << e._reason);
264 } catch (RTI::RestoreInProgress& e) {
265 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe object class: " << e._name << " " << e._reason);
267 } catch (RTI::RTIinternalError& e) {
268 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe object class: " << e._name << " " << e._reason);
271 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe object class.");
277 RTI13ObjectClass::registerObjectInstance(HLAObjectInstance* hlaObjectInstance)
279 if (!_ambassador.valid()) {
280 SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
284 SGSharedPtr<RTI13Federate> federate = _ambassador->_federate.lock();
285 if (!federate.valid()) {
286 SG_LOG(SG_NETWORK, SG_WARN, "Error: Federate is zero.");
291 RTI::ObjectHandle objectHandle = _ambassador->registerObjectInstance(getHandle());
292 RTI13ObjectInstance* objectInstance = new RTI13ObjectInstance(objectHandle, hlaObjectInstance, this, _ambassador.get());
293 federate->insertObjectInstance(objectInstance);
294 return objectInstance;
295 } catch (RTI::ObjectClassNotDefined& e) {
296 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not register object instance: " << e._name << " " << e._reason);
298 } catch (RTI::ObjectClassNotPublished& e) {
299 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not register object instance: " << e._name << " " << e._reason);
301 } catch (RTI::FederateNotExecutionMember& e) {
302 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not register object instance: " << e._name << " " << e._reason);
304 } catch (RTI::ConcurrentAccessAttempted& e) {
305 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not register object instance: " << e._name << " " << e._reason);
307 } catch (RTI::SaveInProgress& e) {
308 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not register object instance: " << e._name << " " << e._reason);
310 } catch (RTI::RestoreInProgress& e) {
311 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not register object instance: " << e._name << " " << e._reason);
313 } catch (RTI::RTIinternalError& e) {
314 SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not register object instance: " << e._name << " " << e._reason);