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 d16d9ed2c..fa6c24916 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 @@ -42,7 +42,6 @@ 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(); /** @@ -51,7 +50,8 @@ interface BluetoothConnectionLimiter { *

* Returns false if the limiter has closed the new connection. */ - boolean contactConnectionOpened(DuplexTransportConnection conn); + boolean contactConnectionOpened(DuplexTransportConnection conn, + boolean incoming); /** * Informs the limiter that a key agreement connection has been opened. 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 423a10850..b2c6400cf 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 @@ -81,7 +81,8 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter { } @Override - public boolean contactConnectionOpened(DuplexTransportConnection conn) { + public boolean contactConnectionOpened(DuplexTransportConnection conn, + boolean incoming) { boolean accept; synchronized (lock) { if (keyAgreementInProgress) { @@ -89,7 +90,7 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter { accept = false; } else { long now = clock.currentTimeMillis(); - accept = isContactConnectionAllowedByLimit(now); + accept = incoming || isContactConnectionAllowedByLimit(now); if (accept) { connections.add(new ConnectionRecord(conn, now)); if (connections.size() > connectionLimit) { 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 a5e0c8024..bf88a74b6 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 @@ -232,7 +232,7 @@ abstract class BluetoothPlugin implements DuplexPlugin, EventListener { return; } LOG.info("Connection received"); - if (connectionLimiter.contactConnectionOpened(conn)) { + if (connectionLimiter.contactConnectionOpened(conn, true)) { backoff.reset(); callback.handleConnection(conn); } @@ -328,7 +328,8 @@ abstract class BluetoothPlugin implements DuplexPlugin, EventListener { if (isNullOrEmpty(uuid)) return null; DuplexTransportConnection conn = connect(address, uuid); if (conn == null) return null; - return connectionLimiter.contactConnectionOpened(conn) ? conn : null; + if (connectionLimiter.contactConnectionOpened(conn, false)) return conn; + return null; } @Override diff --git a/bramble-core/src/test/java/org/briarproject/bramble/plugin/bluetooth/BluetoothConnectionLimiterImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/plugin/bluetooth/BluetoothConnectionLimiterImplTest.java index c389bfe53..8f37d01c3 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/plugin/bluetooth/BluetoothConnectionLimiterImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/plugin/bluetooth/BluetoothConnectionLimiterImplTest.java @@ -1,8 +1,6 @@ package org.briarproject.bramble.plugin.bluetooth; import org.briarproject.bramble.api.event.EventBus; -import org.briarproject.bramble.api.plugin.TransportConnectionReader; -import org.briarproject.bramble.api.plugin.TransportConnectionWriter; import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection; import org.briarproject.bramble.api.sync.event.CloseSyncConnectionsEvent; import org.briarproject.bramble.api.system.Clock; @@ -29,10 +27,6 @@ public class BluetoothConnectionLimiterImplTest extends BrambleMockTestCase { context.mock(DuplexTransportConnection.class, "conn2"); private final DuplexTransportConnection conn3 = context.mock(DuplexTransportConnection.class, "conn3"); - private final TransportConnectionReader reader = - context.mock(TransportConnectionReader.class); - private final TransportConnectionWriter writer = - context.mock(TransportConnectionWriter.class); private final long now = System.currentTimeMillis(); @@ -61,35 +55,30 @@ public class BluetoothConnectionLimiterImplTest extends BrambleMockTestCase { } @Test - public void testLimiterAllowsAttemptToRaiseLimitAtStartup() - throws Exception { + public void testLimiterAllowsAttemptToRaiseLimitAtStartup() { // First outgoing connection is allowed - we're below the limit of 1 assertTrue(limiter.canOpenContactConnection()); - assertTrue(limiter.contactConnectionOpened(conn1)); + assertTrue(limiter.contactConnectionOpened(conn1, false)); // Second outgoing connection is allowed - it's time to try raising // the limit to 2 assertTrue(limiter.canOpenContactConnection()); - assertTrue(limiter.contactConnectionOpened(conn2)); + assertTrue(limiter.contactConnectionOpened(conn2, false)); // Third outgoing connection is not allowed - we're above the limit of 1 assertFalse(limiter.canOpenContactConnection()); - - // Third incoming connection is not allowed - we're above the limit of 1 - expectCloseConnection(conn3); - assertFalse(limiter.contactConnectionOpened(conn3)); } @Test public void testLimiterAllowsThirdConnectionAfterFirstTwoAreClosed() { // First outgoing connection is allowed - we're below the limit of 1 assertTrue(limiter.canOpenContactConnection()); - assertTrue(limiter.contactConnectionOpened(conn1)); + assertTrue(limiter.contactConnectionOpened(conn1, false)); // Second outgoing connection is allowed - it's time to try raising // the limit to 2 assertTrue(limiter.canOpenContactConnection()); - assertTrue(limiter.contactConnectionOpened(conn2)); + assertTrue(limiter.contactConnectionOpened(conn2, false)); // Third outgoing connection is not allowed - we're above the limit of 1 assertFalse(limiter.canOpenContactConnection()); @@ -105,19 +94,19 @@ public class BluetoothConnectionLimiterImplTest extends BrambleMockTestCase { // Third outgoing connection is allowed - we're below the limit of 1 assertTrue(limiter.canOpenContactConnection()); - assertTrue(limiter.contactConnectionOpened(conn3)); + assertTrue(limiter.contactConnectionOpened(conn3, false)); } @Test public void testLimiterRaisesLimitWhenConnectionsAreStable() { // First outgoing connection is allowed - we're below the limit of 1 assertTrue(limiter.canOpenContactConnection()); - assertTrue(limiter.contactConnectionOpened(conn1)); + assertTrue(limiter.contactConnectionOpened(conn1, false)); // Second outgoing connection is allowed - it's time to try raising // the limit to 2 assertTrue(limiter.canOpenContactConnection()); - assertTrue(limiter.contactConnectionOpened(conn2)); + assertTrue(limiter.contactConnectionOpened(conn2, false)); // Third outgoing connection is not allowed - we're above the limit of 1 assertFalse(limiter.canOpenContactConnection()); @@ -135,7 +124,7 @@ public class BluetoothConnectionLimiterImplTest extends BrambleMockTestCase { // Third outgoing connection is allowed - it's time to try raising // the limit to 3 assertTrue(limiter.canOpenContactConnection()); - assertTrue(limiter.contactConnectionOpened(conn3)); + assertTrue(limiter.contactConnectionOpened(conn3, false)); // Fourth outgoing connection is not allowed - we're above the limit // of 2 @@ -146,7 +135,7 @@ public class BluetoothConnectionLimiterImplTest extends BrambleMockTestCase { public void testLimiterIncreasesIntervalWhenConnectionFailsAboveLimit() { // First outgoing connection is allowed - we're below the limit of 1 assertTrue(limiter.canOpenContactConnection()); - assertTrue(limiter.contactConnectionOpened(conn1)); + assertTrue(limiter.contactConnectionOpened(conn1, false)); // Time passes time.set(now + 1); @@ -154,7 +143,7 @@ public class BluetoothConnectionLimiterImplTest extends BrambleMockTestCase { // Second outgoing connection is allowed - it's time to try raising // the limit to 2 assertTrue(limiter.canOpenContactConnection()); - assertTrue(limiter.contactConnectionOpened(conn2)); + assertTrue(limiter.contactConnectionOpened(conn2, false)); // Time passes - the first connection is stable, the second isn't time.set(now + STABILITY_PERIOD_MS); @@ -181,7 +170,7 @@ public class BluetoothConnectionLimiterImplTest extends BrambleMockTestCase { // Third outgoing connection is allowed - it's time to try raising the // limit to 2 again assertTrue(limiter.canOpenContactConnection()); - assertTrue(limiter.contactConnectionOpened(conn3)); + assertTrue(limiter.contactConnectionOpened(conn3, false)); } private void expectCloseSyncConnectionsEvent() { @@ -190,16 +179,4 @@ public class BluetoothConnectionLimiterImplTest extends BrambleMockTestCase { CloseSyncConnectionsEvent.class))); }}); } - - private void expectCloseConnection(DuplexTransportConnection conn) - throws Exception { - context.checking(new Expectations() {{ - oneOf(conn).getReader(); - will(returnValue(reader)); - oneOf(reader).dispose(false, false); - oneOf(conn).getWriter(); - will(returnValue(writer)); - oneOf(writer).dispose(false); - }}); - } }