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