Maemo: Introduce explicit states to SshRunner.

Reviewed-by: kh1
This commit is contained in:
Christian Kandeler
2010-10-04 15:52:13 +02:00
parent 71c6104cc3
commit b2030a13d4
4 changed files with 130 additions and 73 deletions

View File

@@ -85,8 +85,8 @@ void MaemoRunControl::start()
SLOT(handleRemoteOutput(QByteArray))); SLOT(handleRemoteOutput(QByteArray)));
connect(m_runner, SIGNAL(remoteProcessStarted()), this, connect(m_runner, SIGNAL(remoteProcessStarted()), this,
SLOT(handleRemoteProcessStarted())); SLOT(handleRemoteProcessStarted()));
connect(m_runner, SIGNAL(remoteProcessFinished(int)), this, connect(m_runner, SIGNAL(remoteProcessFinished(qint64)), this,
SLOT(handleRemoteProcessFinished(int))); SLOT(handleRemoteProcessFinished(qint64)));
connect(m_runner, SIGNAL(reportProgress(QString)), this, connect(m_runner, SIGNAL(reportProgress(QString)), this,
SLOT(handleProgressReport(QString))); SLOT(handleProgressReport(QString)));
connect(m_runner, SIGNAL(mountDebugOutput(QString)), this, connect(m_runner, SIGNAL(mountDebugOutput(QString)), this,
@@ -118,11 +118,13 @@ void MaemoRunControl::startExecution()
.arg(m_runConfig->arguments().join(QLatin1String(" "))).toUtf8()); .arg(m_runConfig->arguments().join(QLatin1String(" "))).toUtf8());
} }
void MaemoRunControl::handleRemoteProcessFinished(int exitCode) void MaemoRunControl::handleRemoteProcessFinished(qint64 exitCode)
{ {
emit appendMessage(this, if (exitCode != MaemoSshRunner::InvalidExitCode) {
tr("Finished running remote process. Exit code was %1.").arg(exitCode), emit appendMessage(this,
false); tr("Finished running remote process. Exit code was %1.")
.arg(exitCode), false);
}
setFinished(); setFinished();
} }

View File

@@ -61,7 +61,7 @@ private slots:
void startExecution(); void startExecution();
void handleSshError(const QString &error); void handleSshError(const QString &error);
void handleRemoteProcessStarted() {} void handleRemoteProcessStarted() {}
void handleRemoteProcessFinished(int exitCode); void handleRemoteProcessFinished(qint64 exitCode);
void handleRemoteOutput(const QByteArray &output); void handleRemoteOutput(const QByteArray &output);
void handleRemoteErrorOutput(const QByteArray &output); void handleRemoteErrorOutput(const QByteArray &output);
void handleProgressReport(const QString &progressString); void handleProgressReport(const QString &progressString);

View File

