summaryrefslogtreecommitdiff
path: root/tests/test-dbus-pong/test-dbus-pong.cpp
blob: 1eeb1e7f014e3b52755379dbec003e3d2f9d81dd (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*************************************************************************
This file is part of libresourceqt

Copyright (C) 2010 Nokia Corporation.

This library is free software; you can redistribute
it and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation
version 2.1 of the License.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA.
*************************************************************************/



#include <stdio.h>
#include <stdlib.h>

#include <QtCore/QCoreApplication>
#include <QtCore/QTimer>
#include <QtDBus/QtDBus>

#include "pong.h"

QString Pong::ping(const QString &arg)
{
    // Just return back
    return QString("%1").arg(arg);
}

unsigned int Pong::quit()
{
    // Quit application
    QMetaObject::invokeMethod(QCoreApplication::instance(), "quit");
    // Magic number
    return 12345;
}

unsigned int Pong::timeout()
{
    // Timeout in testing application should be set to less than 2 seconds!
    sleep(2);
    // Just to suppress warning
    return 54321;
}

int main(int argc, char **argv)
{
    bool useSessionBus = false;

    QCoreApplication app(argc, argv);
    if (app.arguments().count() > 1) {
        if (app.arguments().at(1) == "--session") {
            useSessionBus = true;
        }
    }

    QDBusConnection* myBus;
    if (useSessionBus) {
        myBus = new QDBusConnection(QDBusConnection::sessionBus().sessionBus());
        qDebug("Using session bus ...");
    }
    else {
        myBus = new QDBusConnection(QDBusConnection::systemBus().systemBus());
        qDebug("Using system bus ...");
    }

    // Check system bus connection
    if (!myBus->isConnected()) {
        // Cleanup!!
        delete myBus;
        myBus = NULL;

        if (useSessionBus) {
            qDebug("Cannot connect to the D-Bus session bus.");
        }
        else {
            qDebug("Cannot connect to the D-Bus system bus.");
        }

        return 1;
    }

    // Create listener service
    if (!myBus->registerService("com.nokia.dbusqeventloop.test")) {
        qDebug("%s", qPrintable(QDBusConnection::systemBus().lastError().message()));
        exit(2);
    }

    Pong pong;
    // Register all slots as dbus methods
    myBus->registerObject("/", &pong, QDBusConnection::ExportAllSlots);

    // Let's go!
    app.exec();

    // Cleanup!!
    delete myBus;
    myBus = NULL;

    return 0;
}