]> git.mxchange.org Git - simgear.git/blob - simgear/hla/HLAFederate.hxx
4c82820cb13c9183a3fe813ea75856c6c4aff6f0
[simgear.git] / simgear / hla / HLAFederate.hxx
1 // Copyright (C) 2009 - 2011  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 #ifndef HLAFederate_hxx
19 #define HLAFederate_hxx
20
21 #include <map>
22
23 #include "HLAObjectInstance.hxx"
24 #include "HLAObjectClass.hxx"
25 #include "HLAInteractionClass.hxx"
26
27 class SGTimeStamp;
28
29 namespace simgear {
30
31 class RTIFederate;
32
33 class HLAFederate : public SGWeakReferenced {
34 public:
35     HLAFederate();
36     virtual ~HLAFederate();
37
38     enum Version {
39         RTI13,
40         RTI1516,
41         RTI1516E
42     };
43
44     Version getVersion() const;
45     bool setVersion(HLAFederate::Version version);
46
47     const std::list<std::string>& getConnectArguments() const;
48     bool setConnectArguments(const std::list<std::string>& connectArguments);
49
50     const std::string& getFederationExecutionName() const;
51     bool setFederationExecutionName(const std::string& federationExecutionName);
52
53     const std::string& getFederationObjectModel() const;
54     bool setFederationObjectModel(const std::string& federationObjectModel);
55
56     const std::string& getFederateType() const;
57     bool setFederateType(const std::string& federateType);
58
59     const std::string& getFederateName() const;
60     bool setFederateName(const std::string& federateName);
61
62     /// connect to an rti
63     bool connect(Version version, const std::list<std::string>& stringList);
64     bool connect();
65     bool disconnect();
66
67     /// Create a federation execution
68     /// Semantically this methods should be static,
69     /// but the nonstatic case could reuse the connection to the server
70     bool createFederationExecution(const std::string& federation, const std::string& objectModel);
71     bool destroyFederationExecution(const std::string& federation);
72     bool createFederationExecution();
73     bool destroyFederationExecution();
74
75     /// Join with federateType the federation execution
76     bool join(const std::string& federateType, const std::string& federation);
77     bool join();
78     bool resign();
79     
80     /// Try to create and join the federation execution.
81     bool createJoinFederationExecution();
82     bool resignDestroyFederationExecution();
83
84
85     /// Time management
86
87     bool enableTimeConstrained();
88     bool disableTimeConstrained();
89
90     bool enableTimeRegulation(const SGTimeStamp& lookahead);
91     bool disableTimeRegulation();
92     bool modifyLookahead(const SGTimeStamp& lookahead);
93
94     /// Advance the logical time by the given time increment.
95     /// Depending on the time constrained mode, this might
96     /// block until the time advance is granted.
97     bool timeAdvanceBy(const SGTimeStamp& timeIncrement);
98     /// Advance the logical time to the given time.
99     /// Depending on the time constrained mode, this might
100     /// block until the time advance is granted.
101     bool timeAdvance(const SGTimeStamp& timeStamp);
102     /// Advance the logical time as far as time advances are available.
103     /// This call should not block and advance the logical time
104     /// as far as currently possible.
105     bool timeAdvanceAvailable();
106
107     bool queryFederateTime(SGTimeStamp& timeStamp);
108     bool queryLookahead(SGTimeStamp& timeStamp);
109
110     /// Process one messsage
111     bool processMessage();
112     /// Process one message but do not wait longer than the relative timeout.
113     bool processMessage(const SGTimeStamp& timeout);
114     /// Process messages until the federate can proceed with the
115     /// next simulation step. That is flush all pending messages and
116     /// depending on the time constrained mode process messages until
117     /// a pending time advance is granted.
118     bool processMessages();
119
120     /// Legacy tick call
121     bool tick(const double& minimum, const double& maximum);
122
123     class ObjectModelFactory {
124     public:
125         virtual ~ObjectModelFactory()
126         { }
127
128         virtual HLAObjectClass* createObjectClass(const std::string& name, HLAFederate& federate)
129         { return new HLAObjectClass(name, federate); }
130         virtual bool subscribeObjectClass(const std::string& objectClassName, const std::string& sharing)
131         { return sharing.find("Subscribe") != std::string::npos; }
132         virtual bool publishObjectClass(const std::string& objectClassName, const std::string& sharing)
133         { return sharing.find("Publish") != std::string::npos; }
134         virtual bool subscribeAttribute(const std::string& objectClassName, const std::string& attributeName, const std::string& sharing)
135         { return sharing.find("Subscribe") != std::string::npos; }
136         virtual bool publishAttribute(const std::string& objectClassName, const std::string& attributeName, const std::string& sharing)
137         { return sharing.find("Publish") != std::string::npos; }
138
139     };
140
141     /// Read an omt xml file
142     bool readObjectModelTemplate(const std::string& objectModel,
143                                  ObjectModelFactory& objectModelFactory);
144
145     HLAObjectClass* getObjectClass(const std::string& name);
146     const HLAObjectClass* getObjectClass(const std::string& name) const;
147
148     HLAInteractionClass* getInteractionClass(const std::string& name);
149     const HLAInteractionClass* getInteractionClass(const std::string& name) const;
150
151 private:
152     HLAFederate(const HLAFederate&);
153     HLAFederate& operator=(const HLAFederate&);
154
155     /// The underlying interface to the rti implementation
156     SGSharedPtr<RTIFederate> _rtiFederate;
157
158     /// Parameters required to connect to an rti
159     Version _version;
160     std::list<std::string> _connectArguments;
161
162     /// Parameters for the federation execution
163     std::string _federationExecutionName;
164     std::string _federationObjectModel;
165
166     /// Parameters for the federate
167     std::string _federateType;
168     std::string _federateName;
169
170     typedef std::map<std::string, SGSharedPtr<HLAObjectClass> > ObjectClassMap;
171     ObjectClassMap _objectClassMap;
172
173     typedef std::map<std::string, SGSharedPtr<HLAInteractionClass> > InteractionClassMap;
174     InteractionClassMap _interactionClassMap;
175
176     friend class HLAInteractionClass;
177     friend class HLAObjectClass;
178 };
179
180 } // namespace simgear
181
182 #endif