@@ -46,6 +46,10 @@
#include <QtCore/QFileInfo> #include <QtCore/QFileInfo>
#include <limits>
#define ASSERT_STATE(state) assertState(state, Q_FUNC_INFO)
using namespace Core; using namespace Core;
namespace Qt4ProjectManager { namespace Qt4ProjectManager {
@@ -55,8 +59,8 @@ MaemoSshRunner::MaemoSshRunner(QObject *parent,
MaemoRunConfiguration *runConfig, bool debugging) MaemoRunConfiguration *runConfig, bool debugging)
: QObject(parent), m_runConfig(runConfig), : QObject(parent), m_runConfig(runConfig),
m_mounter(new MaemoRemoteMounter(this)), m_mounter(new MaemoRemoteMounter(this)),
m_devConfig(runConfig->deviceConfig()), m_shuttingDown(false), m_devConfig(runConfig->deviceConfig()),
m_debugging(debugging) m_debugging(debugging), m_state(Inactive)
{ {
m_procsToKill m_procsToKill
<< QFileInfo(m_runConfig->localExecutableFilePath()).fileName() << QFileInfo(m_runConfig->localExecutableFilePath()).fileName()
@@ -77,27 +81,21 @@ MaemoSshRunner::~MaemoSshRunner() {}
void MaemoSshRunner::start() void MaemoSshRunner::start()
{ {
// Should not happen. ASSERT_STATE(QList<State>() << Inactive << StopRequested);
if (m_shuttingDown) {
emit error(tr("Cannot start deployment, as the clean-up from the last time has not finished yet."));
return;
}
m_stop = false; setState(Connecting);
m_exitStatus = -1; m_exitStatus = -1;
if (m_connection) if (m_connection)
disconnect(m_connection.data(), 0, this, 0); disconnect(m_connection.data(), 0, this, 0);
bool reUse = isConnectionUsable(); m_connection = m_runConfig->deployStep()->sshConnection();
if (!reUse) const bool reUse = isConnectionUsable();
m_connection = m_runConfig->deployStep()->sshConnection();
reUse = isConnectionUsable();
if (!reUse) if (!reUse)
m_connection = SshConnection::create(); m_connection = SshConnection::create();
connect(m_connection.data(), SIGNAL(connected()), this, connect(m_connection.data(), SIGNAL(connected()), this,
SLOT(handleConnected())); SLOT(handleConnected()));
connect(m_connection.data(), SIGNAL(error(SshError)), this, connect(m_connection.data(), SIGNAL(error(SshError)), this,
SLOT(handleConnectionFailure())); SLOT(handleConnectionFailure()));
if (reUse) { if (reUse) {
handleConnected(); handleConnected();
} else { } else {
emit reportProgress(tr("Connecting to device...")); emit reportProgress(tr("Connecting to device..."));
@@ -107,41 +105,44 @@ void MaemoSshRunner::start()
void MaemoSshRunner::stop() void MaemoSshRunner::stop()
{ {
if (m_shuttingDown) if (m_state == PostRunCleaning || m_state == StopRequested
|| m_state == Inactive)
return; return;
m_stop = true; setState(StopRequested);
m_mounter->stop(); cleanup();
if (m_cleaner)
disconnect(m_cleaner.data(), 0, this, 0);
cleanup(false);
} }
void MaemoSshRunner::handleConnected() void MaemoSshRunner::handleConnected()
{ {
if (m_stop) ASSERT_STATE(QList<State>() << Connecting << StopRequested);
return; if (m_state == StopRequested) {
setState(Inactive);
cleanup(true); } else {
setState(PreRunCleaning);
cleanup();
}
} }
void MaemoSshRunner::handleConnectionFailure() void MaemoSshRunner::handleConnectionFailure()
{ {
emit error(tr("Could not connect to host: %1") if (m_state != Inactive)
.arg(m_connection->errorString())); qWarning("Unexpected state %d in %s.", m_state, Q_FUNC_INFO);
const QString errorTemplate = m_state == Connecting
? tr("Could not connect to host: %1") : tr("Connection failed: %1");
emitError(errorTemplate.arg(m_connection->errorString()));
} }
void MaemoSshRunner::cleanup(bool initialCleanup) void MaemoSshRunner::cleanup()
{ {
if (!isConnectionUsable()) ASSERT_STATE(QList<State>() << PreRunCleaning << PostRunCleaning
return; << StopRequested);
emit reportProgress(tr("Killing remote process(es)...")); emit reportProgress(tr("Killing remote process(es)..."));
m_shuttingDown = !initialCleanup;
// pkill behaves differently on Fremantle and Harmattan. // pkill behaves differently on Fremantle and Harmattan.
const char *const killTemplate = "pkill -%2 '^%1$'; pkill -%2 '/%1$';"; const char *const killTemplate = "pkill -%2 '^%1$'; pkill -%2 '/%1$';";
QString niceKill; QString niceKill;
QString brutalKill; QString brutalKill;
foreach (const QString &proc, m_procsToKill) { foreach (const QString &proc, m_procsToKill) {
@@ -163,31 +164,30 @@ void MaemoSshRunner::handleCleanupFinished(int exitStatus)
|| exitStatus == SshRemoteProcess::KilledBySignal || exitStatus == SshRemoteProcess::KilledBySignal
|| exitStatus == SshRemoteProcess::ExitedNormally); || exitStatus == SshRemoteProcess::ExitedNormally);
if (m_shuttingDown) { ASSERT_STATE(QList<State>() << PreRunCleaning << PostRunCleaning
m_unmountState = ShutdownUnmount; << StopRequested);
if (m_state == StopRequested || m_state == PostRunCleaning) {
m_mounter->unmount(); m_mounter->unmount();
return; return;
} }
if (m_stop)
return;
if (exitStatus != SshRemoteProcess::ExitedNormally) { if (exitStatus != SshRemoteProcess::ExitedNormally) {
emit error(tr("Initial cleanup failed: %1") emitError(tr("Initial cleanup failed: %1")
.arg(m_cleaner->errorString())); .arg(m_cleaner->errorString()));
} else { } else {
m_mounter->setConnection(m_connection); m_mounter->setConnection(m_connection);
m_unmountState = InitialUnmount;
m_mounter->unmount(); m_mounter->unmount();
} }
} }
void MaemoSshRunner::handleUnmounted() void MaemoSshRunner::handleUnmounted()
{ {
switch (m_unmountState) { ASSERT_STATE(QList<State>() << PreRunCleaning << PreMountUnmounting
case InitialUnmount: { << PostRunCleaning << StopRequested);
if (m_stop)
return; switch (m_state) {
case PreRunCleaning: {
m_mounter->resetMountSpecifications(); m_mounter->resetMountSpecifications();
MaemoPortList portList = m_devConfig.freePorts(); MaemoPortList portList = m_devConfig.freePorts();
if (m_debugging) { // gdbserver and QML inspector need one port each. if (m_debugging) { // gdbserver and QML inspector need one port each.
@@ -210,49 +210,55 @@ void MaemoSshRunner::handleUnmounted()
MaemoGlobal::remoteProjectSourcesMountPoint()))) MaemoGlobal::remoteProjectSourcesMountPoint())))
return; return;
} }
m_unmountState = PreMountUnmount; setState(PreMountUnmounting);
m_mounter->unmount(); m_mounter->unmount();
break; break;
} }
case PreMountUnmount: case PreMountUnmounting:
if (m_stop) setState(Mounting);
return;
m_mounter->mount(); m_mounter->mount();
break; break;
case ShutdownUnmount: case PostRunCleaning:
Q_ASSERT(m_shuttingDown); case StopRequested:
m_mounter->resetMountSpecifications(); m_mounter->resetMountSpecifications();
m_shuttingDown = false; if (m_state == StopRequested) {
if (m_exitStatus == SshRemoteProcess::ExitedNormally) { emit remoteProcessFinished(InvalidExitCode);
} else if (m_exitStatus == SshRemoteProcess::ExitedNormally) {
emit remoteProcessFinished(m_runner->exitCode()); emit remoteProcessFinished(m_runner->exitCode());
} else if (m_exitStatus == -1) {
emit remoteProcessFinished(-1);
} else { } else {
emit error(tr("Error running remote process: %1") emit error(tr("Error running remote process: %1")
.arg(m_runner->errorString())); .arg(m_runner->errorString()));
} }
m_exitStatus = -1; setState(Inactive);
break; break;
default: ;
} }
} }
void MaemoSshRunner::handleMounted() void MaemoSshRunner::handleMounted()
{ {
if (!m_stop) ASSERT_STATE(QList<State>() << Mounting << StopRequested);
if (m_state == Mounting) {
setState(ReadyForExecution);
emit readyForExecution(); emit readyForExecution();
}
} }
void MaemoSshRunner::handleMounterError(const QString &errorMsg) void MaemoSshRunner::handleMounterError(const QString &errorMsg)
{ {
if (m_shuttingDown) ASSERT_STATE(QList<State>() << PreRunCleaning << PostRunCleaning
m_shuttingDown = false; << PreMountUnmounting << Mounting << StopRequested);
emit error(errorMsg);
emitError(errorMsg);
} }
void MaemoSshRunner::startExecution(const QByteArray &remoteCall) void MaemoSshRunner::startExecution(const QByteArray &remoteCall)
{ {
ASSERT_STATE(ReadyForExecution);
if (m_runConfig->remoteExecutableFilePath().isEmpty()) { if (m_runConfig->remoteExecutableFilePath().isEmpty()) {
emit error(tr("Cannot run: No remote executable set.")); emitError(tr("Cannot run: No remote executable set."));
return; return;
} }
@@ -265,6 +271,7 @@ void MaemoSshRunner::startExecution(const QByteArray &remoteCall)
SIGNAL(remoteOutput(QByteArray))); SIGNAL(remoteOutput(QByteArray)));
connect(m_runner.data(), SIGNAL(errorOutputAvailable(QByteArray)), this, connect(m_runner.data(), SIGNAL(errorOutputAvailable(QByteArray)), this,
SIGNAL(remoteErrorOutput(QByteArray))); SIGNAL(remoteErrorOutput(QByteArray)));
setState(ProcessStarting);
m_runner->start(); m_runner->start();
} }
@@ -273,15 +280,19 @@ void MaemoSshRunner::handleRemoteProcessFinished(int exitStatus)
Q_ASSERT(exitStatus == SshRemoteProcess::FailedToStart Q_ASSERT(exitStatus == SshRemoteProcess::FailedToStart
|| exitStatus == SshRemoteProcess::KilledBySignal || exitStatus == SshRemoteProcess::KilledBySignal
|| exitStatus == SshRemoteProcess::ExitedNormally); || exitStatus == SshRemoteProcess::ExitedNormally);
ASSERT_STATE(QList<State>() << ProcessStarting << StopRequested);
m_exitStatus = exitStatus; m_exitStatus = exitStatus;
cleanup(false); if (m_state != StopRequested) {
setState(PostRunCleaning);
cleanup();
}
} }
bool MaemoSshRunner::addMountSpecification(const MaemoMountSpecification &mountSpec) bool MaemoSshRunner::addMountSpecification(const MaemoMountSpecification &mountSpec)
{ {
if (!m_mounter->addMountSpecification(mountSpec, false)) { if (!m_mounter->addMountSpecification(mountSpec, false)) {
emit error(tr("The device does not have enough free ports " emitError(tr("The device does not have enough free ports "
"for this run configuration.")); "for this run configuration."));
return false; return false;
} }
@@ -294,6 +305,42 @@ bool MaemoSshRunner::isConnectionUsable() const
&& m_connection->connectionParameters() == m_devConfig.server; && m_connection->connectionParameters() == m_devConfig.server;
} }
void MaemoSshRunner::assertState(State expectedState, const char *func)
{
assertState(QList<State>() << expectedState, func);
}
void MaemoSshRunner::assertState(const QList<State> &expectedStates,
const char *func)
{
if (!expectedStates.contains(m_state))
qWarning("Unexpected state %d at %s.", m_state, func);
}
void MaemoSshRunner::setState(State newState)
{
if (newState == Inactive) {
m_mounter->setConnection(SshConnection::Ptr());
if (m_connection) {
disconnect(m_connection.data(), 0, this, 0);
m_connection = SshConnection::Ptr();
}
if (m_cleaner)
disconnect(m_cleaner.data(), 0, this, 0);
}
m_state = newState;
}
void MaemoSshRunner::emitError(const QString &errorMsg)
{
emit error(errorMsg);
setState(Inactive);
}
const qint64 MaemoSshRunner::InvalidExitCode
= std::numeric_limits<qint64>::min();
} // namespace Internal } // namespace Internal
} // namespace Qt4ProjectManager } // namespace Qt4ProjectManager

