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/structure/commands.hxx>
33 #include <simgear/misc/strutils.hxx>
34 #include <simgear/props/props.hxx>
35 #include <simgear/props/props_io.hxx>
37 #include STL_STRSTREAM
39 #include <Main/globals.hxx>
40 #include <Main/viewmgr.hxx>
42 #include <plib/netChat.h>
46 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
47 SG_USING_STD(strstream);
52 * Props connection class.
53 * This class represents a connection to props client.
55 class PropsChannel : public netChat
60 * Current property node name.
77 * Append incoming data to our request buffer.
79 * @param s Character string to append to buffer
80 * @param n Number of characters to append.
82 void collectIncomingData( const char* s, int n );
85 * Process a complete request from the props client.
87 void foundTerminator();
91 * Return a "Node no found" error message to the client.
93 void node_not_found_error( const string& node_name );
99 PropsChannel::PropsChannel()
104 setTerminator( "\r\n" );
111 PropsChannel::collectIncomingData( const char* s, int n )
113 buffer.append( s, n );
120 PropsChannel::node_not_found_error( const string& node_name )
122 string error = "-ERR Node \"";
124 error += "\" not found.";
125 push( error.c_str() );
126 push( getTerminator() );
129 // return a human readable form of the value "type"
131 getValueTypeString( const SGPropertyNode *node )
140 SGPropertyNode::Type type = node->getType();
141 if ( type == SGPropertyNode::UNSPECIFIED ) {
142 result = "unspecified";
143 } else if ( type == SGPropertyNode::NONE ) {
145 } else if ( type == SGPropertyNode::BOOL ) {
147 } else if ( type == SGPropertyNode::INT ) {
149 } else if ( type == SGPropertyNode::LONG ) {
151 } else if ( type == SGPropertyNode::FLOAT ) {
153 } else if ( type == SGPropertyNode::DOUBLE ) {
155 } else if ( type == SGPropertyNode::STRING ) {
167 PropsChannel::foundTerminator()
169 const char* cmd = buffer.getData();
170 SG_LOG( SG_IO, SG_INFO, "processing command = \"" << cmd << "\"" );
172 vector<string> tokens = simgear::strutils::split( cmd );
174 SGPropertyNode* node = globals->get_props()->getNode( path.c_str() );
176 if (!tokens.empty()) {
177 string command = tokens[0];
179 if (command == "ls") {
180 SGPropertyNode* dir = node;
181 if (tokens.size() == 2) {
182 if (tokens[1][0] == '/') {
183 dir = globals->get_props()->getNode( tokens[1].c_str() );
188 dir = globals->get_props()->getNode( s.c_str() );
192 node_not_found_error( tokens[1] );
197 for (int i = 0; i < dir->nChildren(); i++) {
198 SGPropertyNode * child = dir->getChild(i);
199 string line = child->getDisplayName(true);
201 if ( child->nChildren() > 0 ) {
204 if (mode == PROMPT) {
205 string value = child->getStringValue();
206 line += " =\t'" + value + "'\t(";
207 line += getValueTypeString( child );
212 line += getTerminator();
213 push( line.c_str() );
215 } else if ( command == "dump" ) {
217 if ( tokens.size() <= 1 ) {
218 writeProperties( buf, node );
219 buf << ends; // null terminate the string
221 push( getTerminator() );
223 SGPropertyNode *child = node->getNode( tokens[1].c_str() );
225 writeProperties ( buf, child );
226 buf << ends; // null terminate the string
228 push( getTerminator() );
230 node_not_found_error( tokens[1] );
234 else if ( command == "cd" ) {
235 if (tokens.size() == 2) {
237 SGPropertyNode* child = node->getNode( tokens[1].c_str() );
240 path = node->getPath();
242 node_not_found_error( tokens[1] );
245 // Ignore attempt to move past root node with ".."
248 } else if ( command == "pwd" ) {
249 string ttt = node->getPath();
255 push( getTerminator() );
256 } else if ( command == "get" || command == "show" ) {
257 if ( tokens.size() == 2 ) {
259 string value = node->getStringValue ( tokens[1].c_str(), "" );
260 if ( mode == PROMPT ) {
265 tmp += getValueTypeString(
266 node->getNode( tokens[1].c_str() ) );
272 push( getTerminator() );
274 } else if ( command == "set" ) {
275 if ( tokens.size() >= 2 ) {
277 if ( tokens.size() == 3 ) {
282 node->getNode( tokens[1].c_str(), true )
283 ->setStringValue(value.c_str());
285 if ( mode == PROMPT ) {
286 // now fetch and write out the new value as confirmation
288 value = node->getStringValue ( tokens[1].c_str(), "" );
289 tmp = tokens[1] + " = '" + value + "' (";
290 tmp += getValueTypeString( node->getNode( tokens[1].c_str() ) );
293 push( getTerminator() );
296 } else if ( command == "reinit" ) {
297 if ( tokens.size() == 2 ) {
300 for ( unsigned int i = 1; i < tokens.size(); ++i ) {
301 cout << "props: adding subsystem = " << tokens[i] << endl;
302 SGPropertyNode *node = args.getNode("subsystem", i-1, true);
303 node->setStringValue( tokens[i].c_str() );
305 if ( !globals->get_commands()
306 ->execute( "reinit", &args) )
308 SG_LOG( SG_GENERAL, SG_ALERT,
309 "Command " << tokens[1] << " failed.");
310 if ( mode == PROMPT ) {
313 push( getTerminator() );
316 if ( mode == PROMPT ) {
317 tmp += "<completed>";
319 push( getTerminator() );
323 } else if ( command == "run" ) {
325 if ( tokens.size() >= 2 ) {
327 if ( tokens[1] == "reinit" ) {
328 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
329 cout << "props: adding subsystem = " << tokens[i]
332 = args.getNode("subsystem", i-2, true);
333 node->setStringValue( tokens[i].c_str() );
335 } else if ( tokens[1] == "set-outside-air-temp-degc" ) {
336 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
337 cout << "props: set-oat command = " << tokens[i]
340 = args.getNode("temp-degc", i-2, true);
341 node->setStringValue( tokens[i].c_str() );
343 } else if ( tokens[1] == "timeofday" ) {
344 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
345 cout << "props: time of day command = " << tokens[i]
348 = args.getNode("timeofday", i-2, true);
349 node->setStringValue( tokens[i].c_str() );
352 if ( !globals->get_commands()
353 ->execute(tokens[1].c_str(), &args) )
355 SG_LOG( SG_GENERAL, SG_ALERT,
356 "Command " << tokens[1] << " failed.");
357 if ( mode == PROMPT ) {
360 push( getTerminator() );
363 if ( mode == PROMPT ) {
364 tmp += "<completed>";
366 push( getTerminator() );
370 if ( mode == PROMPT ) {
371 tmp += "no command specified";
373 push( getTerminator() );
376 } else if (command == "quit") {
380 } else if ( command == "data" ) {
382 } else if ( command == "prompt" ) {
386 Valid commands are:\r\n\
388 cd <dir> cd to a directory, '..' to move back\r\n\
389 data switch to raw data mode\r\n\
390 dump dump current state (in xml)\r\n\
391 get <var> show the value of a parameter\r\n\
392 help show this help message\r\n\
393 ls [<dir>] list directory\r\n\
394 prompt switch to interactive mode (default)\r\n\
395 pwd display your current path\r\n\
396 quit terminate connection\r\n\
397 run <command> run built in command\r\n\
398 set <var> <val> set <var> to a new <val>\r\n\
399 show <var> synonym for get\r\n";
405 if (mode == PROMPT) {
406 string prompt = node->getPath();
407 if (prompt.empty()) {
411 push( prompt.c_str() );
420 FGProps::FGProps( const vector<string>& tokens )
424 // props,medium,direction,hz,hostname,port#,style
425 if (tokens.size() == 2) {
426 port = atoi( tokens[1].c_str() );
427 set_hz( 5 ); // default to processing requests @ 5Hz
428 } else if (tokens.size() == 7) {
431 int hz = strtol( tokens[3].c_str(), &endptr, 10 );
433 SG_LOG( SG_IO, SG_ALERT, "I/O poll frequency out of range" );
434 set_hz( 5 ); // default to processing requests @ 5Hz
436 SG_LOG( SG_IO, SG_INFO, "Setting I/O poll frequency to "
440 port = atoi( tokens[5].c_str() );
442 throw FGProtocolConfigError( "FGProps: incorrect number of configuration arguments" );
461 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
462 << "is already in use, ignoring" );
467 netChannel::bind( "", port );
468 netChannel::listen( 5 );
469 SG_LOG( SG_IO, SG_INFO, "Props server started on port " << port );
498 FGProps::handleAccept()
501 int handle = accept( &addr );
502 SG_LOG( SG_IO, SG_INFO, "Props server accepted connection from "
503 << addr.getHost() << ":" << addr.getPort() );
504 PropsChannel* channel = new PropsChannel();
505 channel->setHandle( handle );