From 2570a2fb394f0f52b978303828135efa9b37cf1c Mon Sep 17 00:00:00 2001 From: David M Date: Thu, 18 May 2023 15:19:12 +0200 Subject: [PATCH] Issue #212: Added missing connect() method to allow connecting transports manually --- README.md | 2 ++ .../dbus/connections/AbstractConnection.java | 20 +++++++++++++++++++ .../transports/AbstractTransport.java | 12 ++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e0ab5b00..737cbe1f 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,8 @@ The library will remain open source and MIT licensed and can still be used, fork ##### Changes in 4.3.1 (not released yet): - 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)) + - Added missing `connect()` method to `AbstractTransport` to allow connecting the underlying transport manually, thanks to [brett-smith](https://github.com/brett-smith) ([#212](https://github.com/hypfvieh/dbus-java/issues/212) + ##### Changes in 4.3.0 (2023-03-10): diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/AbstractConnection.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/AbstractConnection.java index fa080bb2..347c3ab9 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/AbstractConnection.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/AbstractConnection.java @@ -1192,6 +1192,26 @@ public boolean isConnected() { return transport != null && transport.isConnected(); } + /** + * Connects the underlying transport if it is not already connected. + *

+ * Will work for both, client and server (listening) connections. + *

+ * + * @return true if connection established or already connected, false otherwise + * @throws IOException when connection was not already established and creating the connnection failed + */ + public boolean connect() throws IOException { + if (!transport.isConnected()) { + if (transport.isListening()) { + return transport.listen() != null; + } else { + return transport.connect() != null; + } + } + return false; + } + protected Queue getPendingErrorQueue() { return pendingErrorQueue; } diff --git a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/transports/AbstractTransport.java b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/transports/AbstractTransport.java index 6053c305..b718c454 100644 --- a/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/transports/AbstractTransport.java +++ b/dbus-java-core/src/main/java/org/freedesktop/dbus/connections/transports/AbstractTransport.java @@ -134,6 +134,15 @@ public final SocketChannel connect() throws IOException { return transportConnection.getChannel(); } + /** + * True if this transport connection is a listening (server) connection. + * + * @return boolean + */ + public final boolean isListening() { + return getAddress().isListeningSocket(); + } + /** * Start listening on created transport.
*

@@ -145,7 +154,8 @@ public final SocketChannel connect() throws IOException { * Therefore this method should be called in a loop to accept multiple clients *

* - * @return {@link TransportConnection} containing created {@link SocketChannel} and {@link IMessageReader}/{@link IMessageWriter} + * @return {@link TransportConnection} containing created {@link SocketChannel} and + * {@link IMessageReader}/{@link IMessageWriter} * @throws IOException if connection fails */ public final TransportConnection listen() throws IOException {