diff --git a/dbus-java/src/main/java/org/freedesktop/dbus/connections/AbstractConnection.java b/dbus-java/src/main/java/org/freedesktop/dbus/connections/AbstractConnection.java index c64578e9..0f0b9fbe 100644 --- a/dbus-java/src/main/java/org/freedesktop/dbus/connections/AbstractConnection.java +++ b/dbus-java/src/main/java/org/freedesktop/dbus/connections/AbstractConnection.java @@ -124,6 +124,10 @@ public abstract class AbstractConnection implements Closeable { private ExecutorService senderService; protected AbstractConnection(String address) throws DBusException { + this(address, AbstractConnection.TIMEOUT); + } + + protected AbstractConnection(String address, int timeout) throws DBusException { exportedObjects = new HashMap<>(); importedObjects = new ConcurrentHashMap<>(); @@ -148,7 +152,7 @@ protected AbstractConnection(String address) throws DBusException { try { busAddress = new BusAddress(address); - transport = TransportFactory.createTransport(busAddress, AbstractConnection.TIMEOUT); + transport = TransportFactory.createTransport(busAddress, timeout); connected = true; } catch (IOException | DBusException ioe) { logger.debug("Error creating transport", ioe); diff --git a/dbus-java/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java b/dbus-java/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java index d28f9a0c..d16eb626 100644 --- a/dbus-java/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java +++ b/dbus-java/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java @@ -97,9 +97,9 @@ public final class DBusConnection extends AbstractConnection { * @return {@link DBusConnection} */ public static DBusConnection getConnection(String _address) throws DBusException { - return getConnection(_address, true, true); + return getConnection(_address, true, true, AbstractConnection.TIMEOUT); } - + /** * Connect to the BUS. If a connection already exists to the specified Bus and the shared-flag is true, a reference is returned. * Will register our own session to DBus if registerSelf is true (default). @@ -108,10 +108,11 @@ public static DBusConnection getConnection(String _address) throws DBusException * @param _address The address of the bus to connect to * @param _registerSelf register own session in dbus * @param _shared use a shared connections + * @param _timeout the timeout set for the underlying socket. 0 will block forever on the underlying socket. * @throws DBusException If there is a problem connecting to the Bus. * @return {@link DBusConnection} */ - public static DBusConnection getConnection(String _address, boolean _registerSelf, boolean _shared) + public static DBusConnection getConnection(String _address, boolean _registerSelf, boolean _shared, int _timeout) throws DBusException { // CONNECTIONS.getOrDefault(address, defaultValue) @@ -122,7 +123,7 @@ public static DBusConnection getConnection(String _address, boolean _registerSel c.concurrentConnections.incrementAndGet(); return c; } else { - c = new DBusConnection(_address, _shared, _registerSelf, getDbusMachineId()); + c = new DBusConnection(_address, _shared, _registerSelf, getDbusMachineId(), _timeout); // do not increment connection counter here, it always starts at 1 on new objects! // c.getConcurrentConnections().incrementAndGet(); CONNECTIONS.put(_address, c); @@ -130,11 +131,11 @@ public static DBusConnection getConnection(String _address, boolean _registerSel } } } else { - return new DBusConnection(_address, _shared, _registerSelf, getDbusMachineId()); + return new DBusConnection(_address, _shared, _registerSelf, getDbusMachineId(), _timeout); } } - private static DBusConnection getConnection(Supplier _addressGenerator, boolean _registerSelf, boolean _shared) throws DBusException { + private static DBusConnection getConnection(Supplier _addressGenerator, boolean _registerSelf, boolean _shared, int _timeout) throws DBusException { if (_addressGenerator == null) { throw new DBusException("Invalid address generator"); } @@ -142,7 +143,7 @@ private static DBusConnection getConnection(Supplier _addressGenerator, if (address == null) { throw new DBusException("null is not a valid DBUS address"); } - return getConnection(address, _registerSelf, _shared); + return getConnection(address, _registerSelf, _shared, _timeout); } /** @@ -157,7 +158,7 @@ private static DBusConnection getConnection(Supplier _addressGenerator, * */ public static DBusConnection getConnection(DBusBusType _bustype) throws DBusException { - return getConnection(_bustype, true); + return getConnection(_bustype, true, AbstractConnection.TIMEOUT); } /** @@ -171,8 +172,9 @@ public static DBusConnection getConnection(DBusBusType _bustype) throws DBusExce * */ public static DBusConnection newConnection(DBusBusType _bustype) throws DBusException { - return getConnection(_bustype, false); + return getConnection(_bustype, false, AbstractConnection.TIMEOUT); } + /** * Connect to the BUS. @@ -187,8 +189,7 @@ public static DBusConnection newConnection(DBusBusType _bustype) throws DBusExce * @throws DBusException If there is a problem connecting to the Bus. * */ - public static DBusConnection getConnection(DBusBusType _bustype, boolean _shared) throws DBusException { - + public static DBusConnection getConnection(DBusBusType _bustype, boolean _shared, int _timeout) throws DBusException { switch (_bustype) { case SYSTEM: DBusConnection systemConnection = getConnection(() -> { @@ -197,7 +198,7 @@ public static DBusConnection getConnection(DBusBusType _bustype, boolean _shared bus = DEFAULT_SYSTEM_BUS_ADDRESS; } return bus; - }, true, _shared); + }, true, _shared, _timeout); return systemConnection; case SESSION: DBusConnection sessionConnection = getConnection(() -> { @@ -244,7 +245,7 @@ public static DBusConnection getConnection(DBusBusType _bustype, boolean _shared return s; - }, true, _shared); + }, true, _shared, _timeout); return sessionConnection; default: @@ -285,7 +286,7 @@ private static File determineMachineIdFile() throws DBusException { .orElseThrow(() -> new DBusException("Cannot Resolve Session Bus Address: MachineId file can not be found")); } - private DBusConnection(String _address, boolean _shared, boolean _registerSelf, String _machineId) throws DBusException { + private DBusConnection(String _address, boolean _shared, boolean _registerSelf, String _machineId, int timeout) throws DBusException { super(_address); busnames = new ArrayList<>(); machineId = _machineId; diff --git a/dbus-java/src/main/java/org/freedesktop/dbus/connections/impl/DirectConnection.java b/dbus-java/src/main/java/org/freedesktop/dbus/connections/impl/DirectConnection.java index 274f53f3..b8d99b45 100644 --- a/dbus-java/src/main/java/org/freedesktop/dbus/connections/impl/DirectConnection.java +++ b/dbus-java/src/main/java/org/freedesktop/dbus/connections/impl/DirectConnection.java @@ -49,13 +49,24 @@ public class DirectConnection extends AbstractConnection { private final Logger logger = LoggerFactory.getLogger(getClass()); private final String machineId; + + /** + * Create a direct connection to another application. + * @param address The address to connect to. This is a standard D-Bus address, except that the additional parameter 'listen=true' should be added in the application which is creating the socket. + * @throws DBusException on error + */ + public DirectConnection(String address) throws DBusException { + this(address, AbstractConnection.TIMEOUT); + } + /** * Create a direct connection to another application. * @param address The address to connect to. This is a standard D-Bus address, except that the additional parameter 'listen=true' should be added in the application which is creating the socket. + * @param timeout the timeout set for the underlying socket. 0 will block forever on the underlying socket. * @throws DBusException on error */ - public DirectConnection(String address) throws DBusException { - super(address); + public DirectConnection(String address, int timeout) throws DBusException { + super(address, timeout); machineId = createMachineId(); if (!getAddress().isServer()) { super.listen(); diff --git a/dbus-java/src/test/java/org/freedesktop/dbus/test/LowLevelTest.java b/dbus-java/src/test/java/org/freedesktop/dbus/test/LowLevelTest.java index 3d631638..6175999c 100644 --- a/dbus-java/src/test/java/org/freedesktop/dbus/test/LowLevelTest.java +++ b/dbus-java/src/test/java/org/freedesktop/dbus/test/LowLevelTest.java @@ -13,6 +13,7 @@ import org.freedesktop.dbus.messages.DBusSignal; import org.freedesktop.dbus.messages.Message; import org.freedesktop.dbus.messages.MethodCall; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,6 +24,26 @@ public class LowLevelTest { private final Logger logger = LoggerFactory.getLogger(getClass()); + @Test + public void testTimeout() throws ParseException, IOException, DBusException { + String addr = getAddress(); + logger.debug(addr); + BusAddress address = new BusAddress(addr); + logger.debug(address + ""); + int timeout = 100; + try (AbstractTransport conn = TransportFactory.createTransport(address, timeout)) { + long before = System.currentTimeMillis(); + conn.readMessage(); + long after = System.currentTimeMillis(); + long timeOutMeasured = after - before; + Assertions.assertTrue(timeOutMeasured >= timeout, "To early unblocked"); + //There is no strict limit on this however the 2 times is a safe limit + Assertions.assertTrue(timeOutMeasured < timeout * 2, "Blocking to long"); + System.out.println(timeOutMeasured); + } + } + + @Test public void testLowLevel() throws ParseException, IOException, DBusException { String addr = getAddress();