From fba028db03e9f54a143c0d6558429f4483318639 Mon Sep 17 00:00:00 2001 From: Daniel Lublin Date: Tue, 4 May 2021 16:45:06 +0200 Subject: [PATCH] Disable polling while doing connect-via-BT --- .../event/KeyAgreementStartedEvent.java | 2 +- .../bluetooth/AbstractBluetoothPlugin.java | 14 +++- .../bluetooth/BluetoothConnectionLimiter.java | 9 +-- .../BluetoothConnectionLimiterImpl.java | 21 +++--- .../plugin/bluetooth/BluetoothPlugin.java | 4 ++ .../conversation/BluetoothConnecter.java | 68 +++++++++++-------- 6 files changed, 73 insertions(+), 45 deletions(-) diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/keyagreement/event/KeyAgreementStartedEvent.java b/bramble-api/src/main/java/org/briarproject/bramble/api/keyagreement/event/KeyAgreementStartedEvent.java index 289077cb8..513627ceb 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/keyagreement/event/KeyAgreementStartedEvent.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/keyagreement/event/KeyAgreementStartedEvent.java @@ -3,7 +3,7 @@ package org.briarproject.bramble.api.keyagreement.event; import org.briarproject.bramble.api.event.Event; /** - * An event that is broadcast when a BQP protocol completes. + * An event that is broadcast when a BQP protocol begins. */ public class KeyAgreementStartedEvent extends Event { } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/AbstractBluetoothPlugin.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/AbstractBluetoothPlugin.java index 91f0860a3..bc0d42885 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/AbstractBluetoothPlugin.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/AbstractBluetoothPlugin.java @@ -473,6 +473,16 @@ abstract class AbstractBluetoothPlugin 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 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; diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothConnectionLimiter.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothConnectionLimiter.java index c3b115e58..23e2586cf 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothConnectionLimiter.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothConnectionLimiter.java @@ -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 diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothConnectionLimiterImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothConnectionLimiterImpl.java index e76bbafc6..ac002a3cf 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothConnectionLimiterImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothConnectionLimiterImpl.java @@ -30,34 +30,37 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter { private final List 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"); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java index 1350ee128..5ff607fb8 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/bluetooth/BluetoothPlugin.java @@ -11,6 +11,10 @@ public interface BluetoothPlugin extends DuplexPlugin { boolean isDiscovering(); + void disablePolling(); + + void enablePolling(); + @Nullable DuplexTransportConnection discoverAndConnectForSetup(String uuid); diff --git a/briar-android/src/main/java/org/briarproject/briar/android/conversation/BluetoothConnecter.java b/briar-android/src/main/java/org/briarproject/briar/android/conversation/BluetoothConnecter.java index f53fe0edb..52f73f61c 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/conversation/BluetoothConnecter.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/conversation/BluetoothConnecter.java @@ -176,6 +176,7 @@ class BluetoothConnecter implements EventListener { connect(); } + @UiThread @Override public void eventOccurred(@NonNull Event e) { if (e instanceof ConnectionOpenedEvent) { @@ -192,43 +193,52 @@ class BluetoothConnecter implements EventListener { } private void connect() { + bluetoothPlugin.disablePolling(); pluginManager.setPluginEnabled(ID, true); ioExecutor.execute(() -> { - if (!waitForBluetoothActive()) { - showToast(R.string.bt_plugin_status_inactive); - LOG.warning("Bluetooth plugin didn't become active"); - return; - } - showToast(R.string.toast_connect_via_bluetooth_start); - eventBus.addListener(this); try { - String uuid = null; + if (!waitForBluetoothActive()) { + showToast(R.string.bt_plugin_status_inactive); + LOG.warning("Bluetooth plugin didn't become active"); + return; + } + showToast(R.string.toast_connect_via_bluetooth_start); + eventBus.addListener(this); try { - uuid = transportPropertyManager - .getRemoteProperties(contactId, ID).get(PROP_UUID); - } catch (DbException e) { - logException(LOG, WARNING, e); - } - if (isNullOrEmpty(uuid)) { - LOG.warning("PROP_UUID missing for contact"); - return; - } - DuplexTransportConnection conn = bluetoothPlugin - .discoverAndConnectForSetup(uuid); - if (conn == null) { - if (!isConnectedViaBluetooth(contactId)) { - LOG.warning("Failed to connect"); - showToast(R.string.toast_connect_via_bluetooth_error); - } else { - LOG.info("Failed to connect, but contact connected"); + String uuid = null; + try { + uuid = transportPropertyManager + .getRemoteProperties(contactId, ID) + .get(PROP_UUID); + } catch (DbException e) { + logException(LOG, WARNING, e); } - return; + if (isNullOrEmpty(uuid)) { + LOG.warning("PROP_UUID missing for contact"); + return; + } + DuplexTransportConnection conn = bluetoothPlugin + .discoverAndConnectForSetup(uuid); + if (conn == null) { + if (!isConnectedViaBluetooth(contactId)) { + LOG.warning("Failed to connect"); + showToast( + R.string.toast_connect_via_bluetooth_error); + } else { + LOG.info( + "Failed to connect, but contact connected"); + } + return; + } + connectionManager + .manageOutgoingConnection(contactId, ID, conn); + showToast(R.string.toast_connect_via_bluetooth_success); + } finally { + eventBus.removeListener(this); } - connectionManager.manageOutgoingConnection(contactId, ID, conn); - showToast(R.string.toast_connect_via_bluetooth_success); } finally { - eventBus.removeListener(this); + bluetoothPlugin.enablePolling(); } }); }