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