File: main.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 (55 lines) | stat: -rw-r--r-- 1,570 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
// SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
//
// SPDX-License-Identifier: MIT

#include "coroutine.h"
#include "qcorodbus.h"
#include "qcorotask.h"

#include "common/dbusserver.h"

#include <QCoreApplication>
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusPendingCall>
#include <QDBusReply>
#include <QDateTime>
#include <QDebug>
#include <QTimer>

#include <chrono>
#include <iostream>
#include <thread>
#include <variant>

using namespace std::chrono_literals;

QCoro::Task<> dbusWorker() {
    auto bus = QDBusConnection::sessionBus();
    auto iface = QDBusInterface{DBusServer::serviceName, DBusServer::objectPath,
                                DBusServer::interfaceName, bus};
    qInfo() << "Sending PING";
    QDBusReply<QString> response = co_await iface.asyncCall(QStringLiteral("blockingPing"), 1);
    if (const auto &err = response.error(); err.isValid()) {
        qWarning() << "DBus call failed:" << err.message();
    }
    qInfo() << "Received response:" << response.value();
}

int main(int argc, char **argv) {
    QCoreApplication app(argc, argv);
    auto process = DBusServer::runStadaloneServer();

    QTimer tickTimer;
    QObject::connect(&tickTimer, &QTimer::timeout, &app, []() {
        std::cout << QDateTime::currentDateTime().toString(Qt::ISODateWithMs).toStdString()
                  << " Tick!" << std::endl;
    });
    tickTimer.start(400ms);

    QTimer dbusTimer;
    QObject::connect(&dbusTimer, &QTimer::timeout, &app, dbusWorker);
    dbusTimer.start(2s);

    return app.exec();
}