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 "HLAFederate.hxx"
20 #include "RTIFederate.hxx"
21 #include "RTIInteractionClass.hxx"
22 #include "RTIObjectClass.hxx"
23 #include "HLADataElement.hxx"
24 #include "HLADataType.hxx"
25 #include "HLAOMTXmlVisitor.hxx"
29 HLAFederate::HLAFederate(const SGSharedPtr<RTIFederate>& rtiFederate) :
30 _rtiFederate(rtiFederate)
34 HLAFederate::~HLAFederate()
39 HLAFederate::getFederateType() const
41 return _rtiFederate->getFederateType();
45 HLAFederate::getFederationName() const
47 return _rtiFederate->getFederationName();
51 HLAFederate::createFederationExecution(const std::string& federation, const std::string& objectModel)
53 return _rtiFederate->createFederationExecution(federation, objectModel);
57 HLAFederate::destroyFederationExecution(const std::string& federation)
59 return _rtiFederate->destroyFederationExecution(federation);
63 HLAFederate::join(const std::string& federateType, const std::string& federation)
65 return _rtiFederate->join(federateType, federation);
71 return _rtiFederate->resign();
75 HLAFederate::enableTimeConstrained()
77 return _rtiFederate->enableTimeConstrained();
81 HLAFederate::disableTimeConstrained()
83 return _rtiFederate->disableTimeConstrained();
87 HLAFederate::enableTimeRegulation(const SGTimeStamp& lookahead)
89 return _rtiFederate->enableTimeRegulation(lookahead);
93 HLAFederate::disableTimeRegulation()
95 return _rtiFederate->disableTimeRegulation();
99 HLAFederate::timeAdvanceRequestBy(const SGTimeStamp& dt)
101 return _rtiFederate->timeAdvanceRequestBy(dt);
105 HLAFederate::timeAdvanceRequest(const SGTimeStamp& dt)
107 return _rtiFederate->timeAdvanceRequest(dt);
111 HLAFederate::queryFederateTime(SGTimeStamp& timeStamp)
113 return _rtiFederate->queryFederateTime(timeStamp);
117 HLAFederate::queryLookahead(SGTimeStamp& timeStamp)
119 return _rtiFederate->queryLookahead(timeStamp);
125 return _rtiFederate->tick();
129 HLAFederate::tick(const double& minimum, const double& maximum)
131 return _rtiFederate->tick(minimum, maximum);
135 HLAFederate::readObjectModelTemplate(const std::string& objectModel,
136 HLAFederate::ObjectModelFactory& objectModelFactory)
138 if (!_rtiFederate.valid()) {
139 SG_LOG(SG_IO, SG_ALERT, "Could not process HLA XML object model file: "
140 "No rti federate available!");
144 // The XML version of the federate object model.
145 // This one covers the generic attributes, parameters and data types.
146 HLAOMTXmlVisitor omtXmlVisitor;
148 readXML(objectModel, omtXmlVisitor);
149 } catch (const sg_throwable& e) {
150 SG_LOG(SG_IO, SG_ALERT, "Could not open HLA XML object model file: "
154 SG_LOG(SG_IO, SG_ALERT, "Could not open HLA XML object model file");
158 unsigned numObjectClasses = omtXmlVisitor.getNumObjectClasses();
159 for (unsigned i = 0; i < numObjectClasses; ++i) {
160 const HLAOMTXmlVisitor::ObjectClass* objectClass = omtXmlVisitor.getObjectClass(i);
161 std::string objectClassName = objectClass->getName();
163 SGSharedPtr<HLAObjectClass> hlaObjectClass = objectModelFactory.createObjectClass(objectClassName, *this);
164 if (!hlaObjectClass.valid()) {
165 SG_LOG(SG_IO, SG_INFO, "Ignoring object class \"" << objectClassName << "\".");
169 bool publish = objectModelFactory.publishObjectClass(objectClassName, objectClass->getSharing());
170 bool subscribe = objectModelFactory.subscribeObjectClass(objectClassName, objectClass->getSharing());
172 std::set<unsigned> subscriptions;
173 std::set<unsigned> publications;
175 // process the attributes
176 for (unsigned j = 0; j < objectClass->getNumAttributes(); ++j) {
177 const simgear::HLAOMTXmlVisitor::Attribute* attribute;
178 attribute = objectClass->getAttribute(j);
180 std::string attributeName = attribute->getName();
181 unsigned index = hlaObjectClass->getAttributeIndex(attributeName);
184 SG_LOG(SG_IO, SG_WARN, "RTI does not know the \"" << attributeName << "\" attribute!");
188 SGSharedPtr<HLADataType> dataType;
189 dataType = omtXmlVisitor.getAttributeDataType(objectClassName, attributeName);
190 if (!dataType.valid()) {
191 SG_LOG(SG_IO, SG_WARN, "Could not find data type for attribute \""
192 << attributeName << "\" in object class \"" << objectClassName << "\"!");
194 hlaObjectClass->setAttributeDataType(index, dataType);
196 HLAUpdateType updateType = HLAUndefinedUpdate;
197 if (attribute->_updateType == "Periodic")
198 updateType = HLAPeriodicUpdate;
199 else if (attribute->_updateType == "Static")
200 updateType = HLAStaticUpdate;
201 else if (attribute->_updateType == "Conditional")
202 updateType = HLAConditionalUpdate;
203 hlaObjectClass->setAttributeUpdateType(index, updateType);
205 if (subscribe && objectModelFactory.subscribeAttribute(objectClassName, attributeName, attribute->_sharing))
206 subscriptions.insert(index);
207 if (publish && objectModelFactory.publishAttribute(objectClassName, attributeName, attribute->_sharing))
208 publications.insert(index);
212 hlaObjectClass->publish(publications);
214 hlaObjectClass->subscribe(subscriptions, true);
216 _objectClassMap[objectClassName] = hlaObjectClass;
223 HLAFederate::getObjectClass(const std::string& name)
225 ObjectClassMap::const_iterator i = _objectClassMap.find(name);
226 if (i == _objectClassMap.end())
228 return i->second.get();
231 const HLAObjectClass*
232 HLAFederate::getObjectClass(const std::string& name) const
234 ObjectClassMap::const_iterator i = _objectClassMap.find(name);
235 if (i == _objectClassMap.end())
237 return i->second.get();
241 HLAFederate::getInteractionClass(const std::string& name)
243 InteractionClassMap::const_iterator i = _interactionClassMap.find(name);
244 if (i == _interactionClassMap.end())
246 return i->second.get();
249 const HLAInteractionClass*
250 HLAFederate::getInteractionClass(const std::string& name) const
252 InteractionClassMap::const_iterator i = _interactionClassMap.find(name);
253 if (i == _interactionClassMap.end())
255 return i->second.get();
258 } // namespace simgear