Skip to content

Commit 0b26bed

Browse files
authored
Merge pull request #5 from Technolution/feature/fix_timeout_linux
Feature/fix timeout linux
2 parents 247adb8 + 6bad766 commit 0b26bed

File tree

4 files changed

+54
-17
lines changed

4 files changed

+54
-17
lines changed

dbus-java/src/main/java/org/freedesktop/dbus/connections/AbstractConnection.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ public abstract class AbstractConnection implements Closeable {
124124
private ExecutorService senderService;
125125

126126
protected AbstractConnection(String address) throws DBusException {
127+
this(address, AbstractConnection.TIMEOUT);
128+
}
129+
130+
protected AbstractConnection(String address, int timeout) throws DBusException {
127131
exportedObjects = new HashMap<>();
128132
importedObjects = new ConcurrentHashMap<>();
129133

@@ -148,7 +152,7 @@ protected AbstractConnection(String address) throws DBusException {
148152

149153
try {
150154
busAddress = new BusAddress(address);
151-
transport = TransportFactory.createTransport(busAddress, AbstractConnection.TIMEOUT);
155+
transport = TransportFactory.createTransport(busAddress, timeout);
152156
connected = true;
153157
} catch (IOException | DBusException ioe) {
154158
logger.debug("Error creating transport", ioe);

dbus-java/src/main/java/org/freedesktop/dbus/connections/impl/DBusConnection.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ public final class DBusConnection extends AbstractConnection {
9797
* @return {@link DBusConnection}
9898
*/
9999
public static DBusConnection getConnection(String _address) throws DBusException {
100-
return getConnection(_address, true, true);
100+
return getConnection(_address, true, true, AbstractConnection.TIMEOUT);
101101
}
102-
102+
103103
/**
104104
* Connect to the BUS. If a connection already exists to the specified Bus and the shared-flag is true, a reference is returned.
105105
* Will register our own session to DBus if registerSelf is true (default).
@@ -108,10 +108,11 @@ public static DBusConnection getConnection(String _address) throws DBusException
108108
* @param _address The address of the bus to connect to
109109
* @param _registerSelf register own session in dbus
110110
* @param _shared use a shared connections
111+
* @param _timeout the timeout set for the underlying socket. 0 will block forever on the underlying socket.
111112
* @throws DBusException If there is a problem connecting to the Bus.
112113
* @return {@link DBusConnection}
113114
*/
114-
public static DBusConnection getConnection(String _address, boolean _registerSelf, boolean _shared)
115+
public static DBusConnection getConnection(String _address, boolean _registerSelf, boolean _shared, int _timeout)
115116
throws DBusException {
116117

117118
// CONNECTIONS.getOrDefault(address, defaultValue)
@@ -122,27 +123,27 @@ public static DBusConnection getConnection(String _address, boolean _registerSel
122123
c.concurrentConnections.incrementAndGet();
123124
return c;
124125
} else {
125-
c = new DBusConnection(_address, _shared, _registerSelf, getDbusMachineId());
126+
c = new DBusConnection(_address, _shared, _registerSelf, getDbusMachineId(), _timeout);
126127
// do not increment connection counter here, it always starts at 1 on new objects!
127128
// c.getConcurrentConnections().incrementAndGet();
128129
CONNECTIONS.put(_address, c);
129130
return c;
130131
}
131132
}
132133
} else {
133-
return new DBusConnection(_address, _shared, _registerSelf, getDbusMachineId());
134+
return new DBusConnection(_address, _shared, _registerSelf, getDbusMachineId(), _timeout);
134135
}
135136
}
136137

137-
private static DBusConnection getConnection(Supplier<String> _addressGenerator, boolean _registerSelf, boolean _shared) throws DBusException {
138+
private static DBusConnection getConnection(Supplier<String> _addressGenerator, boolean _registerSelf, boolean _shared, int _timeout) throws DBusException {
138139
if (_addressGenerator == null) {
139140
throw new DBusException("Invalid address generator");
140141
}
141142
String address = _addressGenerator.get();
142143
if (address == null) {
143144
throw new DBusException("null is not a valid DBUS address");
144145
}
145-
return getConnection(address, _registerSelf, _shared);
146+
return getConnection(address, _registerSelf, _shared, _timeout);
146147
}
147148

148149
/**
@@ -157,7 +158,7 @@ private static DBusConnection getConnection(Supplier<String> _addressGenerator,
157158
*
158159
*/
159160
public static DBusConnection getConnection(DBusBusType _bustype) throws DBusException {
160-
return getConnection(_bustype, true);
161+
return getConnection(_bustype, true, AbstractConnection.TIMEOUT);
161162
}
162163

163164
/**
@@ -171,8 +172,9 @@ public static DBusConnection getConnection(DBusBusType _bustype) throws DBusExce
171172
*
172173
*/
173174
public static DBusConnection newConnection(DBusBusType _bustype) throws DBusException {
174-
return getConnection(_bustype, false);
175+
return getConnection(_bustype, false, AbstractConnection.TIMEOUT);
175176
}
177+
176178

177179
/**
178180
* Connect to the BUS.
@@ -187,8 +189,7 @@ public static DBusConnection newConnection(DBusBusType _bustype) throws DBusExce
187189
* @throws DBusException If there is a problem connecting to the Bus.
188190
*
189191
*/
190-
public static DBusConnection getConnection(DBusBusType _bustype, boolean _shared) throws DBusException {
191-
192+
public static DBusConnection getConnection(DBusBusType _bustype, boolean _shared, int _timeout) throws DBusException {
192193
switch (_bustype) {
193194
case SYSTEM:
194195
DBusConnection systemConnection = getConnection(() -> {
@@ -197,7 +198,7 @@ public static DBusConnection getConnection(DBusBusType _bustype, boolean _shared
197198
bus = DEFAULT_SYSTEM_BUS_ADDRESS;
198199
}
199200
return bus;
200-
}, true, _shared);
201+
}, true, _shared, _timeout);
201202
return systemConnection;
202203
case SESSION:
203204
DBusConnection sessionConnection = getConnection(() -> {
@@ -244,7 +245,7 @@ public static DBusConnection getConnection(DBusBusType _bustype, boolean _shared
244245

245246
return s;
246247

247-
}, true, _shared);
248+
}, true, _shared, _timeout);
248249

249250
return sessionConnection;
250251
default:
@@ -285,7 +286,7 @@ private static File determineMachineIdFile() throws DBusException {
285286
.orElseThrow(() -> new DBusException("Cannot Resolve Session Bus Address: MachineId file can not be found"));
286287
}
287288

288-
private DBusConnection(String _address, boolean _shared, boolean _registerSelf, String _machineId) throws DBusException {
289+
private DBusConnection(String _address, boolean _shared, boolean _registerSelf, String _machineId, int timeout) throws DBusException {
289290
super(_address);
290291
busnames = new ArrayList<>();
291292
machineId = _machineId;

dbus-java/src/main/java/org/freedesktop/dbus/connections/impl/DirectConnection.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,24 @@
4949
public class DirectConnection extends AbstractConnection {
5050
private final Logger logger = LoggerFactory.getLogger(getClass());
5151
private final String machineId;
52+
53+
/**
54+
* Create a direct connection to another application.
55+
* @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.
56+
* @throws DBusException on error
57+
*/
58+
public DirectConnection(String address) throws DBusException {
59+
this(address, AbstractConnection.TIMEOUT);
60+
}
61+
5262
/**
5363
* Create a direct connection to another application.
5464
* @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.
65+
* @param timeout the timeout set for the underlying socket. 0 will block forever on the underlying socket.
5566
* @throws DBusException on error
5667
*/
57-
public DirectConnection(String address) throws DBusException {
58-
super(address);
68+
public DirectConnection(String address, int timeout) throws DBusException {
69+
super(address, timeout);
5970
machineId = createMachineId();
6071
if (!getAddress().isServer()) {
6172
super.listen();

dbus-java/src/test/java/org/freedesktop/dbus/test/LowLevelTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.freedesktop.dbus.messages.DBusSignal;
1414
import org.freedesktop.dbus.messages.Message;
1515
import org.freedesktop.dbus.messages.MethodCall;
16+
import org.junit.jupiter.api.Assertions;
1617
import org.junit.jupiter.api.Test;
1718
import org.slf4j.Logger;
1819
import org.slf4j.LoggerFactory;
@@ -23,6 +24,26 @@
2324
public class LowLevelTest {
2425
private final Logger logger = LoggerFactory.getLogger(getClass());
2526

27+
@Test
28+
public void testTimeout() throws ParseException, IOException, DBusException {
29+
String addr = getAddress();
30+
logger.debug(addr);
31+
BusAddress address = new BusAddress(addr);
32+
logger.debug(address + "");
33+
int timeout = 100;
34+
try (AbstractTransport conn = TransportFactory.createTransport(address, timeout)) {
35+
long before = System.currentTimeMillis();
36+
conn.readMessage();
37+
long after = System.currentTimeMillis();
38+
long timeOutMeasured = after - before;
39+
Assertions.assertTrue(timeOutMeasured >= timeout, "To early unblocked");
40+
//There is no strict limit on this however the 2 times is a safe limit
41+
Assertions.assertTrue(timeOutMeasured < timeout * 2, "Blocking to long");
42+
System.out.println(timeOutMeasured);
43+
}
44+
}
45+
46+
2647
@Test
2748
public void testLowLevel() throws ParseException, IOException, DBusException {
2849
String addr = getAddress();

0 commit comments

Comments
 (0)