]> git.mxchange.org Git - flightgear.git/blob - src/Network/props.cxx
Fix some property names.
[flightgear.git] / src / Network / props.cxx
1 // \file props.cxx
2 // Property server class.
3 //
4 // Written by Curtis Olson, started September 2000.
5 // Modified by Bernie Bright, May 2002.
6 //
7 // Copyright (C) 2000  Curtis L. Olson - http://www.flightgear.org/~curt
8 //
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.
13 //
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.
18 //
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.
22 //
23 // $Id$
24
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
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>
36
37 #include <sstream>
38
39 #include <Main/globals.hxx>
40 #include <Main/viewmgr.hxx>
41
42 #include <plib/netChat.h>
43
44 #include "props.hxx"
45
46 SG_USING_STD(stringstream);
47 SG_USING_STD(ends);
48
49 /**
50  * Props connection class.
51  * This class represents a connection to props client.
52  */
53 class PropsChannel : public netChat
54 {
55     netBuffer buffer;
56
57     /**
58      * Current property node name.
59      */
60     string path;
61
62     enum Mode {
63         PROMPT,
64         DATA
65     };
66     Mode mode;
67
68 public:
69     /**
70      * Constructor.
71      */
72     PropsChannel();
73     
74     /**
75      * Append incoming data to our request buffer.
76      *
77      * @param s Character string to append to buffer
78      * @param n Number of characters to append.
79      */
80     void collectIncomingData( const char* s, int n );
81
82     /**
83      * Process a complete request from the props client.
84      */
85     void foundTerminator();
86
87 private:
88     /**
89      * Return a "Node no found" error message to the client.
90      */
91     void node_not_found_error( const string& node_name );
92 };
93
94 /**
95  * 
96  */
97 PropsChannel::PropsChannel()
98     : buffer(512),
99       path("/"),
100       mode(PROMPT)
101 {
102     setTerminator( "\r\n" );
103 }
104
105 /**
106  * 
107  */
108 void
109 PropsChannel::collectIncomingData( const char* s, int n )
110 {
111     buffer.append( s, n );
112 }
113
114 /**
115  * 
116  */
117 void
118 PropsChannel::node_not_found_error( const string& node_name )
119 {
120     string error = "-ERR Node \"";
121     error += node_name;
122     error += "\" not found.";
123     push( error.c_str() );
124     push( getTerminator() );
125 }
126
127 // return a human readable form of the value "type"
128 static string
129 getValueTypeString( const SGPropertyNode *node )
130 {
131     string result;
132
133     if ( node == NULL )
134     {
135         return "unknown";
136     }
137
138     SGPropertyNode::Type type = node->getType();
139     if ( type == SGPropertyNode::UNSPECIFIED ) {
140         result = "unspecified";
141     } else if ( type == SGPropertyNode::NONE ) {
142         result = "none";
143     } else if ( type == SGPropertyNode::BOOL ) {
144         result = "bool";
145     } else if ( type == SGPropertyNode::INT ) {
146         result = "int";
147     } else if ( type == SGPropertyNode::LONG ) {
148         result = "long";
149     } else if ( type == SGPropertyNode::FLOAT ) {
150         result = "float";
151     } else if ( type == SGPropertyNode::DOUBLE ) {
152         result = "double";
153     } else if ( type == SGPropertyNode::STRING ) {
154         result = "string";
155     }
156
157     return result;
158 }
159
160 /**
161  * We have a command.
162  * 
163  */
164 void
165 PropsChannel::foundTerminator()
166 {
167     const char* cmd = buffer.getData();
168     SG_LOG( SG_IO, SG_INFO, "processing command = \"" << cmd << "\"" );
169
170     vector<string> tokens = simgear::strutils::split( cmd );
171
172     SGPropertyNode* node = globals->get_props()->getNode( path.c_str() );
173
174     if (!tokens.empty()) {
175         string command = tokens[0];
176
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() );
182                 } else {
183                     string s = path;
184                     s += "/";
185                     s += tokens[1];
186                     dir = globals->get_props()->getNode( s.c_str() );
187                 }
188
189                 if (dir == 0) {
190                     node_not_found_error( tokens[1] );
191                     goto prompt;
192                 }
193             }
194
195             for (int i = 0; i < dir->nChildren(); i++) {
196                 SGPropertyNode * child = dir->getChild(i);
197                 string line = child->getDisplayName(true);
198
199                 if ( child->nChildren() > 0 ) {
200                     line += "/";
201                 } else {
202                     if (mode == PROMPT) {
203                         string value = child->getStringValue();
204                         line += " =\t'" + value + "'\t(";
205                         line += getValueTypeString( child );
206                         line += ")";
207                     }
208                 }
209
210                 line += getTerminator();
211                 push( line.c_str() );
212             }
213         } else if ( command == "dump" ) {
214             stringstream buf;
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() );
220             } else {
221                 SGPropertyNode *child = node->getNode( tokens[1].c_str() );
222                 if ( child ) {
223                     writeProperties ( buf, child );
224                     buf << ends; // null terminate the string
225                     push( buf.str().c_str() );
226                     push( getTerminator() );
227                 } else {
228                     node_not_found_error( tokens[1] );
229                 }
230             }
231         }
232         else if ( command == "cd" ) {
233             if (tokens.size() == 2) {
234                 try {
235                     SGPropertyNode* child = node->getNode( tokens[1].c_str() );
236                     if ( child ) {
237                         node = child;
238                         path = node->getPath();
239                     } else {
240                         node_not_found_error( tokens[1] );
241                     }
242                 } catch (...) {
243                     // Ignore attempt to move past root node with ".."
244                 }
245             }
246         } else if ( command == "pwd" ) {
247             string ttt = node->getPath();
248             if (ttt.empty()) {
249                 ttt = "/";
250             }
251
252             push( ttt.c_str() );
253             push( getTerminator() );
254         } else if ( command == "get" || command == "show" ) {
255             if ( tokens.size() == 2 ) {
256                 string tmp;     
257                 string value = node->getStringValue ( tokens[1].c_str(), "" );
258                 if ( mode == PROMPT ) {
259                     tmp = tokens[1];
260                     tmp += " = '";
261                     tmp += value;
262                     tmp += "' (";
263                     tmp += getValueTypeString(
264                                      node->getNode( tokens[1].c_str() ) );
265                     tmp += ")";
266                 } else {
267                     tmp = value;
268                 }
269                 push( tmp.c_str() );
270                 push( getTerminator() );
271             }
272         } else if ( command == "set" ) {
273             if ( tokens.size() >= 2 ) {
274                 string value, tmp;
275                 if ( tokens.size() == 3 ) {
276                     value = tokens[2];
277                 } else {
278                     value = "";
279                 }
280                 node->getNode( tokens[1].c_str(), true )
281                     ->setStringValue(value.c_str());
282
283                 if ( mode == PROMPT ) {
284                     // now fetch and write out the new value as confirmation
285                     // of the change
286                     value = node->getStringValue ( tokens[1].c_str(), "" );
287                     tmp = tokens[1] + " = '" + value + "' (";
288                     tmp += getValueTypeString( node->getNode( tokens[1].c_str() ) );
289                     tmp += ")";
290                     push( tmp.c_str() );
291                     push( getTerminator() );
292                 }
293             } 
294         } else if ( command == "reinit" ) {
295             if ( tokens.size() == 2 ) {
296                 string tmp;     
297                 SGPropertyNode args;
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() );
302                 }
303                 if ( !globals->get_commands()
304                          ->execute( "reinit", &args) )
305                 {
306                     SG_LOG( SG_GENERAL, SG_ALERT,
307                             "Command " << tokens[1] << " failed.");
308                     if ( mode == PROMPT ) {
309                         tmp += "*failed*";
310                         push( tmp.c_str() );
311                         push( getTerminator() );
312                     }
313                 } else {
314                     if ( mode == PROMPT ) {
315                         tmp += "<completed>";
316                         push( tmp.c_str() );
317                         push( getTerminator() );
318                     }
319                 }
320             }
321         } else if ( command == "run" ) {
322             string tmp; 
323             if ( tokens.size() >= 2 ) {
324                 SGPropertyNode args;
325                 if ( tokens[1] == "reinit" ) {
326                     for ( unsigned int i = 2; i < tokens.size(); ++i ) {
327                         cout << "props: adding subsystem = " << tokens[i]
328                              << endl;
329                         SGPropertyNode *node
330                             = args.getNode("subsystem", i-2, true);
331                         node->setStringValue( tokens[i].c_str() );
332                     }
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]
336                              << endl;
337                         SGPropertyNode *node
338                             = args.getNode("temp-degc", i-2, true);
339                         node->setStringValue( tokens[i].c_str() );
340                     }
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]
344                              << endl;
345                         SGPropertyNode *node
346                             = args.getNode("temp-degc", i-2, true);
347                         node->setStringValue( tokens[i].c_str() );
348                     }
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]
352                              << endl;
353                         SGPropertyNode *node
354                             = args.getNode("timeofday", i-2, true);
355                         node->setStringValue( tokens[i].c_str() );
356                     }
357                 }
358                 if ( !globals->get_commands()
359                          ->execute(tokens[1].c_str(), &args) )
360                 {
361                     SG_LOG( SG_GENERAL, SG_ALERT,
362                             "Command " << tokens[1] << " failed.");
363                     if ( mode == PROMPT ) {
364                         tmp += "*failed*";
365                         push( tmp.c_str() );
366                         push( getTerminator() );
367                     }
368                 } else {
369                     if ( mode == PROMPT ) {
370                         tmp += "<completed>";
371                         push( tmp.c_str() );
372                         push( getTerminator() );
373                     }
374                 }
375             } else {
376                 if ( mode == PROMPT ) {
377                     tmp += "no command specified";
378                     push( tmp.c_str() );
379                     push( getTerminator() );
380                 }
381             }
382         } else if (command == "quit") {
383             close();
384             shouldDelete();
385             return;
386         } else if ( command == "data" ) {
387             mode = DATA;
388         } else if ( command == "prompt" ) {
389             mode = PROMPT;
390         } else {
391             const char* msg = "\
392 Valid commands are:\r\n\
393 \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";
406             push( msg );
407         }
408     }
409
410  prompt:
411     if (mode == PROMPT) {
412         string prompt = node->getPath();
413         if (prompt.empty()) {
414             prompt = "/";
415         }
416         prompt += "> ";
417         push( prompt.c_str() );
418     }
419
420     buffer.remove();
421 }
422
423 /**
424  * 
425  */
426 FGProps::FGProps( const vector<string>& tokens )
427 {
428     // tokens:
429     //   props,port#
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) {
435         char* endptr;
436         errno = 0;
437         int hz = strtol( tokens[3].c_str(), &endptr, 10 );
438         if (errno != 0) {
439            SG_LOG( SG_IO, SG_ALERT, "I/O poll frequency out of range" );
440            set_hz( 5 );           // default to processing requests @ 5Hz
441         } else {
442             SG_LOG( SG_IO, SG_INFO, "Setting I/O poll frequency to "
443                     << hz << " Hz");
444             set_hz( hz );
445         }
446         port = atoi( tokens[5].c_str() );
447     } else {
448         throw FGProtocolConfigError( "FGProps: incorrect number of configuration arguments" );
449     }
450 }
451
452 /**
453  * 
454  */
455 FGProps::~FGProps()
456 {
457 }
458
459 /**
460  * 
461  */
462 bool
463 FGProps::open()
464 {
465     if ( is_enabled() )
466     {
467         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
468                 << "is already in use, ignoring" );
469         return false;
470     }
471
472     netChannel::open();
473     netChannel::bind( "", port );
474     netChannel::listen( 5 );
475     SG_LOG( SG_IO, SG_INFO, "Props server started on port " << port );
476
477     set_enabled( true );
478     return true;
479 }
480
481 /**
482  * 
483  */
484 bool
485 FGProps::close()
486 {
487     SG_LOG( SG_IO, SG_INFO, "closing FGProps" );   
488     return true;
489 }
490
491 /**
492  * 
493  */
494 bool
495 FGProps::process()
496 {
497     netChannel::poll();
498     return true;
499 }
500
501 /**
502  * 
503  */
504 void
505 FGProps::handleAccept()
506 {
507     netAddress addr;
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 );
513 }