File: testdbusserver.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 (81 lines) | stat: -rw-r--r-- 2,141 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
//
// SPDX-License-Identifier: MIT

#include "testdbusserver.h"

#include <QCoreApplication>
#include <QDBusConnection>
#include <QDBusError>
#include <QDebug>
#include <QTimer>

#include <thread>

#include <chrono>

using namespace std::chrono_literals;

DBusServer::DBusServer() {
    QTimer::singleShot(0, this, &DBusServer::run);
    // Self-terminate if there's no interaction from a client in 30 seconds.
    // Prevents leaking the test server in case the test crashes.
    mSuicideTimer.setInterval(30s);
    mSuicideTimer.setSingleShot(true);
    connect(&mSuicideTimer, &QTimer::timeout, this, []() {
        std::cerr << "No call in 30 seconds, terminating!" << std::endl;
        qApp->exit(1);
    });
}

void DBusServer::run() {
    auto conn = QDBusConnection::sessionBus();
    if (!conn.registerService(serviceName)) {
        qWarning() << "Failed to register service to DBus:" << conn.lastError().message();
    }
    if (!conn.registerObject(objectPath, interfaceName, this, QDBusConnection::ExportAllSlots)) {
        qWarning() << "Failed to register object to DBus" << conn.lastError().message();
    }

    mSuicideTimer.start();
}

void DBusServer::foo() {
    mSuicideTimer.start();
}

void DBusServer::blockFor(int seconds) {
    std::this_thread::sleep_for(std::chrono::seconds(seconds));
    mSuicideTimer.start();
}

QString DBusServer::blockAndReturn(int seconds) {
    std::this_thread::sleep_for(std::chrono::seconds(seconds));
    mSuicideTimer.start();
    return QStringLiteral("Slept for %1 seconds").arg(seconds);
}

QString DBusServer::blockAndReturnMultipleArguments(int seconds, bool &out) {
    std::this_thread::sleep_for(std::chrono::seconds{seconds});
    mSuicideTimer.start();
    out = true;
    return QStringLiteral("Hello World!");
}

QString DBusServer::ping(const QString &ping) {
    mSuicideTimer.start();
    return ping;
}

void DBusServer::quit() {
    mSuicideTimer.stop();
    qApp->quit();
}

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