Skip to content

Commit

Permalink
Issue #238: Fixed issues with autoConnect option; Added methed 'regis…
Browse files Browse the repository at this point in the history
…ter' to DBusConnection to allow registering session manually when it was created with 'registerSelf' = 'false'
  • Loading branch information
hypfvieh committed Oct 11, 2023
1 parent fd27028 commit 284de30
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br>
* Use {@link SaslConfigBuilder#back()} to return to this builder when finished.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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();
Expand All @@ -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.<br>
* 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
public final class DBusConnectionBuilder extends BaseConnectionBuilder<DBusConnectionBuilder, DBusConnection> {

private final String machineId;
private boolean registerSelf = true;
private boolean shared = true;

private DBusConnectionBuilder(BusAddress _address, String _machineId) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -199,7 +188,7 @@ public DBusConnection build() throws DBusException {

c.setDisconnectCallback(getDisconnectCallback());
c.setWeakReferences(isWeakReference());
c.connect(registerSelf);
c.connectImpl();
return c;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 284de30

Please sign in to comment.