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 "HLAInteractionClass.hxx"
26 #include <simgear/debug/logstream.hxx>
28 #include "HLADataElement.hxx"
29 #include "HLAFederate.hxx"
31 #include "RTIInteractionClass.hxx"
35 HLAInteractionClass::HLAInteractionClass(const std::string& name, HLAFederate* federate) :
37 _rtiInteractionClass(0),
39 _subscriptionType(HLAUnsubscribed),
40 _publicationType(HLAUnpublished)
43 SG_LOG(SG_NETWORK, SG_ALERT, "HLAInteractionClass::HLAInteractionClass(): "
44 "No parent federate given for interaction class \"" << getName() << "\"!");
47 federate->_insertInteractionClass(this);
50 HLAInteractionClass::~HLAInteractionClass()
52 // HLAInteractionClass objects only get deleted when the parent federate
53 // dies. So we do not need to deregister there.
55 _clearRTIInteractionClass();
59 HLAInteractionClass::getName() const
64 const SGWeakPtr<HLAFederate>&
65 HLAInteractionClass::getFederate() const
71 HLAInteractionClass::getSubscriptionType() const
73 return _subscriptionType;
77 HLAInteractionClass::setSubscriptionType(HLASubscriptionType subscriptionType)
79 _subscriptionType = subscriptionType;
83 HLAInteractionClass::getPublicationType() const
85 return _publicationType;
89 HLAInteractionClass::setPublicationType(HLAPublicationType publicationType)
91 _publicationType = publicationType;
95 HLAInteractionClass::getNumParameters() const
97 return _parameterVector.size();
101 HLAInteractionClass::addParameter(const std::string& name)
103 unsigned index = _parameterVector.size();
104 _nameIndexMap[name] = index;
105 _parameterVector.push_back(Parameter(name));
106 _resolveParameterIndex(name, index);
111 HLAInteractionClass::getParameterIndex(const std::string& name) const
113 NameIndexMap::const_iterator i = _nameIndexMap.find(name);
114 if (i == _nameIndexMap.end())
120 HLAInteractionClass::getParameterName(unsigned index) const
122 if (_parameterVector.size() <= index)
123 return std::string();
124 return _parameterVector[index]._name;
128 HLAInteractionClass::getParameterDataType(unsigned index) const
130 if (_parameterVector.size() <= index)
132 return _parameterVector[index]._dataType.get();
136 HLAInteractionClass::setParameterDataType(unsigned index, const SGSharedPtr<const HLADataType>& dataType)
138 if (_parameterVector.size() <= index)
140 _parameterVector[index]._dataType = dataType;
144 HLAInteractionClass::getDataElementIndex(HLADataElementIndex& dataElementIndex, const std::string& path) const
147 SG_LOG(SG_NETWORK, SG_ALERT, "HLAObjectClass: failed to parse empty element path!");
150 std::string::size_type len = std::min(path.find_first_of("[."), path.size());
152 while (index < getNumParameters()) {
153 if (path.compare(0, len, getParameterName(index)) == 0)
157 if (getNumParameters() <= index) {
158 SG_LOG(SG_NETWORK, SG_ALERT, "HLAObjectClass: faild to parse data element index \"" << path << "\":\n"
159 << "Parameter \"" << path.substr(0, len) << "\" not found in object class \""
160 << getName() << "\"!");
163 if (!getParameterDataType(index)) {
164 SG_LOG(SG_NETWORK, SG_ALERT, "HLAObjectClass: faild to parse data element index \"" << path << "\":\n"
165 << "Undefined parameter data type in variant record data type \""
166 << getParameterName(index) << "\"!");
169 dataElementIndex.push_back(index);
170 return getParameterDataType(index)->getDataElementIndex(dataElementIndex, path, len);
174 HLAInteractionClass::getDataElementIndex(const std::string& path) const
176 HLADataElementIndex dataElementIndex;
177 getDataElementIndex(dataElementIndex, path);
178 return dataElementIndex;
182 HLAInteractionClass::subscribe()
184 if (!_rtiInteractionClass) {
185 SG_LOG(SG_NETWORK, SG_WARN, "HLAInteractionClass::subscribe(): No RTIInteractionClass!");
188 switch (_subscriptionType) {
189 case HLAUnsubscribed:
190 return _rtiInteractionClass->unsubscribe();
191 case HLASubscribedActive:
192 return _rtiInteractionClass->subscribe(true);
193 case HLASubscribedPassive:
194 return _rtiInteractionClass->subscribe(false);
200 HLAInteractionClass::unsubscribe()
202 if (!_rtiInteractionClass) {
203 SG_LOG(SG_NETWORK, SG_WARN, "HLAInteractionClass::unsubscribe(): No RTIInteractionClass!");
206 return _rtiInteractionClass->unsubscribe();
210 HLAInteractionClass::publish()
212 if (!_rtiInteractionClass) {
213 SG_LOG(SG_NETWORK, SG_WARN, "HLAInteractionClass::publish(): No RTIInteractionClass\"!");
216 switch (_publicationType) {
218 return _rtiInteractionClass->unpublish();
220 return _rtiInteractionClass->publish();
226 HLAInteractionClass::unpublish()
228 if (!_rtiInteractionClass) {
229 SG_LOG(SG_NETWORK, SG_WARN, "HLAInteractionClass::unpublish(): No RTIInteractionClass\"!");
232 return _rtiInteractionClass->unpublish();
236 HLAInteractionClass::_setRTIInteractionClass(RTIInteractionClass* interactionClass)
238 if (_rtiInteractionClass) {
239 SG_LOG(SG_NETWORK, SG_ALERT, "HLAInteractionClass: Setting RTIInteractionClass twice for interaction class \"" << getName() << "\"!");
242 _rtiInteractionClass = interactionClass;
243 if (_rtiInteractionClass->_interactionClass != this) {
244 SG_LOG(SG_NETWORK, SG_ALERT, "HLAInteractionClass: backward reference does not match!");
247 for (unsigned i = 0; i < _parameterVector.size(); ++i)
248 _resolveParameterIndex(_parameterVector[i]._name, i);
252 HLAInteractionClass::_resolveParameterIndex(const std::string& name, unsigned index)
254 if (!_rtiInteractionClass)
256 if (!_rtiInteractionClass->resolveParameterIndex(name, index))
257 SG_LOG(SG_NETWORK, SG_ALERT, "HLAInteractionClass: Could not resolve parameter \""
258 << name << "\" for interaction class \"" << getName() << "\"!");
262 HLAInteractionClass::_clearRTIInteractionClass()
264 if (!_rtiInteractionClass)
266 _rtiInteractionClass->_interactionClass = 0;
267 _rtiInteractionClass = 0;
270 } // namespace simgear