@@ -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 ;
0 commit comments