JSBSim Flight Dynamics Model 1.3.3 (16 Oct 2026)
An Open Source Flight Dynamics and Control Software Library in C++
Loading...
Searching...
No Matches
FGInputSocket Class Reference

Detailed Description

Implements the input from a socket.

This class inputs data from a telnet session. This is a leaf class.

Definition at line 62 of file FGInputSocket.h.

#include <FGInputSocket.h>

+ Inheritance diagram for FGInputSocket:
+ Collaboration diagram for FGInputSocket:

Public Member Functions

 FGInputSocket (FGFDMExec *fdmex, bool isEnabled)
 Constructor.
 
void Disable () override
 Disables the input generation.
 
void Enable () override
 Methods to enable, disable and toggle the input.
 
bool InitModel (void) override
 Initializes the instance.
 
bool Load (Element *el) override
 Init the input directives from an XML file.
 
void Read (bool Holding) override
 Generates the input.
 
bool Toggle () override
 Toggles the input generation.
 
- Public Member Functions inherited from FGInputType
 FGInputType (FGFDMExec *fdmex, bool isEnabled=true)
 Constructor (implement the FGModel interface).
 
 ~FGInputType () override
 Destructor.
 
virtual const std::string & GetInputName (void) const
 Get the name identifier to which the input will be directed.
 
bool InitModel (void) override
 Init the input model according to its configitation.
 
bool Load (Element *el) override
 Init the input directives from an XML file (implement the FGModel interface).
 
bool Run (bool Holding) override
 Executes the input directives (implement the FGModel interface).
 
void SetIdx (unsigned int idx)
 Set the idx for this input instance.
 
virtual void SetInputName (const std::string &name)
 Overwrites the name identifier under which the input will be read.
 
- Public Member Functions inherited from FGModel
 FGModel (FGFDMExec *, const std::string &name)
 Constructor.
 
 ~FGModel () override
 Destructor.
 
virtual SGPath FindFullPathName (const SGPath &path) const
 
FGFDMExec * GetExec (void) const
 
const std::string & GetName (void) const
 
unsigned int GetRate (void) const
 Get the output rate for the model in frames.
 
bool InitModel (void) override
 
void SetPropertyManager (std::shared_ptr< FGPropertyManager > fgpm)
 
void SetRate (unsigned int tt)
 Set the ouput rate for the model in frames.
 
- Public Member Functions inherited from FGModelFunctions
std::string GetFunctionStrings (const std::string &delimeter) const
 Gets the strings for the current set of functions.
 
std::string GetFunctionValues (const std::string &delimeter) const
 Gets the function values.
 
std::shared_ptr< FGFunction > GetPreFunction (const std::string &name)
 Get one of the "pre" function.
 
bool Load (Element *el, FGFDMExec *fdmex, std::string prefix="")
 
void PostLoad (Element *el, FGFDMExec *fdmex, std::string prefix="")
 
void PreLoad (Element *el, FGFDMExec *fdmex, std::string prefix="")
 
void RunPostFunctions (void)
 
void RunPreFunctions (void)
 
- Public Member Functions inherited from FGJSBBase
 FGJSBBase ()
 Constructor for FGJSBBase.
 
virtual ~FGJSBBase ()
 Destructor for FGJSBBase.
 

Protected Member Functions

bool CreateSocket ()
 
- Protected Member Functions inherited from FGInputType
void Debug (int from) override
 
- Protected Member Functions inherited from FGModel
bool Upload (Element *el, bool preLoad)
 Uploads this model in memory.
 

Protected Attributes

bool BlockingInput
 
std::string data
 
std::unique_ptr< FGfdmSocket > socket
 
unsigned int SockPort
 
FGfdmSocket::ProtocolType SockProtocol
 
- Protected Attributes inherited from FGInputType
unsigned int InputIdx
 
- Protected Attributes inherited from FGModel
bool enabled = true
 
unsigned int exe_ctr
 
