aboutsummaryrefslogtreecommitdiffstats
path: root/examples/dbus/pingpong
diff options
context:
space:
mode:
Diffstat (limited to 'examples/dbus/pingpong')
-rw-r--r--examples/dbus/pingpong/ping.py38
-rw-r--r--examples/dbus/pingpong/pingpong.pyproject3
-rw-r--r--examples/dbus/pingpong/pong.py44
3 files changed, 85 insertions, 0 deletions
diff --git a/examples/dbus/pingpong/ping.py b/examples/dbus/pingpong/ping.py
new file mode 100644
index 000000000..d61f25499
--- /dev/null
+++ b/examples/dbus/pingpong/ping.py
@@ -0,0 +1,38 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+"""PySide6 port of the QtDBus pingpong example from Qt v6.x (ping client)"""
+
+import sys
+from PySide6.QtCore import QCoreApplication
+from PySide6.QtDBus import QDBusConnection, QDBusInterface, QDBusReply
+
+
+SERVICE_NAME = 'org.example.QtDBus.PingExample'
+
+
+if __name__ == "__main__":
+ app = QCoreApplication()
+ session_bus = QDBusConnection.sessionBus()
+ if not session_bus.isConnected():
+ print("Cannot connect to the D-Bus session bus.\n"
+ "To start it, run:\n"
+ "\teval `dbus-launch --auto-syntax`\n")
+ sys.exit(-1)
+
+ iface = QDBusInterface(SERVICE_NAME, '/', '', session_bus)
+ if not iface.isValid():
+ print(session_bus.lastError().message())
+ sys.exit(-1)
+
+ argument = sys.argv[1] if len(sys.argv) > 1 else 'Hello'
+ message = iface.call('ping', argument)
+ reply = QDBusReply(message)
+ if not reply.isValid():
+ error = reply.error().message()
+ print(f'ping: Call failed: {error}')
+ sys.exit(-1)
+
+ value = reply.value()
+ print(f'ping: Reply was: {value}')
+ sys.exit(0)
diff --git a/examples/dbus/pingpong/pingpong.pyproject b/examples/dbus/pingpong/pingpong.pyproject
new file mode 100644
index 000000000..cad30b374
--- /dev/null
+++ b/examples/dbus/pingpong/pingpong.pyproject
@@ -0,0 +1,3 @@
+{
+ "files": ["ping.py", "pong.py"]
+}
diff --git a/examples/dbus/pingpong/pong.py b/examples/dbus/pingpong/pong.py
new file mode 100644
index 000000000..0dec6eda0
--- /dev/null
+++ b/examples/dbus/pingpong/pong.py
@@ -0,0 +1,44 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+"""PySide6 port of the QtDBus pingpong example from Qt v6.x (pong server)"""
+
+import sys
+from PySide6.QtCore import QCoreApplication, QObject, Slot
+from PySide6.QtDBus import QDBusConnection
+
+
+SERVICE_NAME = "org.example.QtDBus.PingExample"
+
+
+class Pong(QObject):
+ def __init__(self, parent=None):
+ super().__init__(parent)
+
+ @Slot(str, result=str)
+ def ping(self, arg):
+ print(f'pong: Received ping({arg})')
+ qApp.quit() # noqa: F821
+ return f'ping("{arg}") got called'
+
+
+if __name__ == "__main__":
+ app = QCoreApplication()
+ session_bus = QDBusConnection.sessionBus()
+ if not session_bus.isConnected():
+ print("Cannot connect to the D-Bus session bus.\n"
+ "To start it, run:\n"
+ "\teval `dbus-launch --auto-syntax`\n")
+ sys.exit(-1)
+
+ if not session_bus.registerService(SERVICE_NAME):
+ print(session_bus.lastError().message())
+ sys.exit(-1)
+
+ pong = Pong()
+ session_bus.registerObject('/', pong, QDBusConnection.ExportAllSlots)
+
+ print(f'pong: {SERVICE_NAME} running. Now start ping.py')
+ exit_code = app.exec()
+ print('pong: terminating')
+ sys.exit(exit_code)