1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 Purpose: Manage output of sim parameters to file or stdout
9 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free Software
13 Foundation; either version 2 of the License, or (at your option) any later
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 General Public License for more
21 You should have received a copy of the GNU 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.
25 Further information about the GNU General Public License can also be found on
26 the world wide web at http://www.gnu.org.
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This is the place where you create output routines to dump data for perusal
34 --------------------------------------------------------------------------------
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
43 #include "FGFDMExec.h"
50 static const char *IdSrc = "$Id$";
51 static const char *IdHdr = ID_INPUT;
53 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57 FGInput::FGInput(FGFDMExec* fdmex) : FGModel(fdmex)
60 sFirstPass = dFirstPass = true;
68 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 if (socket) delete socket;
77 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 // This function handles accepting input commands from the socket interface.
82 bool FGInput::Run(void)
84 string line, token, info_string;
85 int start=0, string_start=0, string_end=0;
86 int token_start=0, token_end=0;
89 FGPropertyManager* node=0;
91 if (FGModel::Run()) return true; // fast exit if nothing to do
92 if (port == 0) return true; // Do nothing here if port not defined
93 // This model DOES execute if "Exec->Holding"
95 data = socket->Receive(); // get socket transmission if present
97 if (data.size() > 0) {
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;
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);
112 if (token == "set" || token == "SET" ) { // SET PROPERTY
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");
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);
127 } else if (token == "get" || token == "GET") { // GET PROPERTY
129 token_start = line.find_first_not_of(" ", token_end);
130 if (token_start == string::npos) {
131 socket->Reply("No property argument supplied.\n");
134 token = line.substr(token_start, line.size()-token_start);
137 node = PropertyManager->GetNode(token);
139 socket->Reply("Badly formed property query\n");
143 if (FDMExec->Holding()) { // if holding can query property list
144 string query = FDMExec->QueryPropertyCatalog(token);
145 socket->Reply(query);
147 socket->Reply("Must be in HOLD to search properties\n");
149 } else if (node > 0) {
150 sprintf(buf, "%s = %12.6f\n", token.c_str(), node->getDoubleValue());
154 } else if (token == "hold" || token == "HOLD") { // PAUSE
158 } else if (token == "resume" || token == "RESUME") { // RESUME
162 } else if (token == "quit" || token == "QUIT") { // QUIT
164 // close the socket connection
168 } else if (token == "info" || token == "INFO") { // INFO
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);
178 } else if (token == "help" || token == "HELP") { // HELP
181 " JSBSim Server commands:\n\n"
182 " get {property name}\n"
183 " set {property name} {value}\n"
191 socket->Reply(string("Unknown command: ") + token + string("\n"));
201 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203 bool FGInput::Load(Element* element)
205 string type="", parameter="";
206 string name="", fname="";
209 port = element->GetAttributeValueAsNumber("port");
211 cerr << endl << "No port assigned in input element" << endl;
213 socket = new FGfdmSocket(port);
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
229 // 1: This value explicity requests the normal JSBSim
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
240 void FGInput::Debug(int from)
244 if (debug_lvl <= 0) return;
246 if (debug_lvl & 1) { // Standard console startup message output
247 if (from == 0) { // Constructor
252 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
253 if (from == 0) cout << "Instantiated: FGInput" << endl;
254 if (from == 1) cout << "Destroyed: FGInput" << endl;
256 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
258 if (debug_lvl & 8 ) { // Runtime state variables
260 if (debug_lvl & 16) { // Sanity checking
262 if (debug_lvl & 64) {
263 if (from == 0) { // Constructor
264 cout << IdSrc << endl;
265 cout << IdHdr << endl;