Impose a fixed limit on the number of Bluetooth connections.

This commit is contained in:
akwizgran
2020-05-05 17:07:05 +01:00
parent 708452713d
commit d905451f48
2 changed files with 14 additions and 2 deletions

View File

@@ -20,6 +20,7 @@ interface BluetoothConnectionLimiter {
* Returns true if a contact connection can be opened. This method does not
* need to be called for key agreement connections.
*/
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
boolean canOpenContactConnection();
/**

View File

@@ -9,10 +9,12 @@ import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.util.LogUtils.logException;
@NotNullByDefault
@@ -20,13 +22,16 @@ import static org.briarproject.bramble.util.LogUtils.logException;
class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
private static final Logger LOG =
Logger.getLogger(BluetoothConnectionLimiterImpl.class.getName());
getLogger(BluetoothConnectionLimiterImpl.class.getName());
private final Object lock = new Object();
// The following are locking: lock
@GuardedBy("lock")
private final LinkedList<DuplexTransportConnection> connections =
new LinkedList<>();
@GuardedBy("lock")
private boolean keyAgreementInProgress = false;
@GuardedBy("lock")
private int connectionLimit = 1;
@Override
public void keyAgreementStarted() {
@@ -57,6 +62,9 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
if (keyAgreementInProgress) {
LOG.info("Can't open contact connection during key agreement");
return false;
} else if (connections.size() >= connectionLimit) {
LOG.info("Can't open contact connection due to limit");
return false;
} else {
LOG.info("Can open contact connection");
return true;
@@ -71,6 +79,9 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
if (keyAgreementInProgress) {
LOG.info("Refusing contact connection during key agreement");
accept = false;
} else if (connections.size() > connectionLimit) {
LOG.info("Refusing contact connection due to limit");
accept = false;
} else {
LOG.info("Accepting contact connection");
connections.add(conn);