2 // Property server class.
4 // Written by Curtis Olson, started September 2000.
5 // Modified by Bernie Bright, May 2002.
7 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <simgear/compiler.h>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/structure/commands.hxx>
33 #include <simgear/misc/strutils.hxx>
34 #include <simgear/props/props.hxx>
35 #include <simgear/props/props_io.hxx>
40 #include <Main/globals.hxx>
41 #include <Main/viewmgr.hxx>
43 #include <plib/netChat.h>
47 using std::stringstream;
54 * Props connection class.
55 * This class represents a connection to props client.
57 class PropsChannel : public netChat
62 * Current property node name.
79 * Append incoming data to our request buffer.
81 * @param s Character string to append to buffer
82 * @param n Number of characters to append.
84 void collectIncomingData( const char* s, int n );
87 * Process a complete request from the props client.
89 void foundTerminator();
92 inline void node_not_found_error( const string& s ) const {
93 throw "node '" + s + "' not found";
100 PropsChannel::PropsChannel()
105 setTerminator( "\r\n" );
112 PropsChannel::collectIncomingData( const char* s, int n )
114 buffer.append( s, n );
117 // return a human readable form of the value "type"
119 getValueTypeString( const SGPropertyNode *node )
128 SGPropertyNode::Type type = node->getType();
129 if ( type == SGPropertyNode::UNSPECIFIED ) {
130 result = "unspecified";
131 } else if ( type == SGPropertyNode::NONE ) {
133 } else if ( type == SGPropertyNode::BOOL ) {
135 } else if ( type == SGPropertyNode::INT ) {
137 } else if ( type == SGPropertyNode::LONG ) {
139 } else if ( type == SGPropertyNode::FLOAT ) {
141 } else if ( type == SGPropertyNode::DOUBLE ) {
143 } else if ( type == SGPropertyNode::STRING ) {
155 PropsChannel::foundTerminator()
157 const char* cmd = buffer.getData();
158 SG_LOG( SG_IO, SG_INFO, "processing command = \"" << cmd << "\"" );
160 vector<string> tokens = simgear::strutils::split( cmd );
162 SGPropertyNode* node = globals->get_props()->getNode( path.c_str() );
165 if (!tokens.empty()) {
166 string command = tokens[0];
168 if (command == "ls") {
169 SGPropertyNode* dir = node;
170 if (tokens.size() == 2) {
171 if (tokens[1][0] == '/') {
172 dir = globals->get_props()->getNode( tokens[1].c_str() );
177 dir = globals->get_props()->getNode( s.c_str() );
181 node_not_found_error( tokens[1] );
185 for (int i = 0; i < dir->nChildren(); i++) {
186 SGPropertyNode * child = dir->getChild(i);
187 string line = child->getDisplayName(true);
189 if ( child->nChildren() > 0 ) {
192 if (mode == PROMPT) {
193 string value = child->getStringValue();
194 line += " =\t'" + value + "'\t(";
195 line += getValueTypeString( child );
200 line += getTerminator();
201 push( line.c_str() );
203 } else if ( command == "dump" ) {
205 if ( tokens.size() <= 1 ) {
206 writeProperties( buf, node );
207 buf << ends; // null terminate the string
208 push( buf.str().c_str() );
209 push( getTerminator() );
211 SGPropertyNode *child = node->getNode( tokens[1].c_str() );
213 writeProperties ( buf, child );
214 buf << ends; // null terminate the string
215 push( buf.str().c_str() );
216 push( getTerminator() );
218 node_not_found_error( tokens[1] );
222 else if ( command == "cd" ) {
223 if (tokens.size() == 2) {
224 SGPropertyNode* child = node->getNode( tokens[1].c_str() );
227 path = node->getPath();
229 node_not_found_error( tokens[1] );
232 } else if ( command == "pwd" ) {
233 string pwd = node->getPath();
239 push( getTerminator() );
240 } else if ( command == "get" || command == "show" ) {
241 if ( tokens.size() == 2 ) {
243 string value = node->getStringValue ( tokens[1].c_str(), "" );
244 if ( mode == PROMPT ) {
249 tmp += getValueTypeString(
250 node->getNode( tokens[1].c_str() ) );
256 push( getTerminator() );
258 } else if ( command == "set" ) {
259 if ( tokens.size() >= 2 ) {
261 for (unsigned int i = 2; i < tokens.size(); i++) {
266 node->getNode( tokens[1].c_str(), true )
267 ->setStringValue(value.c_str());
269 if ( mode == PROMPT ) {
270 // now fetch and write out the new value as confirmation
272 value = node->getStringValue ( tokens[1].c_str(), "" );
273 tmp = tokens[1] + " = '" + value + "' (";
274 tmp += getValueTypeString( node->getNode( tokens[1].c_str() ) );
277 push( getTerminator() );
280 } else if ( command == "reinit" ) {
281 if ( tokens.size() == 2 ) {
284 for ( unsigned int i = 1; i < tokens.size(); ++i ) {
285 cout << "props: adding subsystem = " << tokens[i] << endl;
286 SGPropertyNode *node = args.getNode("subsystem", i-1, true);
287 node->setStringValue( tokens[i].c_str() );
289 if ( !globals->get_commands()
290 ->execute( "reinit", &args) )
292 SG_LOG( SG_GENERAL, SG_ALERT,
293 "Command " << tokens[1] << " failed.");
294 if ( mode == PROMPT ) {
297 push( getTerminator() );
300 if ( mode == PROMPT ) {
301 tmp += "<completed>";
303 push( getTerminator() );
307 } else if ( command == "run" ) {
309 if ( tokens.size() >= 2 ) {
311 if ( tokens[1] == "reinit" ) {
312 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
313 cout << "props: adding subsystem = " << tokens[i]
316 = args.getNode("subsystem", i-2, true);
317 node->setStringValue( tokens[i].c_str() );
319 } else if ( tokens[1] == "set-sea-level-air-temp-degc" ) {
320 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
321 cout << "props: set-sl command = " << tokens[i]
324 = args.getNode("temp-degc", i-2, true);
325 node->setStringValue( tokens[i].c_str() );
327 } else if ( tokens[1] == "set-outside-air-temp-degc" ) {
328 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
329 cout << "props: set-oat command = " << tokens[i]
332 = args.getNode("temp-degc", i-2, true);
333 node->setStringValue( tokens[i].c_str() );
335 } else if ( tokens[1] == "timeofday" ) {
336 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
337 cout << "props: time of day command = " << tokens[i]
340 = args.getNode("timeofday", i-2, true);
341 node->setStringValue( tokens[i].c_str() );
343 } else if ( tokens[1] == "play-audio-message" ) {
344 if ( tokens.size() == 4 ) {
345 cout << "props: play audio message = " << tokens[2]
346 << " " << tokens[3] << endl;
347 SGPropertyNode *node;
348 node = args.getNode("path", 0, true);
349 node->setStringValue( tokens[2].c_str() );
350 node = args.getNode("file", 0, true);
351 node->setStringValue( tokens[3].c_str() );
354 if ( !globals->get_commands()
355 ->execute(tokens[1].c_str(), &args) )
357 SG_LOG( SG_GENERAL, SG_ALERT,
358 "Command " << tokens[1] << " failed.");
359 if ( mode == PROMPT ) {
362 push( getTerminator() );
365 if ( mode == PROMPT ) {
366 tmp += "<completed>";
368 push( getTerminator() );
372 if ( mode == PROMPT ) {
373 tmp += "no command specified";
375 push( getTerminator() );
378 } else if ( command == "quit" || command == "exit" ) {
382 } else if ( command == "data" ) {
384 } else if ( command == "prompt" ) {
388 Valid commands are:\r\n\
390 cd <dir> cd to a directory, '..' to move back\r\n\
391 data switch to raw data mode\r\n\
392 dump dump current state (in xml)\r\n\
393 get <var> show the value of a parameter\r\n\
394 help show this help message\r\n\
395 ls [<dir>] list directory\r\n\
396 prompt switch to interactive mode (default)\r\n\
397 pwd display your current path\r\n\
398 quit terminate connection\r\n\
399 run <command> run built in command\r\n\
400 set <var> <val> set <var> to a new <val>\r\n";
405 } catch ( const string& msg ) {
406 string error = "-ERR \"" + msg + "\"";
407 push( error.c_str() );
408 push( getTerminator() );
411 if ( mode == PROMPT ) {
412 string prompt = node->getPath();
413 if (prompt.empty()) {
417 push( prompt.c_str() );
426 FGProps::FGProps( const vector<string>& tokens )
430 // props,medium,direction,hz,hostname,port#,style
431 if (tokens.size() == 2) {
432 port = atoi( tokens[1].c_str() );
433 set_hz( 5 ); // default to processing requests @ 5Hz
434 } else if (tokens.size() == 7) {
437 int hz = strtol( tokens[3].c_str(), &endptr, 10 );
439 SG_LOG( SG_IO, SG_ALERT, "I/O poll frequency out of range" );
440 set_hz( 5 ); // default to processing requests @ 5Hz
442 SG_LOG( SG_IO, SG_INFO, "Setting I/O poll frequency to "
446 port = atoi( tokens[5].c_str() );
448 throw FGProtocolConfigError( "FGProps: incorrect number of configuration arguments" );
467 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
468 << "is already in use, ignoring" );
473 netChannel::bind( "", port );
474 netChannel::listen( 5 );
475 SG_LOG( SG_IO, SG_INFO, "Props server started on port " << port );
487 SG_LOG( SG_IO, SG_INFO, "closing FGProps" );
505 FGProps::handleAccept()
508 int handle = accept( &addr );
509 SG_LOG( SG_IO, SG_INFO, "Props server accepted connection from "
510 << addr.getHost() << ":" << addr.getPort() );
511 PropsChannel* channel = new PropsChannel();
512 channel->setHandle( handle );