File: dbusserver.cpp

package info (click to toggle)
qcoro 0.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,700 kB
  • sloc: cpp: 8,573; python: 32; xml: 26; makefile: 23; sh: 15
file content (53 lines) | stat: -rw-r--r-- 1,589 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
//
// SPDX-License-Identifier: MIT

#include "dbusserver.h"

#include <QCoreApplication>
#include <QDBusConnection>
#include <QDebug>
#include <QThread>

#include <thread>

const QString DBusServer::serviceName = QStringLiteral("org.kde.qoro.dbustest");
const QString DBusServer::objectPath = QStringLiteral("/");
const QString DBusServer::interfaceName = QStringLiteral("org.kde.qoro.dbuserver");

DBusServer::DBusServer() {
    qInfo() << "DBusServer started";
    auto bus = QDBusConnection::sessionBus();
    bus.registerService(serviceName);
    bus.registerObject(objectPath, interfaceName, this, QDBusConnection::ExportAllSlots);
}

QString DBusServer::blockingPing(int seconds) const {
    qInfo() << "S: Received ping request...";
    std::this_thread::sleep_for(std::chrono::seconds{seconds});
    qInfo() << "S: sending PONG response";
    return QStringLiteral("PONG!");
}

std::unique_ptr<QProcess> DBusServer::runStadaloneServer() {
#ifdef SERVER_EXEC_PATH
    auto process = std::make_unique<QProcess>();
    process->setProcessChannelMode(QProcess::ForwardedChannels);
    process->start(QStringLiteral(SERVER_EXEC_PATH), {}, QIODevice::ReadOnly);
    process->waitForStarted();
    if (process->state() != QProcess::Running) {
        qCritical() << "Failed to start server process:" << process->error();
    }
    return process;
#else
    return {};
#endif
}

#ifdef STANDALONE
int main(int argc, char **argv) {
    QCoreApplication app(argc, argv);
    DBusServer server;
    return app.exec();
}
#endif