FGFDMExec * FDMExec
 
std::string Name
 
std::shared_ptr< FGPropertyManager > PropertyManager
 
unsigned int rate
 
- Protected Attributes inherited from FGModelFunctions
FGPropertyReader LocalProperties
 
std::vector< std::shared_ptr< FGFunction > > PostFunctions
 
std::vector< std::shared_ptr< FGFunction > > PreFunctions
 

Additional Inherited Members

- Public Types inherited from FGJSBBase
enum  { eL = 1 , eM , eN }
 Moments L, M, N. More...
 
enum  { eP = 1 , eQ , eR }
 Rates P, Q, R. More...
 
enum  { eU = 1 , eV , eW }
 Velocities U, V, W. More...
 
enum  { eX = 1 , eY , eZ }
 Positions X, Y, Z. More...
 
enum  { ePhi = 1 , eTht , ePsi }
 Euler angles Phi, Theta, Psi. More...
 
enum  { eDrag = 1 , eSide , eLift }
 Stability axis forces, Drag, Side force, Lift. More...
 
enum  { eRoll = 1 , ePitch , eYaw }
 Local frame orientation Roll, Pitch, Yaw. More...
 
enum  { eNorth = 1 , eEast , eDown }
 Local frame position North, East, Down. More...
 
enum  { eLat = 1 , eLong , eRad }
 Locations Radius, Latitude, Longitude. More...
 
enum  {
  inNone = 0 , inDegrees , inRadians , inMeters ,
  inFeet
}
 Conversion specifiers. More...
 
- Static Public Member Functions inherited from FGJSBBase
static constexpr double CelsiusToFahrenheit (double celsius)
 Converts from degrees Celsius to degrees Fahrenheit.
 
static constexpr double CelsiusToKelvin (double celsius)
 Converts from degrees Celsius to degrees Kelvin.
 
static constexpr double CelsiusToRankine (double celsius)
 Converts from degrees Celsius to degrees Rankine.
 
static constexpr double Constrain (double min, double value, double max)
 Constrain a value between a minimum and a maximum value.
 
static bool EqualToRoundoff (double a, double b)
 Finite precision comparison.
 
static bool EqualToRoundoff (double a, float b)
 Finite precision comparison.
 
static bool EqualToRoundoff (float a, double b)
 Finite precision comparison.
 
static bool EqualToRoundoff (float a, float b)
 Finite precision comparison.
 
static constexpr double FahrenheitToCelsius (double fahrenheit)
 Converts from degrees Fahrenheit to degrees Celsius.
 
static constexpr double FeetToMeters (double measure)
 Converts from feet to meters.
 
static const std::string & GetVersion (void)
 Returns the version number of JSBSim.
 
static constexpr double KelvinToCelsius (double kelvin)
 Converts from degrees Kelvin to degrees Celsius.
 
static constexpr double KelvinToFahrenheit (double kelvin)
 Converts from degrees Kelvin to degrees Fahrenheit.
 
static constexpr double KelvinToRankine (double kelvin)
 Converts from degrees Kelvin to degrees Rankine.
 
static constexpr double RankineToCelsius (double rankine)
 Converts from degrees Rankine to degrees Celsius.
 
static constexpr double RankineToKelvin (double rankine)
 Converts from degrees Rankine to degrees Kelvin.
 
static constexpr double sign (double num)
 
- Static Public Attributes inherited from FGJSBBase
static short debug_lvl = 1
 
- Static Protected Member Functions inherited from FGJSBBase
static std::string CreateIndexedPropertyName (const std::string &Property, int index)
 
- Static Protected Attributes inherited from FGJSBBase
static constexpr double degtorad = M_PI / 180.
 
static constexpr double fpstokts = 1.0 / ktstofps
 
static constexpr double fttom = 0.3048
 
static constexpr double hptoftlbssec = 550.0
 
