Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<>();

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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)
Expand All @@ -122,27 +123,27 @@ 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);
return c;
}
}
} else {
return new DBusConnection(_address, _shared, _registerSelf, getDbusMachineId());
return new DBusConnection(_address, _shared, _registerSelf, getDbusMachineId(), _timeout);
}
}

private static DBusConnection getConnection(Supplier<String> _addressGenerator, boolean _registerSelf, boolean _shared) throws DBusException {
private static DBusConnection getConnection(Supplier<String> _addressGenerator, boolean _registerSelf, boolean _shared, int _timeout) throws DBusException {
if (_addressGenerator == null) {
throw new DBusException("Invalid address generator");
}
String address = _addressGenerator.get();
if (address == null) {
throw new DBusException("null is not a valid DBUS address");
}
return getConnection(address, _registerSelf, _shared);
return getConnection(address, _registerSelf, _shared, _timeout);
}

/**
Expand All @@ -157,7 +158,7 @@ private static DBusConnection getConnection(Supplier<String> _addressGenerator,
*
*/
public static DBusConnection getConnection(DBusBusType _bustype) throws DBusException {
return getConnection(_bustype, true);
return getConnection(_bustype, true, AbstractConnection.TIMEOUT);
}

/**
Expand All @@ -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.
Expand All @@ -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(() -> {
Expand All @@ -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(() -> {
Expand Down Expand Up @@ -244,7 +245,7 @@ public static DBusConnection getConnection(DBusBusType _bustype, boolean _shared

return s;

}, true, _shared);
}, true, _shared, _timeout);

return sessionConnection;
default:
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down