qmlprofiler: Only accept commands if we ask for them
authorUlf Hermann <ulf.hermann@theqtcompany.com>
Thu, 21 May 2015 13:39:28 +0000 (15:39 +0200)
committerUlf Hermann <ulf.hermann@theqtcompany.com>
Sat, 6 Jun 2015 15:20:33 +0000 (15:20 +0000)
This way we can shut down the input thread after the last command and we
cannot get commands before we can process them.

Change-Id: Ie1583a338da9c9df0e07c9e09ce185857c5ea66d
Reviewed-by: Joerg Bornemann <joerg.bornemann@theqtcompany.com>
tools/qmlprofiler/commandlistener.cpp
tools/qmlprofiler/commandlistener.h
tools/qmlprofiler/constants.h
tools/qmlprofiler/main.cpp
tools/qmlprofiler/qmlprofilerapplication.cpp
tools/qmlprofiler/qmlprofilerapplication.h

index 1d538d8..369d095 100644 (file)
 #include "constants.h"
 #include <QtCore/QTextStream>
 
-CommandListener::CommandListener(QObject *parent)
-    : QThread(parent)
-    , m_stopRequested(false)
+void CommandListener::readCommand()
 {
-}
-
-void CommandListener::run()
-{
-    QString line;
-    QTextStream in(stdin, QIODevice::ReadOnly);
-    do {
-        line = in.readLine();
-        line = line.trimmed();
-        if (!line.isEmpty()) {
-            emit command(line);
-            if (line == QLatin1String(Constants::CMD_QUIT)
-                    || line == QLatin1String(Constants::CMD_QUIT2))
-                return;
-        }
-    } while (!m_stopRequested && !line.isNull());
+    emit command(QTextStream(stdin).readLine());
 }
index 7d4d43d..e74d532 100644 (file)
 
 #include <QtCore/QThread>
 
-class CommandListener : public QThread
-{
+class CommandListener : public QObject {
     Q_OBJECT
-public:
-    CommandListener(QObject *parent = 0);
+public slots:
+    void readCommand();
 
-    void run();
-
-    void requestStop() { m_stopRequested = true; }
 signals:
     void command(const QString &command);
-
-private:
-    bool m_stopRequested;
 };
 
 #endif // COMMANDLISTENER_H
index 4e5aac7..b925ba0 100644 (file)
 
 namespace Constants {
 
-const char CMD_HELP[] ="help";
-const char CMD_HELP2[] = "h";
-const char CMD_HELP3[] = "?";
-
 const char CMD_RECORD[] ="record";
 const char CMD_RECORD2[] ="r";
 
index 2a85b72..5496072 100644 (file)
@@ -41,12 +41,16 @@ int main(int argc, char *argv[])
     app.parseArguments();
 
     if (app.isInteractive()) {
+        QThread listenerThread;
         CommandListener listener;
+        listener.moveToThread(&listenerThread);
         QObject::connect(&listener, SIGNAL(command(QString)), &app, SLOT(userCommand(QString)));
-        listener.start();
+        QObject::connect(&app, SIGNAL(readyForCommand()), &listener, SLOT(readCommand()));
+        listenerThread.start();
         int exitValue = app.exec();
+        listenerThread.quit();
         // wait for listener to exit
-        listener.wait();
+        listenerThread.wait();
         return exitValue;
     } else {
         return app.exec();
index c2d3375..23483b2 100644 (file)
@@ -249,14 +249,10 @@ QString QmlProfilerApplication::traceFileName() const
 void QmlProfilerApplication::userCommand(const QString &command)
 {
     QString cmd = command.trimmed();
-    if (cmd == Constants::CMD_HELP
-            || cmd == Constants::CMD_HELP2
-            || cmd == Constants::CMD_HELP3) {
-        printCommands();
-    } else if (cmd == Constants::CMD_RECORD
-               || cmd == Constants::CMD_RECORD2) {
+    if (cmd == Constants::CMD_RECORD || cmd == Constants::CMD_RECORD2) {
         m_qmlProfilerClient.sendRecordingStatus(!m_recording);
         m_v8profilerClient.sendRecordingStatus(!m_recording);
+        emit readyForCommand();
     } else if (cmd == Constants::CMD_QUIT
                || cmd == Constants::CMD_QUIT2) {
         print(QLatin1String("Quit"));
@@ -267,6 +263,9 @@ void QmlProfilerApplication::userCommand(const QString &command)
         } else {
             quit();
         }
+    } else {
+        printCommands();
+        emit readyForCommand();
     }
 }
 
@@ -300,6 +299,7 @@ void QmlProfilerApplication::run()
 
     }
     m_connectTimer.start();
+    emit readyForCommand();
 }
 
 void QmlProfilerApplication::tryToConnect()
index d4af3b0..8d2cbff 100644 (file)
@@ -52,6 +52,9 @@ public:
     int exec();
     bool isInteractive() const;
 
+signals:
+    void readyForCommand();
+
 public slots:
     void userCommand(const QString &command);
     void notifyTraceStarted();