]> git.mxchange.org Git - flightgear.git/blob - src/Network/props.cxx
- changed SGPropertyNode::UNKNOWN to SGPropertyNode::UNSPECIFIED
[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     path = "/";
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 // return a human readable form of the value "type"
75 static string getValueTypeString( const SGPropertyNode *node ) {
76     string result;
77
78     if ( node == NULL ) {
79         return "unknown";
80     }
81
82     SGPropertyNode::Type type = node->getType();
83     if ( type == SGPropertyNode::UNSPECIFIED ) {
84         result = "unspecified";
85     } else if ( type == SGPropertyNode::BOOL ) {
86         result = "bool";
87     } else if ( type == SGPropertyNode::INT ) {
88         result = "int";
89     } else if ( type == SGPropertyNode::LONG ) {
90         result = "long";
91     } else if ( type == SGPropertyNode::FLOAT ) {
92         result = "float";
93     } else if ( type == SGPropertyNode::DOUBLE ) {
94         result = "double";
95     } else if ( type == SGPropertyNode::STRING ) {
96         result = "string";
97     }
98
99     return result;
100 }
101
102
103 bool FGProps::process_command( const char *cmd ) {
104     SGIOChannel *io = get_io_channel();
105
106     cout << "processing command = " << cmd;
107     string_list tokens;
108     tokens.clear();
109
110     istrstream in(cmd);
111     
112     while ( !in.eof() ) {
113         string token;
114         in >> token;
115         tokens.push_back( token );
116     }
117
118     string command = tokens[0];
119
120     SGPropertyNode * node = globals->get_props()->getNode(path);
121
122     if ( command == "ls" ) {
123         for (int i = 0; i < (int)node->nChildren(); i++) {
124             SGPropertyNode * child = node->getChild(i);
125             string name = child->getName();
126             string line = name;
127             if ( child->nChildren() > 0 ) {
128                 line += "/\n";
129             } else {
130                 string value = node->getStringValue ( name, "" );
131                 line += " =\t'" + value + "'\t(";
132                 line += getValueTypeString( node->getNode( name ) );
133                 line += ")\n";
134             }
135             io->writestring( line.c_str() );
136         }
137     } else if ( command == "dump" ) {
138         strstream buf;
139         if ( tokens.size() <= 1 ) {
140             writeProperties ( buf, node);
141             io->writestring( buf.str() );
142         }
143         else {
144             SGPropertyNode *child = node->getNode(tokens[1]);
145             if ( child ) {
146                 writeProperties ( buf, child );
147                 io->writestring( buf.str() );
148             } else {
149                 tokens[1] += " Not Found\n";
150                 io->writestring( tokens[1].c_str() );
151             }
152         }
153     } else if ( command == "cd" ) {
154         // string tmp = "current path = " + node.getPath() + "\n";
155         // io->writestring( tmp.c_str() );
156
157         if ( tokens.size() <= 1 ) {
158             // do nothing
159         } else {
160             SGPropertyNode *child = node->getNode(tokens[1]);
161             if ( child ) {
162                 node = child;
163                 path = node->getPath();
164             } else {
165                 tokens[1] += " Not Found\n";
166                 io->writestring( tokens[1].c_str() );
167             }
168         }
169     } else if ( command == "pwd" ) {
170         string ttt = node->getPath();
171         if ( ttt == "" ) {
172             ttt = "/";
173         }
174         ttt += "\n";
175         io->writestring( ttt.c_str() );
176     } else if ( command == "get" || command == "show" ) {
177         if ( tokens.size() <= 1 ) {
178             // do nothing
179         } else {
180             string ttt = "debug = '" + tokens[1] + "'\n";
181             io->writestring( ttt.c_str() );
182
183             string value = node->getStringValue ( tokens[1], "" );
184             string tmp = tokens[1] + " = '" + value + "' (";
185             tmp += getValueTypeString( node->getNode( tokens[1] ) );
186             tmp += ")\n";
187  
188             io->writestring( tmp.c_str() );
189         }
190     } else if ( command == "set" ) {
191         if ( tokens.size() <= 2 ) {
192             // do nothing
193         } else {
194             node->getNode( tokens[1], true )->setStringValue(tokens[2]);
195
196             // now fetch and write out the new value as confirmation
197             // of the change
198             string value = node->getStringValue ( tokens[1], "" );
199             string tmp = tokens[1] + " = '" + value + "' (";
200             tmp += getValueTypeString( node->getNode( tokens[1] ) );
201             tmp += ")\n";
202  
203             io->writestring( tmp.c_str() );
204         }
205     } else if ( command == "quit" ) {
206         close();
207     } else {
208         io->writestring( "\n" );
209         io->writestring( "Valid commands are:\n" );
210         io->writestring( "\n" );
211         io->writestring( "help             show help message\n" );
212         io->writestring( "ls               list current directory\n" );
213         io->writestring( "dump             dump current state (in xml)\n" );
214         io->writestring( "cd <dir>         cd to a directory, '..' to move back\n" );
215         io->writestring( "pwd              display your current path\n" );
216         io->writestring( "get <var>        show the value of a parameter\n" );
217         io->writestring( "show <var>       synonym for get\n" );
218         io->writestring( "set <var> <val>  set <var> to a new <val>\n" );
219         io->writestring( "quit             terminate connection\n" );
220         io->writestring( "\n" );
221     }
222
223     string prompt = node->getPath();
224     if ( prompt == "" ) {
225         prompt = "/";
226     }
227     prompt += "> ";
228     io->writestring( prompt.c_str() );
229
230     return true;
231 }
232
233
234 // process work for this port
235 bool FGProps::process() {
236     SGIOChannel *io = get_io_channel();
237
238     // cout << "processing incoming props command" << endl;
239
240     if ( get_direction() == SG_IO_BI ) {
241         // cout << "  (bi directional)" << endl;
242         while ( io->readline( buf, max_cmd_len ) > 0 ) {
243             SG_LOG( SG_IO, SG_ALERT, "Success reading data." );
244             process_command( buf );
245         }
246     } else {
247         SG_LOG( SG_IO, SG_ALERT, 
248                 "in or out direction not supported for FGProps." );
249     }
250
251     return true;
252 }
253
254
255 // close the channel
256 bool FGProps::close() {
257     SGIOChannel *io = get_io_channel();
258
259     set_enabled( false );
260
261     if ( ! io->close() ) {
262         return false;
263     }
264
265     cout << "successfully closed channel\n";
266
267     return true;
268 }