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