]> git.mxchange.org Git - flightgear.git/blob - utils/fgpanel/FGPanelProtocol.cxx
Merge branch 'next' into durk-atc
[flightgear.git] / utils / fgpanel / FGPanelProtocol.cxx
1 //
2 //  Written and (c) Torsten Dreyer - Torsten(at)t3r_dot_de
3 //
4 //  This program is free software; you can redistribute it and/or
5 //  modify it under the terms of the GNU General Public License as
6 //  published by the Free Software Foundation; either version 2 of the
7 //  License, or (at your option) any later version.
8 // 
9 //  This program is distributed in the hope that it will be useful, but
10 //  WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 //  General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; if not, write to the Free Software
16 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 //
18 #include "FGPanelProtocol.hxx"
19 #include "ApplicationProperties.hxx"
20 #include <simgear/io/sg_socket.hxx>
21 #include <simgear/misc/strutils.hxx>
22
23 class PropertySetter {
24 public:
25   PropertySetter( SGPropertyNode_ptr node ) : _node(node) {}
26   virtual void setValue( const char * value ) = 0;
27 protected:
28   SGPropertyNode_ptr _node;
29 };
30
31 class BoolPropertySetter : public PropertySetter {
32 public:
33   BoolPropertySetter( SGPropertyNode_ptr node ) : PropertySetter(node) {}
34   virtual void setValue( const char * value ) {
35     _node->setBoolValue( atoi( value ) != 0 );
36   }
37 };
38
39 class IntPropertySetter : public PropertySetter {
40 public:
41   IntPropertySetter( SGPropertyNode_ptr node ) : PropertySetter(node) {}
42   virtual void setValue( const char * value ) {
43     _node->setIntValue( atol( value ) );
44   }
45 };
46
47 class FloatPropertySetter : public PropertySetter {
48 public:
49   FloatPropertySetter( SGPropertyNode_ptr node ) : PropertySetter(node) {}
50   virtual void setValue( const char * value ) {
51     _node->setFloatValue( strtof( value, NULL ) );
52   }
53 };
54
55 class DoublePropertySetter : public PropertySetter {
56 public:
57   DoublePropertySetter( SGPropertyNode_ptr node ) : PropertySetter(node) {}
58   virtual void setValue( const char * value ) {
59     _node->setDoubleValue( strtod( value, NULL ) );
60   }
61 };
62
63 class StringPropertySetter : public PropertySetter {
64 public:
65   StringPropertySetter( SGPropertyNode_ptr node ) : PropertySetter(node) {}
66   virtual void setValue( const char * value ) {
67     _node->setStringValue( value );
68   }
69 };
70
71 FGPanelProtocol::FGPanelProtocol( SGPropertyNode_ptr aRoot )
72   : SGSubsystem(),
73    root(aRoot),
74    io(NULL)
75 {
76   SGPropertyNode_ptr outputNode = root->getNode( "protocol/generic/output" );
77   if( outputNode ) {
78     vector<SGPropertyNode_ptr> chunks = outputNode->getChildren( "chunk" );
79     for( vector<SGPropertyNode_ptr>::size_type i = 0; i < chunks.size(); i++ ) {
80       SGPropertyNode_ptr chunk = chunks[i];
81
82       SGPropertyNode_ptr nodeNode = chunk->getNode("node", false );
83       if( nodeNode == NULL )
84         continue;
85
86       SGPropertyNode_ptr node = ApplicationProperties::Properties->getNode( nodeNode->getStringValue(), true );
87
88       string type = "";
89       SGPropertyNode_ptr typeNode = chunk->getNode( "type", false );
90       if( typeNode != NULL ) type = typeNode->getStringValue();
91       if( type == "float" ) {
92         propertySetterVector.push_back( new FloatPropertySetter( node ) );
93       } else if( type == "double" || type == "fixed" ) {
94         propertySetterVector.push_back( new DoublePropertySetter( node ) );
95       } else if( type == "bool" || type == "boolean" ) {
96         propertySetterVector.push_back( new BoolPropertySetter( node ) );
97       } else if( type == "string" ) {
98         propertySetterVector.push_back( new StringPropertySetter( node ) );
99       } else {
100         propertySetterVector.push_back( new IntPropertySetter( node ) );
101       }
102     }
103   }
104 }
105
106 FGPanelProtocol::~FGPanelProtocol()
107 {
108   for( PropertySetterVector::size_type i = 0; i < propertySetterVector.size(); i++ )
109     delete propertySetterVector[i];
110 }
111
112 void FGPanelProtocol::update( double dt )
113 {
114   char buf[8192];
115
116   if( io == NULL )
117     return;
118
119   int length = io->readline( buf, sizeof(buf)-1 );
120   buf[sizeof(buf)-1] = 0;
121   if ( length > 0 ) {
122     vector<string> tokens = simgear::strutils::split( buf, "," );
123     for( vector<string>::size_type i = 0; i < tokens.size(); i++ ) {
124       if( i < propertySetterVector.size() )
125         propertySetterVector[i]->setValue( tokens[i].c_str() );
126     }
127   }
128 }
129
130 void FGPanelProtocol::init()
131 {
132   SGPropertyNode_ptr listenNode = root->getNode( "listen" );
133   if( listenNode == NULL ) {
134     return;
135   }
136
137   string hostname = listenNode->getNode( "host", true )->getStringValue();
138   string port = listenNode->getNode( "port", true )->getStringValue();
139   string style = listenNode->getNode( "style", true )->getStringValue();
140
141   if( io != NULL )
142     delete io;
143
144   io = new SGSocket( hostname, port, style );
145
146   if( !io->open( SG_IO_IN ) ) {
147     cerr << "can't open socket " << style << ":" << hostname << ":" << port << endl;
148   }
149 }
150
151 void FGPanelProtocol::reinit()
152 {
153   init();
154 }