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>
39 #include <Main/globals.hxx>
40 #include <Main/viewmgr.hxx>
42 #include <plib/netChat.h>
46 SG_USING_STD(stringstream);
50 * Props connection class.
51 * This class represents a connection to props client.
53 class PropsChannel : public netChat
58 * Current property node name.
75 * Append incoming data to our request buffer.
77 * @param s Character string to append to buffer
78 * @param n Number of characters to append.
80 void collectIncomingData( const char* s, int n );
83 * Process a complete request from the props client.
85 void foundTerminator();
89 * Return a "Node no found" error message to the client.
91 void node_not_found_error( const string& node_name );
97 PropsChannel::PropsChannel()
102 setTerminator( "\r\n" );
109 PropsChannel::collectIncomingData( const char* s, int n )
111 buffer.append( s, n );
118 PropsChannel::node_not_found_error( const string& node_name )
120 string error = "-ERR Node \"";
122 error += "\" not found.";
123 push( error.c_str() );
124 push( getTerminator() );
127 // return a human readable form of the value "type"
129 getValueTypeString( const SGPropertyNode *node )
138 SGPropertyNode::Type type = node->getType();
139 if ( type == SGPropertyNode::UNSPECIFIED ) {
140 result = "unspecified";
141 } else if ( type == SGPropertyNode::NONE ) {
143 } else if ( type == SGPropertyNode::BOOL ) {
145 } else if ( type == SGPropertyNode::INT ) {
147 } else if ( type == SGPropertyNode::LONG ) {
149 } else if ( type == SGPropertyNode::FLOAT ) {
151 } else if ( type == SGPropertyNode::DOUBLE ) {
153 } else if ( type == SGPropertyNode::STRING ) {
165 PropsChannel::foundTerminator()
167 const char* cmd = buffer.getData();
168 SG_LOG( SG_IO, SG_INFO, "processing command = \"" << cmd << "\"" );
170 vector<string> tokens = simgear::strutils::split( cmd );
172 SGPropertyNode* node = globals->get_props()->getNode( path.c_str() );
174 if (!tokens.empty()) {
175 string command = tokens[0];
177 if (command == "ls") {
178 SGPropertyNode* dir = node;
179 if (tokens.size() == 2) {
180 if (tokens[1][0] == '/') {
181 dir = globals->get_props()->getNode( tokens[1].c_str() );
186 dir = globals->get_props()->getNode( s.c_str() );
190 node_not_found_error( tokens[1] );
195 for (int i = 0; i < dir->nChildren(); i++) {
196 SGPropertyNode * child = dir->getChild(i);
197 string line = child->getDisplayName(true);
199 if ( child->nChildren() > 0 ) {
202 if (mode == PROMPT) {
203 string value = child->getStringValue();
204 line += " =\t'" + value + "'\t(";
205 line += getValueTypeString( child );
210 line += getTerminator();
211 push( line.c_str() );
213 } else if ( command == "dump" ) {
215 if ( tokens.size() <= 1 ) {
216 writeProperties( buf, node );
217 buf << ends; // null terminate the string
218 push( buf.str().c_str() );
219 push( getTerminator() );
221 SGPropertyNode *child = node->getNode( tokens[1].c_str() );
223 writeProperties ( buf, child );
224 buf << ends; // null terminate the string
225 push( buf.str().c_str() );
226 push( getTerminator() );
228 node_not_found_error( tokens[1] );
232 else if ( command == "cd" ) {
233 if (tokens.size() == 2) {
235 SGPropertyNode* child = node->getNode( tokens[1].c_str() );
238 path = node->getPath();
240 node_not_found_error( tokens[1] );
243 // Ignore attempt to move past root node with ".."
246 } else if ( command == "pwd" ) {
247 string ttt = node->getPath();
253 push( getTerminator() );
254 } else if ( command == "get" || command == "show" ) {
255 if ( tokens.size() == 2 ) {
257 string value = node->getStringValue ( tokens[1].c_str(), "" );
258 if ( mode == PROMPT ) {
263 tmp += getValueTypeString(
264 node->getNode( tokens[1].c_str() ) );
270 push( getTerminator() );
272 } else if ( command == "set" ) {
273 if ( tokens.size() >= 2 ) {
275 if ( tokens.size() == 3 ) {
280 node->getNode( tokens[1].c_str(), true )
281 ->setStringValue(value.c_str());
283 if ( mode == PROMPT ) {
284 // now fetch and write out the new value as confirmation
286 value = node->getStringValue ( tokens[1].c_str(), "" );
287 tmp = tokens[1] + " = '" + value + "' (";
288 tmp += getValueTypeString( node->getNode( tokens[1].c_str() ) );
291 push( getTerminator() );
294 } else if ( command == "reinit" ) {
295 if ( tokens.size() == 2 ) {
298 for ( unsigned int i = 1; i < tokens.size(); ++i ) {
299 cout << "props: adding subsystem = " << tokens[i] << endl;
300 SGPropertyNode *node = args.getNode("subsystem", i-1, true);
301 node->setStringValue( tokens[i].c_str() );
303 if ( !globals->get_commands()
304 ->execute( "reinit", &args) )
306 SG_LOG( SG_GENERAL, SG_ALERT,
307 "Command " << tokens[1] << " failed.");
308 if ( mode == PROMPT ) {
311 push( getTerminator() );
314 if ( mode == PROMPT ) {
315 tmp += "<completed>";
317 push( getTerminator() );
321 } else if ( command == "run" ) {
323 if ( tokens.size() >= 2 ) {
325 if ( tokens[1] == "reinit" ) {
326 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
327 cout << "props: adding subsystem = " << tokens[i]
330 = args.getNode("subsystem", i-2, true);
331 node->setStringValue( tokens[i].c_str() );
333 } else if ( tokens[1] == "set-sea-level-air-temp-degc" ) {
334 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
335 cout << "props: set-sl command = " << tokens[i]
338 = args.getNode("temp-degc", i-2, true);
339 node->setStringValue( tokens[i].c_str() );
341 } else if ( tokens[1] == "set-outside-air-temp-degc" ) {
342 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
343 cout << "props: set-oat command = " << tokens[i]
346 = args.getNode("temp-degc", i-2, true);
347 node->setStringValue( tokens[i].c_str() );
349 } else if ( tokens[1] == "timeofday" ) {
350 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
351 cout << "props: time of day command = " << tokens[i]
354 = args.getNode("timeofday", i-2, true);
355 node->setStringValue( tokens[i].c_str() );
358 if ( !globals->get_commands()
359 ->execute(tokens[1].c_str(), &args) )
361 SG_LOG( SG_GENERAL, SG_ALERT,
362 "Command " << tokens[1] << " failed.");
363 if ( mode == PROMPT ) {
366 push( getTerminator() );
369 if ( mode == PROMPT ) {
370 tmp += "<completed>";
372 push( getTerminator() );
376 if ( mode == PROMPT ) {
377 tmp += "no command specified";
379 push( getTerminator() );
382 } else if (command == "quit") {
386 } else if ( command == "data" ) {
388 } else if ( command == "prompt" ) {
392 Valid commands are:\r\n\
394 cd <dir> cd to a directory, '..' to move back\r\n\
395 data switch to raw data mode\r\n\
396 dump dump current state (in xml)\r\n\
397 get <var> show the value of a parameter\r\n\
398 help show this help message\r\n\
399 ls [<dir>] list directory\r\n\
400 prompt switch to interactive mode (default)\r\n\
401 pwd display your current path\r\n\
402 quit terminate connection\r\n\
403 run <command> run built in command\r\n\
404 set <var> <val> set <var> to a new <val>\r\n\
405 show <var> synonym for get\r\n";
411 if (mode == PROMPT) {
412 string prompt = node->getPath();
413 if (prompt.empty()) {
417 push( prompt.c_str() );
426 FGProps::FGProps( const vector<string>& tokens )
430 // props,medium,direction,hz,hostname,port#,style
431 if (tokens.size() == 2) {
432 port = atoi( tokens[1].c_str() );
433 set_hz( 5 ); // default to processing requests @ 5Hz
434 } else if (tokens.size() == 7) {
437 int hz = strtol( tokens[3].c_str(), &endptr, 10 );
439 SG_LOG( SG_IO, SG_ALERT, "I/O poll frequency out of range" );
440 set_hz( 5 ); // default to processing requests @ 5Hz
442 SG_LOG( SG_IO, SG_INFO, "Setting I/O poll frequency to "
446 port = atoi( tokens[5].c_str() );
448 throw FGProtocolConfigError( "FGProps: incorrect number of configuration arguments" );
467 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
468 << "is already in use, ignoring" );
473 netChannel::bind( "", port );
474 netChannel::listen( 5 );
475 SG_LOG( SG_IO, SG_INFO, "Props server started on port " << port );
487 SG_LOG( SG_IO, SG_INFO, "closing FGProps" );
505 FGProps::handleAccept()
508 int handle = accept( &addr );
509 SG_LOG( SG_IO, SG_INFO, "Props server accepted connection from "
510 << addr.getHost() << ":" << addr.getPort() );
511 PropsChannel* channel = new PropsChannel();
512 channel->setHandle( handle );