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_p.h"
44 #include "qpacketprotocol_p.h"
46 #include <QtCore/qdebug.h>
47 #include <QtCore/qstringlist.h>
48 #include <QtNetwork/qnetworkproxy.h>
50 #include <private/qobject_p.h>
54 const int protocolVersion = 1;
55 const QString serverId = QLatin1String("QDeclarativeDebugServer");
56 const QString clientId = QLatin1String("QDeclarativeDebugClient");
58 class QQmlDebugClientPrivate : public QObjectPrivate
60 Q_DECLARE_PUBLIC(QQmlDebugClient)
62 QQmlDebugClientPrivate();
65 QQmlDebugConnection *connection;
68 class QQmlDebugConnectionPrivate : public QObject
72 QQmlDebugConnectionPrivate(QQmlDebugConnection *c);
73 QQmlDebugConnection *q;
74 QPacketProtocol *protocol;
78 QHash <QString, float> serverPlugins;
79 QHash<QString, QQmlDebugClient *> plugins;
81 void advertisePlugins();
82 void connectDeviceSignals();
87 void deviceAboutToClose();
90 QQmlDebugConnectionPrivate::QQmlDebugConnectionPrivate(QQmlDebugConnection *c)
91 : QObject(c), q(c), protocol(0), device(0), gotHello(false)
93 protocol = new QPacketProtocol(q, this);
94 QObject::connect(c, SIGNAL(connected()), this, SLOT(connected()));
95 QObject::connect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
98 void QQmlDebugConnectionPrivate::advertisePlugins()
100 if (!q->isConnected())
104 pack << serverId << 1 << plugins.keys();
105 protocol->send(pack);
109 void QQmlDebugConnectionPrivate::connected()
112 pack << serverId << 0 << protocolVersion << plugins.keys();
113 protocol->send(pack);
117 void QQmlDebugConnectionPrivate::readyRead()
120 QPacket pack = protocol->read();
125 bool validHello = false;
126 if (name == clientId) {
132 if (version == protocolVersion) {
133 QStringList pluginNames;
134 QList<float> pluginVersions;
137 pack >> pluginVersions;
139 const int pluginNamesSize = pluginNames.size();
140 const int pluginVersionsSize = pluginVersions.size();
141 for (int i = 0; i < pluginNamesSize; ++i) {
142 float pluginVersion = 1.0;
143 if (i < pluginVersionsSize)
144 pluginVersion = pluginVersions.at(i);
145 serverPlugins.insert(pluginNames.at(i), pluginVersion);
154 qWarning("QQmlDebugConnection: Invalid hello message");
155 QObject::disconnect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
160 QHash<QString, QQmlDebugClient *>::Iterator iter = plugins.begin();
161 for (; iter != plugins.end(); ++iter) {
162 QQmlDebugClient::State newState = QQmlDebugClient::Unavailable;
163 if (serverPlugins.contains(iter.key()))
164 newState = QQmlDebugClient::Enabled;
165 iter.value()->stateChanged(newState);
169 while (protocol->packetsAvailable()) {
170 QPacket pack = protocol->read();
174 if (name == clientId) {
180 QHash<QString, float> oldServerPlugins = serverPlugins;
181 serverPlugins.clear();
183 QStringList pluginNames;
184 QList<float> pluginVersions;
187 pack >> pluginVersions;
189 const int pluginNamesSize = pluginNames.size();
190 const int pluginVersionsSize = pluginVersions.size();
191 for (int i = 0; i < pluginNamesSize; ++i) {
192 float pluginVersion = 1.0;
193 if (i < pluginVersionsSize)
194 pluginVersion = pluginVersions.at(i);
195 serverPlugins.insert(pluginNames.at(i), pluginVersion);
198 QHash<QString, QQmlDebugClient *>::Iterator iter = plugins.begin();
199 for (; iter != plugins.end(); ++iter) {
200 const QString pluginName = iter.key();
201 QQmlDebugClient::State newSate = QQmlDebugClient::Unavailable;
202 if (serverPlugins.contains(pluginName))
203 newSate = QQmlDebugClient::Enabled;
205 if (oldServerPlugins.contains(pluginName)
206 != serverPlugins.contains(pluginName)) {
207 iter.value()->stateChanged(newSate);
211 qWarning() << "QQmlDebugConnection: Unknown control message id" << op;
217 QHash<QString, QQmlDebugClient *>::Iterator iter =
219 if (iter == plugins.end()) {
220 qWarning() << "QQmlDebugConnection: Message received for missing plugin" << name;
222 (*iter)->messageReceived(message);
228 void QQmlDebugConnectionPrivate::deviceAboutToClose()
230 // This is nasty syntax but we want to emit our own aboutToClose signal (by calling QIODevice::close())
231 // without calling the underlying device close fn as that would cause an infinite loop
232 q->QIODevice::close();
235 QQmlDebugConnection::QQmlDebugConnection(QObject *parent)
236 : QIODevice(parent), d(new QQmlDebugConnectionPrivate(this))
240 QQmlDebugConnection::~QQmlDebugConnection()
242 QHash<QString, QQmlDebugClient*>::iterator iter = d->plugins.begin();
243 for (; iter != d->plugins.end(); ++iter) {
244 iter.value()->d_func()->connection = 0;
245 iter.value()->stateChanged(QQmlDebugClient::NotConnected);
249 bool QQmlDebugConnection::isConnected() const
251 return state() == QAbstractSocket::ConnectedState;
254 qint64 QQmlDebugConnection::readData(char *data, qint64 maxSize)
256 return d->device->read(data, maxSize);
259 qint64 QQmlDebugConnection::writeData(const char *data, qint64 maxSize)
261 return d->device->write(data, maxSize);
264 qint64 QQmlDebugConnection::bytesAvailable() const
266 return d->device->bytesAvailable();
269 bool QQmlDebugConnection::isSequential() const
274 void QQmlDebugConnection::close()
279 emit stateChanged(QAbstractSocket::UnconnectedState);
281 QHash<QString, QQmlDebugClient*>::iterator iter = d->plugins.begin();
282 for (; iter != d->plugins.end(); ++iter) {
283 iter.value()->stateChanged(QQmlDebugClient::NotConnected);
288 bool QQmlDebugConnection::waitForConnected(int msecs)
290 QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
292 return socket->waitForConnected(msecs);
296 QAbstractSocket::SocketState QQmlDebugConnection::state() const
298 QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
300 return socket->state();
302 return QAbstractSocket::UnconnectedState;
305 void QQmlDebugConnection::flush()
307 QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
314 void QQmlDebugConnection::connectToHost(const QString &hostName, quint16 port)
316 QTcpSocket *socket = new QTcpSocket(d);
317 socket->setProxy(QNetworkProxy::NoProxy);
319 d->connectDeviceSignals();
321 connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SIGNAL(stateChanged(QAbstractSocket::SocketState)));
322 connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SIGNAL(error(QAbstractSocket::SocketError)));
323 connect(socket, SIGNAL(connected()), this, SIGNAL(connected()));
324 socket->connectToHost(hostName, port);
325 QIODevice::open(ReadWrite | Unbuffered);
328 void QQmlDebugConnectionPrivate::connectDeviceSignals()
330 connect(device, SIGNAL(bytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64)));
331 connect(device, SIGNAL(readyRead()), q, SIGNAL(readyRead()));
332 connect(device, SIGNAL(aboutToClose()), this, SLOT(deviceAboutToClose()));
337 QQmlDebugClientPrivate::QQmlDebugClientPrivate()
342 QQmlDebugClient::QQmlDebugClient(const QString &name,
343 QQmlDebugConnection *parent)
344 : QObject(*(new QQmlDebugClientPrivate), parent)
346 Q_D(QQmlDebugClient);
348 d->connection = parent;
353 if (d->connection->d->plugins.contains(name)) {
354 qWarning() << "QQmlDebugClient: Conflicting plugin name" << name;
357 d->connection->d->plugins.insert(name, this);
358 d->connection->d->advertisePlugins();
362 QQmlDebugClient::~QQmlDebugClient()
364 Q_D(QQmlDebugClient);
365 if (d->connection && d->connection->d) {
366 d->connection->d->plugins.remove(d->name);
367 d->connection->d->advertisePlugins();
371 QString QQmlDebugClient::name() const
373 Q_D(const QQmlDebugClient);
377 float QQmlDebugClient::serviceVersion() const
379 Q_D(const QQmlDebugClient);
380 if (d->connection->d->serverPlugins.contains(d->name))
381 return d->connection->d->serverPlugins.value(d->name);
385 QQmlDebugClient::State QQmlDebugClient::state() const
387 Q_D(const QQmlDebugClient);
389 || !d->connection->isConnected()
390 || !d->connection->d->gotHello)
393 if (d->connection->d->serverPlugins.contains(d->name))
399 void QQmlDebugClient::sendMessage(const QByteArray &message)
401 Q_D(QQmlDebugClient);
402 if (state() != Enabled)
406 pack << d->name << message;
407 d->connection->d->protocol->send(pack);
408 d->connection->flush();
411 void QQmlDebugClient::stateChanged(State)
415 void QQmlDebugClient::messageReceived(const QByteArray &)
421 #include <qqmldebugclient.moc>