QmlDebugging: Remove QQmlDebugClient
[profile/ivi/qtdeclarative.git] / tools / qmlprofiler / 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
44 #include <private/qpacketprotocol_p.h>
45
46 #include <QtCore/qdebug.h>
47 #include <QtCore/qstringlist.h>
48 #include <QtNetwork/qnetworkproxy.h>
49
50 const int protocolVersion = 1;
51 const QString serverId = QLatin1String("QDeclarativeDebugServer");
52 const QString clientId = QLatin1String("QDeclarativeDebugClient");
53
54 class QQmlDebugClientPrivate
55 {
56 public:
57     QQmlDebugClientPrivate();
58
59     QString name;
60     QQmlDebugConnection *connection;
61 };
62
63 class QQmlDebugConnectionPrivate : public QObject
64 {
65     Q_OBJECT
66 public:
67     QQmlDebugConnectionPrivate(QQmlDebugConnection *c);
68     QQmlDebugConnection *q;
69     QPacketProtocol *protocol;
70     QIODevice *device;
71
72     bool gotHello;
73     QHash <QString, float> serverPlugins;
74     QHash<QString, QQmlDebugClient *> plugins;
75
76     void advertisePlugins();
77     void connectDeviceSignals();
78
79 public Q_SLOTS:
80     void connected();
81     void readyRead();
82     void deviceAboutToClose();
83 };
84
85 QQmlDebugConnectionPrivate::QQmlDebugConnectionPrivate(QQmlDebugConnection *c)
86     : QObject(c), q(c), protocol(0), device(0), gotHello(false)
87 {
88     protocol = new QPacketProtocol(q, this);
89     QObject::connect(c, SIGNAL(connected()), this, SLOT(connected()));
90     QObject::connect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
91 }
92
93 void QQmlDebugConnectionPrivate::advertisePlugins()
94 {
95     if (!q->isConnected())
96         return;
97
98     QPacket pack;
99     pack << serverId << 1 << plugins.keys();
100     protocol->send(pack);
101     q->flush();
102 }
103
104 void QQmlDebugConnectionPrivate::connected()
105 {
106     QPacket pack;
107     pack << serverId << 0 << protocolVersion << plugins.keys();
108     protocol->send(pack);
109     q->flush();
110 }
111
112 void QQmlDebugConnectionPrivate::readyRead()
113 {
114     if (!gotHello) {
115         QPacket pack = protocol->read();
116         QString name;
117
118         pack >> name;
119
120         bool validHello = false;
121         if (name == clientId) {
122             int op = -1;
123             pack >> op;
124             if (op == 0) {
125                 int version = -1;
126                 pack >> version;
127                 if (version == protocolVersion) {
128                     QStringList pluginNames;
129                     QList<float> pluginVersions;
130                     pack >> pluginNames;
131                     if (!pack.isEmpty())
132                         pack >> pluginVersions;
133
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);
141                     }
142
143                     validHello = true;
144                 }
145             }
146         }
147
148         if (!validHello) {
149             qWarning("QQmlDebugConnection: Invalid hello message");
150             QObject::disconnect(protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
151             return;
152         }
153         gotHello = true;
154
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);
161         }
162     }
163
164     while (protocol->packetsAvailable()) {
165         QPacket pack = protocol->read();
166         QString name;
167         pack >> name;
168
169         if (name == clientId) {
170             int op = -1;
171             pack >> op;
172
173             if (op == 1) {
174                 // Service Discovery
175                 QHash<QString, float> oldServerPlugins = serverPlugins;
176                 serverPlugins.clear();
177
178                 QStringList pluginNames;
179                 QList<float> pluginVersions;
180                 pack >> pluginNames;
181                 if (!pack.isEmpty())
182                     pack >> pluginVersions;
183
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);
191                 }
192
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;
199
200                     if (oldServerPlugins.contains(pluginName)
201                             != serverPlugins.contains(pluginName)) {
202                         iter.value()->stateChanged(newSate);
203                     }
204                 }
205             } else {
206                 qWarning() << "QQmlDebugConnection: Unknown control message id" << op;
207             }
208         } else {
209             QByteArray message;
210             pack >> message;
211
212             QHash<QString, QQmlDebugClient *>::Iterator iter =
213                     plugins.find(name);
214             if (iter == plugins.end()) {
215                 qWarning() << "QQmlDebugConnection: Message received for missing plugin" << name;
216             } else {
217                 (*iter)->messageReceived(message);
218             }
219         }
220     }
221 }
222
223 void QQmlDebugConnectionPrivate::deviceAboutToClose()
224 {
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();
228 }
229
230 QQmlDebugConnection::QQmlDebugConnection(QObject *parent)
231     : QIODevice(parent), d(new QQmlDebugConnectionPrivate(this))
232 {
233 }
234
235 QQmlDebugConnection::~QQmlDebugConnection()
236 {
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);
241     }
242 }
243
244 bool QQmlDebugConnection::isConnected() const
245 {
246     return state() == QAbstractSocket::ConnectedState;
247 }
248
249 qint64 QQmlDebugConnection::readData(char *data, qint64 maxSize)
250 {
251     return d->device->read(data, maxSize);
252 }
253
254 qint64 QQmlDebugConnection::writeData(const char *data, qint64 maxSize)
255 {
256     return d->device->write(data, maxSize);
257 }
258
259 qint64 QQmlDebugConnection::bytesAvailable() const
260 {
261     return d->device->bytesAvailable();
262 }
263
264 bool QQmlDebugConnection::isSequential() const
265 {
266     return true;
267 }
268
269 void QQmlDebugConnection::close()
270 {
271     if (isOpen()) {
272         QIODevice::close();
273         d->device->close();
274         emit stateChanged(QAbstractSocket::UnconnectedState);
275
276         QHash<QString, QQmlDebugClient*>::iterator iter = d->plugins.begin();
277         for (; iter != d->plugins.end(); ++iter) {
278             iter.value()->stateChanged(QQmlDebugClient::NotConnected);
279         }
280     }
281 }
282
283 bool QQmlDebugConnection::waitForConnected(int msecs)
284 {
285     QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
286     if (socket)
287         return socket->waitForConnected(msecs);
288     return false;
289 }
290
291 QAbstractSocket::SocketState QQmlDebugConnection::state() const
292 {
293     QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
294     if (socket)
295         return socket->state();
296
297     return QAbstractSocket::UnconnectedState;
298 }
299
300 void QQmlDebugConnection::flush()
301 {
302     QAbstractSocket *socket = qobject_cast<QAbstractSocket*>(d->device);
303     if (socket) {
304         socket->flush();
305         return;
306     }
307 }
308
309 void QQmlDebugConnection::connectToHost(const QString &hostName, quint16 port)
310 {
311     QTcpSocket *socket = new QTcpSocket(d);
312     socket->setProxy(QNetworkProxy::NoProxy);
313     d->device = socket;
314     d->connectDeviceSignals();
315     d->gotHello = false;
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);
321 }
322
323 void QQmlDebugConnectionPrivate::connectDeviceSignals()
324 {
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()));
328 }
329
330 //
331
332 QQmlDebugClientPrivate::QQmlDebugClientPrivate()
333     : connection(0)
334 {
335 }
336
337 QQmlDebugClient::QQmlDebugClient(const QString &name, 
338                                                  QQmlDebugConnection *parent)
339     : QObject(parent),
340       d(new QQmlDebugClientPrivate)
341 {
342     d->name = name;
343     d->connection = parent;
344
345     if (!d->connection)
346         return;
347
348     if (d->connection->d->plugins.contains(name)) {
349         qWarning() << "QQmlDebugClient: Conflicting plugin name" << name;
350         d->connection = 0;
351     } else {
352         d->connection->d->plugins.insert(name, this);
353         d->connection->d->advertisePlugins();
354     }
355 }
356
357 QQmlDebugClient::~QQmlDebugClient()
358 {
359     if (d->connection && d->connection->d) {
360         d->connection->d->plugins.remove(d->name);
361         d->connection->d->advertisePlugins();
362     }
363     delete d;
364 }
365
366 QString QQmlDebugClient::name() const
367 {
368     return d->name;
369 }
370
371 float QQmlDebugClient::serviceVersion() const
372 {
373     if (d->connection->d->serverPlugins.contains(d->name))
374         return d->connection->d->serverPlugins.value(d->name);
375     return -1;
376 }
377
378 QQmlDebugClient::State QQmlDebugClient::state() const
379 {
380     if (!d->connection
381             || !d->connection->isConnected()
382             || !d->connection->d->gotHello)
383         return NotConnected;
384
385     if (d->connection->d->serverPlugins.contains(d->name))
386         return Enabled;
387
388     return Unavailable;
389 }
390
391 void QQmlDebugClient::sendMessage(const QByteArray &message)
392 {
393     if (state() != Enabled)
394         return;
395
396     QPacket pack;
397     pack << d->name << message;
398     d->connection->d->protocol->send(pack);
399     d->connection->flush();
400 }
401
402 void QQmlDebugClient::stateChanged(State)
403 {
404 }
405
406 void QQmlDebugClient::messageReceived(const QByteArray &)
407 {
408 }
409
410 #include <qqmldebugclient.moc>