Maemo: Split up MaemoSshConnection.

We now have two subclasses for deploying files and
running commands, respectively.
This commit is contained in:
ck
2009-12-23 11:57:56 +01:00
parent b3d3089955
commit 19f1df4fe6
5 changed files with 115 additions and 48 deletions

View File

@@ -156,7 +156,7 @@ private:
PortAndTimeoutValidator m_portValidator; PortAndTimeoutValidator m_portValidator;
PortAndTimeoutValidator m_timeoutValidator; PortAndTimeoutValidator m_timeoutValidator;
NameValidator m_nameValidator; NameValidator m_nameValidator;
#ifdef USE_SSH_LIBS #ifdef USE_SSH_LIB
MaemoSshRunner *m_deviceTester; MaemoSshRunner *m_deviceTester;
#endif #endif
QString m_deviceTestOutput; QString m_deviceTestOutput;
@@ -213,7 +213,7 @@ MaemoSettingsWidget::MaemoSettingsWidget(QWidget *parent)
m_ui(new Ui_maemoSettingsWidget), m_ui(new Ui_maemoSettingsWidget),
m_devConfs(MaemoDeviceConfigurations::instance().devConfigs()), m_devConfs(MaemoDeviceConfigurations::instance().devConfigs()),
m_nameValidator(m_devConfs) m_nameValidator(m_devConfs)
#ifdef USE_SSH_LIBS #ifdef USE_SSH_LIB
, m_deviceTester(0) , m_deviceTester(0)
#endif #endif
@@ -392,7 +392,7 @@ void MaemoSettingsWidget::keyFileEditingFinished()
void MaemoSettingsWidget::testConfig() void MaemoSettingsWidget::testConfig()
{ {
#ifdef USE_SSH_LIBS #ifdef USE_SSH_LIB
qDebug("Oh yes, this config will be tested!"); qDebug("Oh yes, this config will be tested!");
if (m_deviceTester) if (m_deviceTester)
return; return;
@@ -430,7 +430,7 @@ void MaemoSettingsWidget::processSshOutput(const QString &data)
void MaemoSettingsWidget::handleSshFinished() void MaemoSettingsWidget::handleSshFinished()
{ {
#ifdef USE_SSH_LIBS #ifdef USE_SSH_LIB
qDebug("================> %s", Q_FUNC_INFO); qDebug("================> %s", Q_FUNC_INFO);
if (!m_deviceTester) if (!m_deviceTester)
return; return;
@@ -449,7 +449,7 @@ void MaemoSettingsWidget::handleSshFinished()
void MaemoSettingsWidget::stopConfigTest() void MaemoSettingsWidget::stopConfigTest()
{ {
#ifdef USE_SSH_LIBS #ifdef USE_SSH_LIB
qDebug("================> %s", Q_FUNC_INFO); qDebug("================> %s", Q_FUNC_INFO);
if (m_deviceTester) { if (m_deviceTester) {
qDebug("Actually doing something"); qDebug("Actually doing something");

View File

@@ -56,15 +56,9 @@ namespace {
ne7ssh ssh; ne7ssh ssh;
} }
MaemoSshConnection::Ptr MaemoSshConnection::connect(
const MaemoDeviceConfig &devConf, bool shell)
{
return MaemoSshConnection::Ptr(new MaemoSshConnection(devConf, shell));
}
MaemoSshConnection::MaemoSshConnection(const MaemoDeviceConfig &devConf, MaemoSshConnection::MaemoSshConnection(const MaemoDeviceConfig &devConf,
bool shell) bool shell)
: m_channel(-1), m_prompt(0), m_stopRequested(false) : m_channel(-1), m_stopRequested(false)
{ {
const QString *authString; const QString *authString;
int (ne7ssh::*connFunc)(const char *, int, const char *, const char *, bool, int); int (ne7ssh::*connFunc)(const char *, int, const char *, const char *, bool, int);
@@ -79,54 +73,89 @@ MaemoSshConnection::MaemoSshConnection(const MaemoDeviceConfig &devConf,
devConf.uname.toAscii(), authString->toAscii(), shell, devConf.timeout); devConf.uname.toAscii(), authString->toAscii(), shell, devConf.timeout);
if (m_channel == -1) if (m_channel == -1)
throw MaemoSshException(tr("Could not connect to host")); throw MaemoSshException(tr("Could not connect to host"));
if (shell) {
m_prompt = devConf.uname == QLatin1String("root") ? "# " : "$ ";
if (!ssh.waitFor(m_channel, m_prompt, devConf.timeout)) {
const QString error = tr("Could not start remote shell: %1").
arg(ssh.errors()->pop(m_channel));
ssh.close(m_channel);
throw MaemoSshException(error);
}
}
} }
MaemoSshConnection::~MaemoSshConnection() MaemoSshConnection::~MaemoSshConnection()
{ {
qDebug("%s", Q_FUNC_INFO); qDebug("%s", Q_FUNC_INFO);
if (m_prompt) {
ssh.send("exit\n", m_channel);
ssh.waitFor(m_channel, m_prompt, 1);
}
ssh.close(m_channel); ssh.close(m_channel);
} }
void MaemoSshConnection::runCommand(const QString &command) const char *MaemoSshConnection::lastError()
{
return ssh.errors()->pop(channel());
}
void MaemoSshConnection::stop()
{
m_stopRequested = true;
}
MaemoInteractiveSshConnection::MaemoInteractiveSshConnection(const MaemoDeviceConfig &devConf)
: MaemoSshConnection(devConf, true), m_prompt(0)
{
m_prompt = devConf.uname == QLatin1String("root") ? "# " : "$ ";
if (!ssh.waitFor(channel(), m_prompt, devConf.timeout)) {
const QString error
= tr("Could not start remote shell: %1").arg(lastError());
throw MaemoSshException(error);
}
}
MaemoInteractiveSshConnection::~MaemoInteractiveSshConnection()
{
ssh.send("exit\n", channel());
ssh.waitFor(channel(), m_prompt, 1);
}
void MaemoInteractiveSshConnection::runCommand(const QString &command)
{ {
if (!ssh.send((command + QLatin1String("\n")).toLatin1().data(), if (!ssh.send((command + QLatin1String("\n")).toLatin1().data(),
m_channel)) { channel())) {
throw MaemoSshException(tr("Error running command: %1") throw MaemoSshException(tr("Error running command: %1")
.arg(ssh.errors()->pop(m_channel))); .arg(lastError()));
} }
bool done; bool done;
do { do {
done = ssh.waitFor(m_channel, m_prompt, 3); done = ssh.waitFor(channel(), m_prompt, 2);
const char * const error = ssh.errors()->pop(m_channel); const char * const error = lastError();
if (error) if (error)
throw MaemoSshException(tr("SSH error: %1").arg(error)); throw MaemoSshException(tr("SSH error: %1").arg(error));
const char * const output = ssh.read(m_channel); const char * const output = ssh.read(channel());
if (output) if (output)
emit remoteOutput(QLatin1String(output)); emit remoteOutput(QLatin1String(output));
} while (!done && !m_stopRequested); } while (!done && !stopRequested());
} }
void MaemoSshConnection::stopCommand() MaemoInteractiveSshConnection::Ptr MaemoInteractiveSshConnection::create(const MaemoDeviceConfig &devConf)
{ {
m_stopRequested = true; return Ptr(new MaemoInteractiveSshConnection(devConf));
} }
MaemoSftpConnection::MaemoSftpConnection(const MaemoDeviceConfig &devConf)
: MaemoSshConnection(devConf, false)
{
// TODO: Initialize sftp subsystem
}
MaemoSftpConnection::~MaemoSftpConnection()
{
}
void MaemoSftpConnection::transferFiles(const QStringList &filePaths,
const QStringList &targetDirs)
{
}
MaemoSftpConnection::Ptr MaemoSftpConnection::create(const MaemoDeviceConfig &devConf)
{
return Ptr(new MaemoSftpConnection(devConf));
}
} // namespace Internal } // namespace Internal
} // namespace Qt4ProjectManager } // namespace Qt4ProjectManager

View File

@@ -42,12 +42,17 @@
#ifndef MAEMOSSHCONNECTION_H #ifndef MAEMOSSHCONNECTION_H
#define MAEMOSSHCONNECTION_H #define MAEMOSSHCONNECTION_H
// #define USE_SSH_LIB
#ifdef USE_SSH_LIB #ifdef USE_SSH_LIB
#include <QtCore/QCoreApplication> #include <QtCore/QCoreApplication>
#include <QtCore/QScopedPointer> #include <QtCore/QScopedPointer>
#include <QtCore/QSharedPointer> #include <QtCore/QSharedPointer>
QT_BEGIN_NAMESPACE
class QStringList;
QT_END_NAMESPACE
class ne7ssh; class ne7ssh;
namespace Qt4ProjectManager { namespace Qt4ProjectManager {
@@ -66,26 +71,59 @@ private:
class MaemoSshConnection : public QObject class MaemoSshConnection : public QObject
{ {
Q_OBJECT
Q_DISABLE_COPY(MaemoSshConnection) Q_DISABLE_COPY(MaemoSshConnection)
friend class MaemoSshFacility;
public: public:
typedef QSharedPointer<MaemoSshConnection> Ptr; void stop();
virtual ~MaemoSshConnection();
static Ptr connect(const MaemoDeviceConfig &devConf, bool shell); protected:
MaemoSshConnection(const MaemoDeviceConfig &devConf, bool shell);
int channel() const { return m_channel; }
bool stopRequested() const {return m_stopRequested; }
const char *lastError();
private:
int m_channel;
volatile bool m_stopRequested;
};
class MaemoInteractiveSshConnection : public MaemoSshConnection
{
Q_OBJECT
Q_DISABLE_COPY(MaemoInteractiveSshConnection)
public:
typedef QSharedPointer<MaemoInteractiveSshConnection> Ptr;
static Ptr create(const MaemoDeviceConfig &devConf);
void runCommand(const QString &command); void runCommand(const QString &command);
void stopCommand(); virtual ~MaemoInteractiveSshConnection();
~MaemoSshConnection();
signals: signals:
void remoteOutput(const QString &output); void remoteOutput(const QString &output);
private: private:
MaemoSshConnection(const MaemoDeviceConfig &devConf, bool shell); MaemoInteractiveSshConnection(const MaemoDeviceConfig &devConf);
int m_channel;
const char *m_prompt; const char *m_prompt;
volatile bool m_stopRequested; };
class MaemoSftpConnection : public MaemoSshConnection
{
Q_OBJECT
Q_DISABLE_COPY(MaemoSftpConnection)
public:
typedef QSharedPointer<MaemoSftpConnection> Ptr;
static Ptr create(const MaemoDeviceConfig &devConf);
void transferFiles(const QStringList &filePaths,
const QStringList &targetDirs);
virtual ~MaemoSftpConnection();
signals:
void fileCopied(const QString &filePath);
private:
MaemoSftpConnection(const MaemoDeviceConfig &devConf);
}; };
} // namespace Internal } // namespace Internal

View File

@@ -54,7 +54,7 @@ MaemoSshRunner::MaemoSshRunner(const MaemoDeviceConfig &devConf, const QString &
void MaemoSshRunner::run() void MaemoSshRunner::run()
{ {
try { try {
m_connection = MaemoSshConnection::connect(m_devConf, true); m_connection = MaemoInteractiveSshConnection::create(m_devConf);
emit connectionEstablished(); emit connectionEstablished();
connect(m_connection.data(), SIGNAL(remoteOutput(QString)), connect(m_connection.data(), SIGNAL(remoteOutput(QString)),
this, SIGNAL(remoteOutput(QString))); this, SIGNAL(remoteOutput(QString)));
@@ -67,7 +67,7 @@ void MaemoSshRunner::run()
void MaemoSshRunner::stop() void MaemoSshRunner::stop()
{ {
if (!m_connection.isNull()) if (!m_connection.isNull())
m_connection->stopCommand(); m_connection->stop();
wait(); wait();
} }

View File

@@ -71,7 +71,7 @@ private:
const MaemoDeviceConfig m_devConf; const MaemoDeviceConfig m_devConf;
const QString m_command; const QString m_command;
QString m_error; QString m_error;
MaemoSshConnection::Ptr m_connection; MaemoInteractiveSshConnection::Ptr m_connection;
}; };
} // namespace Internal } // namespace Internal