1 // Copyright (C) 2009 - 2012  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 #ifdef HAVE_CONFIG_H
19 #  include <simgear_config.h>
20 #endif
21 
22 #include <simgear/compiler.h>
23 
24 #include "RTI13InteractionClass.hxx"
25 
26 #include "RTI13Ambassador.hxx"
27 
28 namespace simgear {
29 
RTI13InteractionClass(HLAInteractionClass * interactionClass,const RTI::InteractionClassHandle & handle,RTI13Ambassador * ambassador)30 RTI13InteractionClass::RTI13InteractionClass(HLAInteractionClass* interactionClass, const RTI::InteractionClassHandle& handle, RTI13Ambassador* ambassador) :
31     RTIInteractionClass(interactionClass),
32     _handle(handle),
33     _ambassador(ambassador)
34 {
35 }
36 
~RTI13InteractionClass()37 RTI13InteractionClass::~RTI13InteractionClass()
38 {
39 }
40 
41 bool
resolveParameterIndex(const std::string & name,unsigned index)42 RTI13InteractionClass::resolveParameterIndex(const std::string& name, unsigned index)
43 {
44     if (!_ambassador.valid()) {
45         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
46         return false;
47     }
48 
49     if (index != _parameterHandleVector.size()) {
50         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Resolving needs to happen in growing index order!");
51         return false;
52     }
53 
54     try {
55         RTI::ParameterHandle parameterHandle = _ambassador->getParameterHandle(name, _handle);
56 
57         ParameterHandleIndexMap::const_iterator i = _parameterHandleIndexMap.find(parameterHandle);
58         if (i != _parameterHandleIndexMap.end()) {
59             SG_LOG(SG_NETWORK, SG_WARN, "RTI: Resolving parameterIndex for parameter \"" << name << "\" twice!");
60             return false;
61         }
62 
63         _parameterHandleIndexMap[parameterHandle] = index;
64         _parameterHandleVector.push_back(parameterHandle);
65 
66         return true;
67 
68     } catch (RTI::NameNotFound& e) {
69         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get interaction class parameter: " << e._name << " " << e._reason);
70         return false;
71     } catch (RTI::FederateNotExecutionMember& e) {
72         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get interaction class parameter: " << e._name << " " << e._reason);
73         return false;
74     } catch (RTI::ConcurrentAccessAttempted& e) {
75         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get interaction class parameter: " << e._name << " " << e._reason);
76         return false;
77     } catch (RTI::RTIinternalError& e) {
78         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get interaction class parameter: " << e._name << " " << e._reason);
79         return false;
80     } catch (...) {
81         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not get interaction class parameter.");
82         return false;
83     }
84 
85     return false;
86 }
87 
88 bool
publish()89 RTI13InteractionClass::publish()
90 {
91     if (!_ambassador.valid()) {
92         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
93         return false;
94     }
95 
96     try {
97         _ambassador->publishInteractionClass(_handle);
98         return true;
99     } catch (RTI::InteractionClassNotDefined& e) {
100         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish interaction class: " << e._name << " " << e._reason);
101         return false;
102     } catch (RTI::FederateNotExecutionMember& e) {
103         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish interaction class: " << e._name << " " << e._reason);
104         return false;
105     } catch (RTI::ConcurrentAccessAttempted& e) {
106         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish interaction class: " << e._name << " " << e._reason);
107         return false;
108     } catch (RTI::SaveInProgress& e) {
109         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish interaction class: " << e._name << " " << e._reason);
110         return false;
111     } catch (RTI::RestoreInProgress& e) {
112         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish interaction class: " << e._name << " " << e._reason);
113         return false;
114     } catch (RTI::RTIinternalError& e) {
115         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish interaction class: " << e._name << " " << e._reason);
116         return false;
117     } catch (...) {
118         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not publish interaction class.");
119         return false;
120     }
121 
122     return false;
123 }
124 
125 bool
unpublish()126 RTI13InteractionClass::unpublish()
127 {
128     if (!_ambassador.valid()) {
129         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
130         return false;
131     }
132 
133     try {
134         _ambassador->unpublishInteractionClass(_handle);
135         return true;
136     } catch (RTI::InteractionClassNotDefined& e) {
137         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish interaction class: " << e._name << " " << e._reason);
138         return false;
139     } catch (RTI::InteractionClassNotPublished& e) {
140         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish interaction class: " << e._name << " " << e._reason);
141         return false;
142     } catch (RTI::FederateNotExecutionMember& e) {
143         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish interaction class: " << e._name << " " << e._reason);
144         return false;
145     } catch (RTI::ConcurrentAccessAttempted& e) {
146         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish interaction class: " << e._name << " " << e._reason);
147         return false;
148     } catch (RTI::SaveInProgress& e) {
149         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish interaction class: " << e._name << " " << e._reason);
150         return false;
151     } catch (RTI::RestoreInProgress& e) {
152         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish interaction class: " << e._name << " " << e._reason);
153         return false;
154     } catch (RTI::RTIinternalError& e) {
155         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish interaction class: " << e._name << " " << e._reason);
156         return false;
157     } catch (...) {
158         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unpublish interaction class.");
159         return false;
160     }
161 
162     return false;
163 }
164 
165 bool
subscribe(bool active)166 RTI13InteractionClass::subscribe(bool active)
167 {
168     if (!_ambassador.valid()) {
169         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
170         return false;
171     }
172 
173     try {
174         _ambassador->subscribeInteractionClass(_handle, active);
175         return true;
176     } catch (RTI::InteractionClassNotDefined& e) {
177         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe interaction class: " << e._name << " " << e._reason);
178         return false;
179     } catch (RTI::FederateNotExecutionMember& e) {
180         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe interaction class: " << e._name << " " << e._reason);
181         return false;
182     } catch (RTI::ConcurrentAccessAttempted& e) {
183         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe interaction class: " << e._name << " " << e._reason);
184         return false;
185     } catch (RTI::FederateLoggingServiceCalls& e) {
186         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe interaction class: " << e._name << " " << e._reason);
187         return false;
188     } catch (RTI::SaveInProgress& e) {
189         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe interaction class: " << e._name << " " << e._reason);
190         return false;
191     } catch (RTI::RestoreInProgress& e) {
192         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe interaction class: " << e._name << " " << e._reason);
193         return false;
194     } catch (RTI::RTIinternalError& e) {
195         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe interaction class: " << e._name << " " << e._reason);
196         return false;
197     } catch (...) {
198         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not subscribe interaction class.");
199         return false;
200     }
201 
202     return false;
203 }
204 
205 bool
unsubscribe()206 RTI13InteractionClass::unsubscribe()
207 {
208     if (!_ambassador.valid()) {
209         SG_LOG(SG_NETWORK, SG_WARN, "Error: Ambassador is zero.");
210         return false;
211     }
212 
213     try {
214         _ambassador->unsubscribeInteractionClass(_handle);
215         return true;
216     } catch (RTI::InteractionClassNotDefined& e) {
217         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe interaction class: " << e._name << " " << e._reason);
218         return false;
219     } catch (RTI::InteractionClassNotSubscribed& e) {
220         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe interaction class: " << e._name << " " << e._reason);
221         return false;
222     } catch (RTI::FederateNotExecutionMember& e) {
223         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe interaction class: " << e._name << " " << e._reason);
224         return false;
225     } catch (RTI::ConcurrentAccessAttempted& e) {
226         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe interaction class: " << e._name << " " << e._reason);
227         return false;
228     } catch (RTI::SaveInProgress& e) {
229         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe interaction class: " << e._name << " " << e._reason);
230         return false;
231     } catch (RTI::RestoreInProgress& e) {
232         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe interaction class: " << e._name << " " << e._reason);
233         return false;
234     } catch (RTI::RTIinternalError& e) {
235         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe interaction class: " << e._name << " " << e._reason);
236         return false;
237     } catch (...) {
238         SG_LOG(SG_NETWORK, SG_WARN, "RTI: Could not unsubscribe interaction class.");
239         return false;
240     }
241 
242     return false;
243 }
244 
245 }
246