]> git.mxchange.org Git - flightgear.git/blob - src/Network/props.cxx
f9c3b8b37c0dc7461bc98169cc87b2c89a606fbb
[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] += " 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                 sprintf(buf, "[%d]", child->getIndex());
153                 line += buf;
154             }
155             if ( child->nChildren() > 0 ) {
156                 line += "/";
157             } else {
158                 if (mode == PROMPT) {
159                     string value = dir->getStringValue ( name, "" );
160                     line += " =\t'" + value + "'\t(";
161                     line += getValueTypeString( dir->getNode( name ) );
162                     line += ")";
163                 }
164             }
165             line += "\n";
166             io->writestring( line.c_str() );
167         }
168     } else if ( command == "dump" ) {
169         strstream buf;
170         if ( tokens.size() <= 1 ) {
171             writeProperties ( buf, node);
172             io->writestring( buf.str() );
173         }
174         else {
175             SGPropertyNode *child = node->getNode(tokens[1]);
176             if ( child ) {
177                 writeProperties ( buf, child );
178                 io->writestring( buf.str() );
179             } else {
180                 tokens[1] += " Not Found\n";
181                 io->writestring( tokens[1].c_str() );
182             }
183         }
184     } else if ( command == "cd" ) {
185         // string tmp = "current path = " + node.getPath() + "\n";
186         // io->writestring( tmp.c_str() );
187
188         if ( tokens.size() <= 1 ) {
189             // do nothing
190         } else {
191             SGPropertyNode *child = node->getNode(tokens[1]);
192             if ( child ) {
193                 node = child;
194                 path = node->getPath();
195             } else {
196                 tokens[1] += " Not Found\n";
197                 io->writestring( tokens[1].c_str() );
198             }
199         }
200     } else if ( command == "pwd" ) {
201         string ttt = node->getPath();
202         if ( ttt == "" ) {
203             ttt = "/";
204         }
205         ttt += "\n";
206         io->writestring( ttt.c_str() );
207     } else if ( command == "get" || command == "show" ) {
208         if ( tokens.size() <= 1 ) {
209             // do nothing
210         } else {
211             string tmp; 
212             string value = node->getStringValue ( tokens[1], "" );
213             if ( mode == PROMPT ) {
214                 string ttt = "debug = '" + tokens[1] + "'\n";
215                 io->writestring( ttt.c_str() );
216
217                 tmp = tokens[1] + " = '" + value + "' (";
218                 tmp += getValueTypeString( node->getNode( tokens[1] ) );
219                 tmp += ")\n";
220             } else {
221                 tmp = value + "\n";
222             }
223             io->writestring( tmp.c_str() );
224         }
225     } else if ( command == "set" ) {
226         if ( tokens.size() <= 2 ) {
227             // do nothing
228         } else {
229             node->getNode( tokens[1], true )->setStringValue(tokens[2]);
230
231             // now fetch and write out the new value as confirmation
232             // of the change
233             string value = node->getStringValue ( tokens[1], "" );
234             string tmp = tokens[1] + " = '" + value + "' (";
235             tmp += getValueTypeString( node->getNode( tokens[1] ) );
236             tmp += ")\n";
237  
238             io->writestring( tmp.c_str() );
239         }
240     } else if ( command == "quit" ) {
241         close();
242         reset();
243         return true;
244     } else if ( command == "data" ) {
245         mode = DATA;
246     } else if ( command == "prompt" ) {
247         mode = PROMPT;
248     } else {
249         io->writestring( "\n" );
250         io->writestring( "Valid commands are:\n" );
251         io->writestring( "\n" );
252         io->writestring( "help             show help message\n" );
253         io->writestring( "ls [<dir>]       list directory\n" );
254         io->writestring( "dump             dump current state (in xml)\n" );
255         io->writestring( "cd <dir>         cd to a directory, '..' to move back\n" );
256         io->writestring( "pwd              display your current path\n" );
257         io->writestring( "get <var>        show the value of a parameter\n" );
258         io->writestring( "show <var>       synonym for get\n" );
259         io->writestring( "set <var> <val>  set <var> to a new <val>\n" );
260         io->writestring( "data             switch to raw data mode\n" );
261         io->writestring( "prompt           switch to interactive mode (default)\n" );
262         io->writestring( "quit             terminate connection\n" );
263         io->writestring( "\n" );
264     }
265
266     if ( mode == PROMPT ) {
267         string prompt = node->getPath();
268         if ( prompt == "" ) {
269             prompt = "/";
270         }
271         prompt += "> ";
272         io->writestring( prompt.c_str() );
273     }
274     return true;
275 }
276
277
278 // process work for this port
279 bool FGProps::process() {
280     SGIOChannel *io = get_io_channel();
281
282     // cout << "processing incoming props command" << endl;
283
284     if ( get_direction() == SG_IO_BI ) {
285         // cout << "  (bi directional)" << endl;
286         while ( io->readline( buf, max_cmd_len ) > 0 ) {
287             SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
288             process_command( buf );
289         }
290     } else {
291         SG_LOG( SG_IO, SG_ALERT, 
292                 "in or out direction not supported for FGProps." );
293     }
294
295     return true;
296 }
297
298
299 // close the channel
300 bool FGProps::close() {
301     SGIOChannel *io = get_io_channel();
302
303     if ( ! io->close() ) {
304         return false;
305     }
306
307     cout << "successfully closed channel\n";
308
309     return true;
310 }