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