summaryrefslogtreecommitdiffstats
path: root/src/declarative/debugger
diff options
context:
space:
mode:
authorBea Lam <bea.lam@nokia.com>2010-04-16 05:33:41 (GMT)
committerBea Lam <bea.lam@nokia.com>2010-04-16 05:38:23 (GMT)
commit34a1a6b5d6399e7bcad136fdaa9a050a0f8bb2dc (patch)
tree9d7585945744454937ac495d7f729a9386f755bb /src/declarative/debugger
parentb458d672dbec892f00019edc1fe06156190401c7 (diff)
downloadQt-34a1a6b5d6399e7bcad136fdaa9a050a0f8bb2dc.zip
Qt-34a1a6b5d6399e7bcad136fdaa9a050a0f8bb2dc.tar.gz
Qt-34a1a6b5d6399e7bcad136fdaa9a050a0f8bb2dc.tar.bz2
Wait for debug clients asynchronously instead of blocking creation of
the engine until a debug client has connected. This makes for easier debugging from Qt Creator when debugging C++ and QML together and when debugging an application that has multiple engines.
Diffstat (limited to 'src/declarative/debugger')
-rw-r--r--src/declarative/debugger/qdeclarativedebugservice.cpp133
-rw-r--r--src/declarative/debugger/qdeclarativedebugservice_p.h7
2 files changed, 57 insertions, 83 deletions
diff --git a/src/declarative/debugger/qdeclarativedebugservice.cpp b/src/declarative/debugger/qdeclarativedebugservice.cpp
index 9d9d1d0..34e73fd 100644
--- a/src/declarative/debugger/qdeclarativedebugservice.cpp
+++ b/src/declarative/debugger/qdeclarativedebugservice.cpp
@@ -60,12 +60,12 @@ class QDeclarativeDebugServer : public QObject
Q_DISABLE_COPY(QDeclarativeDebugServer)
public:
static QDeclarativeDebugServer *instance();
- void wait();
- void registerForStartNotification(QObject *object, const char *receiver);
+ void listen();
+ bool hasDebuggingClient() const;
private Q_SLOTS:
void readyRead();
- void registeredObjectDestroyed(QObject *object);
+ void newConnection();
private:
friend class QDeclarativeDebugService;
@@ -78,14 +78,14 @@ class QDeclarativeDebugServerPrivate : public QObjectPrivate
Q_DECLARE_PUBLIC(QDeclarativeDebugServer)
public:
QDeclarativeDebugServerPrivate();
- void wait();
int port;
QTcpSocket *connection;
QPacketProtocol *protocol;
QHash<QString, QDeclarativeDebugService *> plugins;
QStringList enabledPlugins;
- QList<QPair<QObject*, QByteArray> > notifyClients;
+ QTcpServer *tcpServer;
+ bool gotHello;
};
class QDeclarativeDebugServicePrivate : public QObjectPrivate
@@ -99,56 +99,41 @@ public:
};
QDeclarativeDebugServerPrivate::QDeclarativeDebugServerPrivate()
-: connection(0), protocol(0)
+: connection(0), protocol(0), gotHello(false)
{
}
-void QDeclarativeDebugServerPrivate::wait()
+void QDeclarativeDebugServer::listen()
{
- Q_Q(QDeclarativeDebugServer);
- QTcpServer server;
-
- if (!server.listen(QHostAddress::Any, port)) {
- qWarning("QDeclarativeDebugServer: Unable to listen on port %d", port);
- return;
- }
-
- qWarning("QDeclarativeDebugServer: Waiting for connection on port %d...", port);
-
- for (int i=0; i<notifyClients.count(); i++) {
- if (!QMetaObject::invokeMethod(notifyClients[i].first, notifyClients[i].second)) {
- qWarning() << "QDeclarativeDebugServer: unable to call method" << notifyClients[i].second
- << "on object" << notifyClients[i].first << "to notify of debug server start";
- }
- }
- notifyClients.clear();
+ Q_D(QDeclarativeDebugServer);
- if (!server.waitForNewConnection(-1)) {
- qWarning("QDeclarativeDebugServer: Connection error");
- return;
- }
+ d->tcpServer = new QTcpServer(this);
+ QObject::connect(d->tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
+ if (d->tcpServer->listen(QHostAddress::Any, d->port))
+ qWarning("QDeclarativeDebugServer: Waiting for connection on port %d...", d->port);
+ else
+ qWarning("QDeclarativeDebugServer: Unable to listen on port %d", d->port);
+}
- connection = server.nextPendingConnection();
- connection->setParent(q);
- protocol = new QPacketProtocol(connection, q);
+void QDeclarativeDebugServer::newConnection()
+{
+ Q_D(QDeclarativeDebugServer);
- // ### Wait for hello
- while (!protocol->packetsAvailable()) {
- connection->waitForReadyRead();
- }
- QPacket hello = protocol->read();
- QString name;
- hello >> name >> enabledPlugins;
- if (name != QLatin1String("QDeclarativeDebugServer")) {
- qWarning("QDeclarativeDebugServer: Invalid hello message");
- delete protocol; delete connection; connection = 0; protocol = 0;
+ if (d->connection) {
+ qWarning("QDeclarativeDebugServer error: another client is already connected");
return;
}
- QObject::connect(protocol, SIGNAL(readyRead()), q, SLOT(readyRead()));
- q->readyRead();
+ d->connection = d->tcpServer->nextPendingConnection();
+ d->connection->setParent(this);
+ d->protocol = new QPacketProtocol(d->connection, this);
+ QObject::connect(d->protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
+}
- qWarning("QDeclarativeDebugServer: Connection established");
+bool QDeclarativeDebugServer::hasDebuggingClient() const
+{
+ Q_D(const QDeclarativeDebugServer);
+ return d->gotHello;
}
QDeclarativeDebugServer *QDeclarativeDebugServer::instance()
@@ -163,36 +148,15 @@ QDeclarativeDebugServer *QDeclarativeDebugServer::instance()
bool ok = false;
int port = env.toInt(&ok);
- if (ok && port > 1024)
+ if (ok && port > 1024) {
server = new QDeclarativeDebugServer(port);
+ server->listen();
+ }
}
return server;
}
-void QDeclarativeDebugServer::wait()
-{
- Q_D(QDeclarativeDebugServer);
- d->wait();
-}
-
-void QDeclarativeDebugServer::registerForStartNotification(QObject *object, const char *methodName)
-{
- Q_D(QDeclarativeDebugServer);
- connect(object, SIGNAL(destroyed(QObject*)), SLOT(registeredObjectDestroyed(QObject*)));
- d->notifyClients.append(qMakePair(object, QByteArray(methodName)));
-}
-
-void QDeclarativeDebugServer::registeredObjectDestroyed(QObject *object)
-{
- Q_D(QDeclarativeDebugServer);
- QMutableListIterator<QPair<QObject*, QByteArray> > i(d->notifyClients);
- while (i.hasNext()) {
- if (i.next().first == object)
- i.remove();
- }
-}
-
QDeclarativeDebugServer::QDeclarativeDebugServer(int port)
: QObject(*(new QDeclarativeDebugServerPrivate))
{
@@ -204,6 +168,23 @@ void QDeclarativeDebugServer::readyRead()
{
Q_D(QDeclarativeDebugServer);
+ if (!d->gotHello) {
+ QPacket hello = d->protocol->read();
+ QString name;
+ hello >> name >> d->enabledPlugins;
+ if (name != QLatin1String("QDeclarativeDebugServer")) {
+ qWarning("QDeclarativeDebugServer: Invalid hello message");
+ QObject::disconnect(d->protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
+ d->protocol->deleteLater();
+ d->protocol = 0;
+ d->connection->deleteLater();
+ d->connection = 0;
+ return;
+ }
+ d->gotHello = true;
+ qWarning("QDeclarativeDebugServer: Connection established");
+ }
+
QString debugServer(QLatin1String("QDeclarativeDebugServer"));
while (d->protocol->packetsAvailable()) {
@@ -375,6 +356,12 @@ bool QDeclarativeDebugService::isDebuggingEnabled()
return QDeclarativeDebugServer::instance() != 0;
}
+bool QDeclarativeDebugService::hasDebuggingClient()
+{
+ return QDeclarativeDebugServer::instance() != 0
+ && QDeclarativeDebugServer::instance()->hasDebuggingClient();
+}
+
QString QDeclarativeDebugService::objectToString(QObject *obj)
{
if(!obj)
@@ -390,16 +377,6 @@ QString QDeclarativeDebugService::objectToString(QObject *obj)
return rv;
}
-void QDeclarativeDebugService::waitForClients()
-{
- QDeclarativeDebugServer::instance()->wait();
-}
-
-void QDeclarativeDebugService::notifyOnServerStart(QObject *object, const char *receiver)
-{
- QDeclarativeDebugServer::instance()->registerForStartNotification(object, receiver);
-}
-
void QDeclarativeDebugService::sendMessage(const QByteArray &message)
{
Q_D(QDeclarativeDebugService);
diff --git a/src/declarative/debugger/qdeclarativedebugservice_p.h b/src/declarative/debugger/qdeclarativedebugservice_p.h
index 498edf3..c461ddf 100644
--- a/src/declarative/debugger/qdeclarativedebugservice_p.h
+++ b/src/declarative/debugger/qdeclarativedebugservice_p.h
@@ -68,19 +68,16 @@ public:
static int idForObject(QObject *);
static QObject *objectForId(int);
- static bool isDebuggingEnabled();
static QString objectToString(QObject *obj);
- static void waitForClients();
-
- static void notifyOnServerStart(QObject *object, const char *receiver);
+ static bool isDebuggingEnabled();
+ static bool hasDebuggingClient();
protected:
virtual void enabledChanged(bool);
virtual void messageReceived(const QByteArray &);
private:
- void registerForStartNotification(QObject *object, const char *methodName);
friend class QDeclarativeDebugServer;
};