Skip to content

Commit d3e1b24

Browse files
committed
use qcoro for async ssh control
using coroutines allows use async io without having to deal with multithreading
1 parent 875ec88 commit d3e1b24

File tree

6 files changed

+61
-30
lines changed

6 files changed

+61
-30
lines changed

CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ if(NOT CMAKE_BUILD_TYPE)
2323
)
2424
endif()
2525

26-
set(CMAKE_CXX_STANDARD 17)
26+
set(CMAKE_CXX_STANDARD 20)
2727
set(CMAKE_CXX_STANDARD_REQUIRED ON)
2828

2929
option(APPIMAGE_BUILD "configure build for bundling in an appimage" OFF)
@@ -58,6 +58,13 @@ find_package(
5858
REQUIRED
5959
)
6060

61+
find_package(
62+
QCoro${QT_MAJOR_VERSION}
63+
COMPONENTS Core
64+
REQUIRED
65+
)
66+
qcoro_enable_coroutines()
67+
6168
find_package(LibElf REQUIRED)
6269
find_package(ElfUtils REQUIRED)
6370
find_package(ECM 1.0.0 NO_MODULE REQUIRED)

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ target_link_libraries(
120120
models
121121
PrefixTickLabels
122122
KDAB::kddockwidgets
123+
QCoro::Core
123124
)
124125

125126
if(KFArchive_FOUND)

src/recordhost.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include <QProcess>
1212
#include <QStandardPaths>
1313
#include <QThread>
14+
#include <QTimer>
15+
16+
#include <QCoroCore>
1417

1518
#include <KShell>
1619
#include <KUser>
@@ -282,13 +285,17 @@ void RecordHost::setCurrentWorkingDirectory(const QString& cwd)
282285
emit currentWorkingDirectoryChanged(cwd);
283286
}
284287
} else {
285-
if (!m_remoteDevice->checkIfDirectoryExists(cwd)) {
286-
emit errorOccurred(tr("Working directory folder cannot be found: %1").arg(cwd));
287-
} else {
288-
emit errorOccurred({});
289-
m_cwd = cwd;
290-
emit currentWorkingDirectoryChanged(m_cwd);
291-
}
288+
QTimer::singleShot(0, this, [this, cwd]() -> QCoro::Task<void> {
289+
bool exists = co_await m_remoteDevice->checkIfDirectoryExists(cwd);
290+
if (!exists) {
291+
emit errorOccurred(tr("Working directory folder cannot be found: %1").arg(cwd));
292+
} else {
293+
emit errorOccurred({});
294+
m_cwd = cwd;
295+
emit currentWorkingDirectoryChanged(m_cwd);
296+
}
297+
co_return;
298+
});
292299
}
293300
}
294301

@@ -324,12 +331,17 @@ void RecordHost::setClientApplication(const QString& clientApplication)
324331
} else {
325332
if (!m_remoteDevice->isConnected()) {
326333
emit errorOccurred(tr("Hotspot is not connected to the remote device"));
327-
} else if (!m_remoteDevice->checkIfFileExists(clientApplication)) {
328-
emit errorOccurred(tr("Application file cannot be found: %1").arg(clientApplication));
329334
} else {
330-
emit errorOccurred({});
331-
m_clientApplication = clientApplication;
332-
emit clientApplicationChanged(m_clientApplication);
335+
QTimer::singleShot(0, this, [this, clientApplication]() -> QCoro::Task<void> {
336+
bool exists = co_await m_remoteDevice->checkIfFileExists(clientApplication);
337+
if (!exists) {
338+
emit errorOccurred(tr("Application file cannot be found: %1").arg(clientApplication));
339+
} else {
340+
emit errorOccurred({});
341+
m_clientApplication = clientApplication;
342+
emit clientApplicationChanged(m_clientApplication);
343+
}
344+
});
333345
}
334346
}
335347
}

src/remotedevice.cpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <QStandardPaths>
1212
#include <QThread>
1313

14+
#include <QCoroProcess>
15+
1416
#include <KSharedConfig>
1517

