Class DBusServer
GDBusServer is a helper for listening to and accepting D-Bus
connections. This can be used to create a new D-Bus server, allowing two
peers to use the D-Bus protocol for their own specialized communication.
A server instance provided in this way will not perform message routing or
implement the
org.freedesktop.DBus interface.
To just export an object on a well-known name on a message bus, such as the
session or system bus, you should instead use Gio.busOwnName(org.gnome.gio.BusType, java.lang.String, java.util.Set<org.gnome.gio.BusNameOwnerFlags>, org.gnome.gobject.Closure, org.gnome.gobject.Closure, org.gnome.gobject.Closure).
An example of peer-to-peer communication with GDBus can be found in gdbus-example-peer.c.
Note that a minimal GDBusServer will accept connections from any
peer. In many use-cases it will be necessary to add a
DBusAuthObserver that only accepts connections that have
successfully authenticated as the same user that is running the
GDBusServer. Since GLib 2.68 this can be achieved more simply by passing
the G_DBUS_SERVER_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER flag to the
server.
- Since:
- 2.26
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDBusServer.Builder<B extends DBusServer.Builder<B>>Inner class implementing a builder pattern to construct a GObject with properties.static interfaceFunctional interface declaration of theNewConnectionCallbackcallback.Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClassNested classes/interfaces inherited from interface org.gnome.gio.Initable
Initable.Initable$Impl, Initable.InitableIface -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new DBusServer.DBusServer(MemorySegment address) Create a DBusServer proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected DBusServerasParent()Returns this instance as if it were its parent type.static DBusServer.Builder<? extends DBusServer.Builder> builder()ADBusServer.Builderobject constructs aDBusServerwith the specified properties.booleanemitNewConnection(@Nullable DBusConnection connection) Emits the "new-connection" signal.Gets a D-Bus address string that can be used by clients to connect toserver.getFlags()Gets the flags forserver.getGuid()Gets the GUID forserver,as provided to g_dbus_server_new_sync().static @Nullable TypegetType()Get the GType of the DBusServer classbooleanisActive()Gets whether this DBusServer is active.Emitted when a new authenticated connection has been made.voidstart()Startsserver.voidstop()Stopsserver.static DBusServersync(String address, Set<DBusServerFlags> flags, String guid, @Nullable DBusAuthObserver observer, @Nullable Cancellable cancellable) Creates a new D-Bus server that listens on the first address inaddressthat works.static DBusServersync(String address, DBusServerFlags flags, String guid, @Nullable DBusAuthObserver observer, @Nullable Cancellable cancellable) Creates a new D-Bus server that listens on the first address inaddressthat works.Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getMemoryLayout, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newv, notify, notify, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withPropertiesMethods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, cast, getPrivate, readGClass, writeGClassMethods inherited from class org.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
DBusServer
Create a DBusServer proxy instance for the provided memory address.- Parameters:
address- the memory address of the native object
-
DBusServer
public DBusServer()Creates a new DBusServer.
-
-
Method Details
-
getType
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuperkeyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName(). This will call the native function pointer of this virtual method in the typeclass of the parent type. -
sync
public static DBusServer sync(String address, Set<DBusServerFlags> flags, String guid, @Nullable DBusAuthObserver observer, @Nullable Cancellable cancellable) throws GErrorException Creates a new D-Bus server that listens on the first address inaddressthat works.Once constructed, you can use g_dbus_server_get_client_address() to get a D-Bus address string that clients can use to connect.
To have control over the available authentication mechanisms and the users that are authorized to connect, it is strongly recommended to provide a non-
nullGDBusAuthObserver.Connect to the
GDBusServer::new-connection signal to handle incoming connections.The returned
GDBusServerisn't active - you have to start it with g_dbus_server_start().GDBusServeris used in this example.This is a synchronous failable constructor. There is currently no asynchronous version.
- Parameters:
address- A D-Bus address.flags- Flags from theGDBusServerFlagsenumeration.guid- A D-Bus GUID.observer- AGDBusAuthObserverornull.cancellable- AGCancellableornull.- Returns:
- A
GDBusServerornulliferroris set. Free with g_object_unref(). - Throws:
GErrorException- seeGError- Since:
- 2.26
-
sync
public static DBusServer sync(String address, DBusServerFlags flags, String guid, @Nullable DBusAuthObserver observer, @Nullable Cancellable cancellable) throws GErrorException Creates a new D-Bus server that listens on the first address inaddressthat works.Once constructed, you can use g_dbus_server_get_client_address() to get a D-Bus address string that clients can use to connect.
To have control over the available authentication mechanisms and the users that are authorized to connect, it is strongly recommended to provide a non-
nullGDBusAuthObserver.Connect to the
GDBusServer::new-connection signal to handle incoming connections.The returned
GDBusServerisn't active - you have to start it with g_dbus_server_start().GDBusServeris used in this example.This is a synchronous failable constructor. There is currently no asynchronous version.
- Parameters:
address- A D-Bus address.flags- Flags from theGDBusServerFlagsenumeration.guid- A D-Bus GUID.observer- AGDBusAuthObserverornull.cancellable- AGCancellableornull.- Returns:
- A
GDBusServerornulliferroris set. Free with g_object_unref(). - Throws:
GErrorException- seeGError- Since:
- 2.26
-
getClientAddress
Gets a D-Bus address string that can be used by clients to connect toserver.This is valid and non-empty if initializing the
GDBusServersucceeded.- Returns:
- A D-Bus address string. Do not free, the string is owned
by
server. - Since:
- 2.26
-
getFlags
Gets the flags forserver.- Returns:
- A set of flags from the
GDBusServerFlagsenumeration. - Since:
- 2.26
-
getGuid
Gets the GUID forserver,as provided to g_dbus_server_new_sync().- Returns:
- A D-Bus GUID. Do not free this string, it is owned by
server. - Since:
- 2.26
-
isActive
public boolean isActive()Gets whether this DBusServer is active.- Returns:
trueif server is active,falseotherwise.- Since:
- 2.26
-
start
public void start()Startsserver.- Since:
- 2.26
-
stop
public void stop()Stopsserver.- Since:
- 2.26
-
onNewConnection
public SignalConnection<DBusServer.NewConnectionCallback> onNewConnection(DBusServer.NewConnectionCallback handler) Emitted when a new authenticated connection has been made. Use g_dbus_connection_get_peer_credentials() to figure out what identity (if any), was authenticated.If you want to accept the connection, take a reference to the
connectionobject and returntrue. When you are done with the connection call g_dbus_connection_close() and give up your reference. Note that the other peer may disconnect at any time - a typical thing to do when accepting a connection is to listen to theGDBusConnection::closed signal.If
GDBusServer:flags containsDBusServerFlags.RUN_IN_THREADthen the signal is emitted in a new thread dedicated to the connection. Otherwise the signal is emitted in the thread-default main context (seeMainContext.pushThreadDefault()) of the thread thatserverwas constructed in.You are guaranteed that signal handlers for this signal runs before incoming messages on
connectionare processed. This means that it's suitable to call g_dbus_connection_register_object() or similar from the signal handler.- Parameters:
handler- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- Since:
- 2.26
- See Also:
-
emitNewConnection
Emits the "new-connection" signal. SeeonNewConnection(org.gnome.gio.DBusServer.NewConnectionCallback). -
builder
ADBusServer.Builderobject constructs aDBusServerwith the specified properties. Use the variousset...()methods to set properties, and finish construction withDBusServer.Builder.build().- Returns:
- the builder object
-