Fork me on GitHub
2.15.20
FGx
A cross-platform launcher for FlightGear
telnetslave.cpp
Go to the documentation of this file.
1 // -=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-
2 // FGx FlightGear Launcher // telnetslave.cpp
3 // -=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-
4 // (c) 2010-2012
5 // Yves Sablonier, Pete Morgan
6 // Geoff McLane
7 // GNU GPLv2, see main.cpp and shipped licence.txt for further information
8 
9 #include <QByteArray>
10 #include <QStringList>
11 
12 #include <QTcpSocket>
13 #include <QHostAddress>
14 
15 #include "telnetslave.h"
16 
17 
19  QObject(parent)
20 {
21 
22  current_node_path = "";
23  in_request = false;
24 
25 
26  socket = new QTcpSocket(this);
27 
28  connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
29  this, SLOT(on_error( QAbstractSocket::SocketError))
30  );
31  connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
32  this, SLOT(on_state_changed(QAbstractSocket::SocketState))
33  );
34  connect(socket, SIGNAL(hostFound()), this, SLOT(on_host_found()));
35  connect(socket, SIGNAL(connected()), this, SLOT(on_connected()));
36  connect(socket, SIGNAL(disconnected()), this, SLOT(on_disconnected()));
37  connect(socket, SIGNAL(readyRead()), this, SLOT(on_ready_read()));
38 }
39 
40 //*********************************************************************************************
41  //** Connect / Disconnect
42 //********************************************************************************************
43 void TelnetSlave::telnet_connect(QString address, int portt){
44  hostAddress = address;
45  port = portt;
46  socket->connectToHost(hostAddress, port);
47  //qDebug() << "open" << hostAddress << port;
48 }
49 
50 
51 
53  socket->close();
54 }
55 
56 void TelnetSlave::set_property(QString path, QString value){
57  //TODO
58  //qDebug() << path << "=" << value;
59  QByteArray command("set ");
60  command.append(path).append(" ").append(value).append("\r\n");
61  //QByteArray cmd(command);
62  qDebug() << command;
63  //*** TODO #### ARGH.. this crashes with ASSERT failure in QList<T>::operator[]: "index out of range", file /usr/include/qt4/qlist.h, line 463
64  socket->write( command );
65 }
66 
67 void TelnetSlave::add_que(QString node)
68 {
69  Q_UNUSED(node);
70 }
71 
72 //==========================================================================================
73 //== Get Node
74 //==========================================================================================
75 void TelnetSlave::get_node(QString node_path){
76  if( !socket->isOpen() ){
77  // TODO emit error maybe ?
78  return;
79  }
80  current_node_path = node_path; // << store the "node_path" in the current_node_path vars
81  QByteArray command = QByteArray("ls ").append(node_path).append("\r\n");
82  //** Send request to telnet socket
83  if(in_request){
84  qDebug() << "IN_REQUEST";
85  qDebug() << socket->bytesToWrite();
86  return;
87  }
88  in_request = true;
89  socket->write( command );
90  }
91 
92 
93 //************************************************************
94 //*** READ READY
95 
97 
98  QString reply(socket->readAll());
99  QStringList lines = reply.split("\r\n");
100 
101  //qDebug() << reply;
102  for(int i = 0; i < lines.size(); ++i){
103  QString line = lines.at(i).trimmed();
104  // qDebug() << "line=" << line;
105 
106  //= end line is /> so skip
107  if(line == "/>"){
108  qDebug("END");
109 
110  }else if( line.endsWith("/") ){
111  emit props_folder(current_node_path, line);
112 
113  }else{
114  if(line.count("chat") > 0){
115  qDebug() << "CHAT=" << line << "=" << line.count("chat") ;
116  }
117  // check the = sign is there.
118  if( line.count("=") > 0 ){
119  //* eg right-aileron-pos-norm =\t'0.02699490675'\t(double)
120  //QString val_part = line.section("=", 0, 0).trimmed();
121  //* Split the values on a tab
122  QStringList val_parts = line.split("\t");
123  //ssqDebug() << val_parts;
124 
125  //TODO - maybe some regex
126  //** the node_name ends with " =" so remove eg "my-node ="
127  QString node_name = val_parts[0].left( val_parts[0].length() - 2 );
128 
129  //== node value in enclosed in ' so remove eg 'true'
130  QString node_value = val_parts.size() == 1 ?
131  "" :
132  val_parts[1].replace("'","");
133 
134  //== the node_type is encoded in () eg (double)
135  QString node_type = val_parts.size() == 2 ?
136  "" :
137  val_parts[2].mid(1, val_parts[2].length() - 2);
138 
139  // QString node_name = val_parts[0].trimmed();
140  //qDebug() << "VAL=" << node_name << " = " << node_value << "=" << node_type;
141  //qDebug();
142  emit props_value(current_node_path, node_name, node_value, node_type);
143  }else{
144  qDebug() << "UMM=" << line << "=" << line.count("=") ;
145  //qDebug() << reply;
146  }
147  }
148  } /* for */
149  in_request = false;
150 }
151 
152 //*********************************************************************************************
153  //** Socket Events
154 //*********************************************************************************************
155 
157  qDebug("on_host_found");
158 }
159 
161  qDebug("on_telnet_connected");
162  emit telnet_connected(true);
163 }
164 
166  qDebug("on_telnet_disconnected"); // << "DONE" << telnet_reply;
167  emit telnet_connected(false);
168 }
169 
170 void TelnetSlave::on_error(QAbstractSocket::SocketError socketError){
171  qDebug("on_error");
172  qDebug() << socket->errorString();
173  emit telnet_error(socketError, socket->errorString());
174 }
175 
176 
177 
178 void TelnetSlave::on_state_changed(QAbstractSocket::SocketState socketState ){
179  //qDebug("on_state_changed");
180  qDebug() << "state=" << socketState;
181  Q_UNUSED(socketState);
182 }
void props_value(QString, QString, QString, QString)
QString current_node_path
Definition: telnetslave.h:36
void on_ready_read()
Definition: telnetslave.cpp:96
void props_folder(QString, QString)
void on_connected()
void on_host_found()
void on_error(QAbstractSocket::SocketError)
void get_node(QString path)
Definition: telnetslave.cpp:75
void on_disconnected()
QTcpSocket * socket
Definition: telnetslave.h:48
bool in_request
Definition: telnetslave.h:38
void telnet_connect(QString address, int port)
Definition: telnetslave.cpp:43
void add_que(QString node)
Definition: telnetslave.cpp:67
void on_state_changed(QAbstractSocket::SocketState)
void telnet_disconnect()
Definition: telnetslave.cpp:52
TelnetSlave(QObject *parent=0)
Definition: telnetslave.cpp:18
void telnet_error(QAbstractSocket::SocketError, QString)
void set_property(QString path, QString value)
Definition: telnetslave.cpp:56
QString hostAddress
Definition: telnetslave.h:32
void telnet_connected(bool)