summaryrefslogtreecommitdiff
path: root/tests/test-dbus-pong/test-dbus-pong.cpp
blob: c936cc44a754d5f8d3959ff734dbab81758a708a (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
#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::systemBus().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;
}