From 284de305143f7ddc7ea7f11fec7c6c4ba584d651 Mon Sep 17 00:00:00 2001 From: David M Date: Wed, 11 Oct 2023 07:57:28 +0200 Subject: [PATCH] Issue #238: Fixed issues with autoConnect option; Added methed 'register' to DBusConnection to allow registering session manually when it was created with 'registerSelf' = 'false' --- README.md | 1 + .../connections/config/TransportConfig.java | 9 +++++ .../config/TransportConfigBuilder.java | 13 ++++++ .../dbus/connections/impl/DBusConnection.java | 40 ++++++++++++++----- .../impl/DBusConnectionBuilder.java | 13 +----- .../examples/pulseaudio/PulseAudioDbus.java | 4 +- 6 files changed, 57 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index f7b470cb..77cf53f2 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,7 @@ The library will remain open source and MIT licensed and can still be used, fork - Updated minimum required Java version to 17 - Improved handling of listening connections to allow proper bootstrapping the connection before actually starting accepting new connections (thanks to [brett-smith](https://github.com/brett-smith) ([#213](https://github.com/hypfvieh/dbus-java/issues/213))) - Updated export-object documentation ([#236](https://github.com/hypfvieh/dbus-java/issues/236)) + - Fixed issues with autoConnect option, added method to register to bus by 'Hello' message manually, thanks to [brett-smith](https://github.com/brett-smith) ([#238](https://github.com/hypfvieh/dbus-java/issues/238)) ##### Changes in 4.3.1 (2023-10-03): - Provide classloader to ServiceLoader in TransportBuilder (for loading actual transports) and AbstractTransport (for loading IMessageReader/Writer implementations), thanks to [cthbleachbit](https://github.com/cthbleachbit) ([#210](https://github.com/hypfvieh/dbus-java/issues/210), [PR#211](https://github.com/hypfvieh/dbus-java/issues/211)) diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/config/TransportConfig.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/config/TransportConfig.java index 60762831..ed03cc80 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/config/TransportConfig.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/config/TransportConfig.java @@ -32,6 +32,7 @@ public final class TransportConfig { private String fileGroup; private byte endianess = BaseConnectionBuilder.getSystemEndianness(); + private boolean registerSelf = true; /** * Unix file permissions to set on socket file if this is a server transport (ignored on Windows, does nothing if @@ -151,6 +152,14 @@ public void setEndianess(byte _endianess) { endianess = _endianess; } + public boolean isRegisterSelf() { + return registerSelf; + } + + public void setRegisterSelf(boolean _registerSelf) { + registerSelf = _registerSelf; + } + /** * Toggles the busaddress to be a listening (server) or non listening (client) connection. * @param _listening true to be a server connection diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/config/TransportConfigBuilder.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/config/TransportConfigBuilder.java index c202867b..974215d5 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/config/TransportConfigBuilder.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/config/TransportConfigBuilder.java @@ -102,6 +102,19 @@ public X withAutoConnect(boolean _connect) { return self(); } + /** + * Register the new connection on DBus using 'hello' message. Default is true. + * + * @param _register boolean + * @return this + * + * @since 5.0.0 - 2023-10-11 + */ + public X withRegisterSelf(boolean _register) { + config.setRegisterSelf(_register); + return self(); + } + /** * Switch to the {@link SaslConfigBuilder} to configure the SASL authentication mechanism.
* Use {@link SaslConfigBuilder#back()} to return to this builder when finished. diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java index c39c8817..f297ec29 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java @@ -47,6 +47,9 @@ public final class DBusConnection extends AbstractConnection { private final String machineId; private DBus dbus; + /** Whether the connection was registered using 'Hello' message. */ + private boolean registered; + /** Count how many 'connections' we manage internally. * This is required because a {@link DBusConnection} to the same address will always return the same object and * the 'real' disconnection should only occur when there is no second/third/whatever connection is left. */ @@ -73,10 +76,9 @@ private AtomicInteger getConcurrentConnections() { /** * Connect to bus and register if asked. Should only be called by Builder. * - * @param _registerSelf true to register * @throws DBusException if registering or connection fails */ - void connect(boolean _registerSelf) throws DBusException { + void connectImpl() throws DBusException { // start listening for calls try { listen(); @@ -89,14 +91,32 @@ void connect(boolean _registerSelf) throws DBusException { addSigHandlerWithoutMatch(DBus.NameAcquired.class, h); // register ourselves if not disabled - if (_registerSelf) { - dbus = getRemoteObject("org.freedesktop.DBus", "/org/freedesktop/DBus", DBus.class); - try { - busnames.add(dbus.Hello()); - } catch (DBusExecutionException _ex) { - logger.debug("Error while doing 'Hello' handshake", _ex); - throw new DBusException(_ex.getMessage()); - } + if (getTransportConfig().isRegisterSelf() && getTransport().isConnected()) { + register(); + } + } + + /** + * Register this connection on the bus using 'Hello' message.
+ * Will do nothing if session was already registered. + * + * @throws DBusException when sending message fails + * + * @since 5.0.0 - 2023-10-11 + */ + public void register() throws DBusException { + if (registered) { + return; + } + + dbus = getRemoteObject("org.freedesktop.DBus", "/org/freedesktop/DBus", DBus.class); + + try { + busnames.add(dbus.Hello()); + registered = true; + } catch (DBusExecutionException _ex) { + logger.debug("Error while doing 'Hello' handshake", _ex); + throw new DBusException(_ex.getMessage(), _ex); } } diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnectionBuilder.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnectionBuilder.java index 72905e56..3d30e946 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnectionBuilder.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnectionBuilder.java @@ -20,7 +20,6 @@ public final class DBusConnectionBuilder extends BaseConnectionBuilder { private final String machineId; - private boolean registerSelf = true; private boolean shared = true; private DBusConnectionBuilder(BusAddress _address, String _machineId) { @@ -146,16 +145,6 @@ private static BusAddress validateTransportAddress(BusAddress _address) { return address; } - /** - * Register the new connection on DBus using 'hello' message. Default is true. - * - * @param _register boolean - * @return this - */ - public DBusConnectionBuilder withRegisterSelf(boolean _register) { - registerSelf = _register; - return this; - } /** * Use this connection as shared connection. Shared connection means that the same connection is used multiple times @@ -199,7 +188,7 @@ public DBusConnection build() throws DBusException { c.setDisconnectCallback(getDisconnectCallback()); c.setWeakReferences(isWeakReference()); - c.connect(registerSelf); + c.connectImpl(); return c; } diff --git a/dbus-java-examples/src/main/java/com/github/hypfvieh/dbus/examples/pulseaudio/PulseAudioDbus.java b/dbus-java-examples/src/main/java/com/github/hypfvieh/dbus/examples/pulseaudio/PulseAudioDbus.java index fb401a78..fbaadba3 100755 --- a/dbus-java-examples/src/main/java/com/github/hypfvieh/dbus/examples/pulseaudio/PulseAudioDbus.java +++ b/dbus-java-examples/src/main/java/com/github/hypfvieh/dbus/examples/pulseaudio/PulseAudioDbus.java @@ -35,8 +35,10 @@ public static void main(String[] _args) throws DBusException { } else { DBusConnection connection = DBusConnectionBuilder .forAddress(address) - .withRegisterSelf(false) .withShared(false) + .transportConfig() + .withRegisterSelf(false) + .back() .build(); Properties core1Props = connection.getRemoteObject("org.PulseAudio.Core1", "/org/pulseaudio/core1", Properties.class);