Debugger: Improve output of autotests
[profile/ivi/qtdeclarative.git] / tests / auto / qml / debugger / shared / qqmldebugclient.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qqmldebugclient.h"
43 #include "../../../../../src/plugins/qmltooling/shared/qpacketprotocol.h"
44
45 #include <QtCore/qdebug.h>
46 #include <QtCore/qeventloop.h>
47 #include <QtCore/qstringlist.h>
48 #include <QtCore/qtimer.h>
49 #include <QtNetwork/qnetworkproxy.h>
50
51 const int protocolVersion = 1;
52 const QString serverId = QLatin1String("QDeclarativeDebugServer");
53 const QString clientId = QLatin1String("QDeclarativeDebugClient");
54
55 class QQmlDebugClientPrivate
56 {
57 public:
58     QQmlDebugClientPrivate();
59
60     QString name;
61     QQmlDebugConnection *connection;
62 };
63
64 class QQmlDebugConnectionPrivate : public QObject
65 {
66     Q_OBJECT
67 public:
68     QQmlDebugConnectionPrivate(QQmlDebugConnection *c);
69     QQmlDebugConnection *q;
70     QPacketProtocol *protocol;
71     QIODevice *device;
72     QEventLoop handshakeEventLoop;
73     QTimer handshakeTimer;
74
75     bool gotHello;
76     QHash <QString, float> serverPlugins;
77     QHash<QString, QQmlDebugClient *> plugins;
78
79     void advertisePlugins();
80     void connectDeviceSignals();
81
82 public Q_SLOTS:
83     void connected();
84     void readyRead();
85     void deviceAboutToClose();
86     void handshakeTimeout();
87 };
88
89 QQmlDebugConnectionPrivate::QQmlDebugConnectionPrivate(QQmlDebugConnection *c)
90     : QObject(c), q(c), protocol(0), device(0), gotHello(false)
91 {
92     protocol = new QPacketProtocol(q, this);
93     QObject::connect(c, SIGNAL(connected()), this, SLOT(connected()));
94     QObject::connect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
95
96     handshakeTimer.setSingleShot(true);
97     handshakeTimer.setInterval(3000);
98     connect(&handshakeTimer, SIGNAL(timeout()), SLOT(handshakeTimeout()));
99 }
100
101 void QQmlDebugConnectionPrivate::advertisePlugins()
102 {
103     if (!q->isConnected())
104         return;
105
106     QPacket pack;
107     pack << serverId << 1 << plugins.keys();
108     protocol->send(pack);
109     q->flush();
110 }
111
112 void QQmlDebugConnectionPrivate::connected()
113 {
114     QPacket pack;
115     pack << serverId << 0 << protocolVersion << plugins.keys();
116     protocol->send(pack);
117     q->flush();
118 }
119
120 void QQmlDebugConnectionPrivate::readyRead()
121 {
122     if (!gotHello) {
123         QPacket pack = protocol->read();
124         QString name;
125
126         pack >> name;
127
128         bool validHello = false;
129         if (name == clientId) {
130             int op = -1;
131             pack >> op;
132             if (op == 0) {
133                 int version = -1;
134                 pack >> version;
135                 if (version == protocolVersion) {
136                     QStringList pluginNames;
137                     QList<float> pluginVersions;
138                     pack >> pluginNames;
139                     if (!pack.isEmpty())
140                         pack >> pluginVersions;
141
142                     const int pluginNamesSize = pluginNames.size();
143                     const int pluginVersionsSize = pluginVersions.size();
144                     for (int i = 0; i < pluginNamesSize; ++i) {
145                         float pluginVersion = 1.0;
146                         if (i < pluginVersionsSize)
147                             pluginVersion = pluginVersions.at(i);
148                         serverPlugins.insert(pluginNames.at(i), pluginVersion);
149                     }
150
151                     validHello = true;
152                 }
153             }
154         }
155
156         if (!validHello) {
157             qWarning("QQmlDebugConnection: Invalid hello message");
158             QObject::disconnect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
159             return;
160         }
161         gotHello = true;
162
163         QHash<QString, QQmlDebugClient *>::Iterator iter = plugins.begin();
164         for (; iter != plugins.end(); ++iter) {
165             QQmlDebugClient::State newState = QQmlDebugClient::Unavailable;
166             if (serverPlugins.contains(iter.key()))
167                 newState = QQmlDebugClient::Enabled;
168             iter.value()->stateChanged(newState);
169         }
170
171         handshakeTimer.stop();
172         handshakeEventLoop.quit();
173     }
174
175     while (protocol->packetsAvailable()) {
176         QPacket pack = protocol->read();
177         QString name;
178         pack >> name;
179
180         if (name == clientId) {
181             int op = -1;
182             pack >> op;
183
184             if (op == 1) {
185                 // Service Discovery
186                 QHash<QString, float> oldServerPlugins = serverPlugins;
187                 serverPlugins.clear();
188
189                 QStringList pluginNames;
190                 QList<float> pluginVersions;
191                 pack >> pluginNames;
192                 if (!pack.isEmpty())
193                     pack >> pluginVersions;
194
195                 const int pluginNamesSize = pluginNames.size();
196                 const int pluginVersionsSize = pluginVersions.size();
197                 for (int i = 0; i < pluginNamesSize; ++i) {
198                     float pluginVersion = 1.0;
199                     if (i < pluginVersionsSize)
200                         pluginVersion = pluginVersions.at(i);
201                     serverPlugins.insert(pluginNames.at(i), pluginVersion);
202                 }
203
204                 QHash<QString, QQmlDebugClient *>::Iterator iter = plugins.begin();
205                 for (; iter != plugins.end(); ++iter) {
206                     const QString pluginName = iter.key();
207                     QQmlDebugClient::State newSate = QQmlDebugClient::Unavailable;
208                     if (serverPlugins.contains(pluginName))
209                         newSate = QQmlDebugClient::Enabled;
210
211                     if (oldServerPlugins.contains(pluginName)
212                             != serverPlugins.contains(pluginName)) {
213                         iter.value()->stateChanged(newSate);
214                     }
215                 }
216             } else {
217                 qWarning() << "QQmlDebugConnection: Unknown control message id" << op;
218             }
219         } else {
220             QByteArray message;
221             pack >> message;
222
223             QHash<QString, QQmlDebugClient *>::Iterator iter =
224                     plugins.find(name);
225             if (iter == plugins.end()) {
226                 qWarning() << "QQmlDebugConnection: Message received for missing plugin" << name;
227             } else {
228                 (*iter)->messageReceived(message);
229             }
230         }
231     }
232 }
233
234 void QQmlDebugConnectionPrivate::deviceAboutToClose()
235 {
236     // This is nasty syntax but we want to emit our own aboutToClose signal (by calling QIODevice::close())
237     // without calling the underlying device close fn as that would cause an infinite loop
238     q->QIODevice::close();
239 }
240
241 void QQmlDebugConnectionPrivate::handshakeTimeout()
242 {
243     if (!gotHello) {
244         qWarning() << "Qml Debug Client: Did not get handshake answer in time";
245         handshakeEventLoop.quit();
246     }
247 }
248
249 QQmlDebugConnection::QQmlDebugConnection(QObject *parent)
250     : QIODevice(parent), d(new QQmlDebugConnectionPrivate(this))
251 {
252 }
253
254 QQmlDebugConnection::~QQmlDebugConnection()
255 {
256     QHash<QString, QQmlDebugClient*>::iterator iter = d->plugins.begin();
257     for (; iter != d->plugins.end(); ++iter) {
258         iter.value()->d->connection = 0;
259         iter.value()->stateChanged(QQmlDebugClient::NotConnected);
260     }
261 }
262
263 bool QQmlDebugConnection::isConnected() const
264 {
265     return state() == QAbstractSocket::ConnectedState;
266 }
267
268 qint64 QQmlDebugConnection::readData(char *data, qint64 maxSize)
269 {
270     return d->device->read(data, maxSize);
271 }
272
273 qint64 QQmlDebugConnection::writeData(const char *data, qint64 maxSize)
274 {
275     return d->device->write(data, maxSize);
276 }
277
278 qint64 QQmlDebugConnection::bytesAvailable() const
279 {
280     return d->device->bytesAvailable();
281 }
282
283 bool QQmlDebugConnection::isSequential() const
284 {
285     return true;
286 }
287
288 void QQmlDebugConnection::close()
289 {
290     if (isOpen()) {
291         QIODevice::close();
292         d->device->close();
293         emit stateChanged(QAbstractSocket::UnconnectedState);
294
295         QHash<QString, QQmlDebugClient*>::iterator iter = d->plugins.begin();
296         for (; iter != d->plugins.end(); ++iter) {
297             iter.value()->stateChanged(QQmlDebugClient::NotConnected);
298         }
299     }
300 }
301
302 bool QQmlDebugConnection::waitForConnected(int msecs)
303 {
304     QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
305     if (!socket)
306         return false;
307     if (!socket->waitForConnected(msecs))
308         return false;
309     // wait for handshake
310     d->handshakeTimer.start();
311     d->handshakeEventLoop.exec();
312     return d->gotHello;
313 }
314
315 QAbstractSocket::SocketState QQmlDebugConnection::state() const
316 {
317     QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
318     if (socket)
319         return socket->state();
320
321     return QAbstractSocket::UnconnectedState;
322 }
323
324 void QQmlDebugConnection::flush()
325 {
326     QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
327     if (socket) {
328         socket->flush();
329         return;
330     }
331 }
332
333 void QQmlDebugConnection::connectToHost(const QString &hostName, quint16 port)
334 {
335     QTcpSocket *socket = new QTcpSocket(d);
336     socket->setProxy(QNetworkProxy::NoProxy);
337     d->device = socket;
338     d->connectDeviceSignals();
339     d->gotHello = false;
340     connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SIGNAL(stateChanged(QAbstractSocket::SocketState)));
341     connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SIGNAL(error(QAbstractSocket::SocketError)));
342     connect(socket, SIGNAL(connected()), this, SIGNAL(connected()));
343     socket->connectToHost(hostName, port);
344     QIODevice::open(ReadWrite | Unbuffered);
345 }
346
347 void QQmlDebugConnectionPrivate::connectDeviceSignals()
348 {
349     connect(device, SIGNAL(bytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64)));
350     connect(device, SIGNAL(readyRead()), q, SIGNAL(readyRead()));
351     connect(device, SIGNAL(aboutToClose()), this, SLOT(deviceAboutToClose()));
352 }
353
354 //
355
356 QQmlDebugClientPrivate::QQmlDebugClientPrivate()
357     : connection(0)
358 {
359 }
360
361 QQmlDebugClient::QQmlDebugClient(const QString &name, 
362                                                  QQmlDebugConnection *parent)
363     : QObject(parent),
364       d(new QQmlDebugClientPrivate)
365 {
366     d->name = name;
367     d->connection = parent;
368
369     if (!d->connection)
370         return;
371
372     if (d->connection->d->plugins.contains(name)) {
373         qWarning() << "QQmlDebugClient: Conflicting plugin name" << name;
374         d->connection = 0;
375     } else {
376         d->connection->d->plugins.insert(name, this);
377         d->connection->d->advertisePlugins();
378     }
379 }
380
381 QQmlDebugClient::~QQmlDebugClient()
382 {
383     if (d->connection && d->connection->d) {
384         d->connection->d->plugins.remove(d->name);
385         d->connection->d->advertisePlugins();
386     }
387     delete d;
388 }
389
390 QString QQmlDebugClient::name() const
391 {
392     return d->name;
393 }
394
395 float QQmlDebugClient::serviceVersion() const
396 {
397     if (d->connection->d->serverPlugins.contains(d->name))
398         return d->connection->d->serverPlugins.value(d->name);
399     return -1;
400 }
401
402 QQmlDebugClient::State QQmlDebugClient::state() const
403 {
404     if (!d->connection
405             || !d->connection->isConnected()
406             || !d->connection->d->gotHello)
407         return NotConnected;
408
409     if (d->connection->d->serverPlugins.contains(d->name))
410         return Enabled;
411
412     return Unavailable;
413 }
414
415 void QQmlDebugClient::sendMessage(const QByteArray &message)
416 {
417     if (state() != Enabled)
418         return;
419
420     QPacket pack;
421     pack << d->name << message;
422     d->connection->d->protocol->send(pack);
423     d->connection->flush();
424 }
425
426 void QQmlDebugClient::stateChanged(State)
427 {
428 }
429
430 void QQmlDebugClient::messageReceived(const QByteArray &)
431 {
432 }
433
434 #include <qqmldebugclient.moc>