]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/models/FGInput.cpp
Sync. w. 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 (jsb@hal-pc.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 "FGState.h"
43 #include "FGFDMExec.h"
44
45 #include <fstream>
46 #include <iomanip>
47
48 namespace JSBSim {
49
50 static const char *IdSrc = "$Id$";
51 static const char *IdHdr = ID_INPUT;
52
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 CLASS IMPLEMENTATION
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56
57 FGInput::FGInput(FGFDMExec* fdmex) : FGModel(fdmex)
58 {
59   Name = "FGInput";
60   sFirstPass = dFirstPass = true;
61   socket = 0;
62   port = 0;
63   enabled = true;
64
65   Debug(0);
66 }
67
68 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69
70 FGInput::~FGInput()
71 {
72   delete socket;
73   Debug(1);
74 }
75
76 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77
78 bool FGInput::InitModel(void)
79 {
80   if (!FGModel::InitModel()) return false;
81
82   return true;
83 }
84
85 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 //
87 // This function handles accepting input commands from the socket interface.
88 //
89
90 bool FGInput::Run(void)
91 {
92   string line, token, info_string;
93   int start=0, string_start=0, string_end=0;
94   int token_start=0, token_end=0;
95   char buf[100];
96   double value=0;
97   FGPropertyManager* node=0;
98
99   if (FGModel::Run()) return true; // fast exit if nothing to do
100   if (port == 0) return false;      // Do nothing here if port not defined
101                                     // return false if no error
102   // This model DOES execute if "Exec->Holding"
103
104   data = socket->Receive(); // get socket transmission if present
105
106   if (data.size() > 0) {
107     // parse lines
108     while (1) {
109       string_start = data.find_first_not_of("\r\n", start);
110       if (string_start == string::npos) break;
111       string_end = data.find_first_of("\r\n", string_start);
112       if (string_end == string::npos) break;
113       line = data.substr(string_start, string_end-string_start);
114       if (line.size() == 0) break;
115
116       // now parse individual line
117       token_start = line.find_first_not_of(" ", 0);
118       token_end = line.find_first_of(" ", token_start);
119       token = line.substr(token_start, token_end - token_start);
120
121       if (token == "set" || token == "SET" ) {                   // SET PROPERTY
122
123         token_start = line.find_first_not_of(" ", token_end);
124         token_end = line.find_first_of(" ", token_start);
125         token = line.substr(token_start, token_end-token_start);
126         node = PropertyManager->GetNode(token);
127         if (node == 0) socket->Reply("Unknown property\n");
128         else {
129           token_start = line.find_first_not_of(" ", token_end);
130           token_end = line.find_first_of(" ", token_start);
131           token = line.substr(token_start, token_end-token_start);
132           value = atof(token.c_str());
133           node->setDoubleValue(value);
134         }
135
136       } else if (token == "get" || token == "GET") {             // GET PROPERTY
137
138         token_start = line.find_first_not_of(" ", token_end);
139         if (token_start == string::npos) {
140           socket->Reply("No property argument supplied.\n");
141           break;
142         } else {
143           token = line.substr(token_start, line.size()-token_start);
144         }
145         try {
146           node = PropertyManager->GetNode(token);
147         } catch(...) {
148           socket->Reply("Badly formed property query\n");
149           break;
150         }
151         if (node == 0) {
152           if (FDMExec->Holding()) { // if holding can query property list
153             string query = FDMExec->QueryPropertyCatalog(token);
154             socket->Reply(query);
155           } else {
156             socket->Reply("Must be in HOLD to search properties\n");
157           }
158         } else if (node > 0) {
159           sprintf(buf, "%s = %12.6f\n", token.c_str(), node->getDoubleValue());
160           socket->Reply(buf);
161         }
162
163       } else if (token == "hold" || token == "HOLD") {                  // PAUSE
164
165         FDMExec->Hold();
166
167       } else if (token == "resume" || token == "RESUME") {             // RESUME
168
169         FDMExec->Resume();
170
171       } else if (token == "quit" || token == "QUIT") {                   // QUIT
172
173         // close the socket connection
174         socket->Reply("");
175         socket->Close();
176
177       } else if (token == "info" || token == "INFO") {                   // INFO
178
179         // get info about the sim run and/or aircraft, etc.
180         sprintf(buf, "%8.3f\0", State->Getsim_time());
181         info_string  = "JSBSim version: " + JSBSim_version + "\n";
182         info_string += "Config File version: " + needed_cfg_version + "\n";
183         info_string += "Aircraft simulated: " + Aircraft->GetAircraftName() + "\n";
184         info_string += "Simulation time: " + string(buf) + "\n";
185         socket->Reply(info_string);
186
187       } else if (token == "help" || token == "HELP") {                   // HELP
188
189         socket->Reply(
190         " JSBSim Server commands:\n\n"
191         "   get {property name}\n"
192         "   set {property name} {value}\n"
193         "   hold\n"
194         "   resume\n"
195         "   help\n"
196         "   quit\n"
197         "   info\n\n");
198
199       } else {
200         socket->Reply(string("Unknown command: ") +  token + string("\n"));
201       }
202
203       start = string_end;
204     }
205   }
206
207   return false;
208 }
209
210 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211
212 bool FGInput::Load(Element* element)
213 {
214   string type="", parameter="";
215   string name="", fname="";
216   string property;
217
218   // if the input has already been set up, print a warning message and return
219   if (port > 0) {
220     cerr << "An input port has already been assigned for this run" << endl;
221     return false;
222   }
223
224   port = int(element->GetAttributeValueAsNumber("port"));
225   if (port == 0) {
226     cerr << endl << "No port assigned in input element" << endl;
227   } else {
228     socket = new FGfdmSocket(port);
229   }
230
231   Debug(2);
232
233   return true;
234 }
235
236 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
237 //    The bitmasked value choices are as follows:
238 //    unset: In this case (the default) JSBSim would only print
239 //       out the normally expected messages, essentially echoing
240 //       the config files as they are read. If the environment
241 //       variable is not set, debug_lvl is set to 1 internally
242 //    0: This requests JSBSim not to output any messages
243 //       whatsoever.
244 //    1: This value explicity requests the normal JSBSim
245 //       startup messages
246 //    2: This value asks for a message to be printed out when
247 //       a class is instantiated
248 //    4: When this value is set, a message is displayed when a
249 //       FGModel object executes its Run() method
250 //    8: When this value is set, various runtime state variables
251 //       are printed out periodically
252 //    16: When set various parameters are sanity checked and
253 //       a message is printed out when they go out of bounds
254
255 void FGInput::Debug(int from)
256 {
257   string scratch="";
258
259   if (debug_lvl <= 0) return;
260
261   if (debug_lvl & 1) { // Standard console startup message output
262     if (from == 0) { // Constructor
263     }
264     if (from == 2) {
265     }
266   }
267   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
268     if (from == 0) cout << "Instantiated: FGInput" << endl;
269     if (from == 1) cout << "Destroyed:    FGInput" << endl;
270   }
271   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
272   }
273   if (debug_lvl & 8 ) { // Runtime state variables
274   }
275   if (debug_lvl & 16) { // Sanity checking
276   }
277   if (debug_lvl & 64) {
278     if (from == 0) { // Constructor
279       cout << IdSrc << endl;
280       cout << IdHdr << endl;
281     }
282   }
283 }
284 }