Commit 159c4cfe authored by ck's avatar ck
Browse files

Maemo: Make SSH connections thread-safe.

parent 6cf0247b
......@@ -137,7 +137,6 @@ private slots:
void passwordEditingFinished();
void keyFileEditingFinished();
void testConfig();
void enableTestStop();
void processSshOutput(const QString &data);
void handleSshFinished();
void stopConfigTest();
......@@ -397,6 +396,7 @@ void MaemoSettingsWidget::testConfig()
if (m_deviceTester)
return;
m_ui->testConfigButton->disconnect();
m_ui->testResultEdit->setPlainText(m_defaultTestOutput);
QLatin1String sysInfoCmd("uname -rsm");
QLatin1String qtInfoCmd("dpkg -l |grep libqt "
......@@ -404,22 +404,15 @@ void MaemoSettingsWidget::testConfig()
"|cut -d ' ' -f 2,3 |sed 's/~.*//g'");
QString command(sysInfoCmd + " && " + qtInfoCmd);
m_deviceTester = new MaemoSshRunner(currentConfig(), command);
connect(m_deviceTester, SIGNAL(connectionEstablished()),
this, SLOT(enableTestStop()));
connect(m_deviceTester, SIGNAL(remoteOutput(QString)),
this, SLOT(processSshOutput(QString)));
connect(m_deviceTester, SIGNAL(finished()),
this, SLOT(handleSshFinished()));
m_deviceTester->start();
#endif
}
void MaemoSettingsWidget::enableTestStop()
{
m_ui->testConfigButton->disconnect();
m_ui->testConfigButton->setText(tr("Stop test"));
connect(m_ui->testConfigButton, SIGNAL(clicked()),
this, SLOT(stopConfigTest()));
m_deviceTester->start();
#endif
}
void MaemoSettingsWidget::processSshOutput(const QString &data)
......@@ -453,15 +446,14 @@ void MaemoSettingsWidget::stopConfigTest()
qDebug("================> %s", Q_FUNC_INFO);
if (m_deviceTester) {
qDebug("Actually doing something");
m_deviceTester->disconnect();
m_ui->testConfigButton->disconnect();
const bool buttonWasEnabled = m_ui->testConfigButton->isEnabled();
m_ui->testConfigButton->setEnabled(false);
m_deviceTester->disconnect();
m_deviceTester->stop();
delete m_deviceTester;
m_deviceTester = 0;
m_deviceTestOutput.clear();
m_ui->testConfigButton->setText(tr("Test"));
m_ui->testConfigButton->disconnect();
connect(m_ui->testConfigButton, SIGNAL(clicked()),
this, SLOT(testConfig()));
m_ui->testConfigButton->setEnabled(buttonWasEnabled);
......
......@@ -47,7 +47,7 @@ namespace Qt4ProjectManager {
namespace Internal {
MaemoSshThread::MaemoSshThread(const MaemoDeviceConfig &devConf)
: m_devConf(devConf)
: m_stopRequested(false), m_devConf(devConf)
{
}
......@@ -60,7 +60,8 @@ MaemoSshThread::~MaemoSshThread()
void MaemoSshThread::run()
{
try {
runInternal();
if (!m_stopRequested)
runInternal();
} catch (const MaemoSshException &e) {
m_error = e.error();
}
......@@ -68,13 +69,21 @@ void MaemoSshThread::run()
void MaemoSshThread::stop()
{
m_connection->stop();
m_mutex.lock();
m_stopRequested = true;
const bool hasConnection = !m_connection.isNull();
m_mutex.unlock();
if (hasConnection)
m_connection->stop();
}
void MaemoSshThread::setConnection(const MaemoSshConnection::Ptr &connection)
template <class Conn> typename Conn::Ptr MaemoSshThread::createConnection()
{
typename Conn::Ptr connection = Conn::create(m_devConf);
m_mutex.lock();
m_connection = connection;
emit connectionEstablished();
m_mutex.unlock();
return connection;
}
MaemoSshRunner::MaemoSshRunner(const MaemoDeviceConfig &devConf,
......@@ -86,8 +95,9 @@ MaemoSshRunner::MaemoSshRunner(const MaemoDeviceConfig &devConf,
void MaemoSshRunner::runInternal()
{
MaemoInteractiveSshConnection::Ptr connection
= MaemoInteractiveSshConnection::create(m_devConf);
setConnection(connection);
= createConnection<MaemoInteractiveSshConnection>();
if (stopRequested())
return;
connect(connection.data(), SIGNAL(remoteOutput(QString)),
this, SIGNAL(remoteOutput(QString)));
connection->runCommand(m_command);
......@@ -102,8 +112,9 @@ MaemoSshDeployer::MaemoSshDeployer(const MaemoDeviceConfig &devConf,
void MaemoSshDeployer::runInternal()
{
MaemoSftpConnection::Ptr connection
= MaemoSftpConnection::create(m_devConf);
setConnection(connection);
= createConnection<MaemoSftpConnection>();
if (stopRequested())
return;
connect(connection.data(), SIGNAL(fileCopied(QString)),
this, SIGNAL(fileCopied(QString)));
connection->transferFiles(m_filePaths, m_targetDirs);
......
......@@ -45,6 +45,7 @@
#include "maemodeviceconfigurations.h"
#include "maemosshconnection.h"
#include <QtCore/QMutex>
#include <QtCore/QStringList>
#include <QtCore/QThread>
......@@ -64,19 +65,18 @@ public:
virtual void run();
~MaemoSshThread();
signals:
void connectionEstablished();
protected:
MaemoSshThread(const MaemoDeviceConfig &devConf);
virtual void runInternal()=0;
void setConnection(const MaemoSshConnection::Ptr &connection);
const MaemoDeviceConfig m_devConf;
template <class Conn> typename Conn::Ptr createConnection();
bool stopRequested() const { return m_stopRequested; }
private:
virtual void runInternal()=0;
bool m_stopRequested;
QString m_error;
QMutex m_mutex;
const MaemoDeviceConfig m_devConf;
MaemoSshConnection::Ptr m_connection;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment