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.
18 #include "HLAInteractionClass.hxx"
20 #include <simgear/debug/logstream.hxx>
22 #include "HLADataElement.hxx"
23 #include "HLAFederate.hxx"
25 #include "RTIInteractionClass.hxx"
29 HLAInteractionClass::HLAInteractionClass(const std::string& name, HLAFederate* federate) :
31 _rtiInteractionClass(0),
33 _subscriptionType(HLAUnsubscribed),
34 _publicationType(HLAUnpublished)
37 SG_LOG(SG_NETWORK, SG_ALERT, "HLAInteractionClass::HLAInteractionClass(): "
38 "No parent federate given for interaction class \"" << getName() << "\"!");
41 federate->_insertInteractionClass(this);
44 HLAInteractionClass::~HLAInteractionClass()
46 // HLAInteractionClass objects only get deleted when the parent federate
47 // dies. So we do not need to deregister there.
49 _clearRTIInteractionClass();
53 HLAInteractionClass::getName() const
59 HLAInteractionClass::getSubscriptionType() const
61 return _subscriptionType;
65 HLAInteractionClass::setSubscriptionType(HLASubscriptionType subscriptionType)
67 _subscriptionType = subscriptionType;
71 HLAInteractionClass::getPublicationType() const
73 return _publicationType;
77 HLAInteractionClass::setPublicationType(HLAPublicationType publicationType)
79 _publicationType = publicationType;
83 HLAInteractionClass::getNumParameters() const
85 return _parameterVector.size();
89 HLAInteractionClass::addParameter(const std::string& name)
91 unsigned index = _parameterVector.size();
92 _nameIndexMap[name] = index;
93 _parameterVector.push_back(Parameter(name));
94 _resolveParameterIndex(name, index);
99 HLAInteractionClass::getParameterIndex(const std::string& name) const
101 NameIndexMap::const_iterator i = _nameIndexMap.find(name);
102 if (i == _nameIndexMap.end())
108 HLAInteractionClass::getParameterName(unsigned index) const
110 if (_parameterVector.size() <= index)
111 return std::string();
112 return _parameterVector[index]._name;
116 HLAInteractionClass::getParameterDataType(unsigned index) const
118 if (_parameterVector.size() <= index)
120 return _parameterVector[index]._dataType.get();
124 HLAInteractionClass::setParameterDataType(unsigned index, const SGSharedPtr<const HLADataType>& dataType)
126 if (_parameterVector.size() <= index)
128 _parameterVector[index]._dataType = dataType;
131 HLADataElement::IndexPathPair
132 HLAInteractionClass::getIndexPathPair(const HLADataElement::StringPathPair& stringPathPair) const
134 unsigned index = getParameterIndex(stringPathPair.first);
135 if (getNumParameters() <= index) {
136 SG_LOG(SG_NETWORK, SG_ALERT, "HLAInteractionClass::getIndexPathPair(\""
137 << HLADataElement::toString(stringPathPair)
138 << "\"): Could not resolve attribute \"" << stringPathPair.first
139 << "\" for interaction class \"" << getName() << "\"!");
141 return HLADataElement::IndexPathPair(index, stringPathPair.second);
144 HLADataElement::IndexPathPair
145 HLAInteractionClass::getIndexPathPair(const std::string& path) const
147 return getIndexPathPair(HLADataElement::toStringPathPair(path));
151 HLAInteractionClass::subscribe()
153 if (!_rtiInteractionClass) {
154 SG_LOG(SG_NETWORK, SG_WARN, "HLAInteractionClass::subscribe(): No RTIInteractionClass!");
157 switch (_subscriptionType) {
158 case HLAUnsubscribed:
159 return _rtiInteractionClass->unsubscribe();
160 case HLASubscribedActive:
161 return _rtiInteractionClass->subscribe(true);
162 case HLASubscribedPassive:
163 return _rtiInteractionClass->subscribe(false);
169 HLAInteractionClass::unsubscribe()
171 if (!_rtiInteractionClass) {
172 SG_LOG(SG_NETWORK, SG_WARN, "HLAInteractionClass::unsubscribe(): No RTIInteractionClass!");
175 return _rtiInteractionClass->unsubscribe();
179 HLAInteractionClass::publish()
181 if (!_rtiInteractionClass) {
182 SG_LOG(SG_NETWORK, SG_WARN, "HLAInteractionClass::publish(): No RTIInteractionClass\"!");
185 switch (_publicationType) {
187 return _rtiInteractionClass->unpublish();
189 return _rtiInteractionClass->publish();
195 HLAInteractionClass::unpublish()
197 if (!_rtiInteractionClass) {
198 SG_LOG(SG_NETWORK, SG_WARN, "HLAInteractionClass::unpublish(): No RTIInteractionClass\"!");
201 return _rtiInteractionClass->unpublish();
205 HLAInteractionClass::_setRTIInteractionClass(RTIInteractionClass* interactionClass)
207 if (_rtiInteractionClass) {
208 SG_LOG(SG_NETWORK, SG_ALERT, "HLAInteractionClass: Setting RTIInteractionClass twice for interaction class \"" << getName() << "\"!");
211 _rtiInteractionClass = interactionClass;
212 if (_rtiInteractionClass->_interactionClass != this) {
213 SG_LOG(SG_NETWORK, SG_ALERT, "HLAInteractionClass: backward reference does not match!");
216 for (unsigned i = 0; i < _parameterVector.size(); ++i)
217 _resolveParameterIndex(_parameterVector[i]._name, i);
221 HLAInteractionClass::_resolveParameterIndex(const std::string& name, unsigned index)
223 if (!_rtiInteractionClass)
225 if (!_rtiInteractionClass->resolveParameterIndex(name, index))
226 SG_LOG(SG_NETWORK, SG_ALERT, "HLAInteractionClass: Could not resolve parameter \""
227 << name << "\" for interaction class \"" << getName() << "\"!");
231 HLAInteractionClass::_clearRTIInteractionClass()
233 if (!_rtiInteractionClass)
235 _rtiInteractionClass->_interactionClass = 0;
236 _rtiInteractionClass = 0;
239 } // namespace simgear