Purpose: Manage output of sim parameters to file or stdout
Called by: FGSimExec
- ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
+ ------------- Copyright (C) 1999 Jon S. Berndt (jon@jsbsim.org) -------------
This program is free software; you can redistribute it and/or modify it under
- the terms of the GNU General Public License as published by the Free Software
+ the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
- You should have received a copy of the GNU General Public License along with
+ You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA.
- Further information about the GNU General Public License can also be found on
+ Further information about the GNU Lesser General Public License can also be found on
the world wide web at http://www.gnu.org.
FUNCTIONAL DESCRIPTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
#include "FGInput.h"
-#include "FGState.h"
+#include "FGAircraft.h"
#include "FGFDMExec.h"
-#include <fstream>
+#include "input_output/FGfdmSocket.h"
+#include "input_output/FGXMLElement.h"
+
+#include <sstream>
#include <iomanip>
+#include <cstdlib>
+
+using namespace std;
namespace JSBSim {
-static const char *IdSrc = "$Id$";
+static const char *IdSrc = "$Id: FGInput.cpp,v 1.20 2010/11/18 12:38:06 jberndt Exp $";
static const char *IdHdr = ID_INPUT;
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FGInput::~FGInput()
{
- if (socket) delete socket;
-
+ delete socket;
Debug(1);
}
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+bool FGInput::InitModel(void)
+{
+ if (!FGModel::InitModel()) return false;
+
+ return true;
+}
+
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//
// This function handles accepting input commands from the socket interface.
bool FGInput::Run(void)
{
- string line, token, info_string;
- int start=0, string_start=0, string_end=0;
- int token_start=0, token_end=0;
- char buf[100];
+ string line, token;
+ size_t start=0, string_start=0, string_end=0;
double value=0;
FGPropertyManager* node=0;
if (FGModel::Run()) return true; // fast exit if nothing to do
- if (port == 0) return true; // Do nothing here if port not defined
+ if (port == 0) return false; // Do nothing here if port not defined
+ // return false if no error
// This model DOES execute if "Exec->Holding"
+ RunPreFunctions();
+
data = socket->Receive(); // get socket transmission if present
if (data.size() > 0) {
if (line.size() == 0) break;
// now parse individual line
- token_start = line.find_first_not_of(" ", 0);
- token_end = line.find_first_of(" ", token_start);
- token = line.substr(token_start, token_end - token_start);
+ vector <string> tokens = split(line,' ');
+
+ string command="", argument="", str_value="";
+ if (tokens.size() > 0) {
+ command = to_lower(tokens[0]);
+ if (tokens.size() > 1) {
+ argument = trim(tokens[1]);
+ if (tokens.size() > 2) {
+ str_value = trim(tokens[2]);
+ }
+ }
+ }
- if (token == "set" || token == "SET" ) { // SET PROPERTY
+ if (command == "set") { // SET PROPERTY
- token_start = line.find_first_not_of(" ", token_end);
- token_end = line.find_first_of(" ", token_start);
- token = line.substr(token_start, token_end-token_start);
- node = PropertyManager->GetNode(token);
- if (node == 0) socket->Reply("Unknown property\n");
+ node = PropertyManager->GetNode(argument);
+ if (node == 0)
+ socket->Reply("Unknown property\n");
else {
- token_start = line.find_first_not_of(" ", token_end);
- token_end = line.find_first_of(" ", token_start);
- token = line.substr(token_start, token_end-token_start);
- value = atof(token.c_str());
+ value = atof(str_value.c_str());
node->setDoubleValue(value);
}
+ socket->Reply("");
- } else if (token == "get" || token == "GET") { // GET PROPERTY
+ } else if (command == "get") { // GET PROPERTY
- token_start = line.find_first_not_of(" ", token_end);
- if (token_start == string::npos) {
+ if (argument.size() == 0) {
socket->Reply("No property argument supplied.\n");
break;
- } else {
- token = line.substr(token_start, line.size()-token_start);
}
try {
- node = PropertyManager->GetNode(token);
+ node = PropertyManager->GetNode(argument);
} catch(...) {
socket->Reply("Badly formed property query\n");
break;
}
if (node == 0) {
if (FDMExec->Holding()) { // if holding can query property list
- string query = FDMExec->QueryPropertyCatalog(token);
+ string query = FDMExec->QueryPropertyCatalog(argument);
socket->Reply(query);
} else {
socket->Reply("Must be in HOLD to search properties\n");
}
} else if (node > 0) {
- sprintf(buf, "%s = %12.6f\n", token.c_str(), node->getDoubleValue());
- socket->Reply(buf);
+ ostringstream buf;
+ buf << argument << " = " << setw(12) << setprecision(6) << node->getDoubleValue() << endl;
+ socket->Reply(buf.str());
}
- } else if (token == "hold" || token == "HOLD") { // PAUSE
+ } else if (command == "hold") { // PAUSE
FDMExec->Hold();
+ socket->Reply("");
- } else if (token == "resume" || token == "RESUME") { // RESUME
+ } else if (command == "resume") { // RESUME
FDMExec->Resume();
+ socket->Reply("");
- } else if (token == "quit" || token == "QUIT") { // QUIT
+ } else if (command == "quit") { // QUIT
// close the socket connection
socket->Reply("");
socket->Close();
- } else if (token == "info" || token == "INFO") { // INFO
+ } else if (command == "info") { // INFO
// get info about the sim run and/or aircraft, etc.
- sprintf(buf, "%8.3f\0", State->Getsim_time());
- info_string = "JSBSim version: " + JSBSim_version + "\n";
- info_string += "Config File version: " + needed_cfg_version + "\n";
- info_string += "Aircraft simulated: " + Aircraft->GetAircraftName() + "\n";
- info_string += "Simulation time: " + string(buf) + "\n";
- socket->Reply(info_string);
+ ostringstream info;
+ info << "JSBSim version: " << JSBSim_version << endl;
+ info << "Config File version: " << needed_cfg_version << endl;
+ info << "Aircraft simulated: " << FDMExec->GetAircraft()->GetAircraftName() << endl;
+ info << "Simulation time: " << setw(8) << setprecision(3) << FDMExec->GetSimTime() << endl;
+ socket->Reply(info.str());
- } else if (token == "help" || token == "HELP") { // HELP
+ } else if (command == "help") { // HELP
socket->Reply(
" JSBSim Server commands:\n\n"
}
}
+ RunPostFunctions();
+
return false;
}
string name="", fname="";
string property;
- port = element->GetAttributeValueAsNumber("port");
+ // if the input has already been set up, print a warning message and return
+ if (port > 0) {
+ cerr << "An input port has already been assigned for this run" << endl;
+ return false;
+ }
+
+ port = int(element->GetAttributeValueAsNumber("port"));
if (port == 0) {
cerr << endl << "No port assigned in input element" << endl;
} else {