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