Add InterruptibleConnection interface for easier testing.

This commit is contained in:
akwizgran
2020-05-13 15:35:03 +01:00
parent 8dd993dd9d
commit 0c338b362e
4 changed files with 40 additions and 9 deletions

View File

@@ -5,18 +5,28 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.plugin.TransportId;
import org.briarproject.bramble.api.sync.Priority;
/**
* Chooses one connection per contact and transport to keep open and closes
* any other connections.
*/
@NotNullByDefault
interface ConnectionChooser {
/**
* Adds the given connection to the chooser with the given priority.
* <p>
* If the chooser has a connection with the same contact and transport and
* a lower {@link Priority priority}, that connection will be
* {@link InterruptibleConnection#interruptOutgoingSession() interrupted}.
* If the chooser has a connection with the same contact and transport and
* a higher priority, the newly added connection will be interrupted.
*/
void addConnection(ContactId c, TransportId t, DuplexSyncConnection conn,
void addConnection(ContactId c, TransportId t, InterruptibleConnection conn,
Priority p);
/**
* Removes the given connection from the chooser.
*/
void removeConnection(ContactId c, TransportId t,
DuplexSyncConnection conn);
InterruptibleConnection conn);
}

View File

@@ -31,8 +31,8 @@ class ConnectionChooserImpl implements ConnectionChooser {
@Override
public void addConnection(ContactId c, TransportId t,
DuplexSyncConnection conn, Priority p) {
DuplexSyncConnection close = null;
InterruptibleConnection conn, Priority p) {
InterruptibleConnection close = null;
synchronized (lock) {
Key k = new Key(c, t);
Value best = bestConnections.get(k);
@@ -52,7 +52,7 @@ class ConnectionChooserImpl implements ConnectionChooser {
@Override
public void removeConnection(ContactId c, TransportId t,
DuplexSyncConnection conn) {
InterruptibleConnection conn) {
synchronized (lock) {
Key k = new Key(c, t);
Value best = bestConnections.get(k);
@@ -89,10 +89,10 @@ class ConnectionChooserImpl implements ConnectionChooser {
private static class Value {
private final DuplexSyncConnection connection;
private final InterruptibleConnection connection;
private final Priority priority;
private Value(DuplexSyncConnection connection, Priority priority) {
private Value(InterruptibleConnection connection, Priority priority) {
this.connection = connection;
this.priority = priority;
}

View File

@@ -27,7 +27,8 @@ import javax.annotation.concurrent.GuardedBy;
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
@NotNullByDefault
abstract class DuplexSyncConnection extends SyncConnection {
abstract class DuplexSyncConnection extends SyncConnection
implements InterruptibleConnection {
final Executor ioExecutor;
final ConnectionChooser connectionChooser;
@@ -44,7 +45,8 @@ abstract class DuplexSyncConnection extends SyncConnection {
@GuardedBy("interruptLock")
private boolean interruptWaiting = false;
void interruptOutgoingSession() {
@Override
public void interruptOutgoingSession() {
synchronized (interruptLock) {
if (outgoingSession == null) interruptWaiting = true;
else outgoingSession.interrupt();

View File

@@ -0,0 +1,19 @@
package org.briarproject.bramble.connection;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
/**
* A duplex sync connection that can be closed by interrupting its outgoing
* sync session.
*/
@NotNullByDefault
interface InterruptibleConnection {
/**
* Interrupts the connection's outgoing sync session. If the underlying
* transport connection is alive and the remote peer is cooperative, this
* should result in both sync sessions ending and the connection being
* cleanly closed.
*/
void interruptOutgoingSession();
}