]> git.mxchange.org Git - flightgear.git/blob - utils/GPSsmooth/UGear_telnet.cxx
Better fix for a compilation problem with MSVC 2012
[flightgear.git] / utils / GPSsmooth / UGear_telnet.cxx
1 // \file props.cxx
2 // Property server class.
3 //
4 // Written by Curtis Olson, started September 2000.
5 // Modified by Bernie Bright, May 2002.
6 //
7 // Copyright (C) 2000  Curtis L. Olson - http://www.flightgear.org/~curt
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 //
23 // $Id$
24
25
26 #include <simgear/io/sg_netChat.hxx>
27 #include <simgear/structure/commands.hxx>
28 #include <simgear/misc/strutils.hxx>
29
30 #include <sstream>
31
32 #include "UGear_command.hxx"
33 #include "UGear_telnet.hxx"
34
35 using std::stringstream;
36 using std::ends;
37
38 /**
39  * Props connection class.
40  * This class represents a connection to props client.
41  */
42 class PropsChannel : public simgear::NetChat
43 {
44     simgear::NetBuffer buffer;
45
46     /**
47      * Current property node name.
48      */
49     string path;
50
51     enum Mode {
52         PROMPT,
53         DATA
54     };
55     Mode mode;
56
57 public:
58
59     /**
60      * Constructor.
61      */
62     PropsChannel();
63     
64     /**
65      * Append incoming data to our request buffer.
66      *
67      * @param s Character string to append to buffer
68      * @param n Number of characters to append.
69      */
70     void collectIncomingData( const char* s, int n );
71
72     /**
73      * Process a complete request from the props client.
74      */
75     void foundTerminator();
76
77 private:
78     /**
79      * Return a "Node no found" error message to the client.
80      */
81     void node_not_found_error( const string& node_name );
82 };
83
84 /**
85  * 
86  */
87 PropsChannel::PropsChannel()
88     : buffer(512),
89       path("/"),
90       mode(PROMPT)
91 {
92     setTerminator( "\r\n" );
93 }
94
95 /**
96  * 
97  */
98 void
99 PropsChannel::collectIncomingData( const char* s, int n )
100 {
101     buffer.append( s, n );
102 }
103
104 /**
105  * 
106  */
107 void
108 PropsChannel::node_not_found_error( const string& node_name )
109 {
110     string error = "-ERR Node \"";
111     error += node_name;
112     error += "\" not found.";
113     push( error.c_str() );
114     push( getTerminator() );
115 }
116
117 /**
118  * We have a command.
119  * 
120  */
121 void
122 PropsChannel::foundTerminator()
123 {
124     const char* cmd = buffer.getData();
125     SG_LOG( SG_IO, SG_INFO, "processing command = \"" << cmd << "\"" );
126
127     vector<string> tokens = simgear::strutils::split( cmd );
128
129     if (!tokens.empty()) {
130         string command = tokens[0];
131
132         if ( command == "send" ) {
133             command_mgr.add( tokens[1] );
134         } else if ( command == "quit" ) {
135             close();
136             shouldDelete();
137             return;
138         } else if ( command == "data" ) {
139             mode = DATA;
140         } else if ( command == "prompt" ) {
141             mode = PROMPT;
142         } else {
143             const char* msg = "\
144 Valid commands are:\r\n\
145 \r\n\
146 data               switch to raw data mode\r\n\
147 prompt             switch to interactive mode (default)\r\n\
148 quit               terminate connection\r\n\
149 send <command>     send <command> to UAS\r\n";
150             push( msg );
151         }
152     }
153
154     if (mode == PROMPT) {
155         string prompt = "> ";
156         push( prompt.c_str() );
157     }
158
159     buffer.remove();
160 }
161
162 /**
163  * 
164  */
165 UGTelnet::UGTelnet( const int port_num ):
166     enabled(false)
167 {
168     port = port_num;
169 }
170
171 /**
172  * 
173  */
174 UGTelnet::~UGTelnet()
175 {
176 }
177
178 /**
179  * 
180  */
181 bool
182 UGTelnet::open()
183 {
184     if (enabled ) {
185         printf("This shouldn't happen, but the telnet channel is already in use, ignoring\n" );
186         return false;
187     }
188
189     simgear::NetChannel::open();
190     simgear::NetChannel::bind( "", port );
191     simgear::NetChannel::listen( 5 );
192     printf("Telnet server started on port %d\n", port );
193
194     enabled = true;
195
196     return true;
197 }
198
199 /**
200  * 
201  */
202 bool
203 UGTelnet::close()
204 {
205     SG_LOG( SG_IO, SG_INFO, "closing UGTelnet" );   
206     return true;
207 }
208
209 /**
210  * 
211  */
212 bool
213 UGTelnet::process()
214 {
215     simgear::NetChannel::poll();
216     return true;
217 }
218
219 /**
220  * 
221  */
222 void
223 UGTelnet::handleAccept()
224 {
225     simgear::IPAddress addr;
226     int handle = simgear::NetChannel::accept( &addr );
227     printf("Telent server accepted connection from %s:%d\n",
228            addr.getHost(), addr.getPort() );
229     PropsChannel* channel = new PropsChannel();
230     channel->setHandle( handle );
231 }