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 name = child->getName();
207 if (dir->getChild( name.c_str(), 1 ))
210 sprintf(buf, "[%d]", child->getIndex());
214 if ( child->nChildren() > 0 )
222 string value = dir->getStringValue( name.c_str(), "" );
223 line += " =\t'" + value + "'\t(";
224 line += getValueTypeString(
225 dir->getNode( name.c_str() ) );
230 line += getTerminator();
231 push( line.c_str() );
234 else if ( command == "dump" )
237 if ( tokens.size() <= 1 )
239 writeProperties( buf, node );
240 buf << ends; // null terminate the string
242 push( getTerminator() );
246 SGPropertyNode *child = node->getNode( tokens[1].c_str() );
249 writeProperties ( buf, child );
250 buf << ends; // null terminate the string
252 push( getTerminator() );
256 node_not_found_error( tokens[1] );
260 else if ( command == "cd" )
262 if (tokens.size() == 2)
266 SGPropertyNode* child = node->getNode( tokens[1].c_str() );
270 path = node->getPath();
274 node_not_found_error( tokens[1] );
279 // Ignore attempt to move past root node with ".."
283 else if ( command == "pwd" )
285 string ttt = node->getPath();
292 push( getTerminator() );
294 else if ( command == "get" || command == "show" )
296 if ( tokens.size() == 2 )
299 string value = node->getStringValue ( tokens[1].c_str(), "" );
300 if ( mode == PROMPT )
306 tmp += getValueTypeString(
307 node->getNode( tokens[1].c_str() ) );
315 push( getTerminator() );
318 else if ( command == "set" )
320 if ( tokens.size() == 3 )
322 node->getNode( tokens[1].c_str(), true )->setStringValue(tokens[2].c_str());
324 if ( mode == PROMPT )
326 // now fetch and write out the new value as confirmation
328 string value = node->getStringValue ( tokens[1].c_str(), "" );
329 string tmp = tokens[1] + " = '" + value + "' (";
330 tmp += getValueTypeString( node->getNode( tokens[1].c_str() ) );
333 push( getTerminator() );
337 else if (command == "quit")
343 else if ( command == "data" )
347 else if ( command == "prompt" )
354 Valid commands are:\r\n\
356 cd <dir> cd to a directory, '..' to move back\r\n\
357 data switch to raw data mode\r\n\
358 dump dump current state (in xml)\r\n\
359 get <var> show the value of a parameter\r\n\
360 help show this help message\r\n\
361 ls [<dir>] list directory\r\n\
362 prompt switch to interactive mode (default)\r\n\
363 pwd display your current path\r\n\
364 quit terminate connection\r\n\
365 set <var> <val> set <var> to a new <val>\r\n\
366 show <var> synonym for get\r\n";
374 string prompt = node->getPath();
380 push( prompt.c_str() );
389 FGTelnet::FGTelnet( const vector<string>& tokens )
393 // props,medium,direction,hz,hostname,port#,style
394 if (tokens.size() == 2)
395 port = atoi( tokens[1].c_str() );
396 else if (tokens.size() == 7)
397 port = atoi( tokens[5].c_str() );
399 throw FGProtocolConfigError( "FGTelnet: incorrect number of configuration arguments" );
405 FGTelnet::~FGTelnet()
417 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
418 << "is already in use, ignoring" );
423 netChannel::bind( "", port );
424 netChannel::listen( 5 );
425 SG_LOG( SG_IO, SG_INFO, "Telnet server started on port " << port );
427 set_hz( 5 ); // default to processing requests @ 5Hz
455 FGTelnet::handleAccept()
458 int handle = accept( &addr );
459 SG_LOG( SG_IO, SG_INFO, "Telnet server accepted connection from "
460 << addr.getHost() << ":" << addr.getPort() );
461 TelnetChannel* channel = new TelnetChannel();
462 channel->setHandle( handle );