]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAFederate.cxx
Add modifyLookahead to the rti abstraction.
[simgear.git] / simgear / hla / HLAFederate.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 "HLAFederate.hxx"
19
20 #include "RTIFederate.hxx"
21 #include "RTIInteractionClass.hxx"
22 #include "RTIObjectClass.hxx"
23 #include "HLADataElement.hxx"
24 #include "HLADataType.hxx"
25 #include "HLAOMTXmlVisitor.hxx"
26
27 namespace simgear {
28
29 HLAFederate::HLAFederate(const SGSharedPtr<RTIFederate>& rtiFederate) :
30     _rtiFederate(rtiFederate)
31 {
32 }
33
34 HLAFederate::~HLAFederate()
35 {
36 }
37
38 const std::string&
39 HLAFederate::getFederateType() const
40 {
41     return _rtiFederate->getFederateType();
42 }
43
44 const std::string&
45 HLAFederate::getFederationName() const
46 {
47     return _rtiFederate->getFederationName();
48 }
49
50 bool
51 HLAFederate::createFederationExecution(const std::string& federation, const std::string& objectModel)
52 {
53     return _rtiFederate->createFederationExecution(federation, objectModel);
54 }
55
56 bool
57 HLAFederate::destroyFederationExecution(const std::string& federation)
58 {
59     return _rtiFederate->destroyFederationExecution(federation);
60 }
61
62 bool
63 HLAFederate::join(const std::string& federateType, const std::string& federation)
64 {
65     return _rtiFederate->join(federateType, federation);
66 }
67
68 bool
69 HLAFederate::resign()
70 {
71     return _rtiFederate->resign();
72 }
73
74 bool
75 HLAFederate::enableTimeConstrained()
76 {
77     return _rtiFederate->enableTimeConstrained();
78 }
79
80 bool
81 HLAFederate::disableTimeConstrained()
82 {
83     return _rtiFederate->disableTimeConstrained();
84 }
85
86 bool
87 HLAFederate::enableTimeRegulation(const SGTimeStamp& lookahead)
88 {
89     return _rtiFederate->enableTimeRegulation(lookahead);
90 }
91
92 bool
93 HLAFederate::disableTimeRegulation()
94 {
95     return _rtiFederate->disableTimeRegulation();
96 }
97
98 bool
99 HLAFederate::timeAdvanceRequestBy(const SGTimeStamp& dt)
100 {
101     return _rtiFederate->timeAdvanceRequestBy(dt);
102 }
103
104 bool
105 HLAFederate::timeAdvanceRequest(const SGTimeStamp& dt)
106 {
107     return _rtiFederate->timeAdvanceRequest(dt);
108 }
109
110 bool
111 HLAFederate::queryFederateTime(SGTimeStamp& timeStamp)
112 {
113     return _rtiFederate->queryFederateTime(timeStamp);
114 }
115
116 bool
117 HLAFederate::modifyLookahead(const SGTimeStamp& timeStamp)
118 {
119     return _rtiFederate->modifyLookahead(timeStamp);
120 }
121
122 bool
123 HLAFederate::queryLookahead(SGTimeStamp& timeStamp)
124 {
125     return _rtiFederate->queryLookahead(timeStamp);
126 }
127
128 bool
129 HLAFederate::tick()
130 {
131     return _rtiFederate->tick();
132 }
133
134 bool
135 HLAFederate::tick(const double& minimum, const double& maximum)
136 {
137     return _rtiFederate->tick(minimum, maximum);
138 }
139
140 bool
141 HLAFederate::readObjectModelTemplate(const std::string& objectModel,
142                                      HLAFederate::ObjectModelFactory& objectModelFactory)
143 {
144     if (!_rtiFederate.valid()) {
145         SG_LOG(SG_IO, SG_ALERT, "Could not process HLA XML object model file: "
146                "No rti federate available!");
147         return false;
148     }
149
150     // The XML version of the federate object model.
151     // This one covers the generic attributes, parameters and data types.
152     HLAOMTXmlVisitor omtXmlVisitor;
153     try {
154         readXML(objectModel, omtXmlVisitor);
155     } catch (const sg_throwable& e) {
156         SG_LOG(SG_IO, SG_ALERT, "Could not open HLA XML object model file: "
157                << e.getMessage());
158         return false;
159     } catch (...) {
160         SG_LOG(SG_IO, SG_ALERT, "Could not open HLA XML object model file");
161         return false;
162     }
163
164     unsigned numObjectClasses = omtXmlVisitor.getNumObjectClasses();
165     for (unsigned i = 0; i < numObjectClasses; ++i) {
166         const HLAOMTXmlVisitor::ObjectClass* objectClass = omtXmlVisitor.getObjectClass(i);
167         std::string objectClassName = objectClass->getName();
168
169         SGSharedPtr<HLAObjectClass> hlaObjectClass = objectModelFactory.createObjectClass(objectClassName, *this);
170         if (!hlaObjectClass.valid()) {
171             SG_LOG(SG_IO, SG_INFO, "Ignoring object class \"" << objectClassName << "\".");
172             continue;
173         }
174
175         bool publish = objectModelFactory.publishObjectClass(objectClassName, objectClass->getSharing());
176         bool subscribe = objectModelFactory.subscribeObjectClass(objectClassName, objectClass->getSharing());
177
178         std::set<unsigned> subscriptions;
179         std::set<unsigned> publications;
180
181         // process the attributes
182         for (unsigned j = 0; j < objectClass->getNumAttributes(); ++j) {
183             const simgear::HLAOMTXmlVisitor::Attribute* attribute;
184             attribute = objectClass->getAttribute(j);
185
186             std::string attributeName = attribute->getName();
187             unsigned index = hlaObjectClass->getAttributeIndex(attributeName);
188
189             if (index == ~0u) {
190                 SG_LOG(SG_IO, SG_WARN, "RTI does not know the \"" << attributeName << "\" attribute!");
191                 continue;
192             }
193
194             SGSharedPtr<HLADataType> dataType;
195             dataType = omtXmlVisitor.getAttributeDataType(objectClassName, attributeName);
196             if (!dataType.valid()) {
197                 SG_LOG(SG_IO, SG_WARN, "Could not find data type for attribute \""
198                        << attributeName << "\" in object class \"" << objectClassName << "\"!");
199             }
200             hlaObjectClass->setAttributeDataType(index, dataType);
201
202             HLAUpdateType updateType = HLAUndefinedUpdate;
203             if (attribute->_updateType == "Periodic")
204                 updateType = HLAPeriodicUpdate;
205             else if (attribute->_updateType == "Static")
206                 updateType = HLAStaticUpdate;
207             else if (attribute->_updateType == "Conditional")
208                 updateType = HLAConditionalUpdate;
209             hlaObjectClass->setAttributeUpdateType(index, updateType);
210
211             if (subscribe && objectModelFactory.subscribeAttribute(objectClassName, attributeName, attribute->_sharing))
212                 subscriptions.insert(index);
213             if (publish && objectModelFactory.publishAttribute(objectClassName, attributeName, attribute->_sharing))
214                 publications.insert(index);
215         }
216
217         if (publish)
218             hlaObjectClass->publish(publications);
219         if (subscribe)
220             hlaObjectClass->subscribe(subscriptions, true);
221
222         _objectClassMap[objectClassName] = hlaObjectClass;
223     }
224
225     return true;
226 }
227
228 HLAObjectClass*
229 HLAFederate::getObjectClass(const std::string& name)
230 {
231     ObjectClassMap::const_iterator i = _objectClassMap.find(name);
232     if (i == _objectClassMap.end())
233         return 0;
234     return i->second.get();
235 }
236
237 const HLAObjectClass*
238 HLAFederate::getObjectClass(const std::string& name) const
239 {
240     ObjectClassMap::const_iterator i = _objectClassMap.find(name);
241     if (i == _objectClassMap.end())
242         return 0;
243     return i->second.get();
244 }
245
246 HLAInteractionClass*
247 HLAFederate::getInteractionClass(const std::string& name)
248 {
249     InteractionClassMap::const_iterator i = _interactionClassMap.find(name);
250     if (i == _interactionClassMap.end())
251         return 0;
252     return i->second.get();
253 }
254
255 const HLAInteractionClass*
256 HLAFederate::getInteractionClass(const std::string& name) const
257 {
258     InteractionClassMap::const_iterator i = _interactionClassMap.find(name);
259     if (i == _interactionClassMap.end())
260         return 0;
261     return i->second.get();
262 }
263
264 } // namespace simgear