| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Module: FGInputSocket.cpp |
| 4 | Author: Paul Chavent |
| 5 | Date started: 01/20/15 |
| 6 | Purpose: Manage input of sim parameters to a socket |
| 7 | Called by: FGInput |
| 8 | |
| 9 | ------------- Copyright (C) 2015 Paul Chavent ------------- |
| 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 input routines to dump data for perusal |
| 31 | later. |
| 32 | |
| 33 | HISTORY |
| 34 | -------------------------------------------------------------------------------- |
| 35 | 01/20/15 PC Created |
| 36 | |
| 37 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 38 | INCLUDES |
| 39 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 40 | |
| 41 | #include <cstring> |
| 42 | #include <cstdlib> |
| 43 | #include <sstream> |
| 44 | #include <iomanip> |
| 45 | |
| 46 | #include "FGInputSocket.h" |
| 47 | #include "FGFDMExec.h" |
| 48 | #include "models/FGAircraft.h" |
| 49 | #include "input_output/FGXMLElement.h" |
| 50 | |
| 51 | using namespace std; |
| 52 | |
| 53 | namespace JSBSim { |
| 54 | |
| 55 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 56 | CLASS IMPLEMENTATION |
| 57 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 58 | |
| 59 | FGInputSocket::FGInputSocket(FGFDMExec* fdmex) : |
| 60 | FGInputType(fdmex), socket(0), SockProtocol(FGfdmSocket::ptTCP), |
| 61 | BlockingInput(false) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 66 | |
| 67 | FGInputSocket::~FGInputSocket() |
| 68 | { |
| 69 | delete socket; |
| 70 | } |
| 71 | |
| 72 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 73 | |
| 74 | bool FGInputSocket::Load(Element* el) |
| 75 | { |
| 76 | if (!FGInputType::Load(el)) |
| 77 | return false; |
| 78 | |
| 79 | SockPort = atoi(el->GetAttributeValue("port").c_str()); |
| 80 | |
| 81 | if (SockPort == 0) { |
| 82 | cerr << endl << "No port assigned in input element" << endl; |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | string action = el->GetAttributeValue("action"); |
| 87 | if (to_upper(action) == "BLOCKING_INPUT") |
| 88 | BlockingInput = true; |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 94 | |
| 95 | bool FGInputSocket::InitModel(void) |
| 96 | { |
| 97 | if (FGInputType::InitModel()) { |
| 98 | delete socket; |
| 99 | socket = new FGfdmSocket(SockPort, SockProtocol); |
| 100 | |
| 101 | if (socket == 0) return false; |
| 102 | if (!socket->GetConnectStatus()) return false; |
| 103 | |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 111 | |
| 112 | void FGInputSocket::Read(bool Holding) |
| 113 | { |
| 114 | if (!socket) return; |
| 115 | if (!socket->GetConnectStatus()) return; |
| 116 | |
| 117 | if (BlockingInput) |
| 118 | socket->WaitUntilReadable(); // block until a transmission is received |
| 119 | |
| 120 | string raw_data = socket->Receive(); // read data |
| 121 | |
| 122 | if (!raw_data.empty()) { |
| 123 | size_t start = 0; |
| 124 | |
| 125 | data += raw_data; |
| 126 | |
| 127 | // parse lines |
| 128 | while (1) { |
| 129 | size_t string_start = data.find_first_not_of("\r\n", start); |
| 130 | if (string_start == string::npos) break; |
| 131 | size_t string_end = data.find_first_of("\r\n", string_start); |
| 132 | if (string_end == string::npos) break; |
| 133 | string line = data.substr(string_start, string_end-string_start); |
| 134 | if (line.empty()) break; |
| 135 | |
| 136 | // now parse individual line |
| 137 | vector <string> tokens = split(line,' '); |
| 138 | |
| 139 | string command, argument, str_value; |
| 140 | if (!tokens.empty()) { |
| 141 | command = to_lower(tokens[0]); |
| 142 | if (tokens.size() > 1) { |
| 143 | argument = trim(tokens[1]); |
| 144 | if (tokens.size() > 2) { |
| 145 | str_value = trim(tokens[2]); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if (command == "set") { // SET PROPERTY |
| 151 | FGPropertyNode* node = nullptr; |
| 152 | |
| 153 | if (argument.empty()) { |
| 154 | socket->Reply("No property argument supplied.\r\n"); |
| 155 | break; |
| 156 | } |
| 157 | try { |
| 158 | node = PropertyManager->GetNode(argument); |
| 159 | } catch(...) { |
| 160 | socket->Reply("Badly formed property query\r\n"); |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | if (!node) { |
| 165 | socket->Reply("Unknown property\r\n"); |
| 166 | break; |
| 167 | } else if (!node->hasValue()) { |
| 168 | socket->Reply("Not a leaf property\r\n"); |
| 169 | break; |
| 170 | } else { |
| 171 | if (is_number(trim(str_value))) { |
| 172 | try { |
| 173 | double value = atof_locale_c(str_value); |
| 174 | node->setDoubleValue(value); |
| 175 | } catch(BaseException& e) { |
| 176 | socket->Reply(e.what()); |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | else { |
| 181 | socket->Reply("Invalid number\r\n"); |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | socket->Reply("set successful\r\n"); |
| 186 | |
| 187 | } else if (command == "get") { // GET PROPERTY |
| 188 | FGPropertyNode* node = nullptr; |
| 189 | |
| 190 | if (argument.empty()) { |
| 191 | socket->Reply("No property argument supplied.\r\n"); |
| 192 | break; |
| 193 | } |
| 194 | try { |
| 195 | node = PropertyManager->GetNode(argument); |
| 196 | } catch(...) { |
| 197 | socket->Reply("Badly formed property query\r\n"); |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | if (!node) { |
| 202 | socket->Reply("Unknown property\r\n"); |
| 203 | break; |
| 204 | } else if (!node->hasValue()) { |
| 205 | if (Holding) { // if holding can query property list |
| 206 | string query = FDMExec->QueryPropertyCatalog(argument, "\r\n"); |
| 207 | socket->Reply(query); |
| 208 | } else { |
| 209 | socket->Reply("Must be in HOLD to search properties\r\n"); |
| 210 | } |
| 211 | } else { |
| 212 | ostringstream buf; |
| 213 | buf << argument << " = " << setw(12) << setprecision(6) << node->getDoubleValue() << '\r' << endl; |
| 214 | socket->Reply(buf.str()); |
| 215 | } |
| 216 | |
| 217 | } else if (command == "hold") { // PAUSE |
| 218 | |
| 219 | FDMExec->Hold(); |
| 220 | socket->Reply("Holding\r\n"); |
| 221 | |
| 222 | } else if (command == "resume") { // RESUME |
| 223 | |
| 224 | FDMExec->Resume(); |
| 225 | socket->Reply("Resuming\r\n"); |
| 226 | |
| 227 | } else if (command == "iterate") { // ITERATE |
| 228 | |
| 229 | int argumentInt; |
| 230 | istringstream (argument) >> argumentInt; |
| 231 | if (argument.empty()) { |
| 232 | socket->Reply("No argument supplied for number of iterations.\r\n"); |
| 233 | break; |
| 234 | } |
| 235 | if ( !(argumentInt > 0) ){ |
| 236 | socket->Reply("Required argument must be a positive Integer.\r\n"); |
| 237 | break; |
| 238 | } |
| 239 | FDMExec->EnableIncrementThenHold( argumentInt ); |
| 240 | FDMExec->Resume(); |
| 241 | socket->Reply("Iterations performed\r\n"); |
| 242 | |
| 243 | } else if (command == "quit") { // QUIT |
| 244 | |
| 245 | // close the socket connection |
| 246 | socket->Reply("Closing connection\r\n"); |
| 247 | socket->Close(); |
| 248 | |
| 249 | } else if (command == "info") { // INFO |
| 250 | |
| 251 | // get info about the sim run and/or aircraft, etc. |
| 252 | ostringstream info; |
| 253 | info << "JSBSim version: " << JSBSim_version << "\r\n"; |
| 254 | info << "Config File version: " << needed_cfg_version << "\r\n"; |
| 255 | info << "Aircraft simulated: " << FDMExec->GetAircraft()->GetAircraftName() << "\r\n"; |
| 256 | info << "Simulation time: " << setw(8) << setprecision(3) << FDMExec->GetSimTime() << '\r' << endl; |
| 257 | socket->Reply(info.str()); |
| 258 | |
| 259 | } else if (command == "help") { // HELP |
| 260 | |
| 261 | socket->Reply( |
| 262 | " JSBSim Server commands:\n\r\n" |
| 263 | " get {property name}\r\n" |
| 264 | " set {property name} {value}\r\n" |
| 265 | " hold\r\n" |
| 266 | " resume\r\n" |
| 267 | " iterate {value}\r\n" |
| 268 | " help\r\n" |
| 269 | " quit\r\n" |
| 270 | " info\n\r\n"); |
| 271 | |
| 272 | } else { |
| 273 | socket->Reply(string("Unknown command: ") + command + "\r\n"); |
| 274 | } |
| 275 | |
| 276 | start = string_end; |
| 277 | } |
| 278 | |
| 279 | // Remove processed commands. |
| 280 | size_t last_crlf = data.find_last_of("\r\n"); |
| 281 | if (last_crlf != string::npos) { |
| 282 | if (last_crlf < data.length()-1) |
| 283 | data = data.substr(last_crlf+1); |
| 284 | else |
| 285 | data.clear(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | } |
| 290 | |
| 291 | } |