mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 02:39:05 +01:00
Move responsibility for closing connections from limiter to plugin.
This commit is contained in:
@@ -45,10 +45,9 @@ interface BluetoothConnectionLimiter {
|
||||
boolean canOpenContactConnection();
|
||||
|
||||
/**
|
||||
* Informs the limiter that a contact connection has been opened. The
|
||||
* limiter may close the new connection if key agreement is in progress.
|
||||
* Informs the limiter that a contact connection has been opened.
|
||||
* <p/>
|
||||
* Returns false if the limiter has closed the new connection.
|
||||
* Returns true if the connection is allowed.
|
||||
*/
|
||||
boolean contactConnectionOpened(DuplexTransportConnection conn,
|
||||
boolean incoming);
|
||||
|
||||
@@ -6,7 +6,6 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.sync.event.CloseSyncConnectionsEvent;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@@ -18,10 +17,8 @@ import javax.inject.Inject;
|
||||
|
||||
import static java.lang.Math.min;
|
||||
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.api.plugin.BluetoothConstants.ID;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
@NotNullByDefault
|
||||
@ThreadSafe
|
||||
@@ -83,25 +80,24 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
||||
@Override
|
||||
public boolean contactConnectionOpened(DuplexTransportConnection conn,
|
||||
boolean incoming) {
|
||||
boolean accept;
|
||||
synchronized (lock) {
|
||||
if (keyAgreementInProgress) {
|
||||
LOG.info("Refusing contact connection during key agreement");
|
||||
accept = false;
|
||||
return false;
|
||||
} else {
|
||||
long now = clock.currentTimeMillis();
|
||||
accept = incoming || isContactConnectionAllowedByLimit(now);
|
||||
if (accept) {
|
||||
if (incoming || isContactConnectionAllowedByLimit(now)) {
|
||||
connections.add(new ConnectionRecord(conn, now));
|
||||
if (connections.size() > connectionLimit) {
|
||||
LOG.info("Attempting to raise connection limit");
|
||||
timeOfLastAttempt = now;
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!accept) tryToClose(conn);
|
||||
return accept;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -113,15 +109,6 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
||||
}
|
||||
}
|
||||
|
||||
private void tryToClose(DuplexTransportConnection conn) {
|
||||
try {
|
||||
conn.getWriter().dispose(false);
|
||||
conn.getReader().dispose(false, false);
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connectionClosed(DuplexTransportConnection conn,
|
||||
boolean exception) {
|
||||
|
||||
@@ -235,11 +235,22 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
if (connectionLimiter.contactConnectionOpened(conn, true)) {
|
||||
backoff.reset();
|
||||
callback.handleConnection(conn);
|
||||
} else {
|
||||
tryToClose(conn);
|
||||
}
|
||||
if (!running) return;
|
||||
}
|
||||
}
|
||||
|
||||
private void tryToClose(DuplexTransportConnection conn) {
|
||||
try {
|
||||
conn.getWriter().dispose(false);
|
||||
conn.getReader().dispose(false, false);
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
running = false;
|
||||
@@ -328,8 +339,12 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
if (isNullOrEmpty(uuid)) return null;
|
||||
DuplexTransportConnection conn = connect(address, uuid);
|
||||
if (conn == null) return null;
|
||||
if (connectionLimiter.contactConnectionOpened(conn, false)) return conn;
|
||||
return null;
|
||||
if (connectionLimiter.contactConnectionOpened(conn, false)) {
|
||||
return conn;
|
||||
} else {
|
||||
tryToClose(conn);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user