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 - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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>
40 #include <Main/globals.hxx>
41 #include <Main/viewmgr.hxx>
43 #include <plib/netChat.h>
47 using std::stringstream;
54 * Props connection class.
55 * This class represents a connection to props client.
57 class PropsChannel : public netChat
62 * Current property node name.
79 * Append incoming data to our request buffer.
81 * @param s Character string to append to buffer
82 * @param n Number of characters to append.
84 void collectIncomingData( const char* s, int n );
87 * Process a complete request from the props client.
89 void foundTerminator();
92 inline void node_not_found_error( const string& s ) const {
93 throw "node '" + s + "' not found";
100 PropsChannel::PropsChannel()
105 setTerminator( "\r\n" );
112 PropsChannel::collectIncomingData( const char* s, int n )
114 buffer.append( s, n );
117 // return a human readable form of the value "type"
119 getValueTypeString( const SGPropertyNode *node )
121 using namespace simgear;
130 props::Type type = node->getType();
131 if ( type == props::UNSPECIFIED ) {
132 result = "unspecified";
133 } else if ( type == props::NONE ) {
135 } else if ( type == props::BOOL ) {
137 } else if ( type == props::INT ) {
139 } else if ( type == props::LONG ) {
141 } else if ( type == props::FLOAT ) {
143 } else if ( type == props::DOUBLE ) {
145 } else if ( type == props::STRING ) {
157 PropsChannel::foundTerminator()
159 const char* cmd = buffer.getData();
160 SG_LOG( SG_IO, SG_INFO, "processing command = \"" << cmd << "\"" );
162 vector<string> tokens = simgear::strutils::split( cmd );
164 SGPropertyNode* node = globals->get_props()->getNode( path.c_str() );
167 if (!tokens.empty()) {
168 string command = tokens[0];
170 if (command == "ls") {
171 SGPropertyNode* dir = node;
172 if (tokens.size() == 2) {
173 if (tokens[1][0] == '/') {
174 dir = globals->get_props()->getNode( tokens[1].c_str() );
179 dir = globals->get_props()->getNode( s.c_str() );
183 node_not_found_error( tokens[1] );
187 for (int i = 0; i < dir->nChildren(); i++) {
188 SGPropertyNode * child = dir->getChild(i);
189 string line = child->getDisplayName(true);
191 if ( child->nChildren() > 0 ) {
194 if (mode == PROMPT) {
195 string value = child->getStringValue();
196 line += " =\t'" + value + "'\t(";
197 line += getValueTypeString( child );
202 line += getTerminator();
203 push( line.c_str() );
205 } else if ( command == "dump" ) {
207 if ( tokens.size() <= 1 ) {
208 writeProperties( buf, node );
209 buf << ends; // null terminate the string
210 push( buf.str().c_str() );
211 push( getTerminator() );
213 SGPropertyNode *child = node->getNode( tokens[1].c_str() );
215 writeProperties ( buf, child );
216 buf << ends; // null terminate the string
217 push( buf.str().c_str() );
218 push( getTerminator() );
220 node_not_found_error( tokens[1] );
224 else if ( command == "cd" ) {
225 if (tokens.size() == 2) {
226 SGPropertyNode* child = node->getNode( tokens[1].c_str() );
229 path = node->getPath();
231 node_not_found_error( tokens[1] );
234 } else if ( command == "pwd" ) {
235 string pwd = node->getPath();
241 push( getTerminator() );
242 } else if ( command == "get" || command == "show" ) {
243 if ( tokens.size() == 2 ) {
245 string value = node->getStringValue ( tokens[1].c_str(), "" );
246 if ( mode == PROMPT ) {
251 tmp += getValueTypeString(
252 node->getNode( tokens[1].c_str() ) );
258 push( getTerminator() );
260 } else if ( command == "set" ) {
261 if ( tokens.size() >= 2 ) {
263 for (unsigned int i = 2; i < tokens.size(); i++) {
268 node->getNode( tokens[1].c_str(), true )
269 ->setStringValue(value.c_str());
271 if ( mode == PROMPT ) {
272 // now fetch and write out the new value as confirmation
274 value = node->getStringValue ( tokens[1].c_str(), "" );
275 tmp = tokens[1] + " = '" + value + "' (";
276 tmp += getValueTypeString( node->getNode( tokens[1].c_str() ) );
279 push( getTerminator() );
282 } else if ( command == "reinit" ) {
283 if ( tokens.size() == 2 ) {
286 for ( unsigned int i = 1; i < tokens.size(); ++i ) {
287 cout << "props: adding subsystem = " << tokens[i] << endl;
288 SGPropertyNode *node = args.getNode("subsystem", i-1, true);
289 node->setStringValue( tokens[i].c_str() );
291 if ( !globals->get_commands()
292 ->execute( "reinit", &args) )
294 SG_LOG( SG_GENERAL, SG_ALERT,
295 "Command " << tokens[1] << " failed.");
296 if ( mode == PROMPT ) {
299 push( getTerminator() );
302 if ( mode == PROMPT ) {
303 tmp += "<completed>";
305 push( getTerminator() );
309 } else if ( command == "run" ) {
311 if ( tokens.size() >= 2 ) {
313 if ( tokens[1] == "reinit" ) {
314 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
315 cout << "props: adding subsystem = " << tokens[i]
318 = args.getNode("subsystem", i-2, true);
319 node->setStringValue( tokens[i].c_str() );
321 } else if ( tokens[1] == "set-sea-level-air-temp-degc" ) {
322 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
323 cout << "props: set-sl command = " << tokens[i]
326 = args.getNode("temp-degc", i-2, true);
327 node->setStringValue( tokens[i].c_str() );
329 } else if ( tokens[1] == "set-outside-air-temp-degc" ) {
330 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
331 cout << "props: set-oat command = " << tokens[i]
334 = args.getNode("temp-degc", i-2, true);
335 node->setStringValue( tokens[i].c_str() );
337 } else if ( tokens[1] == "timeofday" ) {
338 for ( unsigned int i = 2; i < tokens.size(); ++i ) {
339 cout << "props: time of day command = " << tokens[i]
342 = args.getNode("timeofday", i-2, true);
343 node->setStringValue( tokens[i].c_str() );
345 } else if ( tokens[1] == "play-audio-message" ) {
346 if ( tokens.size() == 4 ) {
347 cout << "props: play audio message = " << tokens[2]
348 << " " << tokens[3] << endl;
349 SGPropertyNode *node;
350 node = args.getNode("path", 0, true);
351 node->setStringValue( tokens[2].c_str() );
352 node = args.getNode("file", 0, true);
353 node->setStringValue( tokens[3].c_str() );
356 if ( !globals->get_commands()
357 ->execute(tokens[1].c_str(), &args) )
359 SG_LOG( SG_GENERAL, SG_ALERT,
360 "Command " << tokens[1] << " failed.");
361 if ( mode == PROMPT ) {
364 push( getTerminator() );
367 if ( mode == PROMPT ) {
368 tmp += "<completed>";
370 push( getTerminator() );
374 if ( mode == PROMPT ) {
375 tmp += "no command specified";
377 push( getTerminator() );
380 } else if ( command == "quit" || command == "exit" ) {
384 } else if ( command == "data" ) {
386 } else if ( command == "prompt" ) {
390 Valid commands are:\r\n\
392 cd <dir> cd to a directory, '..' to move back\r\n\
393 data switch to raw data mode\r\n\
394 dump dump current state (in xml)\r\n\
395 get <var> show the value of a parameter\r\n\
396 help show this help message\r\n\
397 ls [<dir>] list directory\r\n\
398 prompt switch to interactive mode (default)\r\n\
399 pwd display your current path\r\n\
400 quit terminate connection\r\n\
401 run <command> run built in command\r\n\
402 set <var> <val> set <var> to a new <val>\r\n";
407 } catch ( const string& msg ) {
408 string error = "-ERR \"" + msg + "\"";
409 push( error.c_str() );
410 push( getTerminator() );
413 if ( mode == PROMPT ) {
414 string prompt = node->getPath();
415 if (prompt.empty()) {
419 push( prompt.c_str() );
428 FGProps::FGProps( const vector<string>& tokens )
432 // props,medium,direction,hz,hostname,port#,style
433 if (tokens.size() == 2) {
434 port = atoi( tokens[1].c_str() );
435 set_hz( 5 ); // default to processing requests @ 5Hz
436 } else if (tokens.size() == 7) {
439 int hz = strtol( tokens[3].c_str(), &endptr, 10 );
441 SG_LOG( SG_IO, SG_ALERT, "I/O poll frequency out of range" );
442 set_hz( 5 ); // default to processing requests @ 5Hz
444 SG_LOG( SG_IO, SG_INFO, "Setting I/O poll frequency to "
448 port = atoi( tokens[5].c_str() );
450 throw FGProtocolConfigError( "FGProps: incorrect number of configuration arguments" );
469 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
470 << "is already in use, ignoring" );
475 netChannel::bind( "", port );
476 netChannel::listen( 5 );
477 SG_LOG( SG_IO, SG_INFO, "Props server started on port " << port );
489 SG_LOG( SG_IO, SG_INFO, "closing FGProps" );
507 FGProps::handleAccept()
510 int handle = accept( &addr );
511 SG_LOG( SG_IO, SG_INFO, "Props server accepted connection from "
512 << addr.getHost() << ":" << addr.getPort() );
513 PropsChannel* channel = new PropsChannel();
514 channel->setHandle( handle );