Disable polling while doing connect-via-BT

This commit is contained in:
Daniel Lublin
2021-05-04 16:45:06 +02:00
parent c647c52638
commit fba028db03
6 changed files with 73 additions and 45 deletions

View File

@@ -473,6 +473,16 @@ abstract class AbstractBluetoothPlugin<S, SS> implements BluetoothPlugin,
return discoverSemaphore.availablePermits() == 0;
}
@Override
public void disablePolling() {
connectionLimiter.startLimiting();
}
@Override
public void enablePolling() {
connectionLimiter.endLimiting();
}
@Override
public DuplexTransportConnection discoverAndConnectForSetup(String uuid) {
DuplexTransportConnection conn = discoverAndConnect(uuid);
@@ -501,9 +511,9 @@ abstract class AbstractBluetoothPlugin<S, SS> implements BluetoothPlugin,
if (s.getNamespace().equals(ID.getString()))
ioExecutor.execute(() -> onSettingsUpdated(s.getSettings()));
} else if (e instanceof KeyAgreementListeningEvent) {
ioExecutor.execute(connectionLimiter::keyAgreementStarted);
connectionLimiter.startLimiting();
} else if (e instanceof KeyAgreementStoppedListeningEvent) {
ioExecutor.execute(connectionLimiter::keyAgreementEnded);
connectionLimiter.endLimiting();
} else if (e instanceof RemoteTransportPropertiesUpdatedEvent) {
RemoteTransportPropertiesUpdatedEvent r =
(RemoteTransportPropertiesUpdatedEvent) e;

View File

@@ -7,14 +7,15 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
interface BluetoothConnectionLimiter {
/**
* Informs the limiter that key agreement has started.
* Tells the limiter to not allow regular polling connections (because we
* are about to do key agreement, or connect via BT for setup).
*/
void keyAgreementStarted();
void startLimiting();
/**
* Informs the limiter that key agreement has ended.
* Tells the limiter to no longer limit regular polling connections.
*/
void keyAgreementEnded();
void endLimiting();
/**
* Returns true if a contact connection can be opened. This method does not

View File

@@ -30,34 +30,37 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
private final List<DuplexTransportConnection> connections =
new LinkedList<>();
@GuardedBy("lock")
private boolean keyAgreementInProgress = false;
private int limitingInProgress = 0;
BluetoothConnectionLimiterImpl(EventBus eventBus) {
this.eventBus = eventBus;
}
@Override
public void keyAgreementStarted() {
public void startLimiting() {
synchronized (lock) {
keyAgreementInProgress = true;
limitingInProgress++;
}
LOG.info("Key agreement started");
LOG.info("Limiting started");
eventBus.broadcast(new CloseSyncConnectionsEvent(ID));
}
@Override
public void keyAgreementEnded() {
public void endLimiting() {
synchronized (lock) {
keyAgreementInProgress = false;
limitingInProgress--;
if (limitingInProgress < 0) {
throw new IllegalStateException();
}
}
LOG.info("Key agreement ended");
LOG.info("Limiting ended");
}
@Override
public boolean canOpenContactConnection() {
synchronized (lock) {
if (keyAgreementInProgress) {
LOG.info("Can't open contact connection during key agreement");
if (limitingInProgress > 0) {
LOG.info("Can't open contact connection while limiting");
return false;
} else {
LOG.info("Can open contact connection");

View File

@@ -11,6 +11,10 @@ public interface BluetoothPlugin extends DuplexPlugin {
boolean isDiscovering();
void disablePolling();
void enablePolling();
@Nullable
DuplexTransportConnection discoverAndConnectForSetup(String uuid);