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