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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <simgear/compiler.h>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/misc/strutils.hxx>
33 #include <simgear/misc/props.hxx>
34 #include <simgear/misc/props_io.hxx>
36 #include STL_STRSTREAM
38 #include <Main/globals.hxx>
39 #include <Main/viewmgr.hxx>
41 #include <plib/netChat.h>
45 SG_USING_STD(strstream);
49 * Props connection class.
50 * This class represents a connection to props client.
52 class PropsChannel : public netChat
57 * Current property node name.
74 * Append incoming data to our request buffer.
76 * @param s Character string to append to buffer
77 * @param n Number of characters to append.
79 void collectIncomingData( const char* s, int n );
82 * Process a complete request from the props client.
84 void foundTerminator();
88 * Return a "Node no found" error message to the client.
90 void node_not_found_error( const string& node_name );
96 PropsChannel::PropsChannel()
101 setTerminator( "\r\n" );
108 PropsChannel::collectIncomingData( const char* s, int n )
110 buffer.append( s, n );
117 PropsChannel::node_not_found_error( const string& node_name )
119 string error = "ERR Node \"";
121 error += "\" not found.";
122 push( error.c_str() );
123 push( getTerminator() );
126 // return a human readable form of the value "type"
128 getValueTypeString( const SGPropertyNode *node )
137 SGPropertyNode::Type type = node->getType();
138 if ( type == SGPropertyNode::UNSPECIFIED ) {
139 result = "unspecified";
140 } else if ( type == SGPropertyNode::NONE ) {
142 } else if ( type == SGPropertyNode::BOOL ) {
144 } else if ( type == SGPropertyNode::INT ) {
146 } else if ( type == SGPropertyNode::LONG ) {
148 } else if ( type == SGPropertyNode::FLOAT ) {
150 } else if ( type == SGPropertyNode::DOUBLE ) {
152 } else if ( type == SGPropertyNode::STRING ) {
164 PropsChannel::foundTerminator()
166 const char* cmd = buffer.getData();
167 SG_LOG( SG_IO, SG_INFO, "processing command = \"" << cmd << "\"" );
169 vector<string> tokens = simgear::strutils::split( cmd );
171 SGPropertyNode* node = globals->get_props()->getNode( path.c_str() );
175 string command = tokens[0];
179 SGPropertyNode* dir = node;
180 if (tokens.size() == 2)
182 if (tokens[1][0] == '/')
184 dir = globals->get_props()->getNode( tokens[1].c_str() );
191 dir = globals->get_props()->getNode( s.c_str() );
196 node_not_found_error( tokens[1] );
201 for (int i = 0; i < dir->nChildren(); i++)
203 SGPropertyNode * child = dir->getChild(i);
204 string line = child->getDisplayName(true);
206 if ( child->nChildren() > 0 )
214 string value = child->getStringValue();
215 line += " =\t'" + value + "'\t(";
216 line += getValueTypeString( child );
221 line += getTerminator();
222 push( line.c_str() );
225 else if ( command == "dump" )
228 if ( tokens.size() <= 1 )
230 writeProperties( buf, node );
231 buf << ends; // null terminate the string
233 push( getTerminator() );
237 SGPropertyNode *child = node->getNode( tokens[1].c_str() );
240 writeProperties ( buf, child );
241 buf << ends; // null terminate the string
243 push( getTerminator() );
247 node_not_found_error( tokens[1] );
251 else if ( command == "cd" )
253 if (tokens.size() == 2)
257 SGPropertyNode* child = node->getNode( tokens[1].c_str() );
261 path = node->getPath();
265 node_not_found_error( tokens[1] );
270 // Ignore attempt to move past root node with ".."
274 else if ( command == "pwd" )
276 string ttt = node->getPath();
283 push( getTerminator() );
285 else if ( command == "get" || command == "show" )
287 if ( tokens.size() == 2 )
290 string value = node->getStringValue ( tokens[1].c_str(), "" );
291 if ( mode == PROMPT )
297 tmp += getValueTypeString(
298 node->getNode( tokens[1].c_str() ) );
306 push( getTerminator() );
309 else if ( command == "set" )
311 if ( tokens.size() == 3 )
313 node->getNode( tokens[1].c_str(), true )->setStringValue(tokens[2].c_str());
315 if ( mode == PROMPT )
317 // now fetch and write out the new value as confirmation
319 string value = node->getStringValue ( tokens[1].c_str(), "" );
320 string tmp = tokens[1] + " = '" + value + "' (";
321 tmp += getValueTypeString( node->getNode( tokens[1].c_str() ) );
324 push( getTerminator() );
328 else if (command == "quit")
334 else if ( command == "data" )
338 else if ( command == "prompt" )
345 Valid commands are:\r\n\
347 cd <dir> cd to a directory, '..' to move back\r\n\
348 data switch to raw data mode\r\n\
349 dump dump current state (in xml)\r\n\
350 get <var> show the value of a parameter\r\n\
351 help show this help message\r\n\
352 ls [<dir>] list directory\r\n\
353 prompt switch to interactive mode (default)\r\n\
354 pwd display your current path\r\n\
355 quit terminate connection\r\n\
356 set <var> <val> set <var> to a new <val>\r\n\
357 show <var> synonym for get\r\n";
365 string prompt = node->getPath();
371 push( prompt.c_str() );
380 FGProps::FGProps( const vector<string>& tokens )
384 // props,medium,direction,hz,hostname,port#,style
385 if (tokens.size() == 2) {
386 port = atoi( tokens[1].c_str() );
387 set_hz( 5 ); // default to processing requests @ 5Hz
388 } else if (tokens.size() == 7) {
391 int hz = strtol( tokens[3].c_str(), &endptr, 10 );
393 SG_LOG( SG_IO, SG_ALERT, "I/O poll frequency out of range" );
394 set_hz( 5 ); // default to processing requests @ 5Hz
396 SG_LOG( SG_IO, SG_INFO, "Setting I/O poll frequency to "
400 port = atoi( tokens[5].c_str() );
402 throw FGProtocolConfigError( "FGProps: incorrect number of configuration arguments" );
421 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
422 << "is already in use, ignoring" );
427 netChannel::bind( "", port );
428 netChannel::listen( 5 );
429 SG_LOG( SG_IO, SG_INFO, "Props server started on port " << port );
458 FGProps::handleAccept()
461 int handle = accept( &addr );
462 SG_LOG( SG_IO, SG_INFO, "Props server accepted connection from "
463 << addr.getHost() << ":" << addr.getPort() );
464 PropsChannel* channel = new PropsChannel();
465 channel->setHandle( handle );