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>
32 #include "UGear_command.hxx"
33 #include "UGear_telnet.hxx"
35 using std::stringstream;
39 * Props connection class.
40 * This class represents a connection to props client.
42 class PropsChannel : public simgear::NetChat
44 simgear::NetBuffer buffer;
47 * Current property node name.
65 * Append incoming data to our request buffer.
67 * @param s Character string to append to buffer
68 * @param n Number of characters to append.
70 void collectIncomingData( const char* s, int n );
73 * Process a complete request from the props client.
75 void foundTerminator();
79 * Return a "Node no found" error message to the client.
81 void node_not_found_error( const string& node_name );
87 PropsChannel::PropsChannel()
92 setTerminator( "\r\n" );
99 PropsChannel::collectIncomingData( const char* s, int n )
101 buffer.append( s, n );
108 PropsChannel::node_not_found_error( const string& node_name )
110 string error = "-ERR Node \"";
112 error += "\" not found.";
113 push( error.c_str() );
114 push( getTerminator() );
122 PropsChannel::foundTerminator()
124 const char* cmd = buffer.getData();
125 SG_LOG( SG_IO, SG_INFO, "processing command = \"" << cmd << "\"" );
127 vector<string> tokens = simgear::strutils::split( cmd );
129 if (!tokens.empty()) {
130 string command = tokens[0];
132 if ( command == "send" ) {
133 command_mgr.add( tokens[1] );
134 } else if ( command == "quit" ) {
138 } else if ( command == "data" ) {
140 } else if ( command == "prompt" ) {
144 Valid commands are:\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";
154 if (mode == PROMPT) {
155 string prompt = "> ";
156 push( prompt.c_str() );
165 UGTelnet::UGTelnet( const int port_num ):
174 UGTelnet::~UGTelnet()
185 printf("This shouldn't happen, but the telnet channel is already in use, ignoring\n" );
189 simgear::NetChannel::open();
190 simgear::NetChannel::bind( "", port );
191 simgear::NetChannel::listen( 5 );
192 printf("Telnet server started on port %d\n", port );
205 SG_LOG( SG_IO, SG_INFO, "closing UGTelnet" );
215 simgear::NetChannel::poll();
223 UGTelnet::handleAccept()
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 );