2 // Property telnet server class.
4 // Written by Bernie Bright, started May 2002.
6 // Copyright (C) 2002 Bernie Bright - bbright@bigpond.net.au
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <simgear/compiler.h>
29 #include <simgear/debug/logstream.hxx>
30 #include <simgear/misc/strutils.hxx>
31 #include <simgear/misc/props.hxx>
32 #include <simgear/misc/props_io.hxx>
34 #include STL_STRSTREAM
36 #include <Main/globals.hxx>
37 #include <Main/viewmgr.hxx>
39 #include <plib/netChat.h>
43 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
44 SG_USING_STD(strstream);
49 * Telnet connection class.
50 * This class represents a connection to a telnet-style client.
52 class TelnetChannel : 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 telnet 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 TelnetChannel::TelnetChannel()
101 setTerminator( "\r\n" );
108 TelnetChannel::collectIncomingData( const char* s, int n )
110 buffer.append( s, n );
117 TelnetChannel::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 TelnetChannel::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 FGTelnet::FGTelnet( 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 else if (tokens.size() == 7)
388 port = atoi( tokens[5].c_str() );
390 throw FGProtocolConfigError( "FGTelnet: incorrect number of configuration arguments" );
396 FGTelnet::~FGTelnet()
408 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
409 << "is already in use, ignoring" );
414 netChannel::bind( "", port );
415 netChannel::listen( 5 );
416 SG_LOG( SG_IO, SG_INFO, "Telnet server started on port " << port );
418 set_hz( 5 ); // default to processing requests @ 5Hz
446 FGTelnet::handleAccept()
449 int handle = accept( &addr );
450 SG_LOG( SG_IO, SG_INFO, "Telnet server accepted connection from "
451 << addr.getHost() << ":" << addr.getPort() );
452 TelnetChannel* channel = new TelnetChannel();
453 channel->setHandle( handle );