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/debug/logstream.hxx>
34 #include "UGear_command.hxx"
35 #include "UGear_telnet.hxx"
37 using std::stringstream;
41 * Props connection class.
42 * This class represents a connection to props client.
44 class PropsChannel : public simgear::NetChat
46 simgear::NetBuffer buffer;
49 * Current property node name.
67 * Append incoming data to our request buffer.
69 * @param s Character string to append to buffer
70 * @param n Number of characters to append.
72 void collectIncomingData( const char* s, int n );
75 * Process a complete request from the props client.
77 void foundTerminator();
81 * Return a "Node no found" error message to the client.
83 void node_not_found_error( const string& node_name );
89 PropsChannel::PropsChannel()
94 setTerminator( "\r\n" );
101 PropsChannel::collectIncomingData( const char* s, int n )
103 buffer.append( s, n );
110 PropsChannel::node_not_found_error( const string& node_name )
112 string error = "-ERR Node \"";
114 error += "\" not found.";
115 push( error.c_str() );
116 push( getTerminator() );
124 PropsChannel::foundTerminator()
126 const char* cmd = buffer.getData();
127 SG_LOG( SG_IO, SG_INFO, "processing command = \"" << cmd << "\"" );
129 vector<string> tokens = simgear::strutils::split( cmd );
131 if (!tokens.empty()) {
132 string command = tokens[0];
134 if ( command == "send" ) {
135 command_mgr.add( tokens[1] );
136 } else if ( command == "quit" ) {
140 } else if ( command == "data" ) {
142 } else if ( command == "prompt" ) {
146 Valid commands are:\r\n\
148 data switch to raw data mode\r\n\
149 prompt switch to interactive mode (default)\r\n\
150 quit terminate connection\r\n\
151 send <command> send <command> to UAS\r\n";
156 if (mode == PROMPT) {
157 string prompt = "> ";
158 push( prompt.c_str() );
167 UGTelnet::UGTelnet( const int port_num ):
176 UGTelnet::~UGTelnet()
187 printf("This shouldn't happen, but the telnet channel is already in use, ignoring\n" );
191 simgear::NetChannel::open();
192 simgear::NetChannel::bind( "", port );
193 simgear::NetChannel::listen( 5 );
194 printf("Telnet server started on port %d\n", port );
197 poller.addChannel(this);
208 SG_LOG( SG_IO, SG_INFO, "closing UGTelnet" );
226 UGTelnet::handleAccept()
228 simgear::IPAddress addr;
229 int handle = simgear::NetChannel::accept( &addr );
230 printf("Telent server accepted connection from %s:%d\n",
231 addr.getHost(), addr.getPort() );
232 PropsChannel* channel = new PropsChannel();
233 channel->setHandle( handle );
234 poller.addChannel(channel);