JSBSim Flight Dynamics Model 1.3.0 (09 Apr 2026)
An Open Source Flight Dynamics and Control Software Library in C++
Loading...
Searching...
No Matches
FGInputSocket.cpp
1/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Module: FGInputSocket.cpp
4 Author: Paul Chavent
5 Date started: 01/20/15
6 Purpose: Manage input of sim parameters to a socket
7 Called by: FGInput
8
9 ------------- Copyright (C) 2015 Paul Chavent -------------
10
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU Lesser General Public License as published by the Free Software
13 Foundation; either version 2 of the License, or (at your option) any later
14 version.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
19 details.
20
21 You should have received a copy of the GNU Lesser General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23 Place - Suite 330, Boston, MA 02111-1307, USA.
24
25 Further information about the GNU Lesser General Public License can also be found on
26 the world wide web at http://www.gnu.org.
27
28FUNCTIONAL DESCRIPTION
29--------------------------------------------------------------------------------
30This is the place where you create input routines to dump data for perusal
31later.
32
33HISTORY
34--------------------------------------------------------------------------------
3501/20/15 PC Created
36
37%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38INCLUDES
39%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40
41#include "FGInputSocket.h"
42#include "FGFDMExec.h"
43#include "models/FGAircraft.h"
44#include "FGXMLElement.h"
45#include "string_utilities.h"
46#include "FGLog.h"
47
48using namespace std;
49
50namespace JSBSim {
51
52/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53CLASS IMPLEMENTATION
54%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
55
57 FGInputType(fdmex), socket(0), SockProtocol(FGfdmSocket::ptTCP),
58 BlockingInput(false)
59{
60}
61
62//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63
65{
66 delete socket;
67}
68
69//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70
72{
73 if (!FGInputType::Load(el))
74 return false;
75
76 SockPort = atoi(el->GetAttributeValue("port").c_str());
77
78 if (SockPort == 0) {
79 FGXMLLogging log(el, LogLevel::ERROR);
80 log << "No port assigned in input element\n";
81 return false;
82 }
83
84 string action = el->GetAttributeValue("action");
85 if (to_upper(action) == "BLOCKING_INPUT")
86 BlockingInput = true;
87
88 return true;
89}
90
91//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92
94{
96 delete socket;
97 socket = new FGfdmSocket(SockPort, SockProtocol);
98
99 if (socket == 0) return false;
100 if (!socket->GetConnectStatus()) return false;
101
102 return true;
103 }
104
105 return false;
106}
107
108//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109
110void FGInputSocket::Read(bool Holding)
111{
112 if (!socket) return;
113 if (!socket->GetConnectStatus()) return;
114
115 if (BlockingInput)
116 socket->WaitUntilReadable(); // block until a transmission is received
117
118 string raw_data = socket->Receive(); // read data
119
120 if (!raw_data.empty()) {
121 size_t start = 0;
122
123 data += raw_data;
124
125 // parse lines
126 while (1) {
127 size_t string_start = data.find_first_not_of("\r\n", start);
128 if (string_start == string::npos) break;
129 size_t string_end = data.find_first_of("\r\n", string_start);
130 if (string_end == string::npos) break;
131 string line = data.substr(string_start, string_end-string_start);
132 if (line.empty()) break;
133
134 // now parse individual line
135 vector <string> tokens = split(line,' ');
136
137 string command, argument, str_value;
138 if (!tokens.empty()) {
139 command = to_lower(tokens[0]);
140 if (tokens.size() > 1) {
141 argument = trim(tokens[1]);
142 if (tokens.size() > 2) {
143 str_value = trim(tokens[2]);
144 }
145 }
146 }
147
148 if (command == "set") { // SET PROPERTY
149 SGPropertyNode* node = nullptr;
150
151 if (argument.empty()) {
152 socket->Reply("No property argument supplied.\r\n");
153 break;
154 }
155 try {
156 node = PropertyManager->GetNode(argument);
157 } catch(...) {
158 socket->Reply("Badly formed property query\r\n");
159 break;
160 }
161
162 if (!node) {
163 socket->Reply("Unknown property\r\n");
164 break;
165 } else if (!node->hasValue()) {
166 socket->Reply("Not a leaf property\r\n");
167 break;
168 } else {
169 try {
170 double value = atof_locale_c(str_value);
171 node->setDoubleValue(value);
172 } catch(InvalidNumber& e) {
173 string msg(e.what());
174 msg += "\r\n";
175 socket->Reply(msg);
176 break;
177 }
178 }
179 socket->Reply("set successful\r\n");
180
181 } else if (command == "get") { // GET PROPERTY
182 SGPropertyNode* node = nullptr;
183
184 if (argument.empty()) {
185 socket->Reply("No property argument supplied.\r\n");
186 break;
187 }
188 try {
189 node = PropertyManager->GetNode(argument);
190 } catch(...) {
191 socket->Reply("Badly formed property query\r\n");
192 break;
193 }
194
195 if (!node) {
196 socket->Reply("Unknown property\r\n");
197 break;
198 } else if (!node->hasValue()) {
199 if (Holding) { // if holding can query property list
200 string query = FDMExec->QueryPropertyCatalog(argument, "\r\n");
201 socket->Reply(query);
202 } else {
203 socket->Reply("Must be in HOLD to search properties\r\n");
204 }
205 } else {
206 ostringstream buf;
207 buf << argument << " = " << setw(12) << setprecision(6) << node->getDoubleValue() << '\r' << endl;
208 socket->Reply(buf.str());
209 }
210
211 } else if (command == "hold") { // PAUSE
212
213 FDMExec->Hold();
214 socket->Reply("Holding\r\n");
215
216 } else if (command == "resume") { // RESUME
217
218 FDMExec->Resume();
219 socket->Reply("Resuming\r\n");
220
221 } else if (command == "iterate") { // ITERATE
222
223 int argumentInt;
224 istringstream (argument) >> argumentInt;
225 if (argument.empty()) {
226 socket->Reply("No argument supplied for number of iterations.\r\n");
227 break;
228 }
229 if ( !(argumentInt > 0) ){
230 socket->Reply("Required argument must be a positive Integer.\r\n");
231 break;
232 }
233 FDMExec->EnableIncrementThenHold( argumentInt );
234 FDMExec->Resume();
235 socket->Reply("Iterations performed\r\n");
236
237 } else if (command == "quit") { // QUIT
238
239 // close the socket connection
240 socket->Send("Closing connection\r\n");
241 socket->Close();
242
243 } else if (command == "info") { // INFO
244
245 // get info about the sim run and/or aircraft, etc.
246 ostringstream info;
247 info << "JSBSim version: " << JSBSim_version << "\r\n";
248 info << "Config File version: " << needed_cfg_version << "\r\n";
249 info << "Aircraft simulated: " << FDMExec->GetAircraft()->GetAircraftName() << "\r\n";
250 info << "Simulation time: " << setw(8) << setprecision(3) << FDMExec->GetSimTime() << '\r' << endl;
251 socket->Reply(info.str());
252
253 } else if (command == "help") { // HELP
254
255 socket->Reply(
256 " JSBSim Server commands:\r\n\r\n"
257 " get {property name}\r\n"
258 " set {property name} {value}\r\n"
259 " hold\r\n"
260 " resume\r\n"
261 " iterate {value}\r\n"
262 " help\r\n"
263 " quit\r\n"
264 " info\r\n\r\n");
265
266 } else {
267 socket->Reply(string("Unknown command: ") + command + "\r\n");
268 }
269
270 start = string_end;
271 }
272
273 // Remove processed commands.
274 size_t last_crlf = data.find_last_of("\r\n");
275 if (last_crlf != string::npos) {
276 if (last_crlf < data.length()-1)
277 data = data.substr(last_crlf+1);
278 else
279 data.clear();
280 }
281 }
282
283}
284
285}
std::string GetAttributeValue(const std::string &key)
Retrieves an attribute.
Encapsulates the JSBSim simulation executive.
Definition FGFDMExec.h:185
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:491
void Resume(void)
Resumes execution from a "Hold".
Definition FGFDMExec.h:495
double GetSimTime(void) const
Returns the cumulative simulation time in seconds.
Definition FGFDMExec.h:550
void Hold(void)
Pauses execution by preventing time from incrementing.
Definition FGFDMExec.h:489
bool Load(Element *el) override
Init the input directives from an XML file.
void Read(bool Holding) override
Generates the input.
bool InitModel(void) override
Initializes the instance.
~FGInputSocket() override
Destructor.
FGInputSocket(FGFDMExec *fdmex)
Constructor.
Abstract class to provide functions generic to all the input directives.
Definition FGInputType.h:74
bool Load(Element *el) override
Init the input directives from an XML file (implement the FGModel interface).
bool InitModel(void) override
Init the input model according to its configitation.
The FGfdmSocket class enables JSBSim to communicate via sockets.
Definition FGfdmSocket.h:76
std::string Receive(void)
Receive data from the socket connection.
bool GetConnectStatus(void)
Return the connection status of the socket.
void Close(void)
Close the socket connection if the protocol is TCP.
void WaitUntilReadable(void)
Wait until the TCP socket is readable.
int Reply(const std::string &text)
Send a reply to the client ending by a prompt "JSBSim>".
void Send(void)
Send the internal buffer over the socket connection.
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
Main namespace for the JSBSim Flight Dynamics Model.
Definition FGFDMExec.cpp:71