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 SG_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();
93 * Return a "Node no found" error message to the client.
95 void node_not_found_error( const string& node_name );
101 PropsChannel::PropsChannel()
106 setTerminator( "\r\n" );
113 PropsChannel::collectIncomingData( const char* s, int n )
115 buffer.append( s, n );
122 PropsChannel::node_not_found_error( const string& node_name )
124 string error = "-ERR Node \"";
126 error += "\" not found.";
127 push( error.c_str() );
128 push( getTerminator() );
131 // return a human readable form of the value "type"
133 getValueTypeString( const SGPropertyNode *node )
142 SGPropertyNode::Type type = node->getType();
143 if ( type == SGPropertyNode::UNSPECIFIED ) {
144 result = "unspecified";
145 } else if ( type == SGPropertyNode::NONE ) {
147 } else if ( type == SGPropertyNode::BOOL ) {
149 } else if ( type == SGPropertyNode::INT ) {
151 } else if ( type == SGPropertyNode::LONG ) {
153 } else if ( type == SGPropertyNode::FLOAT ) {
155 } else if ( type == SGPropertyNode::DOUBLE ) {
157 } else if ( type == SGPropertyNode::STRING ) {
169 PropsChannel::foundTerminator()
171 const char* cmd = buffer.getData();
172 SG_LOG( SG_IO, SG_INFO, "processing command = \"" << cmd << "\"" );
174 vector<string> tokens = simgear::strutils::split( cmd );
176 SGPropertyNode* node = globals->get_props()->getNode( path.c_str() );
178 if (!tokens.empty()) {
179 string command = tokens[0];
181 if (command == "ls") {
182 SGPropertyNode* dir = node;
183 if (tokens.size() == 2) {
184 if (tokens[1][0] == '/') {
185 dir = globals->get_props()->getNode( tokens[1].c_str() );
190 dir = globals->get_props()->getNode( s.c_str() );
194 node_not_found_error( tokens[1] );
199 for (int i = 0; i < dir->nChildren(); i++) {
200 SGPropertyNode * child = dir->getChild(i);
201 string line = child->getDisplayName(true);
203 if ( child->nChildren() > 0 ) {
206 if (mode == PROMPT) {
207 string value = child->getStringValue();
208 line += " =\t'" + value + "'\t(";
209 line += getValueTypeString( child );
214 line += getTerminator();
215 push( line.c_str() );
217 } else if ( command == "dump" ) {
219 if ( tokens.size() <= 1 ) {
220 writeProperties( buf, node );
221 buf << ends; // null terminate the string
222 push( buf.str().c_str() );
223 push( getTerminator() );
225 SGPropertyNode *child = node->getNode( tokens[1].c_str() );
227 writeProperties ( buf, child );
228 buf << ends; // null terminate the string
229 push( buf.str().c_str() );
230 push( getTerminator() );
232 node_not_found_error( tokens[1] );
236 else if ( command == "cd" ) {
237 if (tokens.size() == 2) {
239 SGPropertyNode* child = node->getNode( tokens[1].c_str() );
242 path = node->getPath();
244 node_not_found_error( tokens[1] );
247 // Ignore attempt to move past root node with ".."
250 } else if ( command == "pwd" ) {
251 string ttt = node->getPath();
257 push( getTerminator() );
258 } else if ( command == "get" || command == "show" ) {
259 if ( tokens.size() == 2 ) {
261 string value = node->getStringValue ( tokens[1].c_str(), "" );
262 if ( mode == PROMPT ) {
267 tmp += getValueTypeString(
268 node->getNode( tokens[1].c_str() ) );
274 push( getTerminator() );
276 } else if ( command == "set" ) {
277 if ( tokens.size() >= 2 ) {
279 for (unsigned int i = 2; i < tokens.size(); i++) {
284 node->getNode( tokens[1].c_str(), true )
285 ->setStringValue(value.c_str());
287 if ( mode == PROMPT ) {
288 // now fetch and write out the new value as confirmation
290 value = node->getStringValue ( tokens[1].c_str(), "" );
291 tmp = tokens[1] + " = '" + value + "' (";
292 tmp += getValueTypeString( node->getNode( tokens[1].c_str() ) );
295 push( getTerminator() );
298 } else if ( command == "reinit" ) {
299 if ( tokens.size() == 2 ) {
302 for ( unsigned int i = 1; i < tokens.size(); ++i ) {
303 cout << "props: adding subsystem = " << tokens[i] << endl;
304 SGPropertyNode *node = args.getNode("subsystem", i-1, true);
305 node->setStringValue( tokens[i].c_str() );
307 if ( !globals->get_commands()
308 ->execute( "reinit", &args) )
310 SG_LOG( SG_GENERAL, SG_ALERT,
311 "Command " << tokens[1] << " failed.");
312 if ( mode == PROMPT ) {
315 push( getTerminator() );
318 if ( mode == PROMPT ) {
319 tmp += "<completed>";
321 push( getTerminator() );
325 } else if ( command == "run" ) {
327 if ( tokens.size() >= 2 ) {
329 if ( tokens[1] == "reinit" ) {
330 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
331 cout << "props: adding subsystem = " << tokens[i]
334 = args.getNode("subsystem", i-2, true);
335 node->setStringValue( tokens[i].c_str() );
337 } else if ( tokens[1] == "set-sea-level-air-temp-degc" ) {
338 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
339 cout << "props: set-sl command = " << tokens[i]
342 = args.getNode("temp-degc", i-2, true);
343 node->setStringValue( tokens[i].c_str() );
345 } else if ( tokens[1] == "set-outside-air-temp-degc" ) {
346 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
347 cout << "props: set-oat command = " << tokens[i]
350 = args.getNode("temp-degc", i-2, true);
351 node->setStringValue( tokens[i].c_str() );
353 } else if ( tokens[1] == "timeofday" ) {
354 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
355 cout << "props: time of day command = " << tokens[i]
358 = args.getNode("timeofday", i-2, true);
359 node->setStringValue( tokens[i].c_str() );
361 } else if ( tokens[1] == "play-audio-message" ) {
362 if ( tokens.size() == 4 ) {
363 cout << "props: play audio message = " << tokens[2]
364 << " " << tokens[3] << endl;
365 SGPropertyNode *node;
366 node = args.getNode("path", 0, true);
367 node->setStringValue( tokens[2].c_str() );
368 node = args.getNode("file", 0, true);
369 node->setStringValue( tokens[3].c_str() );
372 if ( !globals->get_commands()
373 ->execute(tokens[1].c_str(), &args) )
375 SG_LOG( SG_GENERAL, SG_ALERT,
376 "Command " << tokens[1] << " failed.");
377 if ( mode == PROMPT ) {
380 push( getTerminator() );
383 if ( mode == PROMPT ) {
384 tmp += "<completed>";
386 push( getTerminator() );
390 if ( mode == PROMPT ) {
391 tmp += "no command specified";
393 push( getTerminator() );
396 } else if (command == "quit") {
400 } else if ( command == "data" ) {
402 } else if ( command == "prompt" ) {
406 Valid commands are:\r\n\
408 cd <dir> cd to a directory, '..' to move back\r\n\
409 data switch to raw data mode\r\n\
410 dump dump current state (in xml)\r\n\
411 get <var> show the value of a parameter\r\n\
412 help show this help message\r\n\
413 ls [<dir>] list directory\r\n\
414 prompt switch to interactive mode (default)\r\n\
415 pwd display your current path\r\n\
416 quit terminate connection\r\n\
417 run <command> run built in command\r\n\
418 set <var> <val> set <var> to a new <val>\r\n";
424 if (mode == PROMPT) {
425 string prompt = node->getPath();
426 if (prompt.empty()) {
430 push( prompt.c_str() );
439 FGProps::FGProps( const vector<string>& tokens )
443 // props,medium,direction,hz,hostname,port#,style
444 if (tokens.size() == 2) {
445 port = atoi( tokens[1].c_str() );
446 set_hz( 5 ); // default to processing requests @ 5Hz
447 } else if (tokens.size() == 7) {
450 int hz = strtol( tokens[3].c_str(), &endptr, 10 );
452 SG_LOG( SG_IO, SG_ALERT, "I/O poll frequency out of range" );
453 set_hz( 5 ); // default to processing requests @ 5Hz
455 SG_LOG( SG_IO, SG_INFO, "Setting I/O poll frequency to "
459 port = atoi( tokens[5].c_str() );
461 throw FGProtocolConfigError( "FGProps: incorrect number of configuration arguments" );
480 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
481 << "is already in use, ignoring" );
486 netChannel::bind( "", port );
487 netChannel::listen( 5 );
488 SG_LOG( SG_IO, SG_INFO, "Props server started on port " << port );
500 SG_LOG( SG_IO, SG_INFO, "closing FGProps" );
518 FGProps::handleAccept()
521 int handle = accept( &addr );
522 SG_LOG( SG_IO, SG_INFO, "Props server accepted connection from "
523 << addr.getHost() << ":" << addr.getPort() );
524 PropsChannel* channel = new PropsChannel();
525 channel->setHandle( handle );