static constexpr double in3tom3 = inchtoft*inchtoft*inchtoft/m3toft3
 
static constexpr double inchtoft = 1.0/12.0
 
static constexpr double inhgtopa = 3386.38
 
static const std::string JSBSim_version = JSBSIM_VERSION " " __DATE__ " " __TIME__
 
static constexpr double kgtolb = 2.20462
 
static constexpr double kgtoslug = 0.06852168
 
static constexpr double ktstofps = 1852./(3600*fttom)
 
static constexpr double lbtoslug = 1.0/slugtolb
 
static constexpr double m3toft3 = 1.0/(fttom*fttom*fttom)
 
static const std::string needed_cfg_version = "2.0"
 
static constexpr double psftoinhg = 0.014138
 
static constexpr double psftopa = 47.88
 
static constexpr double radtodeg = 180. / M_PI
 
static constexpr double slugtolb = 32.174049
 Note that definition of lbtoslug by the inverse of slugtolb and not to a different constant you can also get from some tables will make lbtoslug*slugtolb == 1 up to the magnitude of roundoff.
 

Constructor & Destructor Documentation

◆ FGInputSocket()

FGInputSocket ( FGFDMExec *  fdmex,
bool  isEnabled 
)

Constructor.

Definition at line 56 of file FGInputSocket.cpp.

56 :
57 FGInputType(fdmex, isEnabled), SockProtocol(FGfdmSocket::ptTCP),
58 BlockingInput(false)
59{
60}
FGInputType(FGFDMExec *fdmex, bool isEnabled=true)
Constructor (implement the FGModel interface).

Member Function Documentation

◆ CreateSocket()

bool CreateSocket ( )
protected

Definition at line 99 of file FGInputSocket.cpp.

100{
101 socket = std::make_unique<FGfdmSocket>(SockPort, SockProtocol);
102
103 if (!socket) return false;
104 if (!socket->GetConnectStatus()) return false;
105
106 return true;
107}

◆ Disable()

void Disable ( void  )
overridevirtual

Disables the input generation.

Reimplemented from FGInputType.

Definition at line 120 of file FGInputSocket.cpp.

121{
123 socket.reset();
124}
virtual void Disable(void)
Disables the input generation.
+ Here is the call graph for this function:

◆ Enable()

void Enable ( void  )
overridevirtual

Methods to enable, disable and toggle the input.

Reimplemented from FGInputType.

Definition at line 111 of file FGInputSocket.cpp.

112{
113 if (enabled) return; // already enabled
115 CreateSocket();
116}
virtual void Enable(void)
Enables the input generation.
+ Here is the call graph for this function:

◆ InitModel()

bool InitModel ( void  )
overridevirtual

Initializes the instance.

This method basically opens the socket to which inputs will be directed.

Returns
true if the execution succeeded.

Reimplemented from FGModelFunctions.

Definition at line 86 of file FGInputSocket.cpp.

87{
89 return false;
90
91 if (enabled)
92 return CreateSocket();
93
94 return true;
95}
bool InitModel(void) override
Init the input model according to its configitation.
+ Here is the call graph for this function:

◆ Load()

bool Load ( Element *  el)
overridevirtual

Init the input directives from an XML file.

Parameters
elementXML Element that is pointing to the input directives

Reimplemented from FGModel.

Reimplemented in FGUDPInputSocket.

Definition at line 64 of file FGInputSocket.cpp.

65{
66 if (!FGInputType::Load(el))
67 return false;
68
69 SockPort = atoi(el->GetAttributeValue("port").c_str());
70
71 if (SockPort == 0) {
72 FGXMLLogging log(el, LogLevel::ERROR);
73 log << "No port assigned in input element\n";
74 return false;
75 }
76
77 string action = el->GetAttributeValue("action");
78 if (to_upper(action) == "BLOCKING_INPUT")
79 BlockingInput = true;
80
81 return true;
82}
bool Load(Element *el) override
Init the input directives from an XML file (implement the FGModel interface).
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Read()

