| 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 | #include <simgear/debug/logstream.hxx> |
| 30 | |
| 31 | #include <cstdio> |
| 32 | #include <sstream> |
| 33 | |
| 34 | #include "UGear_command.hxx" |
| 35 | #include "UGear_telnet.hxx" |
| 36 | |
| 37 | using std::stringstream; |
| 38 | using std::ends; |
| 39 | |
| 40 | /** |
| 41 | * Props connection class. |
| 42 | * This class represents a connection to props client. |
| 43 | */ |
| 44 | class PropsChannel : public simgear::NetChat |
| 45 | { |
| 46 | simgear::NetBuffer buffer; |
| 47 | |
| 48 | /** |
| 49 | * Current property node name. |
| 50 | */ |
| 51 | string path; |
| 52 | |
| 53 | enum Mode { |
| 54 | PROMPT, |
| 55 | DATA |
| 56 | }; |
| 57 | Mode mode; |
| 58 | |
| 59 | public: |
| 60 | |
| 61 | /** |
| 62 | * Constructor. |
| 63 | */ |
| 64 | PropsChannel(); |
| 65 | |
| 66 | /** |
| 67 | * Append incoming data to our request buffer. |
| 68 | * |
| 69 | * @param s Character string to append to buffer |
| 70 | * @param n Number of characters to append. |
| 71 | */ |
| 72 | void collectIncomingData( const char* s, int n ); |
| 73 | |
| 74 | /** |
| 75 | * Process a complete request from the props client. |
| 76 | */ |
| 77 | void foundTerminator(); |
| 78 | |
| 79 | private: |
| 80 | /** |
| 81 | * Return a "Node no found" error message to the client. |
| 82 | */ |
| 83 | void node_not_found_error( const string& node_name ); |
| 84 | }; |
| 85 | |
| 86 | /** |
| 87 | * |
| 88 | */ |
| 89 | PropsChannel::PropsChannel() |
| 90 | : buffer(512), |
| 91 | path("/"), |
| 92 | mode(PROMPT) |
| 93 | { |
| 94 | setTerminator( "\r\n" ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * |
| 99 | */ |
| 100 | void |
| 101 | PropsChannel::collectIncomingData( const char* s, int n ) |
| 102 | { |
| 103 | buffer.append( s, n ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * |
| 108 | */ |
| 109 | void |
| 110 | PropsChannel::node_not_found_error( const string& node_name ) |
| 111 | { |
| 112 | string error = "-ERR Node \""; |
| 113 | error += node_name; |
| 114 | error += "\" not found."; |
| 115 | push( error.c_str() ); |
| 116 | push( getTerminator() ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * We have a command. |
| 121 | * |
| 122 | */ |
| 123 | void |
| 124 | PropsChannel::foundTerminator() |
| 125 | { |
| 126 | const char* cmd = buffer.getData(); |
| 127 | SG_LOG( SG_IO, SG_INFO, "processing command = \"" << cmd << "\"" ); |
| 128 | |
| 129 | std::vector<std::string> tokens = simgear::strutils::split( cmd ); |
| 130 | |
| 131 | if (!tokens.empty()) { |
| 132 | string command = tokens[0]; |
| 133 | |
| 134 | if ( command == "send" ) { |
| 135 | command_mgr.add( tokens[1] ); |
| 136 | } else if ( command == "quit" ) { |
| 137 | close(); |
| 138 | shouldDelete(); |
| 139 | return; |
| 140 | } else if ( command == "data" ) { |
| 141 | mode = DATA; |
| 142 | } else if ( command == "prompt" ) { |
| 143 | mode = PROMPT; |
| 144 | } else { |
| 145 | const char* msg = "\ |
| 146 | Valid commands are:\r\n\ |
| 147 | \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"; |
| 152 | push( msg ); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if (mode == PROMPT) { |
| 157 | string prompt = "> "; |
| 158 | push( prompt.c_str() ); |
| 159 | } |
| 160 | |
| 161 | buffer.remove(); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * |
| 166 | */ |
| 167 | UGTelnet::UGTelnet( const int port_num ): |
| 168 | enabled(false) |
| 169 | { |
| 170 | port = port_num; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * |
| 175 | */ |
| 176 | UGTelnet::~UGTelnet() |
| 177 | { |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * |
| 182 | */ |
| 183 | bool |
| 184 | UGTelnet::open() |
| 185 | { |
| 186 | if (enabled ) { |
| 187 | printf("This shouldn't happen, but the telnet channel is already in use, ignoring\n" ); |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | simgear::NetChannel::open(); |
| 192 | simgear::NetChannel::bind( "", port ); |
| 193 | simgear::NetChannel::listen( 5 ); |
| 194 | printf("Telnet server started on port %d\n", port ); |
| 195 | |
| 196 | enabled = true; |
| 197 | poller.addChannel(this); |
| 198 | |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * |
| 204 | */ |
| 205 | bool |
| 206 | UGTelnet::close() |
| 207 | { |
| 208 | SG_LOG( SG_IO, SG_INFO, "closing UGTelnet" ); |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * |
| 214 | */ |
| 215 | bool |
| 216 | UGTelnet::process() |
| 217 | { |
| 218 | poller.poll(); |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * |
| 224 | */ |
| 225 | void |
| 226 | UGTelnet::handleAccept() |
| 227 | { |
| 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); |
| 235 | } |