1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qqmldebugclient.h"
43 #include "../../../../../src/plugins/qmltooling/shared/qpacketprotocol.h"
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>
51 const int protocolVersion = 1;
52 const QString serverId = QLatin1String("QDeclarativeDebugServer");
53 const QString clientId = QLatin1String("QDeclarativeDebugClient");
55 class QQmlDebugClientPrivate
58 QQmlDebugClientPrivate();
61 QQmlDebugConnection *connection;
64 class QQmlDebugConnectionPrivate : public QObject
68 QQmlDebugConnectionPrivate(QQmlDebugConnection *c);
69 QQmlDebugConnection *q;
70 QPacketProtocol *protocol;
72 QEventLoop handshakeEventLoop;
73 QTimer handshakeTimer;
76 QHash <QString, float> serverPlugins;
77 QHash<QString, QQmlDebugClient *> plugins;
79 void advertisePlugins();
80 void connectDeviceSignals();
85 void deviceAboutToClose();
86 void handshakeTimeout();
89 QQmlDebugConnectionPrivate::QQmlDebugConnectionPrivate(QQmlDebugConnection *c)
90 : QObject(c), q(c), protocol(0), device(0), gotHello(false)
92 protocol = new QPacketProtocol(q, this);
93 QObject::connect(c, SIGNAL(connected()), this, SLOT(connected()));
94 QObject::connect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
96 handshakeTimer.setSingleShot(true);
97 handshakeTimer.setInterval(3000);
98 connect(&handshakeTimer, SIGNAL(timeout()), SLOT(handshakeTimeout()));
101 void QQmlDebugConnectionPrivate::advertisePlugins()
103 if (!q->isConnected())
107 pack << serverId << 1 << plugins.keys();
108 protocol->send(pack);
112 void QQmlDebugConnectionPrivate::connected()
115 pack << serverId << 0 << protocolVersion << plugins.keys();
116 protocol->send(pack);
120 void QQmlDebugConnectionPrivate::readyRead()
123 QPacket pack = protocol->read();
128 bool validHello = false;
129 if (name == clientId) {
135 if (version == protocolVersion) {
136 QStringList pluginNames;
137 QList<float> pluginVersions;
140 pack >> pluginVersions;
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);
157 qWarning("QQmlDebugConnection: Invalid hello message");
158 QObject::disconnect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
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);
171 handshakeTimer.stop();
172 handshakeEventLoop.quit();
175 while (protocol->packetsAvailable()) {
176 QPacket pack = protocol->read();
180 if (name == clientId) {
186 QHash<QString, float> oldServerPlugins = serverPlugins;
187 serverPlugins.clear();
189 QStringList pluginNames;
190 QList<float> pluginVersions;
193 pack >> pluginVersions;
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);
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;
211 if (oldServerPlugins.contains(pluginName)
212 != serverPlugins.contains(pluginName)) {
213 iter.value()->stateChanged(newSate);
217 qWarning() << "QQmlDebugConnection: Unknown control message id" << op;
223 QHash<QString, QQmlDebugClient *>::Iterator iter =
225 if (iter == plugins.end()) {
226 qWarning() << "QQmlDebugConnection: Message received for missing plugin" << name;
228 (*iter)->messageReceived(message);
234 void QQmlDebugConnectionPrivate::deviceAboutToClose()
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();
241 void QQmlDebugConnectionPrivate::handshakeTimeout()
244 qWarning() << "Qml Debug Client: Did not get handshake answer in time";
245 handshakeEventLoop.quit();
249 QQmlDebugConnection::QQmlDebugConnection(QObject *parent)
250 : QIODevice(parent), d(new QQmlDebugConnectionPrivate(this))
254 QQmlDebugConnection::~QQmlDebugConnection()
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);
263 bool QQmlDebugConnection::isConnected() const
265 return state() == QAbstractSocket::ConnectedState;
268 qint64 QQmlDebugConnection::readData(char *data, qint64 maxSize)
270 return d->device->read(data, maxSize);
273 qint64 QQmlDebugConnection::writeData(const char *data, qint64 maxSize)
275 return d->device->write(data, maxSize);
278 qint64 QQmlDebugConnection::bytesAvailable() const
280 return d->device->bytesAvailable();
283 bool QQmlDebugConnection::isSequential() const
288 void QQmlDebugConnection::close()
293 emit stateChanged(QAbstractSocket::UnconnectedState);
295 QHash<QString, QQmlDebugClient*>::iterator iter = d->plugins.begin();
296 for (; iter != d->plugins.end(); ++iter) {
297 iter.value()->stateChanged(QQmlDebugClient::NotConnected);
302 bool QQmlDebugConnection::waitForConnected(int msecs)
304 QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
307 if (!socket->waitForConnected(msecs))
309 // wait for handshake
310 d->handshakeTimer.start();
311 d->handshakeEventLoop.exec();
315 QAbstractSocket::SocketState QQmlDebugConnection::state() const
317 QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
319 return socket->state();
321 return QAbstractSocket::UnconnectedState;
324 void QQmlDebugConnection::flush()
326 QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
333 void QQmlDebugConnection::connectToHost(const QString &hostName, quint16 port)
335 QTcpSocket *socket = new QTcpSocket(d);
336 socket->setProxy(QNetworkProxy::NoProxy);
338 d->connectDeviceSignals();
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);
347 void QQmlDebugConnectionPrivate::connectDeviceSignals()
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()));
356 QQmlDebugClientPrivate::QQmlDebugClientPrivate()
361 QQmlDebugClient::QQmlDebugClient(const QString &name,
362 QQmlDebugConnection *parent)
364 d(new QQmlDebugClientPrivate)
367 d->connection = parent;
372 if (d->connection->d->plugins.contains(name)) {
373 qWarning() << "QQmlDebugClient: Conflicting plugin name" << name;
376 d->connection->d->plugins.insert(name, this);
377 d->connection->d->advertisePlugins();
381 QQmlDebugClient::~QQmlDebugClient()
383 if (d->connection && d->connection->d) {
384 d->connection->d->plugins.remove(d->name);
385 d->connection->d->advertisePlugins();
390 QString QQmlDebugClient::name() const
395 float QQmlDebugClient::serviceVersion() const
397 if (d->connection->d->serverPlugins.contains(d->name))
398 return d->connection->d->serverPlugins.value(d->name);
402 QQmlDebugClient::State QQmlDebugClient::state() const
405 || !d->connection->isConnected()
406 || !d->connection->d->gotHello)
409 if (d->connection->d->serverPlugins.contains(d->name))
415 void QQmlDebugClient::sendMessage(const QByteArray &message)
417 if (state() != Enabled)
421 pack << d->name << message;
422 d->connection->d->protocol->send(pack);
423 d->connection->flush();
426 void QQmlDebugClient::stateChanged(State)
430 void QQmlDebugClient::messageReceived(const QByteArray &)
434 #include <qqmldebugclient.moc>