void Read ( bool  Holding)
overridevirtual

Generates the input.

Implements FGInputType.

Reimplemented in FGUDPInputSocket.

Definition at line 141 of file FGInputSocket.cpp.

142{
143 if (!socket) return;
144 if (!socket->GetConnectStatus()) return;
145
146 if (BlockingInput)
147 socket->WaitUntilReadable(); // block until a transmission is received
148
149 string raw_data = socket->Receive(); // read data
150
151 if (!raw_data.empty()) {
152 size_t start = 0;
153
154 data += raw_data;
155
156 // parse lines
157 while (1) {
158 size_t string_start = data.find_first_not_of("\r\n", start);
159 if (string_start == string::npos) break;
160 size_t string_end = data.find_first_of("\r\n", string_start);
161 if (string_end == string::npos) break;
162 string line = data.substr(string_start, string_end-string_start);
163 if (line.empty()) break;
164
165 // now parse individual line
166 vector <string> tokens = split(line,' ');
167
168 string command, argument, str_value;
169 if (!tokens.empty()) {
170 command = to_lower(tokens[0]);
171 if (tokens.size() > 1) {
172 argument = trim(tokens[1]);
173 if (tokens.size() > 2) {
174 str_value = trim(tokens[2]);
175 }
176 }
177 }
178
179 if (command == "set") { // SET PROPERTY
180 SGPropertyNode* node = nullptr;
181
182 if (argument.empty()) {
183 socket->Reply("No property argument supplied.\r\n");
184 break;
185 }
186 try {
187 node = PropertyManager->GetNode(argument);
188 } catch(...) {
189 socket->Reply("Badly formed property query\r\n");
190 break;
191 }
192
193 if (!node) {
194 socket->Reply("Unknown property\r\n");
195 break;
196 } else if (!node->hasValue()) {
197 socket->Reply("Not a leaf property\r\n");
198 break;
199 } else {
200 try {
201 double value = atof_locale_c(str_value);
202 node->setDoubleValue(value);
203 } catch(InvalidNumber& e) {
204 string msg(e.what());
205 msg += "\r\n";
206 socket->Reply(msg);
207 break;
208 }
209 }
210 socket->Reply("set successful\r\n");
211
212 } else if (command == "get") { // GET PROPERTY
213 SGPropertyNode* node = nullptr;
214
215 if (argument.empty()) {
216 socket->Reply("No property argument supplied.\r\n");
217 break;
218 }
219 try {
220 node = PropertyManager->GetNode(argument);
221 } catch(...) {
222 socket->Reply("Badly formed property query\r\n");
223 break;
224 }
225
226 if (!node) {
227 socket->Reply("Unknown property\r\n");
228 break;
229 } else if (!node->hasValue()) {
230 // Not a leaf: return the matching entries from the (static) property
231 // catalog. This is a read-only lookup, so it no longer requires the
232 // client to place the sim in HOLD first.
233 socket->Reply(FDMExec->QueryPropertyCatalog(argument, "\r\n"));
234 } else {
235 ostringstream buf;
236 buf << argument << " = " << setw(12) << setprecision(6) << node->getDoubleValue() << '\r' << endl;
237 socket->Reply(buf.str());
238 }
239
240 } else if (command == "hold") { // PAUSE
241
242 FDMExec->Hold();
243 socket->Reply("Holding\r\n");
244
245 } else if (command == "resume") { // RESUME
246
247 FDMExec->Resume();
248 socket->Reply("Resuming\r\n");
249
250 } else if (command == "iterate") { // ITERATE
251
252 int argumentInt;
253 istringstream (argument) >> argumentInt;
254 if (argument.empty()) {
255 socket->Reply("No argument supplied for number of iterations.\r\n");
256 break;
257 }
258 if ( !(argumentInt > 0) ){
259 socket->Reply("Required argument must be a positive Integer.\r\n");
260 break;
261 }
262 FDMExec->EnableIncrementThenHold( argumentInt );
263 FDMExec->Resume();
264 socket->Reply("Iterations performed\r\n");
265
266 } else if (command == "quit") { // QUIT
267
268 // close the socket connection
269 socket->Send("Closing connection\r\n");
270 socket->Close();
271
272 } else if (command == "info") { // INFO
273
274 // get info about the sim run and/or aircraft, etc.
275 ostringstream info;
276 info << "JSBSim version: " << JSBSim_version << "\r\n";
277 info << "Config File version: " << needed_cfg_version << "\r\n";
278 info << "Aircraft simulated: " << FDMExec->GetAircraft()->GetAircraftName() << "\r\n";
279 info << "Simulation time: " << setw(8) << setprecision(3) << FDMExec->GetSimTime() << '\r' << endl;
280 socket->Reply(info.str());
281
282 } else if (command == "help") { // HELP
283
284 socket->Reply(
285 " JSBSim Server commands:\r\n\r\n"
286 " get {property name}\r\n"
287 " set {property name} {value}\r\n"
288 " hold\r\n"
289 " resume\r\n"
290 " iterate {value}\r\n"
291 " help\r\n"
292 " quit\r\n"
293 " info\r\n\r\n");
294
295 } else {
296 socket->Reply(string("Unknown command: ") + command + "\r\n");
297 }
298
299 start = string_end;
300 }
301
302 // Remove processed commands.
303 size_t last_crlf = data.find_last_of("\r\n");
304 if (last_crlf != string::npos) {
305 if (last_crlf < data.length()-1)
306 data = data.substr(last_crlf+1);
307 else
308 data.clear();
309 }
310 }
311
312}
std::shared_ptr< FGAircraft > GetAircraft(void) const
Returns the FGAircraft pointer.
std::string QueryPropertyCatalog(const std::string &check, const std::string &end_of_line="\n")
Retrieves property or properties matching the supplied string.
void EnableIncrementThenHold(int Timesteps)
Turn on hold after increment.
Definition FGFDMExec.h:495
void Resume(void)
Resumes execution from a "Hold".
Definition FGFDMExec.h:499
double GetSimTime(void) const
Returns the cumulative simulation time in seconds.
Definition FGFDMExec.h:554
void Hold(void)
Pauses execution by preventing time from incrementing.
Definition FGFDMExec.h:493
A node in a property tree.
Definition props.hxx:747
double getDoubleValue() const
Get a double value for this node.
bool setDoubleValue(double value)
Set a double value for this node.
bool hasValue() const
Test whether this node contains a primitive leaf value.
Definition props.hxx:810
+ Here is the call graph for this function:

◆ Toggle()

bool Toggle ( void  )
overridevirtual

Toggles the input generation.

Returns
the input generation status i.e. true if the input has been enabled, false if the input has been disabled.

Reimplemented from FGInputType.

Definition at line 128 of file FGInputSocket.cpp.

129{
130 bool enabled_status = FGInputType::Toggle();
131 if (enabled_status)
132 CreateSocket();
133 else
134 socket.reset();
135
136 return enabled_status;
137}
virtual bool Toggle(void)
Toggles the input generation.
+ Here is the call graph for this function:

Member Data Documentation

◆ BlockingInput

bool BlockingInput
protected

Definition at line 95 of file FGInputSocket.h.

◆ data

std::string data
protected

Definition at line 94 of file FGInputSocket.h.

◆ socket

std::unique_ptr<FGfdmSocket> socket
protected

Definition at line 92 of file FGInputSocket.h.

◆ SockPort

unsigned int SockPort
protected

Definition at line 91 of file FGInputSocket.h.

◆ SockProtocol

FGfdmSocket::ProtocolType SockProtocol
protected

Definition at line 93 of file FGInputSocket.h.


The documentation for this class was generated from the following files: