2 // Property server class.
4 // Written by Curtis Olson, started September 2000.
5 // Modified by Bernie Bright, May 2002.
7 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
26 #include <simgear/io/sg_netChat.hxx>
27 #include <simgear/structure/commands.hxx>
28 #include <simgear/misc/strutils.hxx>
29 #include <simgear/math/SGMath.hxx>
33 #include "UGear_command.hxx"
34 #include "UGear_telnet.hxx"
36 using std::stringstream;
40 * Props connection class.
41 * This class represents a connection to props client.
43 class PropsChannel : public simgear::NetChat
45 simgear::NetBuffer buffer;
48 * Current property node name.
66 * Append incoming data to our request buffer.
68 * @param s Character string to append to buffer
69 * @param n Number of characters to append.
71 void collectIncomingData( const char* s, int n );
74 * Process a complete request from the props client.
76 void foundTerminator();
80 * Return a "Node no found" error message to the client.
82 void node_not_found_error( const string& node_name );
88 PropsChannel::PropsChannel()
93 setTerminator( "\r\n" );
100 PropsChannel::collectIncomingData( const char* s, int n )
102 buffer.append( s, n );
109 PropsChannel::node_not_found_error( const string& node_name )
111 string error = "-ERR Node \"";
113 error += "\" not found.";
114 push( error.c_str() );
115 push( getTerminator() );
123 PropsChannel::foundTerminator()
125 const char* cmd = buffer.getData();
126 SG_LOG( SG_IO, SG_INFO, "processing command = \"" << cmd << "\"" );
128 vector<string> tokens = simgear::strutils::split( cmd );
130 if (!tokens.empty()) {
131 string command = tokens[0];
133 if ( command == "send" ) {
134 command_mgr.add( tokens[1] );
135 } else if ( command == "quit" ) {
139 } else if ( command == "data" ) {
141 } else if ( command == "prompt" ) {
145 Valid commands are:\r\n\
147 data switch to raw data mode\r\n\
148 prompt switch to interactive mode (default)\r\n\
149 quit terminate connection\r\n\
150 send <command> send <command> to UAS\r\n";
155 if (mode == PROMPT) {
156 string prompt = "> ";
157 push( prompt.c_str() );
166 UGTelnet::UGTelnet( const int port_num ):
175 UGTelnet::~UGTelnet()
186 printf("This shouldn't happen, but the telnet channel is already in use, ignoring\n" );
190 simgear::NetChannel::open();
191 simgear::NetChannel::bind( "", port );
192 simgear::NetChannel::listen( 5 );
193 printf("Telnet server started on port %d\n", port );
206 SG_LOG( SG_IO, SG_INFO, "closing UGTelnet" );
216 simgear::NetChannel::poll();
224 UGTelnet::handleAccept()
226 simgear::IPAddress addr;
227 int handle = simgear::NetChannel::accept( &addr );
228 printf("Telent server accepted connection from %s:%d\n",
229 addr.getHost(), addr.getPort() );
230 PropsChannel* channel = new PropsChannel();
231 channel->setHandle( handle );