]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGInput.cpp
sync with JSB JSBSim CVS
[flightgear.git] / src / FDM / JSBSim / models / FGInput.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGInput.cpp
4  Author:       Jon Berndt
5  Date started: 12/02/98
6  Purpose:      Manage output of sim parameters to file or stdout
7  Called by:    FGSimExec
8
9  ------------- Copyright (C) 1999  Jon S. Berndt (jon@jsbsim.org) -------------
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
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This is the place where you create output routines to dump data for perusal
31 later.
32
33 HISTORY
34 --------------------------------------------------------------------------------
35 12/02/98   JSB   Created
36
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 INCLUDES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40
41 #include "FGInput.h"
42 #include "FGAircraft.h"
43 #include "FGFDMExec.h"
44
45 #include "input_output/FGfdmSocket.h"
46 #include "input_output/FGXMLElement.h"
47
48 #include <sstream>
49 #include <iomanip>
50 #include <cstdlib>
51
52 using namespace std;
53
54 namespace JSBSim {
55
56 static const char *IdSrc = "$Id: FGInput.cpp,v 1.22 2012/01/21 16:46:09 jberndt Exp $";
57 static const char *IdHdr = ID_INPUT;
58
59 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 CLASS IMPLEMENTATION
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
62
63 FGInput::FGInput(FGFDMExec* fdmex) : FGModel(fdmex)
64 {
65   Name = "FGInput";
66   sFirstPass = dFirstPass = true;
67   socket = 0;
68   port = 0;
69   enabled = true;
70
71   Debug(0);
72 }
73
74 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75
76 FGInput::~FGInput()
77 {
78   delete socket;
79   Debug(1);
80 }
81
82 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83
84 bool FGInput::InitModel(void)
85 {
86   return true;
87 }
88
89 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 //
91 // This function handles accepting input commands from the socket interface.
92 //
93
94 bool FGInput::Run(bool Holding)
95 {
96   string line, token;
97   size_t start=0, string_start=0, string_end=0;
98   double value=0;
99   FGPropertyManager* node=0;
100
101   if (FGModel::Run(Holding)) return true; // fast exit if nothing to do
102   if (port == 0) return false;      // Do nothing here if port not defined
103                                     // return false if no error
104   // This model DOES execute if "Exec->Holding"
105
106   RunPreFunctions();
107
108   data = socket->Receive(); // get socket transmission if present
109
110   if (data.size() > 0) {
111     // parse lines
112     while (1) {
113       string_start = data.find_first_not_of("\r\n", start);
114       if (string_start == string::npos) break;
115       string_end = data.find_first_of("\r\n", string_start);
116       if (string_end == string::npos) break;
117       line = data.substr(string_start, string_end-string_start);
118       if (line.size() == 0) break;
119
120       // now parse individual line
121       vector <string> tokens = split(line,' ');
122   
123       string command="", argument="", str_value="";
124       if (tokens.size() > 0) {
125         command = to_lower(tokens[0]);
126         if (tokens.size() > 1) {
127           argument = trim(tokens[1]);
128           if (tokens.size() > 2) {
129             str_value = trim(tokens[2]);
130           }
131         }
132       }
133
134       if (command == "set") {                   // SET PROPERTY
135
136         node = PropertyManager->GetNode(argument);
137         if (node == 0)
138           socket->Reply("Unknown property\n");
139         else {
140           value = atof(str_value.c_str());
141           node->setDoubleValue(value);
142         }
143         socket->Reply("");
144
145       } else if (command == "get") {             // GET PROPERTY
146
147         if (argument.size() == 0) {
148           socket->Reply("No property argument supplied.\n");
149           break;
150         }
151         try {
152           node = PropertyManager->GetNode(argument);
153         } catch(...) {
154           socket->Reply("Badly formed property query\n");
155           break;
156         }
157         if (node == 0) {
158           if (Holding) { // if holding can query property list
159             string query = FDMExec->QueryPropertyCatalog(argument);
160             socket->Reply(query);
161           } else {
162             socket->Reply("Must be in HOLD to search properties\n");
163           }
164         } else if (node > 0) {
165           ostringstream buf;
166           buf << argument << " = " << setw(12) << setprecision(6) << node->getDoubleValue() << endl;
167           socket->Reply(buf.str());
168         }
169
170       } else if (command == "hold") {                  // PAUSE
171
172         FDMExec->Hold();
173         socket->Reply("");
174
175       } else if (command == "resume") {             // RESUME
176
177         FDMExec->Resume();
178         socket->Reply("");
179         
180       } else if (command == "iterate") {             // ITERATE
181
182         int argumentInt;
183         istringstream (argument) >> argumentInt;
184         if (argument.size() == 0) {
185           socket->Reply("No argument supplied for number of iterations.\n");
186           break;
187         }
188         if ( !(argumentInt > 0) ){
189           socket->Reply("Required argument must be a positive Integer.\n");
190           break;
191         }
192         FDMExec->EnableIncrementThenHold( argumentInt );
193         FDMExec->Resume();
194         socket->Reply("");
195
196       } else if (command == "quit") {                   // QUIT
197
198         // close the socket connection
199         socket->Reply("");
200         socket->Close();
201
202       } else if (command == "info") {                   // INFO
203
204         // get info about the sim run and/or aircraft, etc.
205         ostringstream info;
206         info << "JSBSim version: " << JSBSim_version << endl;
207         info << "Config File version: " << needed_cfg_version << endl;
208         info << "Aircraft simulated: " << FDMExec->GetAircraft()->GetAircraftName() << endl;
209         info << "Simulation time: " << setw(8) << setprecision(3) << FDMExec->GetSimTime() << endl;
210         socket->Reply(info.str());
211
212       } else if (command == "help") {                   // HELP
213
214         socket->Reply(
215         " JSBSim Server commands:\n\n"
216         "   get {property name}\n"
217         "   set {property name} {value}\n"
218         "   hold\n"
219         "   resume\n"
220         "   help\n"
221         "   quit\n"
222         "   info\n\n");
223
224       } else {
225         socket->Reply(string("Unknown command: ") +  token + string("\n"));
226       }
227
228       start = string_end;
229     }
230   }
231
232   RunPostFunctions();
233
234   return false;
235 }
236
237 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238
239 bool FGInput::Load(Element* element)
240 {
241   string type="", parameter="";
242   string name="", fname="";
243   string property;
244
245   // if the input has already been set up, print a warning message and return
246   if (port > 0) {
247     cerr << "An input port has already been assigned for this run" << endl;
248     return false;
249   }
250
251   port = int(element->GetAttributeValueAsNumber("port"));
252   if (port == 0) {
253     cerr << endl << "No port assigned in input element" << endl;
254   } else {
255     socket = new FGfdmSocket(port);
256   }
257
258   Debug(2);
259
260   return true;
261 }
262
263 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264 //    The bitmasked value choices are as follows:
265 //    unset: In this case (the default) JSBSim would only print
266 //       out the normally expected messages, essentially echoing
267 //       the config files as they are read. If the environment
268 //       variable is not set, debug_lvl is set to 1 internally
269 //    0: This requests JSBSim not to output any messages
270 //       whatsoever.
271 //    1: This value explicity requests the normal JSBSim
272 //       startup messages
273 //    2: This value asks for a message to be printed out when
274 //       a class is instantiated
275 //    4: When this value is set, a message is displayed when a
276 //       FGModel object executes its Run() method
277 //    8: When this value is set, various runtime state variables
278 //       are printed out periodically
279 //    16: When set various parameters are sanity checked and
280 //       a message is printed out when they go out of bounds
281
282 void FGInput::Debug(int from)
283 {
284   string scratch="";
285
286   if (debug_lvl <= 0) return;
287
288   if (debug_lvl & 1) { // Standard console startup message output
289     if (from == 0) { // Constructor
290     }
291     if (from == 2) {
292     }
293   }
294   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
295     if (from == 0) cout << "Instantiated: FGInput" << endl;
296     if (from == 1) cout << "Destroyed:    FGInput" << endl;
297   }
298   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
299   }
300   if (debug_lvl & 8 ) { // Runtime state variables
301   }
302   if (debug_lvl & 16) { // Sanity checking
303   }
304   if (debug_lvl & 64) {
305     if (from == 0) { // Constructor
306       cout << IdSrc << endl;
307       cout << IdHdr << endl;
308     }
309   }
310 }
311 }