Hi
I tried to establish a Peer to Peer DBus communication by following the C example included in Gio.
server.py
import gi
from gi.repository import Gio, GLib
xml = (
"<node>"
" <interface name='org.gtk.GDBus.TestPeerInterface'>"
" <method name='HelloWorld'>"
" <arg type='s' name='greeting' direction='in'/>"
" <arg type='s' name='response' direction='out'/>"
" </method>"
" </interface>"
"</node>"
)
node = Gio.DBusNodeInfo.new_for_xml(xml)
loop = GLib.MainLoop()
def allow_mechanism(observer, mechanism):
return True
def authorize(observer, stream, client_credentials):
server_credentials = Gio.Credentials()
print("Client\n", client_credentials.to_string())
print("Server\n", server_credentials.to_string())
if server_credentials.is_same_user(client_credentials):
print("Connected")
return True
return True
def connection_closed(connection, vanished):
print("Connection closed ", vanished)
def handle_method_call(
connection, sender, object_path, interface_name, method_name, params, invocation
):
if method_name == "HelloWorld":
print("Got a 'HelloWorld'")
msg = params.unpack()[0]
greeting = GLib.Variant(
"(s)", (f"Got message : {msg}",))
invocation.return_value(greeting)
def new_connection(server, connection):
credentials = connection.get_peer_credentials()
caps = connection.get_capabilities()
print("Credentials\n", credentials.to_string())
print("Capabilities\n", int(caps))
connection.connect("closed", connection_closed)
reg_id = connection.register_object(
"/org/gtk/GDBus/TestObject", node.interfaces[0], handle_method_call, None, None
)
assert reg_id > 0
return True
address = "unix:abstract=myaddr"
flags = Gio.DBusServerFlags.NONE
guid = Gio.dbus_generate_guid()
observer = Gio.DBusAuthObserver()
observer.connect("allow-mechanism", allow_mechanism)
observer.connect("authorize-authenticated-peer", authorize)
server = Gio.DBusServer.new_sync(address, flags, guid, observer)
server.start()
print("Client addresss ", server.get_client_address())
server.connect("new-connection", new_connection)
try:
loop.run()
except KeyboardInterrupt:
loop.quit()
client.py
from gi.repository import Gio, GLib
connection = Gio.DBusConnection.new_for_address_sync("unix:abstract=myaddr",
Gio.DBusConnectionFlags.AUTHENTICATION_CLIENT,
None,
None)
print("Connected")
print(int(connection.get_capabilities()))
var = GLib.Variant("(s)", ("Hi there",))
val = connection.call_sync(
None,
"/org/gtk/GDBus/TestObject",
"org.gtk.GDBus.TestPeerInterface",
"HelloWorld",
var,
GLib.VariantType.new("(s)"),
Gio.DBusCallFlags.NONE,
500,
None
)
print(val)
After launching server, whenever I execute client.py
, I get connection is closed
error. Why does it happen? Who is closing the connection?
Please help me.