Class DBusServer

All Implemented Interfaces:
Initable, Proxy

@Generated("org.javagi.JavaGI") public class DBusServer extends GObject implements Initable

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(BusType, String, Set, Closure, Closure, 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
  • Constructor Details

    • DBusServer

      public DBusServer(MemorySegment address)
      Create a DBusServer instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • DBusServer

      public DBusServer()
      Create a new DBusServer.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the DBusServer class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected DBusServer asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class GObject
      Returns:
      the instance as if it were its 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 in address that 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-null GDBusAuthObserver.

      Connect to the GDBusServer::new-connection signal to handle incoming connections.

      The returned GDBusServer isn't active - you have to start it with g_dbus_server_start().

      GDBusServer is 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 the GDBusServerFlags enumeration.
      guid - A D-Bus GUID.
      observer - A GDBusAuthObserver or null.
      cancellable - A GCancellable or null.
      Returns:
      A GDBusServer or null if error is set. Free with g_object_unref().
      Throws:
      GErrorException - see GError
      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 in address that 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-null GDBusAuthObserver.

      Connect to the GDBusServer::new-connection signal to handle incoming connections.

      The returned GDBusServer isn't active - you have to start it with g_dbus_server_start().

      GDBusServer is 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 the GDBusServerFlags enumeration.
      guid - A D-Bus GUID.
      observer - A GDBusAuthObserver or null.
      cancellable - A GCancellable or null.
      Returns:
      A GDBusServer or null if error is set. Free with g_object_unref().
      Throws:
      GErrorException - see GError
      Since:
      2.26
    • getClientAddress

      public String getClientAddress()

      Gets a D-Bus address string that can be used by clients to connect to server.

      This is valid and non-empty if initializing the GDBusServer succeeded.

      Returns:
      A D-Bus address string. Do not free, the string is owned by server.
      Since:
      2.26
    • getFlags

      public Set<DBusServerFlags> getFlags()
      Gets the flags for server.
      Returns:
      A set of flags from the GDBusServerFlags enumeration.
      Since:
      2.26
    • getGuid

      public String getGuid()
      Gets the GUID for server, 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:
      true if server is active, false otherwise.
      Since:
      2.26
    • start

      public void start()
      Starts server.
      Since:
      2.26
    • stop

      public void stop()
      Stops server.
      Since:
      2.26
    • onNewConnection

      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 connection object and return true. 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 the GDBusConnection::closed signal.

      If GDBusServer:flags contains DBusServerFlags.RUN_IN_THREAD then the signal is emitted in a new thread dedicated to the connection. Otherwise the signal is emitted in the thread-default main context (see MainContext#pushThreadDefault) of the thread that server was constructed in.

      You are guaranteed that signal handlers for this signal runs before incoming messages on connection are 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

      public boolean emitNewConnection(@Nullable DBusConnection connection)
      Emits the "new-connection" signal. See onNewConnection(DBusServer.NewConnectionCallback).
    • builder

      public static DBusServer.Builder<? extends DBusServer.Builder> builder()
      A DBusServer.Builder object constructs a DBusServer with the specified properties. Use the various set...() methods to set properties, and finish construction with DBusServer.Builder.build().
      Returns:
      the builder object