Maemo: Add support for checking available TCP ports on device.

Reviewed-by: kh1
This commit is contained in:
Christian Kandeler
2010-10-15 15:06:50 +02:00
parent 270cd904f8
commit 235430fd8a
7 changed files with 304 additions and 4 deletions

View File

@@ -37,6 +37,7 @@
#include "maemodeviceconfigurations.h" #include "maemodeviceconfigurations.h"
#include "maemoglobal.h" #include "maemoglobal.h"
#include "maemousedportsgatherer.h"
#include <coreplugin/ssh/sshremoteprocessrunner.h> #include <coreplugin/ssh/sshremoteprocessrunner.h>
@@ -52,6 +53,7 @@ MaemoConfigTestDialog::MaemoConfigTestDialog(const MaemoDeviceConfig &config, QW
: QDialog(parent) : QDialog(parent)
, m_ui(new Ui_MaemoConfigTestDialog) , m_ui(new Ui_MaemoConfigTestDialog)
, m_config(config) , m_config(config)
, m_portsGatherer(new MaemoUsedPortsGatherer(this))
{ {
setAttribute(Qt::WA_DeleteOnClose); setAttribute(Qt::WA_DeleteOnClose);
@@ -59,6 +61,10 @@ MaemoConfigTestDialog::MaemoConfigTestDialog(const MaemoDeviceConfig &config, QW
m_closeButton = m_ui->buttonBox->button(QDialogButtonBox::Close); m_closeButton = m_ui->buttonBox->button(QDialogButtonBox::Close);
connect(m_closeButton, SIGNAL(clicked()), SLOT(stopConfigTest())); connect(m_closeButton, SIGNAL(clicked()), SLOT(stopConfigTest()));
connect(m_portsGatherer, SIGNAL(error(QString)),
SLOT(handlePortListFailure(QString)));
connect(m_portsGatherer, SIGNAL(portListReady()),
SLOT(handlePortListReady()));
startConfigTest(); startConfigTest();
} }
@@ -151,6 +157,37 @@ void MaemoConfigTestDialog::handleMadDeveloperTestResult(int exitStatus)
+ QLatin1String("<br>") + tr("Mad Developer is not installed.<br>" + QLatin1String("<br>") + tr("Mad Developer is not installed.<br>"
"You will not be able to deploy to this device.")); "You will not be able to deploy to this device."));
} }
if (m_config.freePorts().hasMore())
m_portsGatherer->start(m_testProcessRunner->connection(),
m_config.freePorts());
else
finish();
}
void MaemoConfigTestDialog::handlePortListFailure(const QString &errMsg)
{
m_ui->testResultEdit->appendPlainText(tr("Error retrieving list of used ports: %1")
.arg(errMsg));
finish();
}
void MaemoConfigTestDialog::handlePortListReady()
{
const QList<int> &usedPorts = m_portsGatherer->usedPorts();
QString output;
if (usedPorts.isEmpty()) {
output = tr("All specified ports are available.");
} else {
output = tr("The following supposedly free ports are being used on the device:");
foreach (const int port, usedPorts)
output += QLatin1Char(' ') + QString::number(port);
}
m_ui->testResultEdit->appendPlainText(output);
finish();
}
void MaemoConfigTestDialog::finish()
{
if (m_ui->errorLabel->text().isEmpty()) { if (m_ui->errorLabel->text().isEmpty()) {
QPalette palette = m_ui->errorLabel->palette(); QPalette palette = m_ui->errorLabel->palette();
palette.setColor(m_ui->errorLabel->foregroundRole(), palette.setColor(m_ui->errorLabel->foregroundRole(),

View File

@@ -51,6 +51,7 @@ namespace Qt4ProjectManager {
namespace Internal { namespace Internal {
class MaemoDeviceConfig; class MaemoDeviceConfig;
class MaemoUsedPortsGatherer;
/** /**
* A dialog that runs a test of a device configuration. * A dialog that runs a test of a device configuration.
@@ -67,12 +68,15 @@ private slots:
void processSshOutput(const QByteArray &output); void processSshOutput(const QByteArray &output);
void handleConnectionError(); void handleConnectionError();
void handleTestProcessFinished(int exitStatus); void handleTestProcessFinished(int exitStatus);
void handlePortListReady();
void handlePortListFailure(const QString &errMsg);
private: private:
void startConfigTest(); void startConfigTest();
QString parseTestOutput(); QString parseTestOutput();
void handleGeneralTestResult(int exitStatus); void handleGeneralTestResult(int exitStatus);
void handleMadDeveloperTestResult(int exitStatus); void handleMadDeveloperTestResult(int exitStatus);
void finish();
Ui_MaemoConfigTestDialog *m_ui; Ui_MaemoConfigTestDialog *m_ui;
QPushButton *m_closeButton; QPushButton *m_closeButton;
@@ -81,6 +85,7 @@ private:
QSharedPointer<Core::SshRemoteProcessRunner> m_testProcessRunner; QSharedPointer<Core::SshRemoteProcessRunner> m_testProcessRunner;
QString m_deviceTestOutput; QString m_deviceTestOutput;
bool m_qtVersionOk; bool m_qtVersionOk;
MaemoUsedPortsGatherer *const m_portsGatherer;
enum DeviceTest { GeneralTest, MadDeveloperTest }; enum DeviceTest { GeneralTest, MadDeveloperTest };
DeviceTest m_currentTest; DeviceTest m_currentTest;

View File

@@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>661</width> <width>684</width>
<height>324</height> <height>544</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">

View File

@@ -72,6 +72,19 @@ public:
m_ranges.removeFirst(); m_ranges.removeFirst();
return next; return next;
} }
QString toString() const
{
QString stringRep;
foreach (const Range &range, m_ranges) {
stringRep += QString::number(range.first);
if (range.second != range.first)
stringRep += QLatin1Char('-') + QString::number(range.second);
stringRep += QLatin1Char(',');
}
if (!stringRep.isEmpty())
stringRep.remove(stringRep.length() - 1, 1); // Trailing comma.
return stringRep;
}
private: private:
QList<Range> m_ranges; QList<Range> m_ranges;

View File

@@ -0,0 +1,160 @@
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Creator.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "maemousedportsgatherer.h"
#include "maemoglobal.h"
#include <coreplugin/ssh/sshremoteprocessrunner.h>
using namespace Core;
namespace Qt4ProjectManager {
namespace Internal {
MaemoUsedPortsGatherer::MaemoUsedPortsGatherer(QObject *parent) :
QObject(parent), m_running(false)
{
}
MaemoUsedPortsGatherer::~MaemoUsedPortsGatherer() {}
void MaemoUsedPortsGatherer::start(const Core::SshConnection::Ptr &connection,
const MaemoPortList &portList)
{
if (m_running)
qWarning("Unexpected call of %s in running state", Q_FUNC_INFO);
m_remoteStdout.clear();
m_remoteStderr.clear();
m_procRunner = SshRemoteProcessRunner::create(connection);
connect(m_procRunner.data(), SIGNAL(connectionError(Core::SshError)),
SLOT(handleConnectionError()));
connect(m_procRunner.data(), SIGNAL(processClosed(int)),
SLOT(handleProcessClosed(int)));
connect(m_procRunner.data(), SIGNAL(processOutputAvailable(QByteArray)),
SLOT(handleRemoteStdOut(QByteArray)));
connect(m_procRunner.data(), SIGNAL(processErrorOutputAvailable(QByteArray)),
SLOT(handleRemoteStdErr(QByteArray)));
const QString command = MaemoGlobal::remoteSudo()
+ QLatin1String(" lsof -nPi4tcp:") + portList.toString()
+ QLatin1String(" -F n |grep '^n' |sed -r 's/[^:]*:([[:digit:]]+).*/\\1/g' |sort -n |uniq");
m_procRunner->run(command.toUtf8());
m_running = true;
}
void MaemoUsedPortsGatherer::stop()
{
if (!m_running)
return;
m_running = false;
disconnect(m_procRunner->connection().data(), 0, this, 0);
if (m_procRunner->process())
m_procRunner->process()->closeChannel();
}
QList<int> MaemoUsedPortsGatherer::usedPorts() const
{
QList<int> ports;
const QList<QByteArray> &portStrings = m_remoteStdout.split('\n');
foreach (const QByteArray &portString, portStrings) {
if (portString.isEmpty())
continue;
bool ok;
const int port = portString.toInt(&ok);
if (ok) {
ports << port;
} else {
qWarning("%s: Unexpected string '%s' is not a port.",
Q_FUNC_INFO, portString.data());
}
}
return ports;
}
void MaemoUsedPortsGatherer::handleConnectionError()
{
if (!m_running)
return;
emit error(tr("Connection error: %1").
arg(m_procRunner->connection()->errorString()));
stop();
}
void MaemoUsedPortsGatherer::handleProcessClosed(int exitStatus)
{
if (!m_running)
return;
QString errMsg;
switch (exitStatus) {
case SshRemoteProcess::FailedToStart:
errMsg = tr("Could not start remote process: %1")
.arg(m_procRunner->process()->errorString());
break;
case SshRemoteProcess::KilledBySignal:
errMsg = tr("Remote process crashed: %1")
.arg(m_procRunner->process()->errorString());
break;
case SshRemoteProcess::ExitedNormally:
if (m_procRunner->process()->exitCode() == 0) {
emit portListReady();
} else {
errMsg = tr("Remote process failed: %1")
.arg(m_procRunner->process()->errorString());
}
break;
default:
Q_ASSERT_X(false, Q_FUNC_INFO, "Invalid exit status");
}
if (!errMsg.isEmpty()) {
if (!m_remoteStderr.isEmpty()) {
errMsg += tr("\nRemote error output was: %1")
.arg(QString::fromUtf8(m_remoteStderr));
}
emit error(errMsg);
}
stop();
}
void MaemoUsedPortsGatherer::handleRemoteStdOut(const QByteArray &output)
{
m_remoteStdout += output;
}
void MaemoUsedPortsGatherer::handleRemoteStdErr(const QByteArray &output)
{
m_remoteStderr += output;
}
} // namespace Internal
} // namespace Qt4ProjectManager

View File

@@ -0,0 +1,83 @@
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Creator.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef MAEMOUSEDPORTSGATHERER_H
#define MAEMOUSEDPORTSGATHERER_H
#include "maemodeviceconfigurations.h"
#include <QtCore/QList>
#include <QtCore/QObject>
#include <QtCore/QSharedPointer>
#include <QtCore/QString>
namespace Core {
class SshConnection;
class SshRemoteProcessRunner;
}
namespace Qt4ProjectManager {
namespace Internal {
class MaemoUsedPortsGatherer : public QObject
{
Q_OBJECT
public:
explicit MaemoUsedPortsGatherer(QObject *parent = 0);
~MaemoUsedPortsGatherer();
void start(const QSharedPointer<Core::SshConnection> &connection,
const MaemoPortList &portList);
void stop();
QList<int> usedPorts() const;
signals:
void error(const QString &errMsg);
void portListReady();
private slots:
void handleConnectionError();
void handleProcessClosed(int exitStatus);
void handleRemoteStdOut(const QByteArray &output);
void handleRemoteStdErr(const QByteArray &output);
private:
QSharedPointer<Core::SshRemoteProcessRunner> m_procRunner;
QByteArray m_remoteStdout;
QByteArray m_remoteStderr;
bool m_running;
};
} // namespace Internal
} // namespace Qt4ProjectManager
#endif // MAEMOUSEDPORTSGATHERER_H

View File

@@ -32,7 +32,8 @@ HEADERS += \
$$PWD/maemotemplatesmanager.h \ $$PWD/maemotemplatesmanager.h \
$$PWD/maemomountspecification.h \ $$PWD/maemomountspecification.h \
$$PWD/maemoremotemounter.h \ $$PWD/maemoremotemounter.h \
$$PWD/maemoprofilesupdatedialog.h $$PWD/maemoprofilesupdatedialog.h \
$$PWD/maemousedportsgatherer.h
SOURCES += \ SOURCES += \
$$PWD/maemoconfigtestdialog.cpp \ $$PWD/maemoconfigtestdialog.cpp \
@@ -66,7 +67,8 @@ SOURCES += \
$$PWD/maemotemplatesmanager.cpp \ $$PWD/maemotemplatesmanager.cpp \
$$PWD/maemomountspecification.cpp \ $$PWD/maemomountspecification.cpp \
$$PWD/maemoremotemounter.cpp \ $$PWD/maemoremotemounter.cpp \
$$PWD/maemoprofilesupdatedialog.cpp $$PWD/maemoprofilesupdatedialog.cpp \
$$PWD/maemousedportsgatherer.cpp
FORMS += \ FORMS += \
$$PWD/maemoconfigtestdialog.ui \ $$PWD/maemoconfigtestdialog.ui \