]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAObjectClass.cxx
Add an initial implementation of a rti/hla dispatcher.
[simgear.git] / simgear / hla / HLAObjectClass.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 "HLAObjectClass.hxx"
19
20 #include "RTIFederate.hxx"
21 #include "RTIObjectClass.hxx"
22 #include "RTIObjectInstance.hxx"
23 #include "HLADataType.hxx"
24 #include "HLAFederate.hxx"
25 #include "HLAObjectInstance.hxx"
26
27 namespace simgear {
28
29 HLAObjectClass::InstanceCallback::~InstanceCallback()
30 {
31 }
32
33 void
34 HLAObjectClass::InstanceCallback::discoverInstance(const HLAObjectClass&, HLAObjectInstance& objectInstance, const RTIData& tag)
35 {
36 }
37
38 void
39 HLAObjectClass::InstanceCallback::removeInstance(const HLAObjectClass&, HLAObjectInstance& objectInstance, const RTIData& tag)
40 {
41 }
42
43 void
44 HLAObjectClass::InstanceCallback::registerInstance(const HLAObjectClass&, HLAObjectInstance& objectInstance)
45 {
46 }
47
48 void
49 HLAObjectClass::InstanceCallback::deleteInstance(const HLAObjectClass&, HLAObjectInstance& objectInstance)
50 {
51 }
52
53 HLAObjectClass::RegistrationCallback::~RegistrationCallback()
54 {
55 }
56
57 HLAObjectClass::HLAObjectClass(const std::string& name, HLAFederate& federate) :
58     _name(name)
59 {
60     _rtiObjectClass = federate._rtiFederate->createObjectClass(name, this);
61     if (!_rtiObjectClass.valid())
62         SG_LOG(SG_NETWORK, SG_WARN, "HLAObjectClass::HLAObjectClass(): No RTIObjectClass found for \"" << name << "\"!");
63 }
64
65 HLAObjectClass::~HLAObjectClass()
66 {
67 }
68
69 unsigned
70 HLAObjectClass::getNumAttributes() const
71 {
72     if (!_rtiObjectClass.valid()) {
73         SG_LOG(SG_NETWORK, SG_WARN, "HLAObjectClass::getAttributeIndex(): No RTIObject class for object class \"" << getName() << "\"!");
74         return 0;
75     }
76     return _rtiObjectClass->getNumAttributes();
77 }
78
79 unsigned
80 HLAObjectClass::getAttributeIndex(const std::string& name) const
81 {
82     if (!_rtiObjectClass.valid()) {
83         SG_LOG(SG_NETWORK, SG_WARN, "HLAObjectClass::getAttributeIndex(): No RTIObject class for object class \"" << getName() << "\"!");
84         return ~0u;
85     }
86     return _rtiObjectClass->getOrCreateAttributeIndex(name);
87 }
88
89 std::string
90 HLAObjectClass::getAttributeName(unsigned index) const
91 {
92     if (!_rtiObjectClass.valid()) {
93         SG_LOG(SG_NETWORK, SG_WARN, "HLAObjectClass::getAttributeIndex(): No RTIObject class for object class \"" << getName() << "\"!");
94         return 0;
95     }
96     return _rtiObjectClass->getAttributeName(index);
97 }
98
99 const HLADataType*
100 HLAObjectClass::getAttributeDataType(unsigned index) const
101 {
102     if (!_rtiObjectClass.valid()) {
103         SG_LOG(SG_NETWORK, SG_WARN, "HLAObjectClass::getAttributeDataType(): No RTIObject class for object class \"" << getName() << "\"!");
104         return 0;
105     }
106     return _rtiObjectClass->getAttributeDataType(index);
107 }
108
109 void
110 HLAObjectClass::setAttributeDataType(unsigned index, const HLADataType* dataType)
111 {
112     if (!_rtiObjectClass.valid()) {
113         SG_LOG(SG_NETWORK, SG_WARN, "HLAObjectClass::setAttributeDataType(): No RTIObject class for object class \"" << getName() << "\"!");
114         return;
115     }
116     _rtiObjectClass->setAttributeDataType(index, dataType);
117 }
118
119 HLAUpdateType
120 HLAObjectClass::getAttributeUpdateType(unsigned index) const
121 {
122     if (!_rtiObjectClass.valid()) {
123         SG_LOG(SG_NETWORK, SG_WARN, "HLAObjectClass::getAttributeUpdateType(): No RTIObject class for object class \"" << getName() << "\"!");
124         return HLAUndefinedUpdate;
125     }
126     return _rtiObjectClass->getAttributeUpdateType(index);
127 }
128
129 void
130 HLAObjectClass::setAttributeUpdateType(unsigned index, HLAUpdateType updateType)
131 {
132     if (!_rtiObjectClass.valid()) {
133         SG_LOG(SG_NETWORK, SG_WARN, "HLAObjectClass::setAttributeUpdateType(): "
134                "No RTIObject class for object class \"" << getName() << "\"!");
135         return;
136     }
137     _rtiObjectClass->setAttributeUpdateType(index, updateType);
138 }
139
140 HLADataElement::IndexPathPair
141 HLAObjectClass::getIndexPathPair(const HLADataElement::AttributePathPair& attributePathPair) const
142 {
143     unsigned index = getAttributeIndex(attributePathPair.first);
144     if (getNumAttributes() <= index) {
145         SG_LOG(SG_NETWORK, SG_ALERT, "HLAObjectClass::getIndexPathPair(\""
146                << HLADataElement::toString(attributePathPair)
147                << "\"): Could not resolve attribute \"" << attributePathPair.first
148                << "\" for object class \"" << getName() << "\"!");
149     }
150     return HLADataElement::IndexPathPair(index, attributePathPair.second);
151 }
152
153 HLADataElement::IndexPathPair
154 HLAObjectClass::getIndexPathPair(const std::string& path) const
155 {
156     return getIndexPathPair(HLADataElement::toAttributePathPair(path));
157 }
158
159 bool
160 HLAObjectClass::subscribe(const std::set<unsigned>& indexSet, bool active)
161 {
162     if (!_rtiObjectClass.valid()) {
163         SG_LOG(SG_NETWORK, SG_WARN, "HLAObjectClass::subscribe(): No RTIObject class for object class \"" << getName() << "\"!");
164         return false;
165     }
166     return _rtiObjectClass->subscribe(indexSet, active);
167 }
168
169 bool
170 HLAObjectClass::unsubscribe()
171 {
172     if (!_rtiObjectClass.valid()) {
173         SG_LOG(SG_NETWORK, SG_WARN, "HLAObjectClass::unsubscribe(): No RTIObject class for object class \"" << getName() << "\"!");
174         return false;
175     }
176     return _rtiObjectClass->unsubscribe();
177 }
178
179 bool
180 HLAObjectClass::publish(const std::set<unsigned>& indexSet)
181 {
182     if (!_rtiObjectClass.valid()) {
183         SG_LOG(SG_NETWORK, SG_WARN, "HLAObjectClass::publish(): No RTIObject class for object class \"" << getName() << "\"!");
184         return false;
185     }
186     return _rtiObjectClass->publish(indexSet);
187 }
188
189 bool
190 HLAObjectClass::unpublish()
191 {
192     if (!_rtiObjectClass.valid()) {
193         SG_LOG(SG_NETWORK, SG_WARN, "HLAObjectClass::unpublish(): No RTIObject class for object class \"" << getName() << "\"!");
194         return false;
195     }
196     return _rtiObjectClass->unpublish();
197 }
198
199 void
200 HLAObjectClass::startRegistration() const
201 {
202 }
203
204 void
205 HLAObjectClass::stopRegistration() const
206 {
207 }
208
209 HLAObjectInstance*
210 HLAObjectClass::createObjectInstance(RTIObjectInstance* rtiObjectInstance)
211 {
212     return new HLAObjectInstance(this, rtiObjectInstance);
213 }
214
215 void
216 HLAObjectClass::discoverInstance(RTIObjectInstance* objectInstance, const RTIData& tag)
217 {
218     SGSharedPtr<HLAObjectInstance> hlaObjectInstance = createObjectInstance(objectInstance);
219     if (hlaObjectInstance.valid()) {
220         SG_LOG(SG_NETWORK, SG_INFO, "RTI: create new object instance for discovered \""
221                << hlaObjectInstance->getName() << "\" object");
222         _objectInstanceSet.insert(hlaObjectInstance);
223         discoverInstanceCallback(*hlaObjectInstance, tag);
224     } else {
225         SG_LOG(SG_NETWORK, SG_INFO, "RTI: local delete of \"" << objectInstance->getName() << "\"");
226         objectInstance->localDeleteObjectInstance();
227     }
228 }
229
230 void
231 HLAObjectClass::removeInstance(HLAObjectInstance& hlaObjectInstance, const RTIData& tag)
232 {
233     SG_LOG(SG_NETWORK, SG_INFO, "RTI: remove object instance \"" << hlaObjectInstance.getName() << "\"");
234     removeInstanceCallback(hlaObjectInstance, tag);
235     _objectInstanceSet.erase(&hlaObjectInstance);
236 }
237
238 void
239 HLAObjectClass::registerInstance(HLAObjectInstance& objectInstance)
240 {
241     _objectInstanceSet.insert(&objectInstance);
242     registerInstanceCallback(objectInstance);
243 }
244
245 void
246 HLAObjectClass::deleteInstance(HLAObjectInstance& objectInstance)
247 {
248     deleteInstanceCallback(objectInstance);
249     _objectInstanceSet.erase(&objectInstance);
250 }
251
252 void
253 HLAObjectClass::discoverInstanceCallback(HLAObjectInstance& objectInstance, const RTIData& tag) const
254 {
255     if (!_instanceCallback.valid())
256         return;
257     _instanceCallback->discoverInstance(*this, objectInstance, tag);
258 }
259
260 void
261 HLAObjectClass::removeInstanceCallback(HLAObjectInstance& objectInstance, const RTIData& tag) const
262 {
263     if (!_instanceCallback.valid())
264         return;
265     _instanceCallback->removeInstance(*this, objectInstance, tag);
266 }
267
268 void
269 HLAObjectClass::registerInstanceCallback(HLAObjectInstance& objectInstance) const
270 {
271     if (!_instanceCallback.valid())
272         return;
273     _instanceCallback->registerInstance(*this, objectInstance);
274 }
275
276 void
277 HLAObjectClass::deleteInstanceCallback(HLAObjectInstance& objectInstance) const
278 {
279     if (!_instanceCallback.valid())
280         return;
281     _instanceCallback->deleteInstance(*this, objectInstance);
282 }
283
284 void
285 HLAObjectClass::startRegistrationCallback()
286 {
287     if (_registrationCallback.valid())
288         _registrationCallback->startRegistration(*this);
289     else
290         startRegistration();
291 }
292
293 void
294 HLAObjectClass::stopRegistrationCallback()
295 {
296     if (_registrationCallback.valid())
297         _registrationCallback->stopRegistration(*this);
298     else
299         stopRegistration();
300 }
301
302 } // namespace simgear