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 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
46 SG_USING_STD(strstream);
51 * Props connection class.
52 * This class represents a connection to props client.
54 class PropsChannel : public netChat
59 * Current property node name.
76 * Append incoming data to our request buffer.
78 * @param s Character string to append to buffer
79 * @param n Number of characters to append.
81 void collectIncomingData( const char* s, int n );
84 * Process a complete request from the props client.
86 void foundTerminator();
90 * Return a "Node no found" error message to the client.
92 void node_not_found_error( const string& node_name );
98 PropsChannel::PropsChannel()
103 setTerminator( "\r\n" );
110 PropsChannel::collectIncomingData( const char* s, int n )
112 buffer.append( s, n );
119 PropsChannel::node_not_found_error( const string& node_name )
121 string error = "ERR Node \"";
123 error += "\" not found.";
124 push( error.c_str() );
125 push( getTerminator() );
128 // return a human readable form of the value "type"
130 getValueTypeString( const SGPropertyNode *node )
139 SGPropertyNode::Type type = node->getType();
140 if ( type == SGPropertyNode::UNSPECIFIED ) {
141 result = "unspecified";
142 } else if ( type == SGPropertyNode::NONE ) {
144 } else if ( type == SGPropertyNode::BOOL ) {
146 } else if ( type == SGPropertyNode::INT ) {
148 } else if ( type == SGPropertyNode::LONG ) {
150 } else if ( type == SGPropertyNode::FLOAT ) {
152 } else if ( type == SGPropertyNode::DOUBLE ) {
154 } else if ( type == SGPropertyNode::STRING ) {
166 PropsChannel::foundTerminator()
168 const char* cmd = buffer.getData();
169 SG_LOG( SG_IO, SG_INFO, "processing command = \"" << cmd << "\"" );
171 vector<string> tokens = simgear::strutils::split( cmd );
173 SGPropertyNode* node = globals->get_props()->getNode( path.c_str() );
177 string command = tokens[0];
181 SGPropertyNode* dir = node;
182 if (tokens.size() == 2)
184 if (tokens[1][0] == '/')
186 dir = globals->get_props()->getNode( tokens[1].c_str() );
193 dir = globals->get_props()->getNode( s.c_str() );
198 node_not_found_error( tokens[1] );
203 for (int i = 0; i < dir->nChildren(); i++)
205 SGPropertyNode * child = dir->getChild(i);
206 string name = child->getName();
209 if (dir->getChild( name.c_str(), 1 ))
212 sprintf(buf, "[%d]", child->getIndex());
216 if ( child->nChildren() > 0 )
224 string value = dir->getStringValue( name.c_str(), "" );
225 line += " =\t'" + value + "'\t(";
226 line += getValueTypeString(
227 dir->getNode( name.c_str() ) );
232 line += getTerminator();
233 push( line.c_str() );
236 else if ( command == "dump" )
239 if ( tokens.size() <= 1 )
241 writeProperties( buf, node );
242 buf << ends; // null terminate the string
244 push( getTerminator() );
248 SGPropertyNode *child = node->getNode( tokens[1].c_str() );
251 writeProperties ( buf, child );
252 buf << ends; // null terminate the string
254 push( getTerminator() );
258 node_not_found_error( tokens[1] );
262 else if ( command == "cd" )
264 if (tokens.size() == 2)
268 SGPropertyNode* child = node->getNode( tokens[1].c_str() );
272 path = node->getPath();
276 node_not_found_error( tokens[1] );
281 // Ignore attempt to move past root node with ".."
285 else if ( command == "pwd" )
287 string ttt = node->getPath();
294 push( getTerminator() );
296 else if ( command == "get" || command == "show" )
298 if ( tokens.size() == 2 )
301 string value = node->getStringValue ( tokens[1].c_str(), "" );
302 if ( mode == PROMPT )
308 tmp += getValueTypeString(
309 node->getNode( tokens[1].c_str() ) );
317 push( getTerminator() );
320 else if ( command == "set" )
322 if ( tokens.size() == 3 )
324 node->getNode( tokens[1].c_str(), true )->setStringValue(tokens[2].c_str());
326 if ( mode == PROMPT )
328 // now fetch and write out the new value as confirmation
330 string value = node->getStringValue ( tokens[1].c_str(), "" );
331 string tmp = tokens[1] + " = '" + value + "' (";
332 tmp += getValueTypeString( node->getNode( tokens[1].c_str() ) );
335 push( getTerminator() );
339 else if (command == "quit")
345 else if ( command == "data" )
349 else if ( command == "prompt" )
356 Valid commands are:\r\n\
358 cd <dir> cd to a directory, '..' to move back\r\n\
359 data switch to raw data mode\r\n\
360 dump dump current state (in xml)\r\n\
361 get <var> show the value of a parameter\r\n\
362 help show this help message\r\n\
363 ls [<dir>] list directory\r\n\
364 prompt switch to interactive mode (default)\r\n\
365 pwd display your current path\r\n\
366 quit terminate connection\r\n\
367 set <var> <val> set <var> to a new <val>\r\n\
368 show <var> synonym for get\r\n";
376 string prompt = node->getPath();
382 push( prompt.c_str() );
391 FGProps::FGProps( const vector<string>& tokens )
395 // props,medium,direction,hz,hostname,port#,style
396 if (tokens.size() == 2)
397 port = atoi( tokens[1].c_str() );
398 else if (tokens.size() == 7)
399 port = atoi( tokens[5].c_str() );
401 throw FGProtocolConfigError( "FGProps: incorrect number of configuration arguments" );
419 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
420 << "is already in use, ignoring" );
425 netChannel::bind( "", port );
426 netChannel::listen( 5 );
427 SG_LOG( SG_IO, SG_INFO, "Props server started on port " << port );
429 set_hz( 5 ); // default to processing requests @ 5Hz
457 FGProps::handleAccept()
460 int handle = accept( &addr );
461 SG_LOG( SG_IO, SG_INFO, "Props server accepted connection from "
462 << addr.getHost() << ":" << addr.getPort() );
463 PropsChannel* channel = new PropsChannel();
464 channel->setHandle( handle );