]> git.mxchange.org Git - flightgear.git/blob - src/Network/props.cxx
29730db427b48f458f444a511a5b34fb68d6925d
[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 <simgear/compiler.h>
25 #include <simgear/debug/logstream.hxx>
26 #include <simgear/io/iochannel.hxx>
27 #include <simgear/math/sg_types.hxx>
28 #include <simgear/misc/props.hxx>
29
30 #include <stdlib.h>             // atoi() atof()
31
32 #include <strstream>
33
34 #include "props.hxx"
35
36 FG_USING_STD(cout);
37 FG_USING_STD(istrstream);
38
39 FGProps::FGProps() {
40 }
41
42 FGProps::~FGProps() {
43 }
44
45
46 // open hailing frequencies
47 bool FGProps::open() {
48     path = "/";
49
50     if ( is_enabled() ) {
51         FG_LOG( FG_IO, FG_ALERT, "This shouldn't happen, but the channel " 
52                 << "is already in use, ignoring" );
53         return false;
54     }
55
56     SGIOChannel *io = get_io_channel();
57
58     if ( ! io->open( get_direction() ) ) {
59         FG_LOG( FG_IO, FG_ALERT, "Error opening channel communication layer." );
60         return false;
61     }
62
63     set_enabled( true );
64
65     return true;
66 }
67
68
69 // return a human readable form of the value "type"
70 static string getValueTypeString( const SGValue *v ) {
71     string result;
72
73     if ( v == NULL ) {
74         return "unknown";
75     }
76
77     SGValue::Type type = v->getType();
78     if ( type == SGValue::UNKNOWN ) {
79         result = "unknown";
80     } else if ( type == SGValue::BOOL ) {
81         result = "bool";
82     } else if ( type == SGValue::INT ) {
83         result = "int";
84     } else if ( type == SGValue::FLOAT ) {
85         result = "float";
86     } else if ( type == SGValue::DOUBLE ) {
87         result = "double";
88     } else if ( type == SGValue::STRING ) {
89         result = "string";
90     }
91
92     return result;
93 }
94
95
96 bool FGProps::process_command( const char *cmd ) {
97     SGIOChannel *io = get_io_channel();
98
99     cout << "processing command = " << cmd;
100     string_list tokens;
101     tokens.clear();
102
103     istrstream in(cmd);
104     
105     while ( !in.eof() ) {
106         string token;
107         in >> token;
108         tokens.push_back( token );
109     }
110
111     string command = tokens[0];
112
113     SGPropertyNode node( path, &current_properties );
114     if ( node.getPath() == "" ) {
115         node.setPath( "/" );
116     }
117
118     if ( command == "ls" ) {
119         for (int i = 0; i < (int)node.size(); i++) {
120             SGPropertyNode child = node.getChild(i);
121             string name = child.getName();
122             string line = name;
123             if ( child.size() > 0 ) {
124                 line += "/\n";
125             } else {
126                 string value = node.getStringValue ( name, "" );
127                 line += " =\t'" + value + "'\t(";
128                 line += getValueTypeString( node.getValue( name ) );
129                 line += ")\n";
130             }
131             io->writestring( line.c_str() );
132         }
133     } else if ( command == "cd" ) {
134         // string tmp = "current path = " + node.getPath() + "\n";
135         // io->writestring( tmp.c_str() );
136
137         if ( tokens.size() <= 1 ) {
138             // do nothing
139         } else if ( tokens[1] == "." ) {
140             // do nothing
141         } else if ( tokens[1] == ".." ) {
142             // go back one level
143             string current = node.getPath();
144             int pos = current.rfind("/");
145             // cout << "path = " << current << endl;
146             // cout << "new path = " << current.substr(0, pos) << endl;d
147             string tmp = current.substr(0, pos);
148             node.setPath( tmp );
149             path = tmp;
150         } else {
151             // decend to specified child
152             string tmp = node.getPath();
153             tmp += "/" + tokens[1];
154             node.setPath( tmp );
155             path = tmp;
156         }
157     } else if ( command == "show" ) {
158         if ( tokens.size() <= 1 ) {
159             // do nothing
160         } else {
161             string ttt = "debug = '" + tokens[1] + "'\n";
162             io->writestring( ttt.c_str() );
163
164             string value = node.getStringValue ( tokens[1], "" );
165             string tmp = tokens[1] + " = '" + value + "' (";
166             tmp += getValueTypeString( node.getValue( tokens[1] ) );
167             tmp += ")\n";
168  
169             io->writestring( tmp.c_str() );
170         }
171     } else if ( command == "set" ) {
172         if ( tokens.size() <= 2 ) {
173             // do nothing
174         } else {
175             SGValue *v = node.getValue( tokens[1] );
176             if ( v != NULL ) {
177                 SGValue::Type type = v->getType();
178                 if ( type == SGValue::UNKNOWN ) {
179                     v->setUnknownValue( tokens[2] );
180                 } else if ( type == SGValue::BOOL ) {
181                     if ( tokens[2] == "true" ) {
182                         v->setBoolValue( true );
183                     } else if ( tokens[2] == "false" ) {
184                         v->setBoolValue( false );
185                     } else {
186                         v->setBoolValue( atoi( tokens[2].c_str() ) );
187                     }
188                 } else if ( type == SGValue::INT ) {
189                     v->setIntValue( atoi( tokens[2].c_str() ) );
190                 } else if ( type == SGValue::FLOAT ) {
191                     v->setFloatValue( atof( tokens[2].c_str() ) );
192                 } else if ( type == SGValue::DOUBLE ) {
193                     v->setDoubleValue( atof( tokens[2].c_str() ) );
194                 } else if ( type == SGValue::STRING ) {
195                     v->setStringValue( tokens[2] );
196                 }
197
198                 // now fetch and write out the new value as confirmation
199                 // of the change
200                 string value = node.getStringValue ( tokens[1], "" );
201                 string tmp = tokens[1] + " = '" + value + "' (";
202                 tmp += getValueTypeString( node.getValue( tokens[1] ) );
203                 tmp += ")\n";
204  
205                 io->writestring( tmp.c_str() );
206             } else {
207                 string tmp = tokens[1] + " is unknown.\n";
208
209                 io->writestring( tmp.c_str() );
210             }
211
212         }
213     } else if ( command == "quit" ) {
214         close();
215     } else {
216         io->writestring( "\n" );
217         io->writestring( "Valid commands are:\n" );
218         io->writestring( "\n" );
219         io->writestring( "help             show help message\n" );
220         io->writestring( "ls               list current directory\n" );
221         io->writestring( "cd <dir>         cd to a directory, '..' to move back\n" );
222         io->writestring( "show <var>       show the value of a parameter\n" );
223         io->writestring( "set <var> <val>  set <var> to a new <val>\n" );
224         io->writestring( "quit             terminate connection\n" );
225         io->writestring( "\n" );
226     }
227
228     string prompt = node.getPath();
229     if ( prompt == "" ) {
230         prompt = "/";
231     }
232     prompt += "> ";
233     io->writestring( prompt.c_str() );
234
235     return true;
236 }
237
238
239 // process work for this port
240 bool FGProps::process() {
241     SGIOChannel *io = get_io_channel();
242
243     // cout << "processing incoming props command" << endl;
244
245     if ( get_direction() == SG_IO_BI ) {
246         // cout << "  (bi directional)" << endl;
247         while ( io->readline( buf, max_cmd_len ) > 0 ) {
248             FG_LOG( FG_IO, FG_ALERT, "Success reading data." );
249             process_command( buf );
250         }
251     } else {
252         FG_LOG( FG_IO, FG_ALERT, 
253                 "in or out direction not supported for FGProps." );
254     }
255
256     return true;
257 }
258
259
260 // close the channel
261 bool FGProps::close() {
262     SGIOChannel *io = get_io_channel();
263
264     set_enabled( false );
265
266     if ( ! io->close() ) {
267         return false;
268     }
269
270     cout << "successfully closed channel\n";
271
272     return true;
273 }