Commit f82ece4a authored by Tomaz  Canabrava's avatar Tomaz Canabrava
Browse files

WIP test threads

parent 8b0e9fcb
......@@ -18,11 +18,8 @@ ConnectionsModel::ConnectionsModel(QObject *parent)
void ConnectionsModel::start()
{
connect(&timer, &QTimer::timeout, this, &ConnectionsModel::refreshConnections);
timer.setInterval(1000);
timer.start();
QTimer::singleShot(0, this, &ConnectionsModel::refreshConnections);
connect(&m_netstatHelper, &NetstatHelper::queryFinished, this, &ConnectionsModel::refreshConnections, Qt::QueuedConnection);
m_netstatHelper.start();
}
bool ConnectionsModel::busy() const
......@@ -90,7 +87,7 @@ QHash<int, QByteArray> ConnectionsModel::roleNames() const
};
}
void ConnectionsModel::refreshConnections()
void ConnectionsModel::refreshConnections(const QVector<QStringList> &values)
{
if (m_busy) {
return;
......@@ -98,10 +95,8 @@ void ConnectionsModel::refreshConnections()
setBusy(true);
NetstatHelper helper;
QVector<QStringList> result = helper.query();
if (helper.hasError()) {
emit showErrorMessage(i18n("Failed to get connections: %1", helper.errorString()));
if (m_netstatHelper.hasError()) {
emit showErrorMessage(i18n("Failed to get connections: %1", m_netstatHelper.errorString()));
return;
}
......@@ -110,7 +105,7 @@ void ConnectionsModel::refreshConnections()
beginResetModel();
m_connectionsData.clear();
for (const auto connection : result) {
for (const auto connection : values) {
ConnectionsData conn {.protocol = connection.at(0),
.localAddress = connection.at(1),
.foreignAddress = connection.at(2),
......
......@@ -10,6 +10,8 @@
#include <QLoggingCategory>
#include "netstathelper.h"
Q_DECLARE_LOGGING_CATEGORY(ConnectionsModelDebug)
struct ConnectionsData {
......@@ -60,7 +62,7 @@ signals:
void showErrorMessage(const QString &message);
protected slots:
void refreshConnections();
void refreshConnections(const QVector<QStringList> &values);
private:
void setBusy(bool busy);
......@@ -68,6 +70,7 @@ private:
bool m_busy = false;
QVector<ConnectionsData> m_connectionsData;
QTimer timer;
NetstatHelper m_netstatHelper;
};
#endif // CONECTIONSMODEL_H
......@@ -15,11 +15,26 @@ Q_LOGGING_CATEGORY(NetstatHelperDebug, "netstat.helper")
NetstatHelper::NetstatHelper() : m_hasError(false)
{
connect(&m_timer, &QTimer::timeout, this, &NetstatHelper::timeout, Qt::QueuedConnection);
setTerminationEnabled(true);
}
QVector<QStringList> NetstatHelper::query()
void NetstatHelper::timeout()
{
if (m_elapsedTimer.elapsed() > 2000) // wait for at least 2s
{
qDebug() << "Force-Terminate the thread";
terminate();
start();
}
}
// This method runs in a different thread.
void NetstatHelper::run()
{
m_elapsedTimer.restart();
m_hasError = false;
QProcess netstat;
/* parameters passed to ss
* -r, --resolve resolve host names
......@@ -43,9 +58,9 @@ QVector<QStringList> NetstatHelper::query()
m_errorString = netstat.readAllStandardError();
} else {
result = parseSSOutput(netstat.readAllStandardOutput());
emit queryFinished(result);
}
return result;
}
bool NetstatHelper::hasError() const
......
......@@ -6,28 +6,41 @@
#define NETSTATHELPER_H
#include <QVariantMap>
#include <QTimer>
#include <QElapsedTimer>
#include <QLoggingCategory>
#include <QThread>
Q_DECLARE_LOGGING_CATEGORY(NetstatHelperDebug)
class NetstatHelper : public QObject
class NetstatHelper : public QThread
{
Q_OBJECT
public:
NetstatHelper();
void run() override;
public Q_SLOTS:
QVector<QStringList> query();
QString errorString() const;
bool hasError() const;
/*
if the query takes too long, this timeout happens,
closing the old process, and reopening.
*/
void timeout();
signals:
void queryFinished(const QVector<QStringList>& values);
private:
QVector<QStringList> parseSSOutput(const QByteArray &ss);
QString extractAndStrip(const QString &src, const int &index, const int &size);
QString m_errorString;
bool m_hasError;
QTimer m_timer;
QElapsedTimer m_elapsedTimer;
};
#endif // NETSTATHELPER_H
......@@ -46,7 +46,7 @@ ViewBase {
base.errorMessage.visible = true;
} else {
console.log("Starting netstat client");
netStatClient.connectionsModel.start();
base.model.start();
}
}
}
......
Supports Markdown
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