1618
#include "settings.h"
@@ -74,6 +76,10 @@ RemoteDevice::~RemoteDevice()
7476
{
7577
if (m_connection) {
7678
disconnect();
79+
QTimer::singleShot(0, this, [connection = m_connection.release()]() -> QCoro::Task<void> {
80+
co_await qCoro(connection).waitForFinished();
81+
delete connection;
82+
});
7783
}
7884
}
7985

@@ -122,34 +128,36 @@ bool RemoteDevice::isConnected() const
122128

123129
bool RemoteDevice::checkIfProgramExists(const QString& program) const
124130
{
131+
Q_ASSERT(QThread::currentThread() != thread());
132+
// this is only used to check if perf is installed and is run in a background thread
125133
auto ssh = sshProcess({QStringLiteral("command"), program});
126134
ssh->start();
127135
ssh->waitForFinished();
128136
auto exitCode = ssh->exitCode();
129-
ssh->deleteLater();
130137
// 128 -> not found
131138
// perf will return 1 and display the help message
132139
return exitCode != 128;
133140
}
134141

135-
bool RemoteDevice::checkIfDirectoryExists(const QString& directory) const
142+
QCoro::Task<bool> RemoteDevice::checkIfDirectoryExists(const QString& directory) const
136143
{
137-
auto ssh = sshProcess({QStringLiteral("test"), QStringLiteral("-d"), directory});
138-
ssh->start();
139-
ssh->waitForFinished();
140-
auto exitCode = ssh->exitCode();
141-
ssh->deleteLater();
142-
return exitCode == 0;
144+
auto _ssh = sshProcess({QStringLiteral("test"), QStringLiteral("-d"), directory});
145+
auto ssh = qCoro(_ssh.get());
146+
co_await ssh.start();
147+
co_await ssh.waitForFinished();
148+
auto exitCode = _ssh->exitCode();
149+
co_return exitCode == 0;
143150
}
144151

145-
bool RemoteDevice::checkIfFileExists(const QString& file) const
152+
QCoro::Task<bool> RemoteDevice::checkIfFileExists(const QString& file) const
146153
{
147-
auto ssh = sshProcess({QStringLiteral("test"), QStringLiteral("-f"), file});
148-
ssh->start();
149-
ssh->waitForFinished();
150-
auto exitCode = ssh->exitCode();
151-
ssh->deleteLater();
152-
return exitCode == 0;
154+
auto _ssh = sshProcess({QStringLiteral("test"), QStringLiteral("-f"), file});
155+
156+
auto ssh = qCoro(_ssh.get());
157+
co_await ssh.start();
158+
co_await ssh.waitForFinished();
159+
auto exitCode = _ssh->exitCode();
160+
co_return exitCode == 0;
153161
}
154162

155163
QByteArray RemoteDevice::getProgramOutput(const QStringList& args) const

src/remotedevice.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <QFileSystemWatcher>
1313
#include <QTemporaryDir>
1414

15+
#include <QCoroTask>
16+
1517
#include <KConfigGroup>
1618

1719
#include <memory>
@@ -31,8 +33,8 @@ class RemoteDevice : public QObject
3133
bool isConnected() const;
3234

3335
bool checkIfProgramExists(const QString& program) const;
34-
bool checkIfDirectoryExists(const QString& directory) const;
35-
bool checkIfFileExists(const QString& file) const;
36+
QCoro::Task<bool> checkIfDirectoryExists(const QString& directory) const;
37+
QCoro::Task<bool> checkIfFileExists(const QString& file) const;
3638
QByteArray getProgramOutput(const QStringList& args) const;
3739

3840
std::unique_ptr<QProcess> runPerf(const QString& cdw, const QStringList& perfOptions) const;

tests/integrationtests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ecm_add_test(
2222
KF${QT_MAJOR_VERSION}::WindowSystem
2323
KF${QT_MAJOR_VERSION}::KIOCore
2424
KF${QT_MAJOR_VERSION}::Parts
25+
QCoro::Core
2526
TEST_NAME
2627
tst_perfparser
2728
)

0 commit comments

Comments
 (0)