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"
44 #include <private/qpacketprotocol_p.h>
46 #include <QtCore/qdebug.h>
47 #include <QtCore/qstringlist.h>
48 #include <QtNetwork/qnetworkproxy.h>
50 const int protocolVersion = 1;
51 const QString serverId = QLatin1String("QDeclarativeDebugServer");
52 const QString clientId = QLatin1String("QDeclarativeDebugClient");
54 class QQmlDebugClientPrivate
57 QQmlDebugClientPrivate();
60 QQmlDebugConnection *connection;
63 class QQmlDebugConnectionPrivate : public QObject
67 QQmlDebugConnectionPrivate(QQmlDebugConnection *c);
68 QQmlDebugConnection *q;
69 QPacketProtocol *protocol;
73 QHash <QString, float> serverPlugins;
74 QHash<QString, QQmlDebugClient *> plugins;
76 void advertisePlugins();
77 void connectDeviceSignals();
82 void deviceAboutToClose();
85 QQmlDebugConnectionPrivate::QQmlDebugConnectionPrivate(QQmlDebugConnection *c)
86 : QObject(c), q(c), protocol(0), device(0), gotHello(false)
88 protocol = new QPacketProtocol(q, this);
89 QObject::connect(c, SIGNAL(connected()), this, SLOT(connected()));
90 QObject::connect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
93 void QQmlDebugConnectionPrivate::advertisePlugins()
95 if (!q->isConnected())
99 pack << serverId << 1 << plugins.keys();
100 protocol->send(pack);
104 void QQmlDebugConnectionPrivate::connected()
107 pack << serverId << 0 << protocolVersion << plugins.keys();
108 protocol->send(pack);
112 void QQmlDebugConnectionPrivate::readyRead()
115 QPacket pack = protocol->read();
120 bool validHello = false;
121 if (name == clientId) {
127 if (version == protocolVersion) {
128 QStringList pluginNames;
129 QList<float> pluginVersions;
132 pack >> pluginVersions;
134 const int pluginNamesSize = pluginNames.size();
135 const int pluginVersionsSize = pluginVersions.size();
136 for (int i = 0; i < pluginNamesSize; ++i) {
137 float pluginVersion = 1.0;
138 if (i < pluginVersionsSize)
139 pluginVersion = pluginVersions.at(i);
140 serverPlugins.insert(pluginNames.at(i), pluginVersion);
149 qWarning("QQmlDebugConnection: Invalid hello message");
150 QObject::disconnect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
155 QHash<QString, QQmlDebugClient *>::Iterator iter = plugins.begin();
156 for (; iter != plugins.end(); ++iter) {
157 QQmlDebugClient::State newState = QQmlDebugClient::Unavailable;
158 if (serverPlugins.contains(iter.key()))
159 newState = QQmlDebugClient::Enabled;
160 iter.value()->stateChanged(newState);
164 while (protocol->packetsAvailable()) {
165 QPacket pack = protocol->read();
169 if (name == clientId) {
175 QHash<QString, float> oldServerPlugins = serverPlugins;
176 serverPlugins.clear();
178 QStringList pluginNames;
179 QList<float> pluginVersions;
182 pack >> pluginVersions;
184 const int pluginNamesSize = pluginNames.size();
185 const int pluginVersionsSize = pluginVersions.size();
186 for (int i = 0; i < pluginNamesSize; ++i) {
187 float pluginVersion = 1.0;
188 if (i < pluginVersionsSize)
189 pluginVersion = pluginVersions.at(i);
190 serverPlugins.insert(pluginNames.at(i), pluginVersion);
193 QHash<QString, QQmlDebugClient *>::Iterator iter = plugins.begin();
194 for (; iter != plugins.end(); ++iter) {
195 const QString pluginName = iter.key();
196 QQmlDebugClient::State newSate = QQmlDebugClient::Unavailable;
197 if (serverPlugins.contains(pluginName))
198 newSate = QQmlDebugClient::Enabled;
200 if (oldServerPlugins.contains(pluginName)
201 != serverPlugins.contains(pluginName)) {
202 iter.value()->stateChanged(newSate);
206 qWarning() << "QQmlDebugConnection: Unknown control message id" << op;
212 QHash<QString, QQmlDebugClient *>::Iterator iter =
214 if (iter == plugins.end()) {
215 qWarning() << "QQmlDebugConnection: Message received for missing plugin" << name;
217 (*iter)->messageReceived(message);
223 void QQmlDebugConnectionPrivate::deviceAboutToClose()
225 // This is nasty syntax but we want to emit our own aboutToClose signal (by calling QIODevice::close())
226 // without calling the underlying device close fn as that would cause an infinite loop
227 q->QIODevice::close();
230 QQmlDebugConnection::QQmlDebugConnection(QObject *parent)
231 : QIODevice(parent), d(new QQmlDebugConnectionPrivate(this))
235 QQmlDebugConnection::~QQmlDebugConnection()
237 QHash<QString, QQmlDebugClient*>::iterator iter = d->plugins.begin();
238 for (; iter != d->plugins.end(); ++iter) {
239 iter.value()->d->connection = 0;
240 iter.value()->stateChanged(QQmlDebugClient::NotConnected);
244 bool QQmlDebugConnection::isConnected() const
246 return state() == QAbstractSocket::ConnectedState;
249 qint64 QQmlDebugConnection::readData(char *data, qint64 maxSize)
251 return d->device->read(data, maxSize);
254 qint64 QQmlDebugConnection::writeData(const char *data, qint64 maxSize)
256 return d->device->write(data, maxSize);
259 qint64 QQmlDebugConnection::bytesAvailable() const
261 return d->device->bytesAvailable();
264 bool QQmlDebugConnection::isSequential() const
269 void QQmlDebugConnection::close()
274 emit stateChanged(QAbstractSocket::UnconnectedState);
276 QHash<QString, QQmlDebugClient*>::iterator iter = d->plugins.begin();
277 for (; iter != d->plugins.end(); ++iter) {
278 iter.value()->stateChanged(QQmlDebugClient::NotConnected);
283 bool QQmlDebugConnection::waitForConnected(int msecs)
285 QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
287 return socket->waitForConnected(msecs);
291 QAbstractSocket::SocketState QQmlDebugConnection::state() const
293 QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
295 return socket->state();
297 return QAbstractSocket::UnconnectedState;
300 void QQmlDebugConnection::flush()
302 QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
309 void QQmlDebugConnection::connectToHost(const QString &hostName, quint16 port)
311 QTcpSocket *socket = new QTcpSocket(d);
312 socket->setProxy(QNetworkProxy::NoProxy);
314 d->connectDeviceSignals();
316 connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SIGNAL(stateChanged(QAbstractSocket::SocketState)));
317 connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SIGNAL(error(QAbstractSocket::SocketError)));
318 connect(socket, SIGNAL(connected()), this, SIGNAL(connected()));
319 socket->connectToHost(hostName, port);
320 QIODevice::open(ReadWrite | Unbuffered);
323 void QQmlDebugConnectionPrivate::connectDeviceSignals()
325 connect(device, SIGNAL(bytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64)));
326 connect(device, SIGNAL(readyRead()), q, SIGNAL(readyRead()));
327 connect(device, SIGNAL(aboutToClose()), this, SLOT(deviceAboutToClose()));
332 QQmlDebugClientPrivate::QQmlDebugClientPrivate()
337 QQmlDebugClient::QQmlDebugClient(const QString &name,
338 QQmlDebugConnection *parent)
340 d(new QQmlDebugClientPrivate)
343 d->connection = parent;
348 if (d->connection->d->plugins.contains(name)) {
349 qWarning() << "QQmlDebugClient: Conflicting plugin name" << name;
352 d->connection->d->plugins.insert(name, this);
353 d->connection->d->advertisePlugins();
357 QQmlDebugClient::~QQmlDebugClient()
359 if (d->connection && d->connection->d) {
360 d->connection->d->plugins.remove(d->name);
361 d->connection->d->advertisePlugins();
366 QString QQmlDebugClient::name() const
371 float QQmlDebugClient::serviceVersion() const
373 if (d->connection->d->serverPlugins.contains(d->name))
374 return d->connection->d->serverPlugins.value(d->name);
378 QQmlDebugClient::State QQmlDebugClient::state() const
381 || !d->connection->isConnected()
382 || !d->connection->d->gotHello)
385 if (d->connection->d->serverPlugins.contains(d->name))
391 void QQmlDebugClient::sendMessage(const QByteArray &message)
393 if (state() != Enabled)
397 pack << d->name << message;
398 d->connection->d->protocol->send(pack);
399 d->connection->flush();
402 void QQmlDebugClient::stateChanged(State)
406 void QQmlDebugClient::messageReceived(const QByteArray &)
410 #include <qqmldebugclient.moc>