]> git.mxchange.org Git - flightgear.git/blob - src/Network/props.cxx
5cdd5c0a44ed8e3728bec44096a940cedc1192e4
[flightgear.git] / src / Network / props.cxx
1 // props.hxx -- FGFS property manager interaction class
2 //
3 // Written by Curtis Olson, started September 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #include <Main/globals.hxx>
25
26 #include <simgear/compiler.h>
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/io/iochannel.hxx>
29 #include <simgear/math/sg_types.hxx>
30 #include <simgear/misc/props.hxx>
31
32 #include <stdlib.h>             // atoi() atof()
33
34 #include STL_STRSTREAM
35
36 #include "props.hxx"
37
38 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
39 SG_USING_STD(cout);
40 SG_USING_STD(istrstream);
41 #endif
42
43 FGProps::FGProps() {
44 }
45
46 FGProps::~FGProps() {
47 }
48
49
50 // open hailing frequencies
51 bool FGProps::open() {
52     reset();
53
54     if ( is_enabled() ) {
55         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
56                 << "is already in use, ignoring" );
57         return false;
58     }
59
60     SGIOChannel *io = get_io_channel();
61
62     if ( ! io->open( get_direction() ) ) {
63         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
64         return false;
65     }
66
67     set_enabled( true );
68     SG_LOG( SG_IO, SG_INFO, "Opening properties channel communication layer." );
69
70     return true;
71 }
72
73
74 // prepare for new connection
75 bool FGProps::reset() {
76     path = "/";
77     mode = PROMPT;
78     return true;
79 }
80
81
82 // return a human readable form of the value "type"
83 static string getValueTypeString( const SGPropertyNode *node ) {
84     string result;
85
86     if ( node == NULL ) {
87         return "unknown";
88     }
89
90     SGPropertyNode::Type type = node->getType();
91     if ( type == SGPropertyNode::UNSPECIFIED ) {
92         result = "unspecified";
93     } else if ( type == SGPropertyNode::NONE ) {
94         result = "none";
95     } else if ( type == SGPropertyNode::BOOL ) {
96         result = "bool";
97     } else if ( type == SGPropertyNode::INT ) {
98         result = "int";
99     } else if ( type == SGPropertyNode::LONG ) {
100         result = "long";
101     } else if ( type == SGPropertyNode::FLOAT ) {
102         result = "float";
103     } else if ( type == SGPropertyNode::DOUBLE ) {
104         result = "double";
105     } else if ( type == SGPropertyNode::STRING ) {
106         result = "string";
107     }
108
109     return result;
110 }
111
112
113 bool FGProps::process_command( const char *cmd ) {
114     SGIOChannel *io = get_io_channel();
115
116     cout << "processing command = " << cmd;
117     string_list tokens;
118     tokens.clear();
119
120     istrstream in(cmd);
121     
122     while ( !in.eof() ) {
123         string token;
124         in >> token;
125         tokens.push_back( token );
126     }
127
128     string command = tokens[0];
129
130     SGPropertyNode * node = globals->get_props()->getNode(path);
131
132     if ( command == "ls" ) {
133         SGPropertyNode * dir = node;
134         if ( tokens.size() > 2 ) {
135             if ( tokens[1][0] == '/' ) {
136                 dir = globals->get_props()->getNode(tokens[1]);
137             } else {
138                 dir = globals->get_props()->getNode(path + "/" + tokens[1]);
139             }
140             if ( dir == 0 ) {
141                 tokens[1] = "ERR Node \"" + tokens[1] + "\" not found.\n";
142                 io->writestring( tokens[1].c_str() );
143                 return true;
144             }
145         }
146         
147         for (int i = 0; i < (int)dir->nChildren(); i++) {
148             SGPropertyNode * child = dir->getChild(i);
149             string name = child->getName();
150             string line = name;
151             if ( dir->getChild(name, 1) ) {
152                 char buf[16];
153                 sprintf(buf, "[%d]", child->getIndex());
154                 line += buf;
155             }
156             if ( child->nChildren() > 0 ) {
157                 line += "/";
158             } else {
159                 if ( mode == PROMPT ) {
160                     string value = dir->getStringValue ( name, "" );
161                     line += " =\t'" + value + "'\t(";
162                     line += getValueTypeString( dir->getNode( name ) );
163                     line += ")";
164                 }
165             }
166             line += "\n";
167             io->writestring( line.c_str() );
168         }
169     } else if ( command == "dump" ) {
170         strstream buf;
171         if ( tokens.size() <= 1 ) {
172             writeProperties ( buf, node);
173             io->writestring( buf.str() );
174         }
175         else {
176             SGPropertyNode *child = node->getNode(tokens[1]);
177             if ( child ) {
178                 writeProperties ( buf, child );
179                 io->writestring( buf.str() );
180             } else {
181                 tokens[1] = "ERR Node \"" + tokens[1] + "\" not found.\n";
182                 io->writestring( tokens[1].c_str() );
183             }
184         }
185     } else if ( command == "cd" ) {
186         // string tmp = "current path = " + node.getPath() + "\n";
187         // io->writestring( tmp.c_str() );
188
189         if ( tokens.size() <= 1 ) {
190             // do nothing
191         } else {
192             SGPropertyNode *child = node->getNode(tokens[1]);
193             if ( child ) {
194                 node = child;
195                 path = node->getPath();
196             } else {
197                 tokens[1] = "ERR Node \"" + tokens[1] + "\" not found.\n";
198                 io->writestring( tokens[1].c_str() );
199             }
200         }
201     } else if ( command == "pwd" ) {
202         string ttt = node->getPath();
203         if ( ttt == "" ) {
204             ttt = "/";
205         }
206         ttt += "\n";
207         io->writestring( ttt.c_str() );
208     } else if ( command == "get" || command == "show" ) {
209         if ( tokens.size() <= 1 ) {
210             // do nothing
211         } else {
212             string tmp; 
213             string value = node->getStringValue ( tokens[1], "" );
214             if ( mode == PROMPT ) {
215                 //string ttt = "debug = '" + tokens[1] + "'\n";
216                 //io->writestring( ttt.c_str() );
217
218                 tmp = tokens[1] + " = '" + value + "' (";
219                 tmp += getValueTypeString( node->getNode( tokens[1] ) );
220                 tmp += ")\n";
221             } else {
222                 tmp = value + "\n";
223             }
224             io->writestring( tmp.c_str() );
225         }
226     } else if ( command == "set" ) {
227         if ( tokens.size() <= 2 ) {
228             // do nothing
229         } else {
230             node->getNode( tokens[1], true )->setStringValue(tokens[2]);
231
232             // now fetch and write out the new value as confirmation
233             // of the change
234             string value = node->getStringValue ( tokens[1], "" );
235             string tmp = tokens[1] + " = '" + value + "' (";
236             tmp += getValueTypeString( node->getNode( tokens[1] ) );
237             tmp += ")\n";
238  
239             io->writestring( tmp.c_str() );
240         }
241     } else if ( command == "quit" ) {
242         close();
243         reset();
244         return true;
245     } else if ( command == "data" ) {
246         mode = DATA;
247     } else if ( command == "prompt" ) {
248         mode = PROMPT;
249     } else {
250         io->writestring( "\n" );
251         io->writestring( "Valid commands are:\n" );
252         io->writestring( "\n" );
253         io->writestring( "help             show help message\n" );
254         io->writestring( "ls [<dir>]       list directory\n" );
255         io->writestring( "dump             dump current state (in xml)\n" );
256         io->writestring( "cd <dir>         cd to a directory, '..' to move back\n" );
257         io->writestring( "pwd              display your current path\n" );
258         io->writestring( "get <var>        show the value of a parameter\n" );
259         io->writestring( "show <var>       synonym for get\n" );
260         io->writestring( "set <var> <val>  set <var> to a new <val>\n" );
261         io->writestring( "data             switch to raw data mode\n" );
262         io->writestring( "prompt           switch to interactive mode (default)\n" );
263         io->writestring( "quit             terminate connection\n" );
264         io->writestring( "\n" );
265     }
266
267     if ( mode == PROMPT ) {
268         string prompt = node->getPath();
269         if ( prompt == "" ) {
270             prompt = "/";
271         }
272         prompt += "> ";
273         io->writestring( prompt.c_str() );
274     }
275     return true;
276 }
277
278
279 // process work for this port
280 bool FGProps::process() {
281     SGIOChannel *io = get_io_channel();
282     char buf[max_cmd_len];
283
284     // cout << "processing incoming props command" << endl;
285
286     if ( get_direction() == SG_IO_BI ) {
287         // cout << "  (bi directional)" << endl;
288         while ( io->readline( buf, max_cmd_len ) > 0 ) {
289             SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
290             process_command( buf );
291         }
292     } else {
293         SG_LOG( SG_IO, SG_ALERT, 
294                 "in or out direction not supported for FGProps." );
295     }
296
297     return true;
298 }
299
300
301 // close the channel
302 bool FGProps::close() {
303     SGIOChannel *io = get_io_channel();
304
305     if ( ! io->close() ) {
306         return false;
307     }
308
309     cout << "successfully closed channel\n";
310
311     return true;
312 }