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 Lesser 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 Lesser General Public License for more
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.
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.
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 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 bool FGInput::InitModel(void)
80 if (!FGModel::InitModel()) return false;
85 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 // This function handles accepting input commands from the socket interface.
90 bool FGInput::Run(void)
92 string line, token, info_string;
93 int start=0, string_start=0, string_end=0;
94 int token_start=0, token_end=0;
97 FGPropertyManager* node=0;
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"
104 data = socket->Receive(); // get socket transmission if present
106 if (data.size() > 0) {
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;
116 // now parse individual line
117 vector <string> tokens = split(line,' ');
119 string command="", argument="", str_value="";
120 if (tokens.size() > 0) {
121 command = to_lower(tokens[0]);
122 if (tokens.size() > 1) {
123 argument = trim(tokens[1]);
124 if (tokens.size() > 2) {
125 str_value = trim(tokens[2]);
130 if (command == "set") { // SET PROPERTY
132 node = PropertyManager->GetNode(argument);
134 socket->Reply("Unknown property\n");
136 value = atof(str_value.c_str());
137 node->setDoubleValue(value);
141 } else if (command == "get") { // GET PROPERTY
143 if (argument.size() == 0) {
144 socket->Reply("No property argument supplied.\n");
148 node = PropertyManager->GetNode(argument);
150 socket->Reply("Badly formed property query\n");
154 if (FDMExec->Holding()) { // if holding can query property list
155 string query = FDMExec->QueryPropertyCatalog(argument);
156 socket->Reply(query);
158 socket->Reply("Must be in HOLD to search properties\n");
160 } else if (node > 0) {
161 sprintf(buf, "%s = %12.6f\n", argument.c_str(), node->getDoubleValue());
165 } else if (command == "hold") { // PAUSE
170 } else if (command == "resume") { // RESUME
175 } else if (command == "quit") { // QUIT
177 // close the socket connection
181 } else if (command == "info") { // INFO
183 // get info about the sim run and/or aircraft, etc.
184 sprintf(buf, "%8.3f\0", State->Getsim_time());
185 info_string = "JSBSim version: " + JSBSim_version + "\n";
186 info_string += "Config File version: " + needed_cfg_version + "\n";
187 info_string += "Aircraft simulated: " + Aircraft->GetAircraftName() + "\n";
188 info_string += "Simulation time: " + string(buf) + "\n";
189 socket->Reply(info_string);
191 } else if (command == "help") { // HELP
194 " JSBSim Server commands:\n\n"
195 " get {property name}\n"
196 " set {property name} {value}\n"
204 socket->Reply(string("Unknown command: ") + token + string("\n"));
214 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 bool FGInput::Load(Element* element)
218 string type="", parameter="";
219 string name="", fname="";
222 // if the input has already been set up, print a warning message and return
224 cerr << "An input port has already been assigned for this run" << endl;
228 port = int(element->GetAttributeValueAsNumber("port"));
230 cerr << endl << "No port assigned in input element" << endl;
232 socket = new FGfdmSocket(port);
240 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 // The bitmasked value choices are as follows:
242 // unset: In this case (the default) JSBSim would only print
243 // out the normally expected messages, essentially echoing
244 // the config files as they are read. If the environment
245 // variable is not set, debug_lvl is set to 1 internally
246 // 0: This requests JSBSim not to output any messages
248 // 1: This value explicity requests the normal JSBSim
250 // 2: This value asks for a message to be printed out when
251 // a class is instantiated
252 // 4: When this value is set, a message is displayed when a
253 // FGModel object executes its Run() method
254 // 8: When this value is set, various runtime state variables
255 // are printed out periodically
256 // 16: When set various parameters are sanity checked and
257 // a message is printed out when they go out of bounds
259 void FGInput::Debug(int from)
263 if (debug_lvl <= 0) return;
265 if (debug_lvl & 1) { // Standard console startup message output
266 if (from == 0) { // Constructor
271 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
272 if (from == 0) cout << "Instantiated: FGInput" << endl;
273 if (from == 1) cout << "Destroyed: FGInput" << endl;
275 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
277 if (debug_lvl & 8 ) { // Runtime state variables
279 if (debug_lvl & 16) { // Sanity checking
281 if (debug_lvl & 64) {
282 if (from == 0) { // Constructor
283 cout << IdSrc << endl;
284 cout << IdHdr << endl;