feat: finish dbus service: job/jobManager
Log:
This commit is contained in:
@ -24,6 +24,7 @@ target_include_directories(${BIN_NAME} PRIVATE
|
||||
target_link_libraries(${BIN_NAME} PRIVATE
|
||||
Qt${QT_VERSION_MAJOR}::Core
|
||||
Qt${QT_VERSION_MAJOR}::DBus
|
||||
Qt${QT_VERSION_MAJOR}::Concurrent
|
||||
Threads::Threads
|
||||
)
|
||||
|
||||
|
@ -3,24 +3,7 @@
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
#include "jobmanager1service.h"
|
||||
#include <QUuid>
|
||||
#include "global.h"
|
||||
|
||||
JobManager1Service::JobManager1Service() = default;
|
||||
|
||||
JobManager1Service::~JobManager1Service() = default;
|
||||
|
||||
template <typename F, typename... Args>
|
||||
void addJob(const QDBusObjectPath &source, F func, Args &&...args)
|
||||
{
|
||||
// TODO: impl
|
||||
}
|
||||
|
||||
bool JobManager1Service::removeJob(const QDBusObjectPath &job,
|
||||
const QString &status,
|
||||
const QString &message,
|
||||
const QDBusVariant &result)
|
||||
{
|
||||
// TODO: impl
|
||||
return false;
|
||||
}
|
||||
|
@ -9,7 +9,14 @@
|
||||
#include <QMap>
|
||||
#include <QDBusObjectPath>
|
||||
#include <QSharedPointer>
|
||||
#include "jobservice.h"
|
||||
#include <QMutex>
|
||||
#include <QMutexLocker>
|
||||
#include <QtConcurrent>
|
||||
#include <QFuture>
|
||||
#include <QUuid>
|
||||
#include "jobadaptor.h"
|
||||
#include "global.h"
|
||||
|
||||
class JobManager1Service final : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -21,15 +28,52 @@ public:
|
||||
JobManager1Service &operator=(JobManager1Service &&) = delete;
|
||||
|
||||
~JobManager1Service() override;
|
||||
template<typename F,typename ...Args>
|
||||
void addJob(const QDBusObjectPath &source,F func, Args&& ...args);
|
||||
bool removeJob(const QDBusObjectPath &job, const QString &status, const QString &message, const QDBusVariant &result);
|
||||
template <typename F>
|
||||
void addJob(const QDBusObjectPath &source, F func, QVariantList args)
|
||||
{
|
||||
static_assert(std::is_invocable_v<F, QVariant>, "param type must be QVariant.");
|
||||
|
||||
QString objectPath{DDEApplicationManager1JobObjectPath + QUuid::createUuid().toString()};
|
||||
QFuture<QVariantList> future = QtConcurrent::mappedReduced(
|
||||
args.begin(), args.end(), func, &QVariantList::insert, QVariantList{}, QtConcurrent::ReduceOption::OrderedReduce);
|
||||
QSharedPointer<JobService> job{new JobService{future}};
|
||||
auto path = QDBusObjectPath{objectPath};
|
||||
{
|
||||
QMutexLocker locker{&m_mutex};
|
||||
m_jobs.insert(path, job); // Insertion is always successful
|
||||
}
|
||||
emit JobNew(path, source);
|
||||
registerObjectToDbus<decltype(job.data()), JobAdaptor>(job.data(), objectPath, getDBusInterface<JobAdaptor>());
|
||||
auto emitRemove = [this, job, path, future] {
|
||||
decltype(m_jobs)::size_type removeCount{0};
|
||||
{
|
||||
QMutexLocker locker{&m_mutex};
|
||||
removeCount = m_jobs.remove(path);
|
||||
}
|
||||
// removeCount means m_jobs can't find value which corresponding with path
|
||||
// and we shouldn't emit jobRemoved signal because this signal may already has been emit
|
||||
if (removeCount == 0) {
|
||||
qCritical() << "Job already has been removed: " << path.path();
|
||||
return;
|
||||
}
|
||||
QString result{job->status()};
|
||||
for (const auto &val : future.result()) {
|
||||
if (val.canConvert<QDBusError>()) {
|
||||
result = "failed";
|
||||
}
|
||||
break;
|
||||
}
|
||||
emit this->JobRemoved(path, result, future.result());
|
||||
};
|
||||
future.then(QtFuture::Launch::Sync, emitRemove);
|
||||
}
|
||||
|
||||
Q_SIGNALS:
|
||||
void JobNew(const QDBusObjectPath &job, const QDBusObjectPath &source);
|
||||
void JobRemoved(const QDBusObjectPath &job, const QString &status, const QString &message, const QDBusVariant &result);
|
||||
void JobRemoved(const QDBusObjectPath &job, const QString &status, const QVariantList &result);
|
||||
|
||||
private:
|
||||
QMutex m_mutex;
|
||||
QMap<QDBusObjectPath, QSharedPointer<JobService>> m_jobs;
|
||||
};
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
#include "jobservice.h"
|
||||
|
||||
JobService::JobService(const QFuture<QVariant>& job)
|
||||
JobService::JobService(const QFuture<QVariantList>& job)
|
||||
: m_job(job)
|
||||
{
|
||||
}
|
||||
@ -31,7 +31,7 @@ QString JobService::status() const
|
||||
if (m_job.isRunning()) {
|
||||
return "running";
|
||||
}
|
||||
return "failed";
|
||||
Q_UNREACHABLE();
|
||||
}
|
||||
|
||||
void JobService::Cancel()
|
||||
@ -39,7 +39,7 @@ void JobService::Cancel()
|
||||
m_job.cancel();
|
||||
}
|
||||
|
||||
void JobService::Pause()
|
||||
void JobService::Suspend()
|
||||
{
|
||||
m_job.suspend();
|
||||
}
|
||||
|
@ -14,18 +14,23 @@ class JobService : public QObject
|
||||
Q_OBJECT
|
||||
public:
|
||||
~JobService() override;
|
||||
JobService(const JobService &) = delete;
|
||||
JobService(JobService &&) = delete;
|
||||
JobService &operator=(const JobService &) = delete;
|
||||
JobService &operator=(JobService &&) = delete;
|
||||
|
||||
Q_PROPERTY(QString Status READ status)
|
||||
QString status() const;
|
||||
|
||||
public Q_SLOTS:
|
||||
void Cancel();
|
||||
void Pause();
|
||||
void Suspend();
|
||||
void Resume();
|
||||
|
||||
private:
|
||||
explicit JobService(const QFuture<QVariant>& job);
|
||||
QFuture<QVariant> m_job;
|
||||
friend class JobManager1Service;
|
||||
explicit JobService(const QFuture<QVariantList>& job);
|
||||
QFuture<QVariantList> m_job;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -62,7 +62,7 @@ bool registerObjectToDbus(T parent, const QString &path, const QString &interfac
|
||||
"param type must derive QObject");
|
||||
auto &con = ApplicationManager1DBus::instance().CustomBus();
|
||||
if (!con.registerObject(path, interface, new U{parent})) {
|
||||
qWarning() << "register object failed:" << path << interface << con.lastError();
|
||||
qCritical() << "register object failed:" << path << interface << con.lastError();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
Reference in New Issue
Block a user