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 "qpacketprotocol.h"
45 #include <QtCore/qdebug.h>
46 #include <QtCore/qstringlist.h>
47 #include <QtNetwork/qnetworkproxy.h>
49 const int protocolVersion = 1;
50 const QString serverId = QLatin1String("QDeclarativeDebugServer");
51 const QString clientId = QLatin1String("QDeclarativeDebugClient");
53 class QQmlDebugClientPrivate
56 QQmlDebugClientPrivate();
59 QQmlDebugConnection *connection;
62 class QQmlDebugConnectionPrivate : public QObject
66 QQmlDebugConnectionPrivate(QQmlDebugConnection *c);
67 QQmlDebugConnection *q;
68 QPacketProtocol *protocol;
72 QHash <QString, float> serverPlugins;
73 QHash<QString, QQmlDebugClient *> plugins;
75 void advertisePlugins();
76 void connectDeviceSignals();
81 void deviceAboutToClose();
84 QQmlDebugConnectionPrivate::QQmlDebugConnectionPrivate(QQmlDebugConnection *c)
85 : QObject(c), q(c), protocol(0), device(0), gotHello(false)
87 protocol = new QPacketProtocol(q, this);
88 QObject::connect(c, SIGNAL(connected()), this, SLOT(connected()));
89 QObject::connect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
92 void QQmlDebugConnectionPrivate::advertisePlugins()
94 if (!q->isConnected())
98 pack << serverId << 1 << plugins.keys();
103 void QQmlDebugConnectionPrivate::connected()
106 pack << serverId << 0 << protocolVersion << plugins.keys();
107 protocol->send(pack);
111 void QQmlDebugConnectionPrivate::readyRead()
114 QPacket pack = protocol->read();
119 bool validHello = false;
120 if (name == clientId) {
126 if (version == protocolVersion) {
127 QStringList pluginNames;
128 QList<float> pluginVersions;
131 pack >> pluginVersions;
133 const int pluginNamesSize = pluginNames.size();
134 const int pluginVersionsSize = pluginVersions.size();
135 for (int i = 0; i < pluginNamesSize; ++i) {
136 float pluginVersion = 1.0;
137 if (i < pluginVersionsSize)
138 pluginVersion = pluginVersions.at(i);
139 serverPlugins.insert(pluginNames.at(i), pluginVersion);
148 qWarning("QQmlDebugConnection: Invalid hello message");
149 QObject::disconnect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
154 QHash<QString, QQmlDebugClient *>::Iterator iter = plugins.begin();
155 for (; iter != plugins.end(); ++iter) {
156 QQmlDebugClient::State newState = QQmlDebugClient::Unavailable;
157 if (serverPlugins.contains(iter.key()))
158 newState = QQmlDebugClient::Enabled;
159 iter.value()->stateChanged(newState);
163 while (protocol->packetsAvailable()) {
164 QPacket pack = protocol->read();
168 if (name == clientId) {
174 QHash<QString, float> oldServerPlugins = serverPlugins;
175 serverPlugins.clear();
177 QStringList pluginNames;
178 QList<float> pluginVersions;
181 pack >> pluginVersions;
183 const int pluginNamesSize = pluginNames.size();
184 const int pluginVersionsSize = pluginVersions.size();
185 for (int i = 0; i < pluginNamesSize; ++i) {
186 float pluginVersion = 1.0;
187 if (i < pluginVersionsSize)
188 pluginVersion = pluginVersions.at(i);
189 serverPlugins.insert(pluginNames.at(i), pluginVersion);
192 QHash<QString, QQmlDebugClient *>::Iterator iter = plugins.begin();
193 for (; iter != plugins.end(); ++iter) {
194 const QString pluginName = iter.key();
195 QQmlDebugClient::State newSate = QQmlDebugClient::Unavailable;
196 if (serverPlugins.contains(pluginName))
197 newSate = QQmlDebugClient::Enabled;
199 if (oldServerPlugins.contains(pluginName)
200 != serverPlugins.contains(pluginName)) {
201 iter.value()->stateChanged(newSate);
205 qWarning() << "QQmlDebugConnection: Unknown control message id" << op;
211 QHash<QString, QQmlDebugClient *>::Iterator iter =
213 if (iter == plugins.end()) {
214 qWarning() << "QQmlDebugConnection: Message received for missing plugin" << name;
216 (*iter)->messageReceived(message);
222 void QQmlDebugConnectionPrivate::deviceAboutToClose()
224 // This is nasty syntax but we want to emit our own aboutToClose signal (by calling QIODevice::close())
225 // without calling the underlying device close fn as that would cause an infinite loop
226 q->QIODevice::close();
229 QQmlDebugConnection::QQmlDebugConnection(QObject *parent)
230 : QIODevice(parent), d(new QQmlDebugConnectionPrivate(this))
234 QQmlDebugConnection::~QQmlDebugConnection()
236 QHash<QString, QQmlDebugClient*>::iterator iter = d->plugins.begin();
237 for (; iter != d->plugins.end(); ++iter) {
238 iter.value()->d->connection = 0;
239 iter.value()->stateChanged(QQmlDebugClient::NotConnected);
243 bool QQmlDebugConnection::isConnected() const
245 return state() == QAbstractSocket::ConnectedState;
248 qint64 QQmlDebugConnection::readData(char *data, qint64 maxSize)
250 return d->device->read(data, maxSize);
253 qint64 QQmlDebugConnection::writeData(const char *data, qint64 maxSize)
255 return d->device->write(data, maxSize);
258 qint64 QQmlDebugConnection::bytesAvailable() const
260 return d->device->bytesAvailable();
263 bool QQmlDebugConnection::isSequential() const
268 void QQmlDebugConnection::close()
273 emit stateChanged(QAbstractSocket::UnconnectedState);
275 QHash<QString, QQmlDebugClient*>::iterator iter = d->plugins.begin();
276 for (; iter != d->plugins.end(); ++iter) {
277 iter.value()->stateChanged(QQmlDebugClient::NotConnected);
282 bool QQmlDebugConnection::waitForConnected(int msecs)
284 QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
286 return socket->waitForConnected(msecs);
290 QAbstractSocket::SocketState QQmlDebugConnection::state() const
292 QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
294 return socket->state();
296 return QAbstractSocket::UnconnectedState;
299 void QQmlDebugConnection::flush()
301 QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
308 void QQmlDebugConnection::connectToHost(const QString &hostName, quint16 port)
310 QTcpSocket *socket = new QTcpSocket(d);
311 socket->setProxy(QNetworkProxy::NoProxy);
313 d->connectDeviceSignals();
315 connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SIGNAL(stateChanged(QAbstractSocket::SocketState)));
316 connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SIGNAL(error(QAbstractSocket::SocketError)));
317 connect(socket, SIGNAL(connected()), this, SIGNAL(connected()));
318 socket->connectToHost(hostName, port);
319 QIODevice::open(ReadWrite | Unbuffered);
322 void QQmlDebugConnectionPrivate::connectDeviceSignals()
324 connect(device, SIGNAL(bytesWritten(qint64)), q, SIGNAL(bytesWritten(qint64)));
325 connect(device, SIGNAL(readyRead()), q, SIGNAL(readyRead()));
326 connect(device, SIGNAL(aboutToClose()), this, SLOT(deviceAboutToClose()));
331 QQmlDebugClientPrivate::QQmlDebugClientPrivate()
336 QQmlDebugClient::QQmlDebugClient(const QString &name,
337 QQmlDebugConnection *parent)
339 d(new QQmlDebugClientPrivate)
342 d->connection = parent;
347 if (d->connection->d->plugins.contains(name)) {
348 qWarning() << "QQmlDebugClient: Conflicting plugin name" << name;
351 d->connection->d->plugins.insert(name, this);
352 d->connection->d->advertisePlugins();
356 QQmlDebugClient::~QQmlDebugClient()
358 if (d->connection && d->connection->d) {
359 d->connection->d->plugins.remove(d->name);
360 d->connection->d->advertisePlugins();
365 QString QQmlDebugClient::name() const
370 float QQmlDebugClient::serviceVersion() const
372 if (d->connection->d->serverPlugins.contains(d->name))
373 return d->connection->d->serverPlugins.value(d->name);
377 QQmlDebugClient::State QQmlDebugClient::state() const
380 || !d->connection->isConnected()
381 || !d->connection->d->gotHello)
384 if (d->connection->d->serverPlugins.contains(d->name))
390 void QQmlDebugClient::sendMessage(const QByteArray &message)
392 if (state() != Enabled)
396 pack << d->name << message;
397 d->connection->d->protocol->send(pack);
398 d->connection->flush();
401 void QQmlDebugClient::stateChanged(State)
405 void QQmlDebugClient::messageReceived(const QByteArray &)
409 #include <qqmldebugclient.moc>