View File

@@ -67,6 +67,8 @@ public:
QSharedPointer<Core::SshConnection> connection() const { return m_connection; } QSharedPointer<Core::SshConnection> connection() const { return m_connection; }
static const qint64 InvalidExitCode;
signals: signals:
void error(const QString &error); void error(const QString &error);
void mountDebugOutput(const QString &output); void mountDebugOutput(const QString &output);
@@ -75,7 +77,7 @@ signals:
void remoteErrorOutput(const QByteArray &output); void remoteErrorOutput(const QByteArray &output);
void reportProgress(const QString &progressOutput); void reportProgress(const QString &progressOutput);
void remoteProcessStarted(); void remoteProcessStarted();
void remoteProcessFinished(int exitCode); void remoteProcessFinished(qint64 exitCode);
private slots: private slots:
void handleConnected(); void handleConnected();
@@ -87,7 +89,17 @@ private slots:
void handleMounterError(const QString &errorMsg); void handleMounterError(const QString &errorMsg);
private: private:
void cleanup(bool initialCleanup); enum State { Inactive, Connecting, PreRunCleaning, PostRunCleaning,
PreMountUnmounting, Mounting, ReadyForExecution,
ProcessStarting, StopRequested
};
void assertState(State expectedState, const char *func);
void assertState(const QList<State> &expectedStates, const char *func);
void setState(State newState);
void emitError(const QString &errorMsg);
void cleanup();
bool addMountSpecification(const MaemoMountSpecification &mountSpec); bool addMountSpecification(const MaemoMountSpecification &mountSpec);
bool isConnectionUsable() const; bool isConnectionUsable() const;
@@ -100,13 +112,9 @@ private:
QSharedPointer<Core::SshRemoteProcess> m_cleaner; QSharedPointer<Core::SshRemoteProcess> m_cleaner;
QStringList m_procsToKill; QStringList m_procsToKill;
bool m_stop;
int m_exitStatus; int m_exitStatus;
bool m_shuttingDown;
const bool m_debugging; const bool m_debugging;
State m_state;
enum UnmountState { InitialUnmount, PreMountUnmount, ShutdownUnmount };
UnmountState m_unmountState;
}; };
} // namespace Internal } // namespace Internal